View on GitHub

Matlab Dynamic Programming Tools

Matlab Heterogeneous Agents Dynamic Savings and Borrowing Package

This is a work-in-progress Matlab package consisting of functions that facilitate Dynamic Programming and Related Tasks. Materials gathered from various projects in which Matlab code is used. Some of the solutions/algorithms are research outputs developed for specific research papers, other algorithms and methods are commonly-used. Files are the MEconTools repository. Matlab files are linked below by section with livescript files. Tested with Matlab 2019a.

Download and install the Matlab toolbox: MEconTools.mltbx, Toolbox Vignette Manual: bookdown site and bookdown pdf.

Bullet points below show vignette files for functions that are available from MEconTools. Each Vignette file contains various examples for invoking each function. The goal of this repository is to make it easier to find/re-use codes produced for various projects. The vignette files are also collected in the bookdown file:

From other repositories: For dynamic borrowing and savings problems, see Dynamic Asset Repository; For code examples, see also R Example Code, Matlab Example Code, Python Example Code, and Stata Example Code; For intro stat with R, see Intro Statistics for Undergraduates, and intro Math with Matlab, see Intro Mathematics for Economists. See here for all of Fan’s public repositories.

Installation

# Clone Package from Git Bash
git clone https://github.com/fanwangecon/MEconTools.git

Install the Package from inside Matlab:

# Install Matlab Toolbox MEconTools
# toolboxFile = 'C:/Users/fan/MEconTools/MEconTools.mltbx';

1 Savings Dynamic Programming

1. Looped Grid Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html
• Infinite horizon constrained dynamic savings problem with persistent shock.
• The state-space and choice-space share the same asset grid.
• Looped algorithm, slow but easy to modify, useful for developing new models.
• MEconTools: ff_vfi_az_loop()
2. Vectorized Grid Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html
• Vectorized version of ff_vfi_az_loop(), fast and sufficiently approximate value(a,z), but choices not precise.
• Broadcast and vectorized evaluation and maximization.
• Solve u(c) once, and retrieve with cell arrays.
• MEconTools: ff_vfi_az_vec()
3. Looped Exact FOC Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html
• Infinite horizon constrained dynamic savings problem with persistent shock.
• The state-space is on a grid, the choice space are continuous percentages of cash-on-hand.
• Looped exact savings-percentage algorithm, slow but high precision at low grid size.
• Solves for EV(ap,z) given shock state and for a savings choice. Bisection based on FOC with analytical du(c(ap))/dap and spline slopes dEV(ap,z)/dap.
• MEconTools: ff_vfi_az_bisec_loop() + ff_optim_bisec_savezrone()
4. Vectorized Exact FOC Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html
5. Looped Exact Value Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html
• Looped infinite horizon constrained dynamic savings problem with persistent shock.
• The state-space is on a grid, the choice space are continuous percentages of cash-on-hand.
• Evaluate value at choice grid iteratively by zooming-in to construct finer savings percentages.
• MEconTools: ff_vfi_az_mzoom_loop() + ff_optim_mzoom_savezrone()
6. Vectorized Exact Value Infinite Horizon Dynamic Savings Problem: mlx | m | pdf | html

2 Stationary Distribution

1. Looped Grid Stationary Distribution Dynamic Savings Problem: mlx | m | pdf | html
• Stationary distribution for infinite horizon constrained dynamic savings problem with persistent shock.
• The state-space and choice-space share the same asset grid.
• Looped algorithm.
• MEconTools: ff_ds_az_loop()
2. Looped Exact Stationary Distribution Dynamic Savings Problem: mlx | m | pdf | html
• Stationary distribution for infinite horizon constrained dynamic savings problem with persistent shock.
• The state-space is on a grid, the choice space are continuous percentages of cash-on-hand.
• Looped algorithm.
• MEconTools: ff_ds_az_cts_loop()
3. Vectorized Exact Stationary Distribution Dynamic Savings Problem: mlx | m | pdf | html

3 Summarize Policy and Value

1. Summarize ND Array Policy and Value Functions: mlx | m | pdf | html
• Given an NDarray matrix with N1, N2, …, ND dimensions. Generate average and standard deviation for the 3rd dimension, grouping by the other dimensions.
• For example, show the 5th dimension as the column groups, and the other variables generate combinations shown as rows.
• The resulting summary statistics table contains mean and standard deviation among other statistics over the policy or value contained in the ND array.
• MEconTools: ff_summ_nd_array()

4 Distributional Analysis

