Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Dynamic programming is the recursive way of writing a sequential decision problem, and every model later in the course is a variation on it: the Bellman equation, the classification of DP models, and one inventory model solved by backwards induction.

What is dynamic programming?ยถ

โ€œDP is recursive method for solving sequential decision problemsโ€

๐Ÿ“– Rust 2006, New Palgrave Dictionary of Economics

In computer science the meaning of the term is broader: DP is a general algorithm design technique for solving problems with overlapping sub-problems.

Dynamic programming in economicsยถ

DP provides a framework to study decision making over time and under uncertainty and can accommodate learning, strategic interactions between agents (game theory) and market interactions (equilibrium theory)

Many important problems and economic models are analyzed and solved using dynamic programming:

Dynamic discrete choice (DDC)ยถ

Extend the discrete choice framework to repeated discrete choice over time

maxโกฮด={ฮด0.....ฮดT}Eฮด[โˆ‘t=0Tฮฒtut(st,dt)โˆฃs0=s]\max_{\mathbf{\delta}= \{\delta_0..... \delta_{T}\}} \mathbb{E}{_{\mathbf{\delta}}} \big[ \sum_{t=0}^T \beta^t u_t(s_t,d_t) | s_0=s \big]

where Eฮด\mathbb{E}_{\delta} denotes expectation with respect to the controlled stochastic process {st,dt}\{s_t,d_t\} induced by the decision rule ฮด\delta.

Bellmanโ€™s Principle of Optimalityยถ

An optimal policy has a property that whatever the initial state and initial decision are, the remaining decisions must constitute an optimal policy with regard to the state resulting from the first decision.

๐Ÿ“– Bellman, 1957 โ€œDynamic Programmingโ€

Breaking the problem into sequence of small problemsยถ

Bellman equationยถ

Solution is given by:

The optimal choices are revealed along the solution of the Bellman equation as decisions which solve the maximization problem in the right hand side (argโกmaxโก\arg\max of the maximization in the Bellman equation)

Classification of DP modelsยถ

There are many problems where DP methods are applicable, therefore it is important to quickly classify your problem to make others understand exactly what follows

Whether time is continuous or discreteยถ

  1. Discrete time

    • time periods tt, t+1t+1

    • dynamics given by difference equations

  2. Continuous time

    • all entities in the model are functions of time

    • dynamics given by differential equation

    • so, math is very different

    • continuous time for cleaner theoretical models, sometimes also solved numerically

    • not part of this course

  3. Continuous time with discrete events

    • continuous time dynamics, but discrete events (e.g.ย Poisson process) that change the state of the system

    • appropriate in many settings such as search

    • significant simplification is certain settings

    • ๐Ÿ“– DSE2024 lecture by Peter Arcidiacono on the topic

    • also not part of this course

Whether horizon is finite or infiniteยถ

  1. Finite horizon

    • there is terminal period TT

    • special form of Bellman equation in period TT

    • in other words, as if V(atย T+1)=0V(\text{at } T +1 ) = \mathbb{0}

    • value function and policy function are time dependent

    • solved by backwards induction with TT number of steps

V(state)=maxโกdecisions[U(state,decision)]ย atย terminalย periodย TV(\text{state}) = \max_{\text{decisions}} \big[ U(\text{state},\text{decision}) \big] \text{ at terminal period } T
  1. Infinite horizon

    • time subscripts are dropped, primes for next period values instead

    • solution is given by fixed point of the Bellman operator

    • have to actually solve a functional equation

Most problems can be specified and solved in both finite or infinite horizon

Whether choice space is discrete, continuous, mixed discrete-continuous or discretizedยถ

  1. Problems with discrete choice

    • Rust model of bus engine replacement next week

    • Inventory management of discrete goods (later today)

    • Thousands other problems

    • ๐Ÿ“– Aguirregabiria & Mira (2010) โ€œDynamic discrete choice structural models: A surveyโ€

  2. Problems with continuous choice

    • cake eating and consumption-savings models (later in the course)

    • discretized?

    • treated as continuous?

    • require interpolating of value function in Bellman equation!

    • ๐Ÿ“– Carroll (2006) EGM paper, and Iskhakov (2015) multidimensional EGM paper

  3. Problems with discrete and continuous choice

    • much more complicated: kinks in value functions, discontinuous policy function

    • require global optimization in Bellman equation which is not easy

    • one way out is to discretize choices at the cost of reduced accuracy

    • ๐Ÿ“– Iskhakov et al. (2017) DCEGM paper

