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.

So far every dynamic choice in the course was discrete, and the maximum in the Bellman equation was taken over a short list. With a continuous choice the maximum is an optimization problem in every state! We will learn a few solution techniques for the models with continuous choice in the next few classes before addressing the estimation of these models. Along the way we need to remind ourselves of function approximation.

Cake eatingΒΆ

The cake eating problem has the structure of the all so important consumption-savings model and an analytic solution, so every numerical method can be checked against the truth.

The consumer maximizes the discounted utility over the whole horizon,

max⁑{ct}t=0βˆžβˆ‘t=0∞βtu(ct),0β©½ctβ©½Wt.\max_{\{c_t\}_{t=0}^{\infty}} \sum_{t=0}^{\infty} \beta^t u(c_t), \qquad 0 \leqslant c_t \leqslant W_t.

Look how easy it is to get the familiar Bellman equation in this case. Let

Vt(Wt)=max⁑{0β©½cΟ„β©½WΟ„}Ο„=0βˆžβˆ‘Ο„=0βˆžΞ²Ο„u(cΟ„)=V_t(W_t) = \max_{\{0 \leqslant c_\tau \leqslant W_\tau\}_{\tau=0}^{\infty}} \sum_{\tau=0}^{\infty} \beta^\tau u(c_\tau) =
=max⁑0β©½ctβ©½Wt(u(ct)+Ξ²max⁑{0β©½cΟ„β©½WΟ„βˆ’ct}Ο„=1βˆžβˆ‘Ο„=1βˆžΞ²Ο„u(cΟ„))== \max_{0 \leqslant c_t \leqslant W_t} \Big( u(c_t) + \beta \max_{\{0 \leqslant c_\tau \leqslant W_\tau-c_t\}_{\tau=1}^{\infty}} \sum_{\tau=1}^{\infty} \beta^\tau u(c_\tau) \Big) =
=max⁑0β©½ctβ©½Wt(u(ct)+Ξ²Vt+1(Wtβˆ’ct))= \max_{0 \leqslant c_t \leqslant W_t} \Big( u(c_t) + \beta V_{t+1}(W_t-c_t) \Big)

The last equation is due to the fact that infinitely many time periods look the same from the point of view of today and tomorrow. Realizing further that in this case nothing can depend on index of time, we can drop the time index to arrive at

V(W)=max⁑0β©½cβ©½W{u(c)+Ξ²V(Wβˆ’c)}V(W) = \max_{0 \leqslant c \leqslant W} \big\{ u(c) + \beta V(W - c) \big\}

The right-hand side defines the Bellman operator T(V)(W)T(V)(W) from space of value functions to itself

The question of this class is how to compute T(V)T(V) when cc and WW are continuous.

  • Classify this dynamic optimization problem by type (see the classification in Class 7)

  • What is different from the models you saw in macro economics?

  • Can we solve this analytically?

The analytic solutionΒΆ

Let u(c)=log⁑(c)u(c) = \log(c), what is the solution?

c⋆(W)=(1βˆ’Ξ²) W,V(W)=log⁑W1βˆ’Ξ²+log⁑(1βˆ’Ξ²)1βˆ’Ξ²+Ξ²log⁑β(1βˆ’Ξ²)2c^\star(W) = (1-\beta)\, W, \qquad V(W) = \frac{\log W}{1-\beta} + \frac{\log(1-\beta)}{1-\beta} + \frac{\beta \log\beta}{(1-\beta)^2}

The consumer eats a constant fraction 1βˆ’Ξ²1-\beta of what is left, every period.

The analytic solution exists only because the model is so simple

Its value, at least here, as well as any analytic property is the ability to verify numerical methods

  • Would the same approach be possible with u(c)=cu(c) = \sqrt{c}?

  • The Bellman operator is a contraction. Given this, how should we compute the maximum on its right-hand side?

Solving on a gridΒΆ

The simplest numerical approach replaces both the state and the choice with a finite grid, Wβƒ—={W1,…,Wn}\vec{W} = \{W_1, \dots, W_n\} on [0,Wβ€Ύ][0, \overline{W}]. To avoid evaluating VV between grid points, make the next period cake the decision variable so we have:

Vi(Wj)=max⁑Wkβ©½Wj{u(Wjβˆ’Wk)+Ξ²Viβˆ’1(Wk)},j=1,…,nV_i(W_j) = \max_{W_k \leqslant W_j} \big\{ u(W_j - W_k) + \beta V_{i-1}(W_k) \big\}, \qquad j = 1, \dots, n

Every term is known on the grid, so one iteration is a maximum over an nΓ—nn \times n matrix of choices, exactly as in the discrete models of the earlier classes:

Source
<Figure size 1200x450 with 2 Axes>
Largest error in consumption: 0.293

The value function is close, but the policy is a staircase. On this grid the consumer can only eat the difference of two grid points, so with 100 points on [0,10][0,10] every consumption level is a multiple of about 0.1, whatever the true optimum.

Where it breaks: the choice set is tied to the state grid. Refining the state grid is the only way to refine the choices, the cost of one iteration grows with n2n^2, and in a model where next period’s state is not a grid point at all, the trick is not available. That is the case as soon as savings earn interest or income is random.

  • Why is the error largest at small cakes, relative to the size of the cake?

  • With interest, Wt+1=R(Wtβˆ’ct)W_{t+1} = R(W_t - c_t) for R>1R > 1, do we have a solver in which the problem can be converted in the same way?

Function approximationΒΆ

Any method that separates the choice from the state grid needs the value function between grid points. The interpolation problem: given nodes a=x0<x1<β‹―<xn=ba = x_0 < x_1 < \dots < x_n = b and the values f(xi)f(x_i) of an unknown function, find a function ss from a chosen class with s(xi)=f(xi)s(x_i) = f(x_i) for all ii, and use s(x)s(x) in place of f(x)f(x) at any x∈[a,b]x \in [a,b].

The quality of an approximation is measured in the uniform norm aka infinity norm, βˆ₯fβˆ’sβˆ₯∞=max⁑x∈[a,b]∣f(x)βˆ’s(x)∣\lVert f - s \rVert_\infty = \max_{x \in [a,b]} |f(x) - s(x)|, the largest error anywhere on the interval, which is what matters when ss stands in for ff inside a solver.

There are two families of methods:

SplinesΒΆ

To compute spline interpolation one would need to solve the following system:

Linear interpolation is the spline of order 2: continuous, with kinks at the knots.

βˆ₯fβˆ’sβˆ₯∞⩽h28βˆ₯fβ€²β€²βˆ₯∞\lVert f - s \rVert_\infty \leqslant \frac{h^2}{8} \lVert f'' \rVert_\infty

Halving the spacing divides the error by four.

The cubic spline, need two boundary conditions

The coefficients solve a tridiagonal linear system, at a cost proportional to nn.

For a clamped spline and ff with four continuous derivatives,

βˆ₯fβˆ’sβˆ₯∞⩽5384 h4βˆ₯f(4)βˆ₯∞\lVert f - s \rVert_\infty \leqslant \frac{5}{384}\, h^4 \lVert f^{(4)} \rVert_\infty

so halving the spacing divides the error by sixteen.

In Python both are available:

Polynomial approximationΒΆ

A polynomial of degree nn through n+1n+1 distinct nodes exists and is unique.

How good it is (in terms of approximation error) depends on where the nodes are.

The error has two factors.

  1. With equally spaced nodes the node polynomial is small in the middle of the interval and grows rapidly towards its ends.

For functions whose derivatives grow fast, such as f(x)=1/(1+25x2)f(x) = 1/(1+25x^2) on [βˆ’1,1][-1,1], the error near the ends grows without bound as nn increases: the Runge phenomenon. More nodes make the approximation worse.

  1. The nodes that make max⁑x∈[βˆ’1,1]∣∏i(xβˆ’xi)∣\max_{x \in [-1,1]} \big|\prod_i (x - x_i)\big| smallest are the zeros of the Chebyshev polynomial Tn+1T_{n+1}, where the maximum equals 2βˆ’n2^{-n}, its smallest possible value.