1. Gateway Joint Probability Mass Statistics: mlx | m | pdf | html
• Given model policy functions and stationary distribution, compute distributional statistics.
• Given discrete probability mass function f(s), and information y(s), x(s), z(s) at each element of the state-space, compute statistics for each variable, y, x, z, which are all discrete random variables.
• Compute correlation and covariance betwen input discrete random variables.
• MEconTools: ff_simu_stats()
2. Discrete Random Variable Distributional Statistics: mlx | m | pdf | html
• Model simulation generates discrete random variables, calculate mean, standard deviation, min, max, percentiles, and proportion of outcomes held by x percentiles, etc.
• MEconTools: ff_disc_rand_var_stats()
3. Generate Discrete Random Variable: mlx | m | pdf | html
• Given mass at state space points, and y, c, a, z and other outcomes or other information at each corresponding state space points, generate discrete random variable, with unique sorted values, and mass for each unique sorted values.
• Generate additional joint distributions: if initial distribution is over f(a,z), generate joint distribution of f(y,a) or f(y,z).
• MEconTools: ff_disc_rand_var_mass2outcomes()
4. Discrete Random Variable Correlation and Covariance: mlx | m | pdf | html
• Given probability mass function f(s), X(s), and Y(s), compute the covariance and correlation betwen X and Y.
• MEconTools: ff_disc_rand_var_mass2covcor()

5 Optimizers

1. Bisection Exact Optimal Savings Share Multiple States: mlx | m | pdf | html
• Given a First Order Condition function handle that takes the fraction of resources (cash-on-hand) saved as the input, solve for the optimal savings fraction via bisection. Solve this concurrently for many elements of the state-space. The function handle contains the FOC with parameters and state-space elements embeded.
• MEconTools: ff_optim_bisec_savezrone()
2. Multisection Exact Optimal Savings Share Multiple States: mlx | m | pdf | html
• Given a First Order Condition function handle that takes the fraction of resources (cash-on-hand) saved as the input, solve for the optimal savings fraction via multisection where there are multiple evaluations per iteration of the FOC. Solve this concurrently for many elements of the state-space. The function handle contains the FOC with parameters and state-space elements embeded.
• MEconTools: ff_optim_mlsec_savezrone()
3. Vectorized Zooming Exact Optimal Savings Share Multiple States: mlx | m | pdf | html
• Given a Utility (not FOC) function handle that takes the fraction of resources (cash-on-hand) saved as the input, solve for the optimal savings fraction via iterative zooming where there are multiple evaluations per iteration of the utility function. Solve this concurrently for many elements of the state-space. The function handle contains the utilty function with parameters and state-space elements embeded.
• MEconTools: ff_optim_mzoom_savezrone()

6 Graphs

1. Multiple Line Graph Function: mlx | m | pdf | html
• Policy and Value Function graphs, x-axis one state, color another state, y-axis are value and policies.
• Distributional graphs, x-axis one state, y-axis and color another state, size show distributional mass.
• MEconTools: ff_graph_grid()

7 Support Tools

1. Organizes and Prints Container Map Key and Values: mlx | m | pdf | html
• Summarizes the contents of a map container by data types. Includes, scalar, array, matrix, string, functions, tensors (3-tuples), tesseracts (4-tuples).
• MEconTools: ff_container_map_display()

8 Data Structures

1. Log and Power Spaced Asset and Choice Grids: mlx | m | pdf | html
• Generate linear, log-space, power-space, or threshold-cut asset or choice grids.
• MEconTools: ff_saveborr_grid()
2. Randomly Perturb Some Parameter Value with Varying Magnitudes: mlx | m | pdf | html
• There is a starting estimation parameter value, perturb this initial starting points. Use a scalar so that 0 means almost no change, and 1 means maximum change to control how much to perturb the parameter. This is used for multi-start estimation
• MEconTools: ff_perturb_logn()
3. Find Real-valued Domain of a Function: mlx | m | pdf | html
• Checks for valid domain for function that generates real-valued outcomes, and identifies values along the domain that generates positive and negative Values.
• MEconTools: ff_nonimg_posnegbd()

9 Common Functions

1. Discretize AR1 Normal Shock Tauchen (1986): mlx | m | pdf | html
• Mean zero AR(1) shock discretize following Tauchen (1986).
• MEconTools: ffy_tauchen()
2. Discretize AR1 Normal Shock Rouwenhorst (1995): mlx | m | pdf | html
• Mean zero AR(1) shock discretize following Rouwenhorst (1995).
• MEconTools: ffy_rouwenhorst()

10 System

1. Search and Find File Names: mlx | m | pdf | html
2. Execute and Export Livescript Files: mlx | m | pdf | html      