What about state space?ยถ

  1. This is true in general when using numerical solvers: state space is discretized within some reasonable bounds

    • choice of upper bounds, number and placement of grid points influence the (accuracy of the) solution

  2. Another approach to represent state space is to project value and/or policy function onto space of orthogonal polynomials (Chebyshev polynomials in particular)

    • only works well when value function is sufficiently smooth

Whether model includes stochastic processesยถ

  1. Deterministic models

    • No random elements, all motion rules deterministic

    • No need for expectation operator in Bellman equation

V(state)=maxโกdecisions[U(state,decision)+ฮฒโ‹…V(nextย state)]V(\text{state}) = \max_{\text{decisions}} \big[ U(\text{state},\text{decision}) + \beta \cdot V(\text{next state}) \big]
  1. Stochastic models with idiosyncratic shocks

    • expectation does not have to be conditioned on current period shocks

    • which opens a cheaper way of writing the fixed point, and we use it on Thursday

  2. General form stochastic models

    • expectation in Bellman equation has to be computed with quadrature or Monte Carlo integration

Whether choice is part of the problem at allยถ

For the last example:

  • Does it make sense that the number of tilings zero for every odd nn?

  • What is the complexity of this algorithm in nn?

For dynamic programming classification:

  • Which of the five questions changes the solution method you have to write, and which only changes the code that evaluates the payoff?

  • Where does the inventory model below sit in each of these classifications?

Example: Inventory management modelยถ

Consider the following problem in discrete time and finite horizon t=0,โ€ฆ,Tt=0,\dots,T

The notation is:

The sales in period tt are given by st=minโก{xt,dt}s_t = \min\{x_t,d_t\}.

Inventory to be stored till next period is given by kt=maxโก{xtโˆ’dt,0}+qt=xt+1k_t = \max\{x_t-d_t,0\} + q_t = x_{t+1}.

The profit in period tt is given by

ฯ€t=pโ‹…salestโˆ’rโ‹…xt+1โˆ’cโ‹…(orderย madeย inย periodย t)=stpโˆ’ktrโˆ’c1{qt>0}==pminโก{xt,dt}โˆ’r[maxโก{xtโˆ’dt,0}+qt]โˆ’c1{qt>0}\begin{array}{rcl} \pi_t & = & p \cdot \text{sales}_t - r \cdot x_{t+1} - c \cdot (\text{order made in period }t) \\ & = & s_t p - k_t r - c \mathbb{1}\{q_t>0\} = \\ & = & p \min\{x_t,d_t\} - r \big[ \max\{x_t-d_t,0\} + q_t \big] - c \mathbb{1}\{q_t>0\} \end{array}

Assuming all qtโ‰ฅ0q_t \ge 0, let ฯƒ={qt}t=1,โ€ฆ,T\sigma = \{q_t\}_{t=1,\dots,T} denote a feasible inventory policy.

The expected profit maximizing problem is given by

maxโกฯƒE[โˆ‘t=0Tฮฒtฯ€t],{\max}_{\sigma} \mathbb{E}\Big[ \sum_{t=0}^{T} \beta^t \pi_t \Big],

where ฮฒ\beta is discount factor.

Bellman equation for the problemยถ

  • What are states and decisions?

  • What are the motion rules?

  • What is the instantaneous payoff/utility?

When dtd_t is stochastic the policy has to be a function of the period tt inventory xtx_t. What about deterministic dtd_t?

  • What is the sequence of events (timing assumptions)?

  • (beginning of period)

  1. current inventory

  2. demand

  3. order (choice)

  4. stored inventory

  • (end of period)

So, both xtx_t and dtd_t are taken into account for the new order to be made, forming the state space.