The error bound becomes βˆ₯fβˆ’pnβˆ₯∞⩽βˆ₯f(n+1)βˆ₯∞2n(n+1)!\lVert f - p_n \rVert_\infty \leqslant \dfrac{\lVert f^{(n+1)} \rVert_\infty}{2^n (n+1)!}.

The Chebyshev polynomials are

Tk(x)=cos⁑(karccos⁑x),T_k(x) = \cos\big(k \arccos x\big),

or equivalently

T0(x)=1,β€…β€ŠT1(x)=x,Tk+1(x)=2x Tk(x)βˆ’Tkβˆ’1(x),T_0(x) = 1,\; T_1(x) = x,\quad T_{k+1}(x) = 2x\,T_k(x) - T_{k-1}(x),

The n+1n+1 zeros of Tn+1T_{n+1}, Chebyshev nodes, are

zj=cos⁑(2j+12(n+1)Ο€),j=0,…,nz_j = \cos\big(\tfrac{2j+1}{2(n+1)}\pi\big), j = 0, \dots, n

They cluster towards the ends of [βˆ’1,1][-1,1].

The polynomials are orthogonal with respect to the weight (1βˆ’z2)βˆ’1/2(1-z^2)^{-1/2}, and also discretely orthogonal on the Chebyshev nodes, so the interpolating polynomial pn(z)=βˆ‘k=0nckTk(z)p_n(z) = \sum_{k=0}^{n} c_k T_k(z) has explicit coefficients, no linear system needed:

ck=2βˆ’1{k=0}n+1βˆ‘j=0nf(zj) Tk(zj)c_k = \frac{2 - \mathbb{1}\{k = 0\}}{n+1} \sum_{j=0}^{n} f(z_j)\, T_k(z_j)

A general interval [a,b][a,b] is mapped to [βˆ’1,1][-1,1] by z=2(xβˆ’a)/(bβˆ’a)βˆ’1z = 2(x-a)/(b-a) - 1, as in the flexible logit of Class 13.

For ff with kk continuous derivatives, Chebyshev interpolation of degree nn satisfies Judd, 1998

βˆ₯fβˆ’pnβˆ₯∞⩽(2Ο€log⁑(n+1)+1)(nβˆ’k)!n!(Ο€2)k(bβˆ’a)k2kβˆ₯f(k)βˆ₯∞,\lVert f - p_n \rVert_\infty \leqslant \Big(\frac{2}{\pi}\log(n+1) + 1\Big) \frac{(n-k)!}{n!} \Big(\frac{\pi}{2}\Big)^k \frac{(b-a)^k}{2^k} \lVert f^{(k)} \rVert_\infty,

which falls faster than any power of nn when ff is smooth: Chebyshev interpolation is within a logarithmic factor of the best polynomial approximation in the uniform norm.

The four methods side by side, first with 12 nodes on a function with some curvature, then with a growing number of nodes on the Runge function:

Source
<Figure size 1400x1600 with 2 Axes>

In economic models we control the three things that determine the accuracy:

Where it breaks:

Discretized choice with interpolationΒΆ

With an interpolated value function the choice no longer has to live on the state grid.

Keep the grid W⃗\vec{W} for the state, and give the choice its own grid D⃗(Wj)\vec{D}(W_j) for every state point.

Scale to the feasible interval [0,Wj][0, W_j] so that the constraint 0β©½cβ©½W0 \leqslant c \leqslant W is respected by construction:

Vi(Wj)=max⁑c∈Dβƒ—(Wj){u(c)+Ξ²V^iβˆ’1(Wjβˆ’c)}V_i(W_j) = \max_{c \in \vec{D}(W_j)} \big\{ u(c) + \beta \hat V_{i-1}(W_j - c) \big\}

where V^iβˆ’1\hat V_{i-1} is the interpolated value function and ii denotes the VFI iteration index

  • Where in the code below are the grids?

  • Why not use common grid over choices?

  • Where is interpolation in the code and which algorithm is used?

Source
<Figure size 1200x450 with 2 Axes>
Largest error in consumption: 0.060
state grid  choice grid  max error in c
        50           50          0.1162
       100          100          0.0597
       100          500          0.0365
       500          100          0.0106
       500          500          0.0121

