Skip to content
Matlab implementation of DC-EGM algorithm from Iskhakov, Jorgensen, Rust and Schjerning (QE, 2017)
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This repository contains Matlab implementation of EGM and DC-EGM algorithms for solving dynamic stochastic lifecycle models of consumption and savings, with additional discrete choices.

Three models are solved using these methods:

  • Phelps model of consumption and savings with stochastic returns (and credit constraints) (EGM)
  • Deaton model of consumption and savings with idiosyncratic wage shocks and credit constraints (EGM)
  • Model of consumption, saving and retirement decisions with idiosyncratic wage shocks, credit constraints and absorbing retirement (DC-EGM)

The code also contains the polyline.m class which presents a set of tools for working with linearly interpolated functions, including the upper envelope algorithm. The code also contains the easy start implementation of EGM algorithm in just 13 lines of code.


  1. Christopher D. Carroll "[The method of endogenous gridpoints for solving dynamic stochastic optimization problems] (" (Economics Letters, 2006)
  2. Iskhakov, Jorgensen, Rust and Schjerning "[The Endogenous Grid Method for Discrete-Continuous Dynamic Choice Models with (or without) Taste Shocks] (" (Quantitative Economics, 2017)


Lecture notes from the ZICE2017 lecture on DC-EGM

Installation and running

  1. Make sure the files are in the Matlab work directory
  2. Run addpath('utils') (this is done automatically in run.m script)
  3. Run your script, or run run.m for examples


The minimal implementation of the EGM algorithms for the stochastic consumption-savings lifecycle model with credit constraints is

% Parameters
EXPN=10         % Number of quadrature points to calculate expectation
MMAX=10         % Maximum wealth
NM=100          % Number of grid points
TBAR=25         % Number of time periods
SIGMA=0.25      % Sigma parameter in logNormal distribution
Y=1             % Wage income
R=0.05          % Interest rate
DF=0.95         % Discount factor

% 13 lines EGM implementation
[quadp quadw]=quadpoints(EXPN,0,1);     % create quadrature notes and weights
quadstnorm=norminv(quadp,0,1);          % prepare quadrature points for calculation of expectations of Normal
savingsgrid=linspace(0,MMAX,NM);        % post-decision grid on savings
policy{TBAR}.w=[0 MMAX];                % terminal period wealth
policy{TBAR}.c=[0 MMAX];                % terminal period optimal consumption
for it=TBAR-1:-1:1                      % main backwards induction loop
 w1=Y+exp(quadstnorm*SIGMA)*(1+R)*savingsgrid;  % next period wealth (budget equation), matrix for all savings and all shocks
 c1=interp1(policy{it+1}.w,policy{it+1}.c,w1,'linear','extrap'); %next period optimal consumption
 rhs=quadw'*(1./c1);                    % RHS of the Euler equation (with log utility)
 policy{it}.c=[0 1./(DF*(1+R)*rhs)];    % current period optimal consumption rule
 policy{it}.w=[0 savingsgrid+policy{it}.c(2:end)]; % current period endogenous grid on wealth

% Plot the optimal policy functions
for it=TBAR:-1:1
  hold all
set(gca,'XLim',[0 MMAX])
ylabel(gca,'Optimal consumption')
title(gca,'Optimal consumption rules by age')

The solution of the problems is a set of policy function saved in policy structure.

Solution and simulations for the Phelps model with stochastic returns (EGM)

Retirement model: optimal consumption rules, value functions and probability to remain working (DC-EGM)

Note the kinks in the value function of the worker and discontinuities in the optimal consumption rules.

The kinks in the value function of the worker and discontinuities in the optimal consumption rules are smoothed with extreme value distributed taste shocks

Simulated wealth and consumption profiles, and the histogram of simulated retirement ages

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.