V(xt,dt)=maxโกqtโ‰ฅ0{ฯ€t+ฮฒE[V(xt+1,dt+1)โˆฃxt,dt,qt]}=maxโกqtโ‰ฅ0{stpโˆ’ktrโˆ’c1{qt>0}+ฮฒE[V(kt,dt+1)]}\begin{array}{rcl} V(x_t,d_t) &=& \max_{q_t \ge 0} \Big\{ \pi_t + \beta \mathbb{E}\Big[ V\big(x_{t+1} , d_{t+1} \big) \Big| x_t,d_t,q_t \Big] \Big\} \\ &=& \max_{q_t \ge 0} \Big\{ s_t p - k_t r - c \mathbb{1}\{q_t>0\} + \beta \mathbb{E}\Big[ V\big( k_t, d_{t+1} \big) \Big] \Big\} \end{array}
st=minโก{xt,dt}kt=maxโก{xtโˆ’dt,0}+qt\begin{array}{rcl} s_t &=& \min\{x_t,d_t\} \\ k_t &=& \max\{x_t-d_t,0\} + q_t \end{array}

The expectation in the Bellman equation is taken over the distribution of the next period demand dt+1d_{t+1}, which we assume is independent of any other variables and across time (idiosyncratic), thus the conditioning on (xt,dt,st)(x_t,d_t,s_t) can be meaningfully dropped.

Expectation can be written as an integral over the distribution of demand F(d)F(d), and since inventory is discrete it's natural to assume demand is as well.

The integral then transforms into a sum over the possible value of demand, weighted by their probabilities pr(d)pr(d)

V(xt,dt)=maxโกqtโ‰ฅ0{stpโˆ’ktrโˆ’c1{qt>0}+ฮฒโˆซV(kt,d)โˆ‚F(d)}=maxโกqtโ‰ฅ0{stpโˆ’ktrโˆ’c1{qt>0}+ฮฒโˆ‘dV(kt,d)pr(d)}\begin{array}{rcl} V(x_t,d_t) &=& \max_{q_t \ge 0} \Big\{ s_t p - k_t r - c \mathbb{1}\{q_t>0\} + \beta \int V\big( k_t, d \big) \partial F(d) \Big\} \\ &=& \max_{q_t \ge 0} \Big\{ s_t p - k_t r - c \mathbb{1}\{q_t>0\} + \beta \sum_d V\big( k_t, d \big) pr(d) \Big\} \end{array}

First deterministic demandยถ

Letโ€™s focus first on a deterministic case: let dd be fixed and constant over time. How does the Bellman equation change?

In the deterministic case with fixed dd, it can be simply dropped from the state space, and the Bellman equation can be simplified to

V(xt)=maxโกqtโ‰ฅ0{pminโก{xt,d}โˆ’r[maxโก{xtโˆ’d,0}+qt]โˆ’c1{qt>0}V(x_t) = \max_{q_t \ge 0} \Big\{ p \min\{x_t,d\} - r \big[ \max\{x_t-d,0\} + q_t \big] - c \mathbb{1}\{q_t>0\}
+ฮฒV(maxโก{xtโˆ’d,0}+qt)}+ \beta V\big( \max\{x_t-d,0\} + q_t \big) \Big\}

How does this convert to code? What is in rows, what is in columns?

Source
Inventory model labeled "test"
Paramters (c,p,r,ฮฒ) = (3.2,2.5,0.5,0.95)
Demand=4
Upper bound on inventory 10
Current profits with zero orders
 [ 0.   2.5  5.   7.5 10.   9.5  9.   8.5  8.   7.5  7. ]
Current inventory
 [ 0  1  2  3  4  5  6  7  8  9 10]
Current sales
 [0 1 2 3 4 4 4 4 4 4 4]
Current orders
 [[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]]
Next period inventory
 [[ 0  0  0  0  0  1  2  3  4  5  6]
 [ 1  1  1  1  1  2  3  4  5  6  7]
 [ 2  2  2  2  2  3  4  5  6  7  8]
 [ 3  3  3  3  3  4  5  6  7  8  9]
 [ 4  4  4  4  4  5  6  7  8  9 10]
 [ 5  5  5  5  5  6  7  8  9 10 11]
 [ 6  6  6  6  6  7  8  9 10 11 12]
 [ 7  7  7  7  7  8  9 10 11 12 13]
 [ 8  8  8  8  8  9 10 11 12 13 14]
 [ 9  9  9  9  9 10 11 12 13 14 15]
 [10 10 10 10 10 11 12 13 14 15 16]]