The policy is now five times closer to the truth with the same number of points. The table shows that both grids matter. A choice grid of ncn_c shares pins consumption down to within W/ncW/n_c, but here the grid of shares happens to contain a point within 0.001 of the optimal share 1βˆ’Ξ²1-\beta, so most of the remaining error comes from the interpolation of V^\hat V on the state grid. The error is not even monotone in the grid sizes, a warning against reading accuracy off a single run.

How to increase the accuracy further:

  • Linear interpolation of VV under-states it between the grid points, because VV is concave. Which consumption choices does that bias favor?

  • Why does the error not disappear when the state grid is refined with the choice grid held fixed?

The consumption-savings modelΒΆ

The cake eating model becomes the workhorse consumption-savings model of Deaton (1991) once savings earn interest and the consumer has an income:

V(M)=max⁑0β©½cβ©½M{u(c)+β Ey V(R(Mβˆ’c)+y~⏟=Mβ€²)}V(M) = \max_{0 \leqslant c \leqslant M} \Big\{ u(c) + \beta\, \mathbb{E}_{y}\, V\big(\underbrace{R(M - c) + \tilde{y}}_{=M'}\big) \Big\}

Cake eating is the special case R=1R = 1 and y~=0\tilde{y} = 0.

The timing of the model separates the choice from the uncertainty:

Mβ€…β€Šβ†’β€…β€Šc(M)β€…β€Šβ†’β€…β€ŠA=Mβˆ’c(M)β€…β€Šβ†’β€…β€ŠMβ€²=RA+y~M \;\rightarrow\; c(M) \;\rightarrow\; A = M - c(M) \;\rightarrow\; M' = RA + \tilde{y}

Savings AA, the post-decision state, carry all the information about the current period that next period needs, and the constraint 0β©½cβ©½M0 \leqslant c \leqslant M is the same as 0β©½Aβ©½M0 \leqslant A \leqslant M.

Computing expectationsΒΆ

Let income be log-normal, log⁑y~∼N(0,Οƒ2)\log\tilde{y} \sim N(0, \sigma^2), so y~>0\tilde{y} > 0 and Ey~=exp⁑(Οƒ2/2)\mathbb{E}\tilde{y} = \exp(\sigma^2/2). The expectation in the Bellman equation is an integral, and it has to be computed at every state and every choice on every iteration. We approximate it by quadrature, a weighted sum over fixed nodes yky_k with weights Ο‰k\omega_k:

EyV(RA+y~)=∫V(RA+y) dF(y)β‰ˆβˆ‘k=1KΟ‰k V(RA+yk)\mathbb{E}_y V(RA + \tilde y) = \int V(RA + y)\, dF(y) \approx \sum_{k=1}^{K} \omega_k\, V(RA + y_k)

How to choose the nodes and the weights, and how quadrature compares with Monte Carlo integration, is covered in Class 16.

Both discretized solvers carry over to this model, with one difference. On the grid, next period wealth is chosen from the state grid and consumption is backed out as c=Mβˆ’(Mβ€²βˆ’y)/Rc = M - (M' - y)/R, which works only when income is deterministic: with random income Mβ€²M' is not a choice, so the on-grid trick fails. With a choice grid, consumption is the choice and V^\hat V is interpolated at Mβ€²=R(Mβˆ’c)+ykM' = R(M-c) + y_k for every quadrature node.

The Euler equationΒΆ

The first order condition for an interior choice and the envelope theorem,

uβ€²(c⋆)=Ξ²R EyVβ€²(R(Mβˆ’c⋆)+y~),Vβ€²(M)=Ξ²R EyVβ€²(R(Mβˆ’c⋆)+y~),u'(c^\star) = \beta R\, \mathbb{E}_y V'\big(R(M - c^\star) + \tilde y\big), \qquad V'(M) = \beta R\, \mathbb{E}_y V'\big(R(M - c^\star) + \tilde y\big),

give Vβ€²(M)=uβ€²(c⋆(M))V'(M) = u'(c^\star(M)), and together the Euler equation

uβ€²(c⋆(M))=Ξ²R Ey uβ€²(c⋆(R[Mβˆ’c⋆(M)]+y~⏟=Mβ€²))u'\big(c^\star(M)\big) = \beta R\, \mathbb{E}_y\, u'\Big(c^\star\big(\underbrace{R[M - c^\star(M)] + \tilde{y}}_{=M'}\big)\Big)

The marginal utility of consuming today equals the discounted expected marginal utility of saving it and consuming tomorrow. When the borrowing constraint binds, c⋆=Mc^\star = M and the left side is larger. The Euler equation holds for the exact solution, whichever method computed it, so it is the basis of the solvers of the next two classes and of the accuracy checks for all of them.

Checking the accuracy of a solutionΒΆ

The cake eating model has an analytic solution; the consumption-savings model does not. Two checks still measure the accuracy of any numerical solution: a special case of the model with a sharp prediction, and the Euler equation, which holds at every point.

Constant consumption: Keane’s testΒΆ

Take the deterministic model, y~=y\tilde{y} = y with certainty, and let Ξ²R=1\beta R = 1. Then the Euler equation becomes

uβ€²(c⋆(M))=uβ€²(c⋆(Mβ€²))β‡’c⋆(M)=c⋆(Mβ€²)u'\big(c^\star(M)\big) = u'\big(c^\star(M')\big) \quad\Rightarrow\quad c^\star(M) = c^\star(M')

so as long as the borrowing constraint does not bind, consumption is the same in every period. Along such a path wealth must also stay constant, otherwise it diverges. From M0β©ΎyM_0 \geqslant y the consumer eats the annuity value of the wealth,

c⋆=(Rβˆ’1) M0+yR,Mt=M0Β forΒ allΒ t,c^\star = \frac{(R-1)\, M_0 + y}{R}, \qquad M_t = M_0 \text{ for all } t,

and saves A=(M0βˆ’y)/Rβ©Ύ0A = (M_0 - y)/R \geqslant 0, so the constraint never binds.

Keane’s test uses this prediction: solve the deterministic model with Ξ²R=1\beta R = 1 numerically, simulate the consumer from some initial wealth, and check that the simulated consumption profile is flat, at the level c⋆c^\star. An accurate solver passes. An inaccurate one produces consumption that drifts up or down over time. The error of the policy function accumulates through wealth, so the test magnifies small errors that a plot of the policy function hides.

The test is cheap and has a known answer, but it checks the solution only along the simulated paths, and only for one parameter combination. It is a necessary condition for an accurate solver, not a sufficient one.

Euler equation residualsΒΆ

The Euler equation holds for the exact solution at every point where the borrowing constraint does not bind, at any parameter values. For a numerical policy c(M)c(M), interpolated between the grid points, the relative Euler equation residual

Ξ΅(M)=uβ€²(c(M))βˆ’Ξ²R Ey uβ€²(c(R(Mβˆ’c(M))+y~))uβ€²(c(M))\varepsilon(M) = \frac{u'\big(c(M)\big) - \beta R\, \mathbb{E}_y\, u'\Big(c\big(R(M - c(M)) + \tilde y\big)\Big)}{u'\big(c(M)\big)}

measures how far the policy is from satisfying it, as a fraction of marginal utility. It is computed on a grid much denser than the solver’s, to catch the errors between the grid points, and only where c(M)<Mc(M) < M: at a corner solution the Euler equation holds as an inequality, and the residual is not an error.

The two checks complement each other. The Euler residuals apply at any parameter values and at every point of the state space, but only to one step of the dynamics. The constant consumption check applies to one special case, but follows the errors as they accumulate over a whole simulated path. The homework runs both on the two solvers with discretized choice of this class.

  • Why must wealth stay constant when consumption is constant, Ξ²R=1\beta R = 1 and income is deterministic? What happens to wealth if consumption is a little too high?

  • Can a solver pass Keane’s test and still be inaccurate? Can it have small Euler residuals and still fail Keane’s test?

ReferencesΒΆ
  1. Deaton, A. (1991). Saving and Liquidity Constraints. Econometrica, 59(5), 1221–1248. 10.2307/2938366
  2. Judd, K. L. (1998). Numerical Methods in Economics (p. 633). MIT Press. https://mitpress.mit.edu/9780262100717/numerical-methods-in-economics/