A Index and Code Links
A.1 Data Structures links
A.1.1 Section 1.1 Matrices and Arrays links
- Array Reshape, Repeat and Expand: mlx | m | pdf | html
- Reshape and flatten arrays.
- m: reshape()
- Array Index Slicing and Subsetting to Replace and Expand: mlx | m | pdf | html
- Index based column and row expansions.
- Anonymous function to slice array subsets.
- m: sub2ind() + @(it_subset_n, it_ar_n) unique(round(((0:1:(it_subset_n-1))/(it_subset_n-1)) times (it_ar_n-1)+1))
- Find the Maximum Value and Index in Matrix Over Columns and Overall: mlx | m | pdf | html
- Given 2D array, find the maximum value and index for each column.
- Find the maximum value in a 2D array’s row and column indexes.
- m: max() + ind2sub() + maxk()
- Array Broadcasting Examples: mlx | m | pdf | html
- broadcast means: array + array’ + matrix = matrix.
- Grid States, Choices and Optimal Choices Example: mlx | m | pdf | html
- States, choices, and find max.
- Accumarray Examples: mlx | m | pdf | html
- Accumarray to sum up probabilities/values for discrete elements of arrays.
- m: unique() + reshape() + accumarray()
- Matlab Miscellaneous Array and Numeric Operations: mlx | m | pdf | html
- Loop over numbers, find modulus (remainder) and quotient given divisor.
- Check data and parameter types.
- Compare approximately similar values.
- Find imaginary elements of array.
- m: imag() + isfloat() + iscell() + isnan() + isnumeric()
A.1.2 Section 1.2 ND Dimensional Arrays links
- All Possible Combinations of Arrays as Table or Random Subset Mesh: mlx | m | pdf | html
- Generate a Table based on all possible combinations of several arrays.
- Draw randomly from array, permutate arrays.
- m: ndgrid() + cell2mat(cellfun(@(m) m(:), cl_mt_all, ‘uni,’ 0))
- 3D, 4D, ND Arrays Reshape and Summarize: mlx | m | pdf | html
- Slice 2D matrixes out of ND matrixes. The 2D matrix is contiguous, but can be intermediate dimensions.
- Summarize a nd dimensional matrix along one or two dimensions group by various other dimensions.
- m: permute(mn, [3,1,2,4]) + squeeze(num2cell(mn, [1,2])) + celldisp() + ndgrid()
- ND Array Wide to Long Reshape to Table Dataframe with Variable Values for Each Dimension: mlx | m | pdf | html
- Given 2D policy function f(a,z), generate table/dataframe with a, z, and f(a,z) columns.
- There is a ND Array where each dimension is a different attribute, generate 2D dataframe with columns for attribute values and ND Array values stored as a single column.
- There might be many NaN values in the ND array, drop NaN values in the ND array for 2D dataframe. Find the non-NaN values along each index dimension.
- m: cell() + NaN() + isnan() + ind2sub() + find()
A.1.3 Section 1.3 Cells links
- Combine Cells: mlx | m | pdf | html
- Combine string cell arrays and string.
- m: [{st_param}, ls_st_param_key, cl_st_param_keys]
- List Comprehension with Cells: mlx | m | pdf | html
- Cell2mat, cellfun, anonymous function list comprehension over cells.
- Find min and max of all arrays in cells.
- Find length of all arrays in cells; find index of elements of one array in another cell array.
- Trim and concatenate floats to single string.
- m: cell2mat() + cellfun() + strcmp() + strtrim() + find() + cell2mat(cellfun(@(m) find(strcmp(ls_st_param_key, m)), cl_st_param_keys, ‘UniformOutput,’ false)) + cellfun(@(x) strtrim(x), cellstr(st_fl_rand), ‘UniformOutput,’ false)
- Permutate Cells: mlx | m | pdf | html
- Generate all possible combinations of various arrays contained in cell array.
- m: ndgrid() + cell2mat() + array2table() + cell2mat(cellfun(@(m) m(:), cl_mt_all, ‘uni,’ 0))
- Nested Cells: mlx | m | pdf | html
- Cell of cells with inner cell having multiple types.
- m: linspace() + cell([4,1]) + clns_parm_tstar{1} = {‘fl_crra,’ ‘CRRA,’ linspace(1, 2, it_simu_vec_len)} + disp(clns_parm_tstar(1)) + disp(clns_parm_tstar{1}{1})
A.1.4 Section 1.4 Characters and Strings links
- Basic String Operations, DisplayDisplay, Search, Join and Split: mlx | m | pdf | html
- Generating a string for file suffix based on date and time.
- Print info segment of multiple numeric and string parameters, scalar and array.
- Compose string and rounded numeric array.
- Cut string suffix and append new suffix.
- m: *datestr(now, ‘mm-dd-yyyy-HH-MM’) + char() + compose() + strjoin() + contains() + matches() + str_sub = split(string, “.”) + strcat(str_sub{1}, ’_m.m’)*
- String Arrays Operations, Join, Find, Replace and the Alphabet: mlx | m | pdf | html
- Generate string arrays and cell strings.
- String array from single and double quoted strings.
- Duplicate strings, concatenate string, and paste strings jointly with separator.
- Find string element positions, replace substrings.
- m: repmat() + num2str() + string() + strcat() + strjoin() + fprintf() + strcmp() + strrep() + cel2mat(cellfun(@(m) find(strcmp()))) + cellstr() + ‘a’:‘z’
- Convert and Cancatenate String and Numeric Array Concatenations: mlx | m | pdf | html
- Generate rounded string array matrix with leading zero, leading space, decimal round from numeric matrix.
- Generate a string from a numeric array and join with positional counter.
- Create a title string by joining rounded parameter and parameter names, with decimal formatting.
- Concatenate multiple numeric arrays together with strings and format.
- m: num2str() + compose() + cellstr() + strcat() + strjoin() + %.2f
A.1.5 Section 1.5 Map Containers links
- Container Map Basics: mlx | m | pdf | html
- Numeric container map, dynamically filled container map.
- Numeric scalar, string, matrix as values for map container.
- Get values for multiple keys in map.
- m: isKey() + strjoin() + containers.Map(‘KeyType,’ ‘char,’ ‘ValueType,’ ‘any’) + map.keys() + map.values() + values(param_map, {‘share_unbanked_j,’ ‘equi_r_j’})
- Container Map Display Swtich Key and Values and Subseting: mlx | m | pdf | html
- Loop over map, display keys and values.
- Select Container map subset by keys.
- Generate new container map by switching the values to keys and keys to values.
- m: strjoin() + keys(map) + values(map) + containers.Map(keys, values) + cellfun(@(x) num2str(x(:)), num_cell, ‘uni,’ 0);
- Cell Override: mlx | m | pdf | html
- Override default map with externally fed map, update existing and add new keys.
- m: param_map_updated = [param_map_old; param_map_updates_new]
A.1.6 Section 1.6 Map Structure Array links
- Struct of Map Container for Nested Value Retrieval: mlx | m | pdf | html
- There is a list of parameters, use several container maps to store information about parameters, and combine them in a struct.
- Use struct to in effect make single-line nested container map calls.
- m: struct
A.2 Functions and Programming links
A.2.1 Section 2.1 Development and Debugging links
- Matlab Errors and Warnings: mlx | m | pdf | html
- Turn off warning messages.
- m: lastwarn + warning(‘off,’ st_warn_id);
- Matlab Profiling and Testing to Improve Code Speed: mlx | m | pdf | html
- Profile code segment and save profiling results to HTML folder in the current directory.
- m: profile on + profile off + profsave(profile(‘info’), spn) + matlab.desktop.editor.getActiveFilename + fileparts() + fullfile()
A.2.2 Section 2.2 varargin Default Parameters links
- Use varargin as a Function Parameter: mlx | m | pdf | html
- Default parameters allow for maintaining code testability.
- Use varargin for functions with limited parameters.
- m: varargin + cell2mat() + function [out_put] = func_name(varargin)
- Container Default Parameter with varargin and Input Type Check: mlx | m | pdf | html
- The varargin structure could lead to excessive code lines. Container Map works well with large parameter structure.
- Core model functions with potentially many parameters, possibly override default generation to save time.
- m: varargin + function [out_put] = func_name(varargin) + cm_defaults = {cm_a, cm_b} + [cm_defaults{1:optional_params_len}] = varargin{:} + cm_c = [cm_a;cm_b]
A.2.3 Section 2.3 Dynamic Functions links
- Anonymous Function Examples: mlx | m | pdf | html
- Define a wage equation where individuals working part time earn a fraction of the full time earnings.
- m: gamrnd() + f_x = @(x) x
- Dynamically Generate M File: mlx | m | pdf | html
- Get current working file file name and path.
- Generate a m file from strings, add file to path, can call that m file.
- m: matlab.desktop.editor.getActiveFilename + fileparts() + fullfile() + addpath() + fopen() + fprintf() + fclose()
A.3 Distributional Processes links
A.3.1 Section 3.1 Time Series links
- Autoregressive Process AR(1): mlx | m | pdf | html
- The Mean and standard deviation of an AR(1) process.
- Simulate and graph an AR(1) persistent process.
- Simulate log income process with parameters estimated from Indian income data.
- m: normrnd() + for it_t=1:1:length(ar_shk) + plot(ar_t, ar_y)
- Moving Average of Neighboring Values: mlx | m | pdf | html
- Compute moving average of surrounding values with different windows.
- Visualize moving averages compare actual to smoothed average to fully flat moving average.
- m: movmean()
- MEconTools: ff_graph_grid()
A.3.2 Section 3.2 Cross-sectional Data links
- Mincer Wage Earnings Equation with Experience, Education and Gamma Shocks: mlx | m | pdf | html
- Define a wage equation where individuals working part time earn a fraction of the full time earnings.
- Wage at different education and experience levels.
- Simluate wage with an array of gamma distribution shocks.
- m: gamrnd() + f_x = @(x) x + histogram()
- MEconTools: ff_graph_grid + ff_simu_stats
A.4 Simulation links
A.4.1 Section 4.1 Normal Distribution links
- Compute CDF for Normal and Bivariate Normal Distributions: mlx | m | pdf | html
- CDF for normal random variable through simulation and with NORMCDF function.
- CDF for bivariate normal random variables through simulation and with NORMCDF function, using cholesky deomposition to model correlation from uniform random draws.
- m: mvncdf + norminv
- Cholesky Decomposition Correlated Two Dimensional Normal Shock: mlx | m | pdf | html
- Draw two correlated normal shocks using the MVNRND function.
- Draw two correlated normal shocks from uniform random variables using Cholesky Decomposition.
- m: mvnrnd + corrcoef + norminv
- Cholesky Decomposition Correlated Five Dimensional Normal Shock: mlx | m | pdf | html
- Generate variance-covariance matrix from correlation and standard deviation.
- Draw five correlated normal shocks using the MVNRND function.
- Draw five correlated normal shocks from uniform random variables using Cholesky Decomposition.
- m: mvnrnd + corrcoef + norminv + subplot
A.5 Estimation links
A.5.1 Section 5.1 Linear Estimation links
- Estimate and Solve for Parameters in Linear System of Equation and OLS Regression: mlx | m | pdf | html
- Fit a line through the origin with two points of data.
- Solve/estimate an exactly identified system of linear equations.
- m: fitlm() + fc_ols_lin = @(y, x) (x’x)^(-1)(x’y);
A.5.2 Section 5.2 Nonlinear Estimation links
- Matlab Simple Nonlinear Estimation: mlx | m | pdf | html
- Nonlinear estimation using fminunc.
- m: optimset() + fminunc()
A.6 Graphs links
A.6.1 Section 6.1 Figure Components links
- Image Pick Safe Colors: mlx | m | pdf | html
- Display safe colors.
- m: blue = [57 106 177]./255 + fill(x, y, cl_colors{it_color})
- Figure Titling and Legend: mlx | m | pdf | html
- Multi-line titles, add legend lines.
- Add to legend, select legend to show.
- m: title({‘Cash-on-Hand’ ‘\(\alpha + \beta = \zeta\)’},‘Interpreter,’‘latex’) + legend([g1, g2, g3], {‘near,’‘linear,’‘spline’}, ‘Location,’ ‘best,’ ‘NumColumns,’ 1, ‘FontSize,’ 12, ‘TextColor,’ ‘black’);
- Graph Many Lines Legend for Subset: mlx | m | pdf | html
- State-space plots with color spectrum: can not show all states in legend, show subset, add additional line to plot and legend.
- m: jet() + numel() + fliplr() + jet(numel(chart)), set(chart(m), ‘Color,’ clr(m,:))
A.6.2 Section 6.2 Basic Figure Types links
- Scatter Plot Examples: mlx | m | pdf | html
- Scatter multiple lines different colors, shapes and sizes.
- m: scatter(x, y, size) + Marker + MarkerEdgeColor + MarkerEdgeAlpha + MarkerFaceColor + MarkerFaceAlpha
- Scatter Plot Examples: mlx | m | pdf | html
- Scatter and lines multiple lines different colors, shapes and sizes.
- X axis, Y axis, and 45 degree line.
- m: xline(0) + yline(0) + refline([1 0]) + plot(x,y) + HandleVisibility + Color + LineStyle + LineWidth
- Three variables Scatter and Lines with Color Spectrum: mlx | m | pdf | html
- Two dimensional matrix for x and y, a third variable with color spectrum set via loop.
- m: plot(2d, 2d) + jet + set(chart(m), ‘Color,’ clr)
A.6.3 Section 6.3 Graph Functions links
- Matlab Plot Polynomials, Budget and Indifference Functions: mlx | m | pdf | html
- Use fplot to plot a one variable function.
- Plot budget constraint and indifference curve.
- m: fplot() + xline() + yline() + title([char(f_x)],‘Interpreter,’“none”);
A.6.4 Section 6.4 Write and Read Plots links
- Graph Generate EPS Postscript Figures: mlx | m | pdf | html
- EPS vector graphics, avoid bitmap (jpg, png), use vector graphics.
- m: figure(‘Renderer,’ ‘Painters’)
A.7 Tables links
A.7.1 Section 7.1 Basic Table Generation links
- Table Fill Data Row by Row or with Random Data: mlx | m | pdf | html
- Generate an empty table and fill with data row by row.
- Convert a random matrix to a table with column and row names defined with arrays.
- m: table() + array2table() + strcat() + addvars() + matlab.lang.makeValidName()
- Order, Sort and Rename Columns: mlx | m | pdf | html
- Convert a matrix to table with mean and sd columns. Rearrange and rename columns.
- m: array2table() + rng() + addvars() + movevars() + removevars() + matlab.lang.makeValidName() + tb.Properties.VariableNames + tb.Properties.RowNames
- Array Based Row and Column Names: mlx | m | pdf | html
- Generate a column and row named table. Convert row names to a column as strings. Remove Row Names.
- Generate string-keys based on column names and values for a subset of columns.
- m: array2table() + string() + strcat(‘rowA=,’ string((1:size(mt, 1)))) + tb_test_a.Properties.VariableNames + tb_test_a.Properties.RowNames + addvars(tb, rownames, ‘Before,’ 1) + strcat() + strjoin() + cellfun(@(x) f(x), ar_x)
- Select Subset of Rows and Columns: mlx | m | pdf | html
- Conditional selection based on cell values and column and row names.
- Select if row value matches any value from an arry of values.
- Load a excel file and select based on column name string conditions.
- m: tb(strcmp(tb.v1, “b”),:) + tb(tb.va==0.4,:) + readtable() + startsWith() + endsWith() + contains() + matches()
A.7.2 Section 7.2 Table Joining links
- Join Table by Keys: mlx | m | pdf | html
- Left join larger table with smaller table with common keys.
- m: join() + table()
- Stack Matlab Tables: mlx | m | pdf | html
- Append columns to existing table. Stack tables vertically and horizontally.
- m: array2table() + [tb_a tb_b] + [tb_a; tb_b] + tb.Properties.VariableNames + tb.Properties.RowNames
- Stack and Join Estimation and Simulation Results: mlx | m | pdf | html
- Stack different tables together with varying columns via outerjoin, store with parallel parfor.
- Stack different estiamtion results together into a common table where columns show parameter names and other strings.
- Simulate a model, column combine simulation parameters with multi-row simulation results. Then row stack results from multiple simulations together.
- m: array2table() + outerjoin() + addvars()
- parallel: parfor
A.7.3 Section 7.3 Summarize links
- Table Summarize and Aggregate by Groups: mlx | m | pdf | html
- Group table by one or several group variables, and aggregate over another numerical variable within group.
- m: groupsummary() + table() + movevars()
Hai, Rong, and James J. Heckman. 2017. “Inequality in Human Capital and Endogenous Credit Constraints.” Review of Economic Dynamics 25: 4–36. https://doi.org/https://doi.org/10.1016/j.red.2017.01.001.
The MathWorks Inc. 2019. MATLAB. https://www.mathworks.com/products/matlab.html.
Wang, Fan. 2020. MEconTools: Tools for Analyzing Matlab Data Structures and Dynamic Programming. https://fanwangecon.github.io/MEconTools/.
Xie, Yihui. 2020. Bookdown: Authoring Books and Technical Documents with r Markdown. https://CRAN.R-project.org/package=bookdown.