Current profits
 [[ 0.   2.5  5.   7.5 10.   9.5  9.   8.5  8.   7.5  7. ]
 [-3.7 -1.2  1.3  3.8  6.3  5.8  5.3  4.8  4.3  3.8  3.3]
 [-4.2 -1.7  0.8  3.3  5.8  5.3  4.8  4.3  3.8  3.3  2.8]
 [-4.7 -2.2  0.3  2.8  5.3  4.8  4.3  3.8  3.3  2.8  2.3]
 [-5.2 -2.7 -0.2  2.3  4.8  4.3  3.8  3.3  2.8  2.3  1.8]
 [-5.7 -3.2 -0.7  1.8  4.3  3.8  3.3  2.8  2.3  1.8  1.3]
 [-6.2 -3.7 -1.2  1.3  3.8  3.3  2.8  2.3  1.8  1.3  0.8]
 [-6.7 -4.2 -1.7  0.8  3.3  2.8  2.3  1.8  1.3  0.8  0.3]
 [-7.2 -4.7 -2.2  0.3  2.8  2.3  1.8  1.3  0.8  0.3 -0.2]
 [-7.7 -5.2 -2.7 -0.2  2.3  1.8  1.3  0.8  0.3 -0.2 -0.7]
 [-8.2 -5.7 -3.2 -0.7  1.8  1.3  0.8  0.3 -0.2 -0.7 -1.2]]

Backwards inductionยถ

Backwards induction algorithm is used to solve finite horizon models

Solver for the finite horizon dynamic programming problems

1. Start at t=T
1. Solve Bellman equation at t, record optimal choice  
1. Decrease t unless t=1, and return to previous step.  

As result, for all t=1,..,T have found the optimal choice (as a function of state)

First, we need to code up the Bellman equation

Source
Value =
[ 0.   2.5  5.   7.5 10.   9.5  9.   8.5  8.   7.5  7. ]
Policy =
[0 0 0 0 0 0 0 0 0 0 0]

Value =
[ 4.3    6.8    9.3   11.8   14.3   14.3   14.3   15.625 17.5   16.525
 15.55 ]
Policy =
[4 4 4 4 4 3 2 0 0 0 0]

Value =
[ 9.425 11.925 14.425 16.925 19.425 19.425 19.425 19.71  21.585 21.085
 20.585]
Policy =
[8 8 8 8 8 7 6 0 0 0 0]

Source
Time period 5

[[ 0.   0.   0.   0.   0. ]
 [ 0.   0.   0.   0.   2.5]
 [ 0.   0.   0.   0.   5. ]
 [ 0.   0.   0.   0.   7.5]
 [ 0.   0.   0.   0.  10. ]
 [ 0.   0.   0.   0.   9.5]
 [ 0.   0.   0.   0.   9. ]
 [ 0.   0.   0.   0.   8.5]
 [ 0.   0.   0.   0.   8. ]
 [ 0.   0.   0.   0.   7.5]
 [ 0.   0.   0.   0.   7. ]] 

Time period 4

[[ 0.     0.     0.     4.3    0.   ]
 [ 0.     0.     0.     6.8    2.5  ]
 [ 0.     0.     0.     9.3    5.   ]
 [ 0.     0.     0.    11.8    7.5  ]
 [ 0.     0.     0.    14.3   10.   ]
 [ 0.     0.     0.    14.3    9.5  ]
 [ 0.     0.     0.    14.3    9.   ]
 [ 0.     0.     0.    15.625  8.5  ]
 [ 0.     0.     0.    17.5    8.   ]
 [ 0.     0.     0.    16.525  7.5  ]
 [ 0.     0.     0.    15.55   7.   ]] 

Time period 3

[[ 0.     0.     9.425  4.3    0.   ]
 [ 0.     0.    11.925  6.8    2.5  ]
 [ 0.     0.    14.425  9.3    5.   ]
 [ 0.     0.    16.925 11.8    7.5  ]
 [ 0.     0.    19.425 14.3   10.   ]
 [ 0.     0.    19.425 14.3    9.5  ]
 [ 0.     0.    19.425 14.3    9.   ]
 [ 0.     0.    19.71  15.625  8.5  ]
 [ 0.     0.    21.585 17.5    8.   ]
 [ 0.     0.    21.085 16.525  7.5  ]
 [ 0.     0.    20.585 15.55   7.   ]] 

Time period 2

[[ 0.      13.30575  9.425    4.3      0.     ]
 [ 0.      15.80575 11.925    6.8      2.5    ]
 [ 0.      18.30575 14.425    9.3      5.     ]
 [ 0.      20.80575 16.925   11.8      7.5    ]
 [ 0.      23.30575 19.425   14.3     10.     ]
 [ 0.      23.30575 19.425   14.3      9.5    ]
 [ 0.      23.30575 19.425   14.3      9.     ]
 [ 0.      24.57875 19.71    15.625    8.5    ]
 [ 0.      26.45375 21.585   17.5      8.     ]
 [ 0.      25.95375 21.085   16.525    7.5    ]
 [ 0.      25.45375 20.585   15.55     7.     ]] 

Time period 1

[[17.9310625 13.30575    9.425      4.3        0.       ]
 [20.4310625 15.80575   11.925      6.8        2.5      ]
 [22.9310625 18.30575   14.425      9.3        5.       ]
 [25.4310625 20.80575   16.925     11.8        7.5      ]
 [27.9310625 23.30575   19.425     14.3       10.       ]
 [27.9310625 23.30575   19.425     14.3        9.5      ]
 [27.9310625 23.30575   19.425     14.3        9.       ]
 [28.2654625 24.57875   19.71      15.625      8.5      ]
 [30.1404625 26.45375   21.585     17.5        8.       ]
 [29.6404625 25.95375   21.085     16.525      7.5      ]
 [29.1404625 25.45375   20.585     15.55       7.       ]] 

Optimal policy:
 [[8. 8. 8. 4. 0.]
 [8. 8. 8. 4. 0.]
 [8. 8. 8. 4. 0.]
 [8. 8. 8. 4. 0.]
 [8. 8. 8. 4. 0.]
 [7. 7. 7. 3. 0.]
 [6. 6. 6. 2. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
Source
<Figure size 1200x800 with 1 Axes>
<Figure size 1200x800 with 1 Axes>
<Figure size 1200x800 with 1 Axes>
<Figure size 1200x800 with 1 Axes>

Back to stochastic demandยถ

Fixing dd was a convenience for the first implementation. Putting the randomness back changes nothing about the method โ€” backwards induction still runs from TT down to 1, one Bellman operator per period โ€” and only two things about the code.

The state is a pair. With dd random the manager knows the inventory and the realized demand when ordering, so the value function is defined on the grid of (x,d)(x,d) rather than on xx alone, and so is the policy qโ‹†(x,d)q^\star(x,d).

The Bellman maximand carries an expectation. Next period demand dโ€ฒd' is not known when the order is placed, so the continuation value is averaged over it:

Vt(x,d)=maxโกqโ‰ฅ0{pminโก{x,d}โˆ’r[maxโก{xโˆ’d,0}+q]โˆ’c1{q>0}V_t(x,d) = \max_{q \ge 0} \Big\{ p \min\{x,d\} - r \big[ \max\{x-d,0\} + q \big] - c \mathbb{1}\{q>0\}
+ฮฒโˆ‘dโ€ฒVt+1(maxโก{xโˆ’d,0}+q,dโ€ฒ)pr(dโ€ฒ)}+ \beta \sum_{d'} V_{t+1}\big( \max\{x-d,0\} + q, d' \big) pr(d') \Big\}

with the terminal period unchanged: order nothing, collect the last periodโ€™s profit,

VT(x,d)=pminโก{x,d}โˆ’rmaxโก{xโˆ’d,0}V_T(x,d) = p \min\{x,d\} - r \max\{x-d,0\}

A distribution for demand. Take it discrete, on the same grid as the inventory, so that no interpolation is needed anywhere.

Let demand follow a truncated geometric distribution on {0,1,โ€ฆ,N} \{0,1,\dots,N\} ,

pri=(1โˆ’ฮป)iฮป,i=0,1,โ€ฆ,N,pr_i = (1-\lambda)^i \lambda, \qquad i = 0,1,\dots,N,

with the last probability corrected so that the vector sums to one โ€” the truncated tail is piled onto prN pr_N . The parameter ฮปโˆˆ(0,1)\lambda \in (0,1) controls how fast demand dies off; the mean of the untruncated distribution is (1โˆ’ฮป)/ฮป(1-\lambda)/\lambda.

Source
<Figure size 1200x400 with 2 Axes>

Two things to read off the left panel. The distribution is decreasing, so on this specification small demands are the likely ones, and ฮป\lambda moves the mean without changing that shape. And the mass sitting on the last grid point is the truncation: at ฮป=0.15\lambda=0.15 it is 0.017, small but not nothing, which is the price of capping demand at the top of the inventory grid. If a run of the model ever leans on that point, the grid is too short.

The right panel is what the manager actually faces โ€” one path of realized demand. There is no smoothness to exploit and no trend to extrapolate: each period is an independent draw, which is exactly the assumption that lets us write the expectation as a sum with fixed weights.

  • The sum over dโ€ฒd' sits inside the maximization over qq. Does it have to?

  • The value function is now a matrix rather than a vector. Which axis should hold what, and where does the expectation go in a vectorized implementation?

  • Does the managerโ€™s order depend on xx and dd separately, or only on some combination of them? Look at the timing of events again.

Origin of the term Dynamic Programmingยถ

The 1950โ€™s were not good years for mathematical research. We had a very interesting gentleman in Washington named Wilson. He was Secretary of Defence, and he actually had a pathological fear and hatred of the word โ€œresearchโ€. Iโ€™m not using the term lightly; Iโ€™m using it precisely. His face would suffuse, he would turn red, and he would get violent if people used the term, research, in his presence. You can imagine how he felt, then, about the term, mathematical. Hence, I felt I had to do something to shield Wilson and the Air Force from the fact that I was really doing mathematics inside the RAND Corporation.

What title, what name, could I choose?

In the first place, I was interested in planning, in decision-making, in thinking. But planning, is not a good word for various reasons. I decided therefore to use the word, โ€œprogrammingโ€. I wanted to get across the idea that this was dynamic, this was multistage, this was time-varying.

I thought, letโ€™s kill two birds with one stone. Letโ€™s take a word which has an absolutely precise meaning, namely dynamic, in the classical physical sense. It also has a very interesting property as an adjective, and that is itโ€™s impossible to use the word, dynamic, in the pejorative sense.

Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it as an umbrella for my activities.

๐Ÿ“– Bellmanโ€™s autobiography โ€œThe Eye of the Hurricaneโ€

Referencesยถ
  1. Aguirregabiria, V., & Mira, P. (2010). Dynamic discrete choice structural models: A survey. Journal of Econometrics, 156(1), 38โ€“67. 10.1016/j.jeconom.2009.09.007
  2. Carroll, C. D. (2006). The method of endogenous gridpoints for solving dynamic stochastic optimization problems. Economics Letters, 91(3), 312โ€“320. 10.1016/j.econlet.2005.09.013
  3. Iskhakov, F. (2015). Multidimensional endogenous gridpoint method: Solving triangular dynamic stochastic optimization problems without root-finding operations. Economics Letters, 135, 72โ€“76. 10.1016/j.econlet.2015.07.033
  4. Iskhakov, F., Jรธrgensen, T., Rust, J., & Schjerning, B. (2017). The endogenous grid method for discrete-continuous dynamic choice models with (or without) taste shocks. Quantitative Economics, 8(2), 317โ€“365. https://doi.org/10.3982/QE643
  5. Rust, J. (2016). Dynamic programming. In The new palgrave dictionary of economics (pp. 1โ€“26). Palgrave Macmillan UK. https://doi.org/10.1057/978-1-349-95121-5โ‚932-1
  6. Sargent, T. J., & Stachurski, J. (2025). Dynamic Programming: Finite States. Cambridge University Press. 10.1017/9781009540780
  7. Adda, J., & Cooper, R. W. (2023). Dynamic Economics: Quantitative Methods and Applications. MIT Press. https://mitpress.mit.edu/9780262547888/dynamic-economics