NM4M
NM4M
RICARDO MARTO U N I V E R S I T Y O F P E N N S Y LV A N I A
NUMERICAL
METHODS FOR
MACROECONOMISTS
W I T H J U L I A A N D M AT L A B C O D E S
Contents
1 Introduction 9
2 Nonlinear Equations 15
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Labor-Leisure Choice . . . . . . . . . . . . . . . . . . . . . 16
2.3 Government Spending and Taxation . . . . . . . . . . . . 24
2.4 General Equilibrium . . . . . . . . . . . . . . . . . . . . . 31
2.5 Equivalent and Compensating Variations . . . . . . . . . 34
2.6 Solving Nonlinear Equations Numerically . . . . . . . . 37
2.7 Heterogenous Agents . . . . . . . . . . . . . . . . . . . . . 44
2.8 MATLAB: A Worked-Out Example . . . . . . . . . . . . . 47
5 Graphing 89
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.2 William Playfair . . . . . . . . . . . . . . . . . . . . . . . . 89
5.3 Some Basic Principals . . . . . . . . . . . . . . . . . . . . 90
4 jeremy greenwood university of pennsylvania ricardo marto university of pennsylvania
6 Deterministic Dynamics 97
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.2 The World of Robinson Crusoe . . . . . . . . . . . . . . . 98
6.3 The Euler Equation . . . . . . . . . . . . . . . . . . . . . . 99
6.4 The Steady State . . . . . . . . . . . . . . . . . . . . . . . . 102
6.5 Dynamic Programming Formulation . . . . . . . . . . . . 103
6.6 Consumption Smoothing . . . . . . . . . . . . . . . . . . 107
6.7 Dynamics . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.8 The Value Function: A More Formal Analysis . . . . . . 112
6.9 A Linear-Quadratic Optimization Problem . . . . . . . . 121
6.10 Adding a Labor-Leisure Choice . . . . . . . . . . . . . . . 124
6.11 The Taxation of Capital and Labor Income . . . . . . . . 125
6.12 The Extended Path and Multiple Shooting Algorithms . 127
6.13 MATLAB: A Worked-Out Example . . . . . . . . . . . . . 132
References 275
numerical methods for macroeconomists with julia and matlab codes 7
This primer will cover some of the numerical methods that are used
in modern macroeconomics. You will learn how to:
1. Solve nonlinear equations via bisection and Newton’s method;
2. Compute maximization problems by golden section search, dis-
cretization, and the particle swarm algorithm;
3. Simulate difference equations using the extended path and multiple
shooting algorithms;
4. Differentiate and integrate functions numerically;
9. Solve for policy functions using the Coleman, endogenous grid, and
parameterized expectation algorithms;
10. Solve the Aiyagari heterogeneous agent model with and without
aggregate uncertainty.
This will be done while studying economic problems, such as the de-
termination of labor supply, economic growth, and business cycle anal-
ysis. Calculus is an integral part of the primer and some elementary
probability theory will be drawn upon. The MATLAB programming
language will be used. It is time to move into the modern age and
learn these techniques. Besides, using computers to solve economic
models is fun. The primer is self contained so little prior knowledge is
required.
If you would like to use this book for teaching or other purposes
please contact Jeremy Greenwood:
Telephone, (215) 898-1505; Fax, (215) 746-2947;
Email, [email protected]—this email address
is reserved exclusively for matters concerning the book.
There cannot be a language more universal and more simple, more free
from errors and obscurities,....more worthy to express the invariable re-
lations of natural things than mathematics. It interprets all phenomena
by the same language, as if to attest the unity and simplicity of the plan
of the universe, and to make still more evident that unchangeable order
which presides over all natural causes.
Joseph Fourier, Analytical Theory of Heat, 1822
Of course, not all models have all of these ingredients. Some mod-
els don’t have consumers, others don’t have firms, and yet others ex-
clude governments; there can be various combinations of these factors.
Economies can be studied using old-fashioned pencil-and-paper tech-
niques and/or modern numerical methods. Pencil-and-paper tech-
niques are useful for developing propositions and theorems about
economies. As model economies become more sophisticated it be-
comes increasingly difficult to develop propositions and theorems.
Computers can be used to develop properties about economies, just
as they are used in aerospace engineering to develop properties about
air and spacecraft. Additionally, they allow for concrete quantitative
predictions that are useful for policymakers, which are devoid in gen-
eral mathematical analyzes.
Static economies can often be characterized as either solutions to
nonlinear equation systems or as solutions to maximization problems
in conjunction with an algorithm mimicking a Walrasian auctioneer.
Chapter 2 studies labor supply in a static setting. This is done with
and without government spending and taxation. It shows how this
problem can be setup as the solution to a nonlinear equation. Various
properties of the labor supply problem are established using pencil-
and-paper techniques. The chapter then turns to discussing how this
problem can be solved numerically using a nonlinear equation solver
employing either the bisection algorithm or Newton’s method. At the
end of the chapter, a MATLAB program is presented that solves a
monopolist’s pricing problem in static setting. Later on in Chapters 6
and 9, this example is made both dynamic and dynamic, stochastic.
In Chapter 3 the problem is recast as the solution to a maximization
problem in conjunction with a Walrasian auctioneer. Three techniques
introduction 11
2.1 Introduction
Z (h) = 0. (2.1.1)
u = U (c)
represent the utility function for consumption. It gives the level of hap-
piness, u, that person realizes if they consume the amount, c. Utility is
an ordinal concept, not an cardinal one. It specifies how different con-
sumption levels are ranked. The precise numbers assigned to partic-
ular consumption levels are meaningless. It’s important to remember
this when conducting welfare experiments, as will be discussed in Sec-
tion 2.5. Some typical properties imposed jointly on a utility function,
or on the ordinal ranking, are:
U (c) = ln c (logarithmic);
U (c) = c1−ρ /(1 − ρ) − 1/(1 − ρ), with ρ ≥ 0 (isoelastic);
U (c) = −e−γc , with γ > 0 (exponential);
U (c) = αc − βc2 /2, with α, β > 0 and for c < α/β (quadratic).
Likewise, let
v = V (1 − h )
-0 .5
returns a negative value for util-
-1 .0 ity, u. The crra and ln utility
0 1 2 2 .5 3 4
ln functions can yield both nega-
1 .1
tive and positive values for util-
p o s itiv e ,↑
0 .0 ity. When ρ > 1 the crra utility
n e g a tiv e ,↓
-1 .1
function is more concave than
the ln one and less so when ρ <
-2 .2 c rra
1. The quadratic utility function
0 1 2 3 4 declines when c > α/β. Hence,
c , c o n s u m p tio n
it is only good for c < α/β.
The peak of the quadratic util-
ity function occurs at c = α/β =
that he enjoys the fraction 1 − h in leisure. Leisure can be thought of 2.5.
as a good, just as consumption is, so the properties for V are the same
as those imposed on U.
c = wh + a.
U1 (wh + a)w − V1 (1 − h) = 0,
h hours worked
Here the marginal benefit of working (at h = 0) is less than its marginal
cost. It’s likely that there exists a large enough value for a such that
the corner solution holds for sure. For example, think about the situa-
tion where lima→∞ U1 ( a) = 0. The person is so wealthy that an extra
unit of consumption is worthless to them. In this case (not shown)
the marginal cost curve in Figure 2.2.3 would lie above the marginal
benefit curve at h = 0, which is flat at zero for all values of h.
The impact that shifts in wealth, a, and wages, w, have on labor
supply, h, are now analyzed. The notions of income and substitution
effects, as advanced by Hicks (1939) and Slutsky (1915), come into play
here. Sir John Hicks (1904-1989) was a
British economist. Hicks won the
Nobel Prize in Economics in 1972.
The impact of an increase in wealth, a He brought many important ideas
Suppose the worker is wealthier; i.e., increase a. By examining the into economics: income and
substitution effects, compensating
righthand side of equation (2.2.1), it is clear that the marginal cost
and equivalent variations, and the
curve in Figure 2.2.3 is not a function of a. From the lefthand side of IS-LM model.
nonlinear equations 21
MB, MC
Figure 2.2.3: Labor-leisure
LHS, MB
choice. The optimal level of
RHS, MC
hours worked, h, occurs where
the marginal benefit from work-
ing, MB, equals its marginal
cost, MC–compare with equa-
tion (2.2.1). An increase in
wealth, a, causes the MB curve
to shift down, which results in a
h’ h hours worked
decline in hours worked from h
to h0 . This illustrates the wealth
effect on labor supply.
(2.2.1) it can be seen that the marginal benefit curve is. In particular,
for any given level of h, an increase in a will cause c = wh + a to
rise. Hence U1 (wh + a) falls, due to diminishing marginal utility or
the fact that the utility function is strictly concave. Thus, the marginal
benefit curve shifts down. This results in a fall in labor supply, h–
see Figure 2.2.3. When the person gets wealthier, they would like to
spread the windfall across both consumption and leisure. They won’t
use all of the windfall for consumption because the marginal utility
of consumption is declining so that each extra unit of consumption
is worth less and less. Hence some of the gain in wealth should be
directed toward leisure.
To obtain the mathematical transliteration of the graphical analysis
take the total differential of (2.2.1) with respect to a and h. This gives
which yields
dh −U11 (wh + a)w
= < 0. (2.2.2)
da U11 (wh + a)w2 + V11 (1 − h)
The concepts of total differentials and total derivatives are reviewed
in Chapter A. The sign of the above expression results from the fact
that U11 (wh + a) and V11 (1 − h) are both negative because the utility
functions for goods and leisure are assumed to be strictly concave.
As will be seen, the term on the righthand side of (2.2.2) is closely
connected with the income effect from a rise in wages.
U11 (wh + a)w2 dh + U11 (wh + a)hwdw + U1 (wh + a)dw = −V11 (1 − h)dh,
(2.2.3)
1 1
θ × w = (1 − θ ) .
wh + a 1−h
| {z } | {z }
MB MC
θw − (1 − θ ) a a
h= = θ − (1 − θ ) ,
w w
at least when there is an interior solution. Note that it is possible for
h = 0, which occurs when the above equation returns a negative solu-
tion for h. A negative value for hours worked, h, is invalid; the lowest
it can be is zero. There are three cases to consider.
(i) Labor supply, h, is increasing in wages , w, when a > 0. Hence,
the substitution effect is larger than the income effect. As an illus-
tration of this case, think of a married household where the husband
works a fixed work week and a is his income. Here, w represents the
nonlinear equations 23
1 hη
× w = ,
c − h 1+ η / (1 + η ) c − h1+ η / (1 + η )
so that
h = w1/η .
24 numerical methods for macroeconomists with julia and matlab codes
g+λ = |{z}
τwh . (2.3.1)
| {z }
disbursements receipts
nonlinear equations 25
To calculate the effect of taxation on labor supply follow the path out-
lined in Chapter 1:
1. Solve the individual’s labor-leisure choice with labor taxation and
transfer payments to obtain the individual’s first-order condition.
It is easy to see intuitively how the government can affect the worker.
First, the presence of taxation distorts the person’s labor supply deci-
sion, as shown by the (1 − τ ) term. This works as a negative substitu-
tion effect. Second, the government takes away some of the economy’s
resources, as reflected by the g term. This operates as a negative in-
come effect–the situation by portrayed in Figure 2.3.1. If there is no
government spending, such as when all revenue is rebated back as
transfer payments, there will be no income effect associated with the
taxation. The case of transfers is discussed next.
c = (1 − τ )wh + a + λ = wh + a.
or equivalently just set g = 0 in (2.3.3). The way taxes enter the above
expression suggests that only the substitution effect will be opera-
tional.
Now an increase in taxes reduces the marginal benefit from work-
ing. Hence, labor supply falls. Parroting the above exercises one gets
dh U1 (wh + a)w
= < 0.
dτ U11 (wh + a)(1 − τ )w2 + V11 (1 − h)
This is just the substitution effect–compare with the second term in
(2.2.3). In terms of Figure 2.2.3 an increase in taxes will shift the MB
curve down for any given value of h. This transpires because the after-
tax wage rate, (1 − τ )w, drops. The MC curve remains fixed.
Remark 5. (The meaning of representative agent models) The construct
of a representative agent is a stand-in device for millions of identical
individuals each maximizing their own welfare while taking the ac-
tions of other parties in the economy as given. A huge blunder for
a macroeconomist to make is to substitute the government’s budget
constraint into the consumer/worker’s one before the maximization
is done. If this is done, then (2.2.1) will reappear instead of (2.3.2).
Hence, there would be no apparent effect of taxes on labor supply. To
understand the mistake suppose that there are n identical agents in
the economy. Let the representative agent choose labor supply in the
amount h. Suppose that the other n − 1 people pick h. Of course in
equilibrium h = h, because they are all the same. The government’s
budget constraint can be written as
or
λ = τwh/n + (n − 1)τwh/n.
The person knows if he works one unit of time more, and no one else
does, his transfers will increase by τw/n. The person’s maximization
problem is now
where λ has been solved out for in his budget constraint. The individ-
ual cannot tell his neighbor what to do, so he must take h as given in
this maximization problem. The agent’s first-order condition is
Consider the case where all tax revenue is used to finance government
spending on goods and services, g. Since the consumer/worker does
not value the government spending, g does not enter his utility func-
tion, in contrast to the case of valued government spending discussed
below. Since there are no lump-sum transfers, λ = 0 so that
τwh = g.
Whether hours worked will fall or rise depends on whether the sub-
stitution or income effect dominates.
U (c + ωg) + V (1 − h),
U ( c ) + G ( g ) + V (1 − h ).
Again, observe that this is one equation in one unknown. Note that the
marginal tax rate, T1 (wh), is higher than the average one, T (wh)/wh,
because the tax function is convex. When considering the disincentive
effect of distortional taxation it is important to use the marginal tax
rate and not the average one. It is the marginal tax rate that governs
the substitution effect, not the average one. The difference between
average and marginal tax rates will be touched upon in Chapter 4,
which discusses why American work more than Europeans.
nonlinear equations 31
Production will now be introduced into the above setting, which brings
into the analysis the notion of a production function. Output, o, will Leon Walras (1834-1910) was a
now be produced using capital, k, in addition to labor, h. Capital French economist at the University
is assumed to be in fixed supply. This assumption is abandoned in of Lausanne. He is best known for
his book Éléments d’économie
Chapter 6, where the supply of capital is endogenously determined.
politique pure. This book founded
general equilibrium theory. He
2.4.1 derived Walras’s law that states the
Production Functions
sum of excess demands across
Assume that output, o, can be produced with capital, k, and labor, h, in markets must sum to zero. This
line with the following constant-returns-to-scale production function: implies that any given market must
be in equilibrium, if all other
markets are in equilibrium.
o = F (k, h).
Generally, it is assumed that inputs and outputs are all positive. So,
some typical properties imposed on a production function are:
The linear production is not strictly concave, just concave. Also, the
quadratic production function is not always increasing in k and l so
the restriction on the bottom line is needed. It does not satisfy the
constant-returns-to-scale assumption, either.
F2 (k, h) = w
|{z} . (2.4.1)
| {z }
marginal product of labor marginal cost
The situation is portrayed in Figure 2.4.1. The firm can hire as much
labor as it desires at the wage w. The function F2 implicitly defines
the demand for labor. It shows the marginal product of the last unit of
labor hired. Due to diminishing returns, or the fact that F22 < 0, this
schedule is downward sloping. The firm hires up to the point where
the marginal product of labor equals the wage rate.
nonlinear equations 33
F (k, h) = r , (2.4.2)
| 1 {z } |{z}
marginal product of capital marginal cost
h* labor, h
2.4.3 Equilibrium
To characterize the determination of hours worked in the economy
(with taxes) solve out for w using (2.4.1) in the consumer/worker’s
first-order condition for labor (2.3.2) to get
One should solve out for w after solving the representative consumer/-
worker’s problem. Next, substitute out for λ using the government’s
budget constraint (2.3.1) to obtain
One might think that the individual owns the economy’s fixed capi-
tal stock. Each period capital, k, will earn its marginal product, F1 (k, h).
Thus, it is reasonable to let a = rk = F1 (k, h)k. That is, the worker earns
34 numerical methods for macroeconomists with julia and matlab codes
rental on capital accruing from the operation of the firm. This will be
discussed in more detail later on. If one solves out for a in this fashion,
the result will still be one equation in one unknown:
U1 ( F2 (k, h)h + F1 (k, h)k − g)(1 − τ ) F2 (k, h) = V1 (1 − h),
| {z }
c
so that
U1 ( F (k, h) − g)(1 − τ ) F2 (k, h) = V1 (1 − h), (2.4.3)
because by the constant-returns-to-scale assumption1 1
See Chapter A for a proof of Euler’s
theorem.
F2 (k, h)h + F1 (k, h)k = F (k, h) (Euler’s Theorem).
With constant returns to scale, payments to the factors of production,
F2 (k, h)h + F1 (k, h)k = wh + rk, completely exhaust output, F (k, h), so
that no economics profits are earned; i.e., F (k, h) − wh − rk = 0. Now,
equation (2.4.3) represents one equation in one unknown, h. By the
implicit function theorem, a solution of the form h = H (k, τ, g) exits.
It’s easy to see that (2.4.3) fits the form of (2.1.1); just write
Z (h) ≡ U1 ( F (k, h) − g)(1 − τ ) F2 (k, h) − V1 (1 − h) = 0. (2.4.4)
So embedded in this single equation are the outcome of the consumer/-
worker’s labor-leisure choice problem, the upshot of the firm’s profit
maximization problem, the government’s budget constraint, and market-
clearing conditions.
U (c A (1 + e)) + V (1 − h A ) = W B ,
so that
U −1 (W B − V (1 − h A ))
1+e = ,
cA
where U −1 is the inverse of the function U. The fraction e is called the
equivalent variation (EV). Chapter 8 uses the concept of an equivalent
variation to compute the welfare cost of business cycles.
U (c A (1 + e)) + V (1 − h A ) = ln(1 + e) + U (c A ) + V (1 − h A ) = W B ,
implying
ln(1 + e) = W B − U (c A ) − V (1 − h A ) = W B − W A ,
so that
e = exp(W B − W A ) − 1.
U (c B (1 + ψ)) + V (1 − h B ) = W A .
36 numerical methods for macroeconomists with julia and matlab codes
Example 8. (CV with logarithmic Utility) Again let U (c) = ln(c). Re-
tracing the steps of the previous example while making the appropri-
ate adjustments leads to
ln(1 + ψ) = W A − U (c B ) − V (1 − h B ) = W A − W B ,
so that
ψ = exp(W A − W B ) − 1.
β
ψ = exp[(1 − β)(W A − W B )] − 1 = exp{ [ln(µ A ) − ln(µ B )]} − 1.
1−β
To make this more concrete, let the annual discount factor, β, be 0.95
and the annual growth rate in regime A, µ A , be 3 percent. The table
below reports the compensating variation for various growth rates in
regime B.
nonlinear equations 37
Shifts in Growth
Growth rate in Regime B, % CV, %
(µ B − 1) × 100% ψ × 100%
1.0 45
2.0 20
3.0 0
4.0 -17
5.0 -31
6.0 -42
So, a person would be willing to give 17 percent of his regime B’s
consumption stream to increase growth from 3 to 4 percent and would
have to have a compensation of 45 percent to move to a situation where
the economy grows at only 1 percent. These are large numbers. Lu-
cas’s conclusion is that growth effects are important. The welfare cost
of business cycles is discussed in Chapter 8. The welfare costs of busi-
ness cycles turn out to be much smaller, so many economists feel that
studying economic growth is more important than studying business
cycles.
In all of the above labor supply problems the difference between the
lefthand and righthand side of the first-order conditions is a decreas-
ing function in h. This occurs because as h increases the left-hand side
drops, while the right-hand, which is being subtracted off, moves up.
For example, recall equation (2.4.4) which stated
Z (h) = 0.
This is called finding the zero or root for the function Z. Before pro-
ceeding, some properties will be imposed on the function Z, largely
for heuristic purposes.
38 numerical methods for macroeconomists with julia and matlab codes
(a) If
| Z (h j )| < ε,
Go to Step 4.
(a) If
Z (h j ) < 0,
then set hl,j+1 = h j and hu,j+1 = hu,j ; i.e., reset the lower bound.
Return to Step 1.
(a) If
| Z (h j )| < ε,
then stop. A solution has been found.
(b) Else
| Z (h j )| ≥ ε,
and go to Step 3.
Z(h j )
h j +1 = h j − .
Z1 (h j )
Go back to Step 1.
The process is shown in Figure 2.6.2. At the guess h j one follows the
tangent line down to the axis to get the revised guess h j+1 . Note that
the equation for the tangent line is
y = aj + bj hj.
0 = a j + b j h j +1 ,
implying
aj
h j +1 = − .
bj
nonlinear equations 41
All that is needed is the coefficients a j and b j . From the formula for a
straight line, b j = Z1 (h j ). To find a j note that Z (h j ) = a j + b j h j so that
a j = Z (h j ) − b j h j = Z (h j ) − Z1 (h j )h j . Therefore,
−aj
z }| {
− Z (h j ) + Z1 (h j )h j Z(h j )
h j +1 = = hj − .
Z (h j ) Z1 (h j )
| 1{z }
bj
U1 (wh + a)w − V1 (1 − h) = 0,
Remark 10. Let Z (h) = U1 (wh + a)w − V1 (1 − h). One could solve the
following equation for h:
Note that this equation will return a zero for the true solution.
Z ( h ) = Z ( h j ) + J ( h j ) ( h j − h ),
n ×1 n ×1 n×n n ×1
where Zij refers to the derivative of the i-th row of Z with respect to
its j-th argument. Now, at the h that solves (2.6.2) it transpires that
0 = Z (h j ) + J (h j )(h − h j ),
h = h j − J ( h j ) −1 Z ( h j ).
h j +1 = h j − J ( h j ) −1 Z ( h j ).
n ×1 n ×1 n×n n ×1
U (ci ) + V (1 − hi ), for i = 1, · · · , I,
o = F (k, h),
subject to
ci = (1 − τi )wπi hi + rk i + λ.
The upshot of this maximization problem is
This results in
F1 (k, h) = r, (2.7.2)
and
F2 (k, h) = w. (2.7.3)
λ = µ1 τ1 wπ1 h1 + · · · + µi τi wπi hi + · · · + µ I τI wπ I h I
I
= ∑ µi τi wπi hi . (2.7.4)
i =1
Now, note that (2.7.4) and (2.7.6) could be used to solve out for λ
and h. The result would be a system of I equations in I unknowns,
h1 , h2 , · · · , h I . Solving this system of equations on the computer may
not be an easy business, depending on how large I is. Instead, consider
the following algorithm which involves just solving one equation in
one unknown at a time.
1. Enter iteration j with a guess for the wage rate, w, and transfer
payments, λ, denoted by w j and λ j . Note that a guess for w amounts
to a guess for r because from the equation w = F2 (1, h), so that one
can solve for h and hence r using the relationship r = F1 (1, h).
3. Calculate what wages and transfer payments are at the solution for
the hi ’s:
I
w = F2 (1, ∑ µi πi hi ),
i =1
| {z }
h
and
I
λ= ∑ µi τi wπi hi .
i =1
Compute a revised guess for wages and transfer payment using the
formulae
(w + w j )/2,
and
(λ + λ j )/2.
(a) If
|w j+1 − w j |/2 + |λ j+1 − λ j |/2 < ε,
then stop.
(b) Otherwise, return to step 1 with the new guesses.
7 % Logarithmic u t i l i t y f u n c t i o n
8 f i g u r e ( 1 ) % Command t o open f i g u r e window 1
9 l n u t i l = l o g ( cons ) ; % Generate v e c t o r o f u t i l s
10 p l o t ( cons , l n u t i l ) % P l o t cons and u t i l s
11 t i t l e ( ’ Log U t i l i t y ’ ) % Make t i t l e
12 y l a b e l ( ’ U t i l s , u ’ ) % Make l a b e l f o r v e r t i c a l a x i s
13 x l a b e l ( ’ Consumption , c ’ ) % Make l a b e l f o r h o r i z o n t a l a x i s
14
15 % Exponential u t i l i t y function
16 figure (2)
17 gamma = 1 . 0 ; % Parameter f o r e x p o n e n t i a l
18 e x p u t i l = −exp ( −gamma * cons ) ;
19 p l o t ( cons , e x p u t i l )
20 t i t l e ( ’ Exponential U t i l i t y ’ )
21 ylabel ( ’ Utils , u ’ )
22 x l a b e l ( ’ Consumption , c ’ )
23 % CRRA u t i l i t y f u n c t i o n
24
25 figure (3)
26 rho = 1 . 5 ; % Parameter f o r c r r a
27 c r r a u t i l = cons . ( 1 − rho ) /(1 − rho ) − 1/(1 − rho ) ;
28 p l o t ( cons , c r r a u t i l )
29 title ( ’ Isoelastic Utility ’ )
30 ylabel ( ’ Utils , u ’ )
31 x l a b e l ( ’ Consumption , c ’ )
32 % Quadratic u t i l i t y f u n c t i o n
33
48 numerical methods for macroeconomists with julia and matlab codes
34 figure (4)
35 alpha = . 5 ; % C o e f f i c i e n t s f o r q u a d r a t i c terms
36 beta = . 2 ;
37 q u a d u t i l = alpha * cons − b e t a * cons . 2 / 2 ;
38 % C r e a t e v e r t i c a l l i n e where q u a d r a t i c u t i l i t y f u n c t i o n peaks
39 maxpty = 0 : . 7 / 5 8 : . 7 ; % Generate v e r t i c a l y p o i n t s
40 % Generate x v e c t o r with t h e c o n s t a n t term alpha/ b e t a
41 maxptx = ones ( 1 , 5 9 ) * alpha/ b e t a ; % Generate x v e c t o r with t h e
constant
42 % P l o t u t i l i t y f u n c t i o n plus v e r t i c a l l i n e
43 p l o t ( cons , q u a d u t i l , maxptx , maxpty )
44 t i t l e ( ’ Quadratic U t i l i t y ’ )
45 ylabel ( ’ Utils , u ’ )
46 x l a b e l ( ’ Consumption , c ’ )
47 % Plot a l l u t i l i t y functions
48
49 figure (5)
50 t i t l e ( ’ U t i l i t y Functions ’ )
51 p l o t ( cons ’ , l n u t i l ’ , cons ’ , e x p u t i l ’ , cons ’ , c r r a u t i l ’ , cons ’ ,
quadutil ’ )
52 t i t l e ( ’ U t i l i t y Functions ’ )
53 ylabel ( ’ Utils , u ’ )
54 x l a b e l ( ’ Consumption , c ’ )
55 % Make a legend i n t h e s o u t h e a s t c o r n e r o f t h e graph .
56 legend ( ’ l o g ’ , ’ exp ’ , ’ c r r a ’ , ’ q u a d r a t i c ’ , ’ l o c a t i o n ’ , ’ s o u t h e a s t ’ )
α − βo = γo ,
| {z } |{z}
MR MC
1 % main .m
2 % Monopoly ProblemMain Program
3 c l e a r a l l % C l e a r a l l numbers from p re vi o us runs
4 c l c % Clear screen
5 g l o b a l alpha b e t a gamma
6
7 % S e t parameters f o r model
8 % Demand curve
9 alpha = 1 ; % c o n s t a n t
10 beta = 0 . 5 ; % slope
11 % Cost f u n c t i o n
12 gamma = 0 . 5 ; % q u a d r a t i c term
13 % P l o t marginal revenue and marginal c o s t
14
15 % C o n s t r u c t g r i d o f output p o i n t s
16 o g r i d = 0 : alpha /( b e t a * 1 0 0 ) : alpha/ b e t a ;
17 % o g r i d runs from 0 t o alpha/ b e t a i n
18 % i n c r e m e n t s o f alpha /( b e t a * 1 0 0 )
19
38 % Display r e s u l t s
39 d i s p l a y ( ’ R e s u l t s f o r t h e monopoly model ’ )
40 d i s p l a y ( ’ output , p r i c e , p r o f i t s , and markup ’ )
41 d i s p l a y ( [ output p r i c e p r o f i t s markup ] )
nonlinear equations 51
1 % R e s u l t s f o r t h e monopoly model
2 output , p r i c e , p r o f i t s , and markup
0.9
3 ans =
4 1.0000 0.7500 0.5000 1.5000
0.8
0.7
0.6
0.5
from the MATLAB program for
0.4
the monopoly problem. Graphs
should always be given titles
0.3
and the axes labeled. You may
0.2
have to increase the font size to
get the labels to look nice.
0.1
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Output
3 Maximization (and Minimization)
3.1 Introduction
subject to
G (h) = 0.
subject to
G (h) = 0.
1. Enter iteration j with the brackets, hl,j and hu,j , around the maximal
value such that hl,j ≤ h∗ ≤ hu,j .
2. Construct two test points, denoted by pl,j and pu,j , which are given
by
pl,j = hl,j + (1 − α)(hu,j − hl,j ), (3.2.1)
maximization (and minimization) 55
and
(a) If
F ( pl,j ) > F ( pu,j ).
Since the function is unimodal this implies that h∗ < pu,j . So the
upper bracket, hu,j+1 , should be reset on the next iteration. In
particular,
hl,j+1 = hl,j and hu,j+1 = pu,j . (3.2.3)
(b) Else
F ( pl,j ) < F ( pu,j ),
Take the case in point 3(a) where the new bracket for iteration j + 1 is
(hl,j+1 , hu,j+1 ) = (hl,j , pu,j ); i.e., the upper bound is being adjusted–the
second case can be analyzed in a similar manner. Now, suppose the
restriction below is imposed where
i.e., the old lower test point becomes the new upper test point. As will
be seen, this condition implies that ratio of the distance between the
test point and its closest bound to the distance between the test point
and its farthest bound is kept constant across iterations. Then, it must
happen that
pl,j = pu,j+1
= hl,j+1 + α(hu,j+1 − hl,j+1 ) [by updating (3.2.2)]
= hl,j + α( pu,j − hl,j ) [by (3.2.3)]. (3.2.5)
maximization (and minimization) 57
Next, using (3.2.1) and (3.2.2) to substitute out for pl,j and pu,j on the
left and right, this can be rewritten as
α2 + α − 1 = 0. (3.2.6)
hu,j+1 − pu,j+1
= [using (3.2.3) and (3.2.4)].
pu,j+1 − hl,j+1
The formula implies that the ratio of the length of the line segment
from a bracket to the nearest test point over the length of the other
bracket to the same test point is preserved across iterations. So cer-
tain ratios are held constant in line with the golden ratio. The above
formula motivates the choice of pu,j+1 = pl,j . To derive the first
line in the formula, note that the numerator on the lefthand side is
pl,j − hl,j = (1 − α)(hu,j − hl,j ) by (3.2.1). Turn to the denominator
on the left. By subtracting (3.2.1) from (3.2.2) it can be seen that
hu,j − pl,j = pu,j − hl,j . But, (3.2.2) states that pu,j − hl,j = α(hu,j − hl,j ).
Therefore, the ratio on the lefthand side is (1 − α)/α. The formula for
the Golden ratio (3.2.6) implies that this is just α. Now turn to right-
hand side of the top line. By summing (3.2.1) and (3.2.2) it can be seen
that pu,j − pl,j = (2α − 1)(hu,j − hl,j ). Therefore, the ratio on the right-
hand side is (2α − 1)/(1 − α). Again, this is just α from the golden
ratio formula (3.2.6). Thus, the lefthand and the righthand hold with
equality at the Golden ratio.1 1
This equation implies that (1 − α)/α =
(2α − 1)/(1 − α), so as before α must
solve α2 + α − 1 = 0.
Speed of Convergence
Also, note that
Return to the general equilibrium labor supply problem, that was cast
in Section 2.4 of Chapter 2. Now, the solution for h cannot be simply
found in a single shot by solving one equation in one unknown.
1. Enter iteration j with a guess for the solution for labor supply de-
noted by h j . A guess for h amounts to guesses for r, w, and λ using
the equations r j = F1 (k, h j ), w j = F2 (k, h j ), and λ j = τF2 (k, h j )h j − g.
h j+1 = (h + h j )/2.
(a) If
|h j+1 − h j | < ε,
then stop.
(b) Otherwise, return to step 1 with the new guess.
This is the simplest technique, but the least accurate. Here the domain
of the objective function is discretized and the range of the function is
evaluated on each point in this discrete set. In particular, suppose that
h must lie in the discrete set H. The maximization problem (3.1.1) thus
appears as
max F (h).
h∈H
This amounts to just searching a list of numbers and finding the max-
imal value, something computers can do quickly. If the grid of points
in H is fine enough, then h j should be reasonably close to the solu-
tion, h∗ , that obtains from maximization problem where h is allowed
to vary continuously. The situation is shown in Figure 3.4.1. Discrete
maximization can handle constraints fairly easily. For example, lower
and upper bounds on h can be imposed by restricting elements in the
set H to lie in within the range imposed by the bounds. Discrete max-
imization is often used to solve dynamic programming problems and
is returned to in Chapter 9.
called the inertial component and operates to keep the bot moving in
j j∗ j
the same direction. The second component, β × ζ i × (hi − hi ), reflects
j∗
individual learning. Here hi is best position that bot i has personally
experienced in the past up to and including iteration j. This term
causes the bot to return to the location where it did the best. The
j
coefficient ζ i ∈ [0, 1] is a random number that bot i draws on iteration
j. This encourages the bot’s to search new regions of the space where
j j
the control variable lies. The last term, γ × ξ i × (h j∗ − hi ), is the social
learning component. The variable h j∗ is the best position that any bot
has found in the past. This entices the bot to move to regions where the
swarm has found to be productive. Again, there is some randomness
j
in this move since ξ i ∈ [0, 1] is a randomly drawn number. Figure 3.5.1
portrays the situation.
1. Initialize I bots.
(a) Randomly assign an initial position, h0i , in the control space for
each of the i = 1, · · · , I bots.
(b) Likewise, for each bot i randomly pick an initial step size, s0i .
62 numerical methods for macroeconomists with julia and matlab codes
j
2. On a generic iteration j compute F (hi ) for all i = 1, · · · , I.
j j∗ j +1∗ j
(a) Update personal best. If F (hi ) > F (hi ), then set hi = hi .
(b) Update the swarm’s best. To do this, find the bot that is doing
the best on iteration j. I.e., find
j j
m = arg max{ F (h1 ), · · · , F (h I )}.
i
j j
If F (hm ) > F (h j∗ ), then set h j+1∗ = hm .
The particle swarm algorithm is good for global optimization. Its cod-
ing is simple and it is perfect for parallel computing, where the prob-
lems for many bots can be solved simultaneously. Its main disadvan-
tage is that during the final stages it is slow to home in on the optimal
solution. But, for these stages the algorithm could switch to speedier
local maximizers, such as golden-section search.2 2
The parameter α should lie between 0
and 1for convergence reasons. Normally
values between 1and 3are selected for β
3.6 Calibration and γ.
Economic theory comes alive when confronted with data. One method
of matching economic models with data is calibration. This method-
ology was introduced into economics by Kydland and Prescott (1982)
in a now famous paper. An elementary introduction to calibration is
contained in Prescott and Chandler (2008). Calibration often refers
to adjusting an instrument, scientific or otherwise, so that it matches
some known benchmarks. For example, a guitar can be tuned so that
the A string has the Stuttgart pitch of 440Hz. After the guitar has been
tuned (calibrated) it can be used to play songs in key with others. In
economics the model is treated as an instrument and its parameters
can be adjusted so that it matches certain features in the data. After an
economic model has been calibrated (tuned) it can be used to conduct
policy analysis or thought experiments.
Consider the following economic model
o = M ( p ),
where di is the ith data target and Mi (u, v) is the model’s prediction
for this target. Different criteria could be used for the minimization
problem, or for the objective function. Sometimes the data targets can
be hit exactly. Often this is done using a nonlinear equation solver
instead of a minimization routine, as the example below show. When
this can be done for the data targets, the model’s prediction will exactly
match targets so that di = Mi (u, v). So, this procedure can also be
thought of as solving the above problem. Calibration is a very close
cousin of econometrics.
(wh)1−ρ
max{θ + (1 − θ ) ln(1 − h)}, with 0 < θ < 1 and ρ ≥ 0.
h 1−ρ
which has the first-order condition
1−θ
wh )−ρ × |{z}
θ (|{z} w = .
1−h
IE SE
Now, in 1900 the average male worked 63 hours a week. This dropped
to only 44 hours in 2018–see Figure 3.6.1. Over this time period real
wages rose by a factor of 7.7. Is the above model consistent with
these facts? There are both income and substitution effects associated
64 numerical methods for macroeconomists with julia and matlab codes
To model this, suppose that the joy a young women gets from a
sexual relationship is given by ej, which is distributed across women
according to a Weibull distribution:
So, a young woman is sexually active when the joy of sex, j, exceeds
the expected cost of a pregnancy, φO. The threshold level of joy, j∗ , at
which a woman is indifferent between having sex or not is given by
j∗ = φO .
|{z} |{z}
benefit cost
All women with a level of joy, ej, above the threshold, j∗ , will
participate in premarital sexual activity. At the threshold the joy of
sex, j∗ , is equal to its expected cost, φO. The fraction of women with
premarital sexual experience then reads
To calibrate this to the U.S. data note that there are 3 parameters,
namely β, η, and O. Observations are at hand for the levels of pre-
marital sexual activity and the failure rates for three years spanning
the 20th century. In this case it is impossible to get a perfect fit by
solving a system of 3 equations in 3 unknowns. If instead the param-
eter values are chosen to minimize the model’s prediction errors for
premarital sex for these three years, then β, η, and O must solve
7 % S e t parameters f o r model
8 % Demand curve
9 alpha = 1 ; % c o n s t a n t
10 beta = 0 . 5 ; % slope
11 % Cost f u n c t i o n
12 gamma = 0 . 5 ; % q u a d r a t i c term
13
14 % P l o t t h e Monopolist ’ s O b j e c t i v e Function
15 % C o n s t r u c t g r i d o f output p o i n t s
16 o g r i d = 0 : alpha /( b e t a * 1 0 0 ) : alpha/ b e t a ;
17 % o g r i d runs from 0 t o alpha/ b e t a i n
18 % i n c r e m e n t s o f alpha /( b e t a * 1 0 0 )
19 figure (1)
20 p l o t ( ogrid , − n p r o f i t s ( o g r i d ) )
21 t i t l e ( ’ O b j e c t i v e Function ’ )
22 x l a b e l ( ’ Output ’ )
23 ylabel ( ’ Profits ’ )
24
25 % C a l l up MATLAB minimizer t o s o l v e f o r output
26 output = fminbnd ( @ n p r o f i t s , 0 , 2 ) ; % n p r o f i t s i s t h e o b j e c t i v e
function
27
28 p r i c e = alpha − b e t a * output / 2 ;
29 c o s t = gamma * output2 / 2 ;
30 p r o f i t s = p r i c e * output − c o s t ;
31 markup = p r i c e /(gamma * output ) ;
32
33 % Display r e s u l t s
34 d i s p l a y ( ’ R e s u l t s f o r t h e monopoly model ’ )
35 d i s p l a y ( ’ output , p r i c e , p r o f i t s , and markup ’ )
36 d i s p l a y ( [ output p r i c e p r o f i t s markup ] )
37
38 % D i s c r e t e maximization
39
40 o s e t = 0 : 2 / 9 9 : 2 ; % D i s c r e t i z e domain f o r maximization
41 [ maxvalue , o p o i n t ] = max( − n p r o f i t s ( o s e t ) ) ; % Find optimal p o i n t
42 % maxvalue = maximal value o f o b j e c t i v e f u n c t i o n
43 % o p o i n t = p o i n t number o f t h e optimal value o f output
44 % o s e t ( o p o i n t ) = optimal l e v e l o f output
45
46 p r i c e = alpha − b e t a * o s e t ( o p o i n t ) / 2 ;
47 c o s t = gamma * o s e t ( o p o i n t ) 2 / 2 ;
48 p r o f i t s = p r ic e * oset ( opoint ) − c o s t ;
49 markup = p r i c e /(gamma * o s e t ( o p o i n t ) ) ;
50
51 % Display r e s u l t s
52 d i s p l a y ( ’ R e s u l t s f o r t h e monopoly m o d e l d i s c r e t e maximization ’ )
53 d i s p l a y ( ’ output , p r i c e , p r o f i t s , and markup ’ )
54 d i s p l a y ( [ o s e t ( o p o i n t ) p r i c e p r o f i t s markup ] )
68 numerical methods for macroeconomists with julia and matlab codes
6 % R e s u l t s f o r t h e monopoly m o d e l d i s c r e t e maximization
7 output , p r i c e , p r o f i t s , and markup
8 ans =
9 0.9899 0.7525 0.4999 1.5204
4.1 Introduction
λ+g = τc c + τh wh,
| {z } | {z }
expenditure revenue
c + g + δk = o,
(1 + τc )c = (1 − τh )wh + rk + λ,
where w is the wage rate and r is the rental rate. Observe that the sales
tax, τc , increases the price of consumption, c.
The first-order condition for labor is
(1 − τh ) w 1
=α ,
(1 + τc ) c 100 − h
| {z }
≡(1−τ )
or
(1 − τ ) w 1
=α ,
c 100 − h
where
τc + τh
τ≡ ,
1 + τc
is the effective tax on labor. The consumption tax, τc , creates a dis-
incentive to work, just as the labor income tax, τh , does. This makes
sense. When deciding how much to work the individual considers the
relative price of leisure in terms of consumption goods; i.e., he looks at
the forgone consumption that a marginal increase in leisure will cost.
Raising the price of consumption, via a consumption tax, reduces the
relative price of leisure in a manner similar to increasing the labor
income tax.
w = (1 − θ )zkθ h−θ ,
w = (1 − θ )o/h.
d ln h 1 (c/o )
(1 − τ ) =− α < 0.
dτ α(c/o )/(1 − τ ) + (1 − θ ) 1 − τ
dh 100(1 − θ ) (c/o )
=− < 0. (4.2.3)
dα [α(c/o )/(1 − τ ) + (1 − θ )]2 1 − τ
Some examples will now be presented, which echo the theory of labor
income taxation presented in Chapter 2. In these examples, let labor be
the only factor of production. Therefore, assume a linear production
function of the form o = wh. Thus, set θ = 0.
In the above equation use the facts that c = (1 − τ )wh and g = τwh.
This gives
(100 − h)(1 − τ )o = α[1 − (1 − ξ )τ ]wh2 .
Therefore,
100
h= .
α [1 − (1 − ξ ) τ ] / (1 − τ ) + 1
If ξ = 0, then the result in Example 11 obtains, and when ξ = 1,
the result in Example 11 occurs. So, this case is just a hybrid of the
previous two cases.
The national income and product accounts (NIPA) are a key source of
data for macroeconomists. While national income accounting is one of
74 numerical methods for macroeconomists with julia and matlab codes
C = WL + Π + PI.
C + I = WL + Π + PI + D.
Final Goods
Expenditure Income
Labor
Firms
IT. Indirect taxes are included in the expenditure on the lefthand side
and hence they only need to be added to the righthand side to obtain
C + I + G = WL + Π + PI + D + IT.
GDP ≡ C + I + G + E − M = WL + Π + PI + D + IT.
The above equation gives the national income identity for gross do-
mestic product, GDP. For net domestic product, NDP, depreciation is
subtracted off of both sides to obtain
NI ≡ NDP − IT = C + I − D + G + E − M − IT = WL + Π + PI.
• Labor income tax, τh . The labor income tax is comprised of two taxes,
viz a social security tax, τss , and an income tax, τinc . The social
security tax rate is estimated to be
The number 1.6 translates the average income tax rate into a higher
marginal one–recall from Chapter 2 that with progressive taxation
the marginal rate must lie above the average one as is illustrated
there by Figure 2.3.2. The translation factor is based on the 40%
marginal tax rate estimated in Feenberg and Coutts (1993). They
take a representative sample of households and see by how much
tax revenue will increase if household income rises by 1%. They
then estimate the marginal tax rate as (change in tax revenue)/(change
in labor income). Assuming that this markup from average to marginal
tax rates is valid for all countries is a bit heroic.
• Mapping consumption and output in the model to the data, c and o. Ef-
fective consumption, c, and output, o, in the model are assumed to
have the following relationship with consumption, C, and GDP in the
data:
c = C + G − GMILITARY − ITc ,
o = GDP − IT.
From the above examples, taxes will have a bigger effect on labor
supply the smaller is the income effect. Assuming that nonmilitary
government spending, G − GMILITARY , is substitutable for private con-
sumption in a one-to-one fashion amplifies the negative impact that
taxation has on labor supply.
c = (1 − τ 1 )wh + (1 − τ 1 )φwh + λ.
Clearly, joint taxation creates a larger disincentive effect for the second
worker, here the wife, than taxing each person separately.
why do americans work so much more than europeans? 79
4.5.2 Results
c2 λ
c1 + = (1 − τss )wh + ,
1+r 1+r
where c1 and c2 are consumption in the first and second periods.
Clearly, with this budget constraint, the individual’s labor-leisure choice
will be distorted by the presence of the tax. Under private accounts the
person will recognize that λ = (1 + r )τss wh; i.e., that is he will inter-
nalize the fact that he will get back with interest any money that goes
into his private account. Substituting out for λ in the above budget
constraint then gives
c2
c1 + = wh.
1+r
Hence, a system of private accounts does not distort the person’s labor-
leisure choice.
why do americans work so much more than europeans? 81
4.6 Conclusions
The results are sensitive to the implied elasticity of labor supply with
respect to taxes. This elasticity depends how the revenue from taxation
is used because this governs the relative strength of the income and
substitution effects from a shift in taxation. A high elasticity implies
that hours worked will be very responsive to changes in taxation. A
high elasticity occurs when the revenue from taxation is either rebated
back in the form of lump-sum transfer payments or when government
spending on goods and services is highly substitutable with private
spending. In these two cases the income effect associated with taxa-
tion is mitigated so that just the substitution effect remains. The dead-
weight loss from labor income taxation will be high. A high elasticity
of labor supply implies that reforms to the social security system, such
as private-saving accounts, will significantly improve welfare. Such
accounts encourage people to work longer.
Here is some MATLAB code that computes the solution for hours
worked in Prescott’s model for the 7 countries involved. The code
has two parts.
7
min{∑[ hdata93
i
−96
− hmodel93
i
−96
(α)]2
α
i
7
+ ∑[ hdata70
i
−74
− hmodel70
i
−74
(α)]2 },
i
82 numerical methods for macroeconomists with julia and matlab codes
−96 −74
where hdata93 i and hdata70i are the data for hours worked in
−96
country i for 1993-1996 and 1970-1974, respectively, and hmodel93 i
−74
and hmodel70 i are the model’s predictions for this variable. Note
that the model’s prediction is a function of α, as can be seen from
equation (4.2.2).
This problem can be solved numerically two ways. First, one could
solve the first-order associated with this minimization problem using
the techniques outlined in Chapter 2 for solving a nonlinear equation.
The first-order condition associated with this minimization problem is
−96
7 dhmodel93 (α)
∑[hdata93
i
−96
− hmodel93
i
−96
(α)]
dα
i
i
−74
7 dhmodel70 (α)
+ ∑[ hdata70
i
−74
− hmodel70
i
−74
(α)] i
= 0.
i
dα
The second part of the program picks α to solve this equation. Equa-
tion (4.2.3) gives the formula for dhmodel (α)/dα. The program then
just repeats what was done in the first part using this new value for
α. The third part of the program employs a minimization algorithm to
compute α, following the procedure discussed in Chapter 3.
16
17 % Parameter v a l u e s
18 alpha = 1 . 5 4 ; % Weight on l e i s u r e
19 theta = 0 . 3 2 ; % Capitals share
20
21 % C a l c u l a t e Model ’ s P r e d i c t i o n s
22 % 1993 −96
23 hmodel9396 = hours ( tau9396 , c t o o 9 3 9 6 ) ;
24
25 % Function f o r model ’ s s o l u t i o n
26 e r r o r 9 3 9 6 = hdata9396 −hmodel9396 ; % P r e d i c t i o n e r r o r
27 disp ( ’ R e s u l t s using P r e s c o t t s alpha ’ )
28 disp ( ’ alpha ’ )
29 disp ( alpha )
30 disp ( ’ R e s u l t s 1 9 9 3 t o 96 ’ )
31 disp ( ’ Data Model E r r o r ’ )
32 disp ( [ hdata9396 ’ , hmodel9396 ’ , e r r o r 9 3 9 6 ’ ] )
33
34 % 1970 −74
35 disp ( ’ R e s u l t s 1 9 7 0 t o 74 ’ )
36 disp ( ’ Data Model E r r o r ’ )
37 hmodel7074 = hours ( tau7074 , c t o o 7 0 7 4 ) ;
38 e r r o r 7 0 7 4 = hdata7074 −hmodel7074 ;
39 disp ( [ hdata7074 ’ , hmodel7074 ’ , e r r o r 7 0 7 4 ’ ] )
40
56 % 1970 −74
57 % Figure 2
58 heading = ’ F i t o f M o d e l P r e s c o t t s alpha , 1970 t o 1974 ’ ;
59 makefigures ( 2 , hdata7074 , hmodel7074 , heading )
60
75 disp ( ’ R e s u l t s 1 9 9 3 t o 96 ’ )
76 disp ( ’ Data Model E r r o r ’ )
77 disp ( [ hdata9396 ’ , hmodel9396 ’ , e r r o r 9 3 9 6 ’ ] )
78
79 % 1970 −74
80 disp ( ’ R e s u l t s 1 9 7 0 t o 74 ’ )
81 disp ( ’ Data Model E r r o r ’ )
82 hmodel7074 = hours ( tau7074 , c t o o 7 0 7 4 ) ;
83 e r r o r 7 0 7 4 = hdata7074 −hmodel7074 ;
84 disp ( [ hdata7074 ’ , hmodel7074 ’ , e r r o r 7 0 7 4 ’ ] )
85 disp ( ’ norm ’ )
86 disp ( norm ( [ e r r o r 9 3 9 6 e r r o r 7 0 7 4 ] ) )
87
1 f u n c t i o n [ h ] = hours ( tau , c t o o )
2 % This f u n c t i o n computes hours worked using P r e s c o t t ’ s formula
3 % I t t a k e s t h e v e c t o r o f t a x r a t e s , tau , and t h e v e c t o r o f
4 % consumption/output r a t i o s , ctoo , as
5 % i n p u t s and g i v e s a v e c t o r f o r hours , h , as t h e output .
6 g l o b a l alpha t h e t a
7 h = 100 * (1 − t h e t a ) . / ( alpha * c t o o . / ( 1 − tau ) + (1 − t h e t a ) ) ;
8 % Note t h a t . / d i v i d e s one v e c t o r by another
9 % component by component .
10 end
1 f u n c t i o n [ zero ] = f o c ( alpha )
2 % This f u n c t i o n s p e c i f i e s t h e f i r s t −order c o n d i t i o n f o r t h e
3 % l e a s t −s q u a r e s o p t i m i z a t i o n problem . .
4 global theta
5 g l o b a l tau9396 c t o o 9 3 9 6 hdata9396 tau7074 c t o o 7 0 7 4 hdata7074 .
6 % C a l c u l a t e t h e model ’ s s o l u t i o n f o r hours worked f o r a given
alpha
7 hmodel9396 = 100 * (1 − t h e t a ) . / ( alpha * c t o o 9 3 9 6 . / ( 1 − tau9396 ) +
(1 − t h e t a ) ) ;
8 hmodel7074 = 100 * (1 − t h e t a ) . / ( alpha * c t o o 7 0 7 4 . / ( 1 − tau7074 ) +
(1 − t h e t a ) ) ; .
9 % Write out t h e f i r s t −order c o n d i t i o n f o r t h e minimization
problem
10 % i n v o l v i n g t h e c h o i c e o f alpha
11 zero = −( hdata9396 − hmodel9396 ) . * derivh ( alpha , tau9396 ,
ctoo9396 ) . . .
12 − ( hdata7074 − hmodel7074 ) . * derivh ( alpha , tau7074 ,
ctoo7074 ) ; .
13 % Note t h a t . * m u l t i p l i e s one v e c t o r by another component
14 % by component . Also , note t h a t t h e use o f . . . a l l o w s t h e
15 % e x e c u t a b l e s t a t e m e n t t o flow over two l i n e s .
16 zero = 2 * sum ( zero ) ;
17 % sum i s a MATLAB command t h a t adds a c r o s s t h e components o f
a vector .
18 end
A Function Specifying the Sum of the Squares for the Optimal Choice
of α-foc.m
1 f u n c t i o n [ s o s ] = sumofsquares ( alpha )
2 % This f u n c t i o n s p e c i f i e s t h e o b j e c t i v e f u n c t i o n f o r t h e l e a s t −
squares
3 % o p t i m i z a t i o n problem .
4 global theta
5 g l o b a l tau9396 c t o o 9 3 9 6 hdata9396 tau7074 c t o o 7 0 7 4 hdata7074
6 % C a l c u l a t e t h e model ’ s s o l u t i o n f o r hours worked f o r a given
alpha
7 hmodel9396 = 100 * (1 − t h e t a ) . / ( alpha * c t o o 9 3 9 6 . / ( 1 − tau9396 ) +
(1 − t h e t a ) ) ;
8 hmodel7074 = 100 * (1 − t h e t a ) . / ( alpha * c t o o 7 0 7 4 . / ( 1 − tau7074 ) +
(1 − t h e t a ) ) ;
9 % Write out t h e sum o f t h e s q u a r e s f o r t h e minimization
problem
10 % i n v o l v i n g t h e c h o i c e o f alpha
11 s o s = ( hdata9396 − hmodel9396 ) . 2 . . .
12 + ( hdata7074 − hmodel7074 ) . 2 ;
13 % Note t h a t . * m u l t i p l i e s one v e c t o r by another component by
component .
14 % Also , note t h a t t h e use o f . . . a l l o w s our e x e c u t a b l e
s t a t e m e n t t o flow
15 % over two l i n e s .
16 s o s = sum ( s o s ) ; % Sum
17 % sum i s a MATLAB command t h a t adds a c r o s s t h e components o f
a vector
18 end
86 numerical methods for macroeconomists with julia and matlab codes
Fit of Model--Prescotts alpha, 1993 to 1996 Fit of Model--Prescotts alpha, 1970 to 1974
30 36
Jap
Jap
28
34 Figure 4.7.1: Goodness of fit
26
32
using Prescott’s α, 1993-96 and
30
24
USA
Ita
1970-74.
28
model
model
UK
USA
22 26
Can Fra
Can
24 Ger UK
20
Fra Ger
22
Ita
18
20
16 18
16 18 20 22 24 26 28 18 20 22 24 26 28 30
data data
Fit of Model--fitted alpha, 1993 to 1996 Fit of Model--fitted alpha, 1970 to 1974
28 34
Jap
Jap
32
26
Figure 4.7.2: Goodness of fit us-
30
model
22 26 Ita
UK
USA
24
20 Can Fra
Can
Ger UK
22
18 Fra Ger
20
Ita
16 18
16 18 20 22 24 26 28 18 20 22 24 26 28 30
data data
5.1 Introduction
Graphs can make a paper or presentation come alive. Tufte (2001) says
that Charles Joseph Minard’s (1781-1870) graph portraying the fate of
Napoleon’s army during its invasion of Russia in 1812 “may well be
the best statistical graph ever drawn.” The graph combines a map with
a time-series showing the size of Napoleon’s army as it travelled from
the Polish-Russian border to Moscow and back. The width of the thick
red line illustrates the size of the army as it advances towards Moscow.
The width of the black line shows the size of it as it retreats. The brutal
temperatures facing the army are shown in the bottom panel, which is
in sync with the upper one.
and Denmark and Norway, on the other. Figure 5.2.2 is a bar graph
illustrating Scottish exports to and imports from various countries for
the year 1781. One of Playfair’s pie charts exhibiting the fractions of
the Turkish empire (before 1789) located in Africa, Asia, and Europe is
presented in Figure 5.2.3.
(a) Captions, colors, fonts, labels, and lines. Axes should be labeled,
in fonts large enough to read, and graphs should have titles in
captions. Table 5.3.1 shows how fonts can be use to empha-
size something. The caption should also explain the graphical
construct, if needed. On a time series plot one can distinguish
between the lines by using different line colors and styles. The
same is true for the bars on a histogram portraying different data
objects. They can be distinguished using different colors and fill
patterns. Additionally, make sure that different colors reproduce
well in black and white, if this is a requirement.
A Couple of Axes
The mathematician The mathematician Table 5.3.1: The most commonly
Plotting his past relations Plotting his past relations used consonant in English is
"ex" and "why" axis "ex" and "why" axis the letter t. By using a bold
font the mind can more quickly
see this on the version of the
haiku shown on the right. This
is a take on an example in
Schwabish (2014).
92 numerical methods for macroeconomists with julia and matlab codes
Women
Labor-Force Participation, % Ages 20-64
100 100
Men
90 Figure 5.3.1: The graphs show
Labor-Force Participation, % Ages 20-64
Men
80 80
the rise in U.S. female labor-
70
10
tio. The right panel illustrates
0
1840 1860 1880 1900 1920 1940 1960 1980 2000 2020 2040
0
1860 1880 1900 1920 1940 1960 1980 2000 2020
the same graph with a high
Year
Year data-ink ratio. Source: Green-
wood et al. (2021b)
graphing 93
5 %% Housekeeping
6 clear
7 close a l l
8 %% Reproduce a v e r s i o n o f P l a y f a i r ’ s bar c h a r t f o r t h e United
S t a t e s . To do t h i s , t a k e t h e United S t a t e s ’ 15 l a r g e s t
trading partners .
9 x = [1:15];
10 % Load e x p o r t and import data .
11 y = [120.3539.5;...
12 298.7318.5;...
13 265.0346.5;...
14 75.0142.6;...
15 57.7125.9;...
16 56.374.3;...
17 66.260.8;...
18 36.352.5;...
19 33.154.4;...
20 23.254.7;...
21 30.245.8;...
22 49.424.6;...
23 39.531.2;...
graphing 95
24 10.757.5;...
25 22.241.1];
26 figure ;
27
28 % C r e a t e bar c h a r t with two a x i s .
29 % Subsequent commands t a r g e t r i g h t a x i s .
30 yyaxis r i g h t ;
31 % Make h o r i z o n t a l bar c h a r t x v e r t i c a l , y h o r i z o n t a l
32 H=barh ( x , y , ’ grouped ’ ) ; % b a r s a r e grouped by row
33 H( 1 ) . FaceColor = ’ k ’ ; % s e t f i r s t bar t o b l a c k
34 % Set object properties for chart
35 s e t ( gca , ’ y t i c k l a b e l ’ , { ’ China ’ , ’ Canada ’ , ’ Mexico ’ , ’ Japan ’ , ’ Germany
’ , ’ Korea , South ’ , ’ United Kingdom ’ , ’ France ’ , ’ I n d i a ’ , ’ I t a l y ’ , ’
Taiwan ’ , ’ Netherlands ’ , ’ B r a z i l ’ , ’ I r e l a n d ’ , ’ S w i t z e r l a n d ’ } ) ;
36 t i t l e ( { ’ f o n t s i z e { 1 6 }U. S . Imports & Exports i n 2018 ’ ; ’ f o n t s i z e
{ 1 0 } B i l l i o n s o f d o l l a r s g o o d s only . ( Source : U. S . Census
Bureau ) ’ } )
37 % Add t e x t t o bottom o f c h a r t
38 t e x t ( 3 0 0 , 0 , ’ Red ( top ) b a r s a r e Imports . Bl a ck ( bottom ) b a r s a r e
Exports . ’ , ’ V e r t i c a l A l i g n m e n t ’ , ’ bottom ’ , ’
HorizontalAlignment ’ , ’ c e n t e r ’ ) ;
6.1 Introduction
Often macro models specify that the state of economy, k t , evolves ac-
cording to a second-order nonlinear difference equation of the follow-
ing form:
k t = D (k t−1, k t−2 ), for t = 3, 4, · · · . (6.1.1)
The function D may represent the upshot of individuals’ and firms’
dynamic choice problems, government policies, and market-clearing
conditions. By specifying the economy at an elemental level it is hoped
the function D will capture the behavioral changes of individuals to
the state of the economy.
To start this difference equation off at time t = 3, one would need
to know both k1 and k2 . In period 1 it is reasonable to assume that
state of the economy has been predetermined, say at kinitial . So, one
can employ the starting condition
k1 = kinitial .
where ksteady state is the long-run steady state. Thus, the goal is to solve
the above difference equation subject to two boundary conditions, one
at the beginning of time and the other at the end. This falls into a class
of problems known as two-point boundary value problems.
Three solution methods for solving this problem are presented. The
classic way of solving such problems is multiple shooting. If one knew
k1 and k2 , then the solution for the time path {k t }∞ t=1 could be com-
puted by just iterating on (6.1.1). As mentioned, in economics gener-
ally only k1 is known. Multiple shooting selects a value for k2 such
that the economy converges over time to the long-run steady state,
ksteady state . Another algorithm is the extended-path method. This al-
gorithm turns the difference equation (6.1.1) on its head. Updating
98 numerical methods for macroeconomists with julia and matlab codes
equation (6.1.1) by two periods gives k t+2 = D (k t+1, k t ).1 Rewrite this 1
That is, just add 2 to the subscripts
equation as k t+1 = D e (k t , k t+2 ). For each time period t, the extended- in this equation. The updated equation
holds for all t ≥ 1.
path method solves for next period’s capital stock, k t+1 , given its cur-
rent value, k t , and an expectation about its future value two periods
down the road, k t+2 . The algorithm is constructed in such a way so
that upon convergence the expectation about the path for the k t+2 ’s
coincides with the actual path for the k t+2 ’s and also so that the econ-
omy converges to a long-run steady state. The extended-path method
and multiple shooting are discussed in Section 6.12. The last solution
method treats (6.1.1) as a second-order linear difference equation that
comes out of a linear-quadratic optimization problem. This method is
explained in Section 6.9.
The discussion in the chapter will be centered around the neoclassi-
cal growth model, which is the workhorse of modern macroeconomics.
The model has its roots in work by Frank P. Ramsey (1903-1930). The
transitional dynamics for the neoclassical growth model are fully char-
acterized using pencil-and-paper techniques. While doing this, Bell-
man (1957) concept of dynamic programming and the value function is
presented. Properties of the value function for the neoclassical growth
model are derived. The contraction mapping principal underlying
much of dynamic programming is discussed. This is done in an in-
tuitive way, at the sacrifice of some rigor. The numerical techniques
introduced are illustrated in Section 6.13 using a dynamic version of
the monopolist’s pricing problem, first introduced in Chapter 2.
where ct+ j is the person’s consumption in period t + j. Utility in period Mariner: Who lived Eight and Twenty Years
all alone in an un-inhabited Island of the
t + j, U (ct+ j ), is discounted at the rate β j . Since β < 1, β j is decreasing
Coast of America, near the Mouth of the
in j so the further off a utility is in the future the less Robinson cares Great River of Oroonoque; Having been cast
about it. Note that β j → 0, as j → ∞. on shore by Shipwreck, where-in all the Men
Output in period t + j, or ot+ j , is produced in line with the following perished but himself.” This is believed to be
constant-returns-to-scale production function the first English novel. Interestingly,
historians suggest that the book is based on
which uses the period-(t + j) inputs, capital, k t+ j , and labor, ht+ j . To captain, Selkirk was left alone in 1704 on one
of the Juan Fernandez Islands for four and a
begin with, suppose that Robinson Crusoe supplies just one fixed unit
half years. The seaman who went ashore in
1709 to retrieve him said he found “a man
clothed in goat’skins, who looked wilder than
the first owners of them.”
deterministic dynamics 99
k t + j +1 = (1 − δ ) k t + j + i t + j .
This is a version of Ramsey (1928) growth model. Frank P. Ramsey (1903-1930) was a
Robinson Crusoe’s goal in life is to maximize his lifetime utility by British economist and
picking optimally his consumption and investment in each period. His mathematician. He died at the very
young age of 26. In economics he is
period-t problem can be written as
known for his work on the growth
∞ model, optimal taxation, and
max
{ct+ j ,it+ j }∞
∑ β j U ( c t + j ), subjective probability. In
j =0 j =0
mathematics he started a branch of
subject to the economy’s resource constraint, combinatorics which is now known
as Ramsey theory. He was named
ct+ j + it+ j = Fe(k t+ j , 1) = F (k t+ j ), the Senior Wrangler or the top
undergraduate in mathematics at
the law of motion for capital, Cambridge.
k t + j +1 = (1 − δ ) k t + j + i t + j ,
and the initial condition, k t . Robinson Crusoe’s problem has been cast
as starting in some arbitrary period, t. Often the first period is taken
as t = 1.2 Substitute out for ct+ j and it+ j in the utility functions using 2
His period-1 problem can be written as
the resource constraint and the law of motion for capital. The problem ∞
now appears as
max
{ct ,it }∞
∑ β t −1 U ( c t ),
t =1 t =1
son Crusoe will realize if he enters period t with the capital stock, k t . subject to c1+ j + i1+ j = F (k1+ j ) and
k1+ j+1 = (1 − δ)k1+ j + i1+ j . Now, do a
This is called the value function. It plays an important role in modern change of variable by setting t = 1 + j.
macroeconomics. Note that if j starts at 0 then t must start
at 1. The period-1 problem then obtains.
Thus, the gross growth rate in consumption, ct+ j+1 /ct+ j , rises with the
deterministic dynamics 101
If one knew k t and k t+1 then the above difference equation could be
used to solve for k t+2 (by setting j = 0). One now has k t+2 and k t+1 in
hand that can be used to get k t+3 . By iterating on the entire time path
102 numerical methods for macroeconomists with julia and matlab codes
for the k t+ j ’s can be uncovered. That is, one could proceed as follows:
k t +2 = D ( k t +1 , k t ),
k t +3 = D ( k t +2 , k t +1 ) = D ( D ( k t +1 , k t ), k t +1 ),
| {z }
k t +2
k t +4 = D ( k t +3 , k t +2 ) = D ( D ( D (k t+1 , k t ), k t+1 ), D (k t+1 , k t )),
| {z } | {z }
k t +3 k t +2
.. ..
. .
In a steady state the capital stock will be constant at some level denoted
by k∗ . Therefore, in a steady state k t+ j = k t+ j+1 = k t+ j+2 = · · · = k∗ .
This implies that ct+ j = ct+ j+1 = ct+ j+2 = · · · and hence U1 (ct+ j ) =
U1 (ct+ j+1 ) = U1 (ct+ j+2 ) = · · · . So, in this situation the above Euler
equation reduces to
β[ F1 (k∗ ) + (1 − δ)] = 1.
Therefore, k∗ is determined by
F1 (k∗ ) = 1/β − 1 + δ.
F1 (k∗ ) = ι + δ,
the marginal product of capital is equal to the (net) rate of time pref-
erence, ι, plus the depreciation rate, δ. Since F1 is a strictly decreasing
function of k, this (nontrivial) steady state is unique.
deterministic dynamics 103
Note how k t+1 can be separated from the inner maximization prob-
lem. This can only be done since the return function U ( F (k t ) + (1 −
δ)k t − k t+1 ) doesn’t involve future values of the control variable, here
{ k t + j +1 } ∞
j=1 . This allows the maximization to be separated into two
maximization operations, with the max operator in the outer problem
cascading over the one in the inner problem.
The agent’s period-t dynamic programming problem is
This is called the Bellman equation. (To get the period t + j problem
just rewrite t as t + j). Effectively, this is just a two-period problem;
viz, today and the future. The future is encapsulated in the function
V (k t+1 ). This function gives the maximal level of lifetime utility that
can be obtained in period t + 1 contingent on Robinson having the
capital stock k t+1 .
The first-order condition for optimality in the period-t is
further below. The steady-state capital stock, k∗ , must satisfy the con-
dition
k ∗ = K ( k ∗ ).
(6.5.4)
U1 ( F (k t+ j ) + (1 − δ)k t+ j − k t+ j+1 )
= βU1 ( F (k t+ j+1 ) + (1 − δ)k t+ j+1 − k t+ j+2 )[ F1 (k t+ j+1 ) + (1 − δ)].
This is the Euler equation (6.3.1) again. So, the two approaches yield
the same solution to optimization problem.
106 numerical methods for macroeconomists with julia and matlab codes
Proof. Consider two points, viz k1t and k2t . Take a convex combination
of these two points; let kθt = θk1t + (1 − θ )k2t , for θ ∈ (0, 1). Need to
show that θV (k1t ) + (1 − θ )V (k2t ) < V (kθt ). Now, let kθt+ j = θk∗t+1 j + (1 −
θ )k∗t+2 j , where k∗t+1 j and k∗t+2 j are the optimal solutions for k1t+ j in (6.2.1)
starting off from the initial condition k t = k1t and k t = k2t , respectively.
(So, to be clear, in this proof an asterisk does not refer to the steady-
state value for capital.) It will be shown that the kθt+ j ’s are feasible
deterministic dynamics 107
Last note that on the righthand side of the above expression kθt+ j and
kθt+ j+1 are not optimal so that
∞
θV (k1t ) + (1 − θ )V (k2t ) < ∑ β j U ( F(k∗t+θ j ) + (1 − δ)k∗t+θ j − k∗t+θ j+1 ) = V (kθt ).
j =0
Imagine that Robinson Crusoe gets a tiny bit more capital in period t.
This will increase his period-t output by F1 (k t ) + (1 − δ). One would
expect that he will consume some of this windfall increase in output
and that he will save the rest. That is, one might expect that
I.e., that fact that K1 (k t ) > 0 implies that Robinson must be saving
some of the windfall increase in capital, while the condition K1 (k t ) <
F1 (k t ) + (1 − δ) means that he must be consuming part of it.
108 numerical methods for macroeconomists with julia and matlab codes
Property (6.6.1) can be derived in two ways. The first approach uses
the first-order condition connected with the dynamic programming
problem (6.5.2). Update (6.5.3) to get
The notation ·t+ j signifies that the arguments of the function are being
evaluated at time t + j. Note that the above derivation assumes that the
value function is continuously twice differentiable; there may be some
points where this is not the case. Equation (6.6.3) implies that period-
(t + j) consumption, ct+ j , will rise because dk t+ j+1 /dk t+ j < F1 (·t+ j ) +
1 − δ. So, the representative agent is both consuming and saving (since
0 < dk t+ j+1 /dk t+ j ) some of income resulting from an increase in the
period-(t + j) capital stock. Now, by updating the above formula it
follows that 0 < dk t+ j+2 /dk t+ j+1 < F1 (·t+ j+1 ) + 1 − δ. Therefore, the
increase in k t+ j will also cause both ct+ j+1 and k t+ j+2 to rise. And,
the increase in k t+ j+2 will induce ct+ j+2 and k t+ j+3 to move up and
so on. So, the effect of increase in the period-(t + j) capital stock will
propagate throughout the entire future increasing consumption and
the capital stock in every period.
The second approach employs the Euler equation (6.3.1).3 To show 3
The second approach can be skipped
that property (6.6.1) is consistent with the Euler equation (6.3.1), sub- for those not interested in formalities.
stitute out for k t+ j+2 using the updated relationship k t+ j+2 = K i (k t+ j+1 )
to get
where
dk t+ j+1
0< = K1i+1 < F1 + 1 − δ,
dk t+ j
because U11 (·t+ j )/∆ < 1. Hence, the property is self-fulfilling. Now,
limi→∞ K1i = K. Thus,
dk t+ j+1
0< = K1 < F1 + 1 − δ.
dk t+ j
6.7 Dynamics
Lemma 19. (If below the steady state, then rise, while if above, then fall.) If
k t+ j < k∗ , then k t+ j < k t+ j+1 , and if k t+ j > k∗ , then k t+ j > k t+ j+1 .
The dynamics for the Ramsey growth model can now be charac-
terized, with Lemma 19 in hand. They are portrayed in Figure 6.7.2.
110 numerical methods for macroeconomists with julia and matlab codes
because the slope of the 45 degree line is one. A local solution for
dk t+ j+1 /dk t+ j around the nontrivial steady state is given in Section
9.5. This is obtained by linearizing the Euler equation (6.3.1) around
the (unique nontrivial) steady state. It will be reaffirmed then that
(6.7.1) holds by examining the linear difference equation that arises
from the linearized Euler equation.
To conclude, the model’s transitional dynamics are as displayed
by Figure 6.7.2. When starting off below the steady state the capi-
tal stock monotonically increases until it converges to its steady-state
value. Along the transition path toward the steady state, the interest
rate steadily falls. To see this, note that the period-(t + j) gross interest
rate is given by U1 ( F (k t+ j ) + (1 − δ)k t+ j − k t+ j+1 )/[ βU1 ( F (k t+ j+1 ) +
(1 − δ)k t+ j+1 − k t+ j+2 )] = F1 (k t+ j+1 ) + 1 − δ. The term on the left is
the amount of period-(t + j + 1) consumption that the person must
receive in order to sacrifice a unit of period-(t + j) consumption. This
deterministic dynamics 111
k*
k* kt+j
The above analysis suggests that the neoclassical growth model can be
written as
The messy time subscripts have been eliminated in the above dynamic
programming problem where next period’s capital stock has a prime
symbol attached to it. This can be done because there is no notion of
time in Robinson Crusoe’s problem. All that matters is the amount of
capital that he enters a period with. The goal is to answer the following
questions concerning the value function, V:
1. Will V exist?
2. Is V unique?
3. Is V continuous?
4. Is V continuously differentiable?
5. Is V increasing in k?
6. Is V concave in k?
2. ρ( x, y) = ρ(y, x ),
3. ρ( x, z) ≤ ρ( x, y) + ρ(y, z).
where x (t) and y(t) are two functions in C . This is called the uniform
metric.
114 numerical methods for macroeconomists with julia and matlab codes
L.A., y
y(t)=1+t-t2
ρ(x,y)=0.25
1.15
1.10
1.05
x(t)=1
1.00
1 1 1 1 2
ρ( xn , xm ) = | − | ≤ + < ε, if m, n > .
m n m n ε
Given this it is often useful to focus attention on those metric spaces
(S , ρ) where all Cauchy sequences are guaranteed to converge to a
point in the space.
0.2
0.0
y = x0.5
y = x0.67 to a straight line, which is just
0.4 a concave, increasing function.
y = x0.75
y = x0.83
0.2 y=x
0.0
As the name implies, after applying the operator T the distance be-
tween functions contracts; i.e., the distance between Tx and Ty is
smaller than between x and y.
V ≤ W + ρ(V, W ).
TV ≤ T (W + ρ(V, W )) ≤ TW + βρ(V, W ).
| {z } | {z }
Monotonicity Discounting
Thus,
TV − TW ≤ βρ(V, W ).
TW − TV ≤ βρ(V, W ).
Consequently,
| TV − TW | ≤ βρ(V, W ),
so that
ρ( TV, TW ) ≤ βρ(V, W ).
Therefore T is a contraction.
deterministic dynamics 119
Is T a contraction?
= ( TW )(k).
2. Discounting.
= ( TV )(k) + βa.
Vi ( x0 ) = Wi ( x0 ), for i = 1, 2, ..., l.
x0 x
deterministic dynamics 121
ψ 2 φ b02
U (b k0 ) = τ + αb
k, b k0 + ρb
k − λb k0 − b
kb k − k .
2 2
Note the slight abuse of notation in redefining U to now be a func-
tion of b k0 . The derivatives in the above Taylor expansion could
k and b
be computed numerically using the formulae presented for numerical
first- and second-derivatives presented in Chapter 8.
· · · + β j U ( k t + j , k t + j +1 ) + β j +1 U ( k t + j +1 , k t + j +2 ) + · · ·
or
λ − ρb k0 = β(α + ρb
k + φb k00 − ψb
k 0 ).
λ = βα.
Therefore,
−ρbk + φbk0 = β(ρbk00 − ψbk0 ).
Conjecture a solution of the form
k 0 = ηb
b k,
deterministic dynamics 123
so that
k00 = ηb
b k0 .
There is no constant term since the decision rule has been defined in
terms of deviations from the steady state. Note that if η was negative,
then the capital stock would oscillate around the steady state, which
Figure 6.7.2 rules out. So, one should expect that η > 0. Now, if
0 < η < 1, then the difference equation is stable and convergence to
the steady state will be monotone.
Using the conjectured decision rule in the above Euler equation
gives
−ρbk + φbk0 = β(ρηbk0 − ψbk0 ),
which can be rewritten as
ρ
k0 =
b k.
b
(φ + βψ − βρη )
Hence, η must solve the quadratic
ρ
η= .
φ + βψ − βρη
Cross multiplying gives
− βρη 2 + (φ + βψ)η − ρ = 0.
This equation will have two roots. One will lie between 0 and 1 and
the other will be greater than 1. The stable root corresponds to the
situation where the decision rule crosses the 450 degree line in Figure
6.7.2.
To prove this formally, observe that when η = 0 the lefthand side of
this equation is negative. When η = 1 then the lefthand side is positive
because
βρ = −U11 (∗),
φ = −U11 (∗),
ρ = −U11 (∗)[ F1 (∗) + (1 − δ)],
so that
− βρ + (φ + βψ) − ρ =
U11 (∗) − U11 (∗) − U11 (∗)[ F1 (∗) + (1 − δ)]
− βU1 (∗) F11 (∗) + U11 (∗)[ F1 (∗) + (1 − δ)]
= − βU1 (∗) F11 (∗) > 0.
where ht+ j is the amount of work effort expended. This utility function
was introduced in Chapter 2. The function G : R+ → R+ gives the
disutility of work effort, measured in consumption units; assume that
it is strictly convex. Express the production function as
o t + j = F ( k t + j , h t + j ).
There will be two first-order conditions; viz, one for ht+ j and the other
for k t+ j+1 . The first-order condition governing period-(t + j) labor
effort, or ht+ j , is
which simplifies to
F2 (k t+ j , ht+ j ) = G1 (ht+ j ).
h t + j = H ( k t + j ).
k t+ j+1 , is
for j = 0, 1, · · · . The problem has been reduced to the earlier one; i.e.,
the solution for the model is once again represented by a second-order
nonlinear difference equation for the capital stock.
Adding income taxation into the above framework with a labor choice
is straightforward. This can be done along the lines discussed in Chap-
ter 2. To see how, let labor income be taxed at the rate τh . Likewise,
let the tax rate on capital income, net of the cost of depreciation, be
τk . For simplicity assume that all tax revenue is rebated back to the
consumer/worker in the form of lump-sum transfer payments, λ. Sup-
pose that consumer/workers own all the capital in the economy, which
they rent out to firms at the rate r. Likewise, they supply labor at the
wage rate w. Assume that tastes and technology have the forms given
in the previous section.
The representative consumer/worker’s period-(t + j) budget con-
straint reads
As before, this equation can be used to substitute out for ct+ j in the
consumer/worker’s utility function. The government’s budget con-
straint will appear as
U1 (ct+ j − G (ht+ j )) = β[1 + (1 − τk )(rt+ j+1 − δ)] × U1 (ct+ j+1 − G (ht+ j+1 )),
where ct+ j and ct+ j+1 are given by the consumer/worker’s con-
straint. The first-order condition for labor is
(1 − τh )wt+ j = G1 (ht+ j ).
c t + j + k t + j +1 = w t + j h t + j + r t + j k t + j + (1 − δ ) k t + j .
3. Use the firm’s first-order conditions to solve out for the wage and
rental rates in consumer/worker’s formula for consumption. After
employing Euler’s theorem, this will result in7 7
If government spending is added into
the mix, then the government’s budget
c t + j = F ( k t + j , h t + j ) + (1 − δ ) k t + j − k t + j +1 . constraint will read
Things are a little more complicated now, but this is just one equation
in the unknown variable e.
deterministic dynamics 127
subject to
c t + k t +1 = F ( k t , z t ) + (1 − δ ) k t ,
and the initial condition, k1 . Note the presence of the technology shock
in the production function. The Euler equation for this model is
(a) If so, exit the algorithm since a solution has been found.
j +1 T
(b) If not, set {k t } t =1 = {k t }tT=1 . Repeat step one using this new
guess.
(a) Start off in period 3. Given the starting value k1 and the guess
j j
k2 one gets k3 .
j j j
(b) Move on to period 4. Given k2 and k3 one can obtain k4 . and so
on.
(c) Proceed down the time path in the above manner until one gets
j j
to period T. Here one will enter the period with k T −2 and k T −1 .
The goal is to solve for the final capital stock, k T .
130 numerical methods for macroeconomists with julia and matlab codes
j
2. At time T it is desired that k T ' k∗ , the steady-state capital stock.
So, the algorithm amounts to solving the following nonlinear equa-
tion for k2 ,
KT (k1 , k2 ; z1 , · · · , z T ) − k∗ = 0, (6.12.5)
j j
(a) When a value for k2 is found that sets |k T − k∗ | < ε, the non-
linear equation solver will terminate since a solution has been
found.
(b) If not, the nonlinear equation solver will try a new guess for k2 ,
j +1
denoted by k2 . It will repeat step one using this new guess.
The idea of the algorithm is to pick the starting value for the period-
2 capital stock so that the second-order difference equation k t+2 =
D (k t+1 , k t , zt+1 , zt ) converges to the steady state at the end of T peri-
j
ods. Now, if k2 6= k2 the computed sequence of k’s will tend to diverge
away in an explosive manner from the true solution.
To see this, suppose that economy starts off from a value for k1 that
lies in some small neighborhood of the final steady state. Now, make
a guess for k2 that lies above the true value. By using (6.12.4) when
deterministic dynamics 131
The inequality follows from the fact that in a vicinity of the steady
state F1 (·t ) + (1 − δ) ' 1/β, U11 (·t ) = U11 (·t+1 ), etc. This says that the
impact of choosing a value for k2 that is too large will be magnified on
k3 . Employing (6.12.4) again for when t = 2, then gives
dk4 U (· )[dk3 /dk2 − F1 (·2 ) − (1 − δ)]
= 11 2
dk2 β[ F1 (·3 ) + (1 − δ)]U11 (·3 )
{ β[ F1 (·3 ) + (1 − δ)]2 U11 (·3 ) + βF11 (·3 )U1 (·3 )}dk3 /dk2
+
β[ F1 (·3 ) + (1 − δ)]U11 (·3 )
' 1 + βF11 (·2 )U1 (·2 )/U11 (·2 )
+ { F1 (·3 ) + (1 − δ) + βF11 (·3 )U1 (·3 )/U11 (·3 )}dk3 /dk2
dk
> 3.
dk2
Hence, the error in the startup value for the difference equation will
cascade into the future in an explosive manner. The situation is por-
trayed in Figure 6.12.1.
Explosive
Path
k*
True Path
k2 j
k2
k1
1 2 3 T t
Reverse Shooting
The difference equation (6.12.1) could also be run backwards. Specif-
j
ically, note that one could use (6.12.1) to solve for a value of k t given
132 numerical methods for macroeconomists with julia and matlab codes
j j
values for k t+1 and k t+2 . Again, this describes one equation in one
unknown. Represent the solution by
←
−
k t = D ( k t +1 , k t +2 ; z t +1 , z t ).
Start the system off at time T from the terminal condition k T = k∗ and
z T = z∗ . Given a value for k T −1 one could run the above difference
equation backwards in time to get k T −2 , k T −3 , · · · , k1 . This iterative
scheme can be thought of as yielding a solution for k1 as a function of
k T −1 and k∗ . Represent this function by K1 (k T −1 , k∗ ; z T , z T −1 , · · · , z1 ).
Clearly, k T −1 should be chosen so that
K1 (k T −1 , k∗ ; z T , z T −1 , · · · , z1 ) − k1 = 0;
where ct is period-t total cost and ot−1 is the monopolist’s level of out-
put in period t − 1. This cost function introduces a dynamic element
into the analysis. By producing more in period t − 1 the monopolist
can reduce his costs in period t. Think about this as adding learning
by doing into the analysis.8 8
For the learning-by-doing interpreta-
The monopolist’s period-t revenue is tion to make sense, assume that ot >
κot−1 . This will be the case in the set-
β 2 ting discussed here.
pt ot = αot −
o .
2 t
This implies that his period-t profits, πt , read
β 2 γ
o − (ot − κot−1 )2 .
αot −
2 t 2
Therefore, the monopolist’s maximization problem is to pick his out-
put in each period to maximize the present value of his profits. Sup-
pose that the monopolist’s discount factor is δ. The mathematical
transliteration of this problem is
∞
β 2 γ
max { ∑ δt−1 [αot − o − (ot − κot−1 )2 ]}.
{ot }∞
t =1 t =1
2 t 2
Note that ot appears in exactly two periods in this optimization prob-
lem: in periods t and t + 1. To see this, write out the objective function
as
β γ β γ
· · · + δt−1 [αot − ot2 − (ot − κot−1 )2 ] + δt [αot+1 − ot2+1 − (ot+1 − κot )2 ] + · · · .
2 2 2 2
The first-order condition associated with this maximization problem is
α − βot = γ(ot − κot−1 ) − δγκ (ot+1 − κot ),
| {z } | {z }
MR MC
which sets marginal revenue, MR, equal to marginal cost, MC. Ob-
serve that when the monopolist increases his output in period t he will
reduce his cost in period t + 1 (at least when ot+1 > κot ). The above
first-order condition represents a linear 2nd-order difference equation
in output.
Steady-State Output
Let o ∗ denote the steady-state level of output. It must solve the equa-
tion
α − βo ∗ = γ(o ∗ − κo ∗ ) − δγκ (o ∗ − κo ∗ )
α − βo ∗ = γ(1 − κ )o ∗ − δγκ (1 − κ )o ∗ ,
which implies
α α
o∗ = = .
β + γ(1 − κ ) − δγκ (1 − κ ) β + γ(1 − κ )(1 − δκ )
Observe that the steady-state level of output is increasing in κ. So,
adding learning in the model raises the steady-state level of output.
134 numerical methods for macroeconomists with julia and matlab codes
(1 − ψ ) α
η= . (6.13.2)
β + γ(1 − κ ) − δγκ (1 − κ )
If this is the case, then one can rewrite the first-order condition as
Therefore,
so that
α + δγκη γκ
ot = 2
+ o t −1 .
γ + β − δγκψ + δγκ γ + β − δγκψ + δγκ 2
−δγκψ2 + (γ + β + δγκ 2 )ψ − γκ = 0.
Since this is a quadratic equation there will be two roots. Observe that
the lefthand side of the above equation is negative when ψ = 0 and
that its derivative is positive at this point. The expression is positive
when ψ = 1 (since the steady-state level of output must be positive).
The lefthand side eventually becomes negative as ψ becomes large. It
is easy to solve this equation for ψ on the computer. Given the solution
for ψ one can recover the solution for η using (6.13.2). The time path
for output is then obtained by iterating on (6.13.1) starting from o0 = 0.
Multiple Shooting
Now, the second-order difference equation for output, associated with
the first-order condition arising from the above maximization problem,
can be rewritten as
α β + γ + δγκ 2 γκ
o t +1 = − + ot − o ,
δγκ δγκ δγκ t−1
deterministic dynamics 135
or
ot+1 = d + eot + f ot−1 ,
α γκ δγκ
ot = 2
+ 2
o t −1 + o t +1 .
β + γ + δγκ β + γ + δγκ β + γ + δγκ 2
ot = g + hot−1 + iot+1 ,
9 % S e t parameters f o r model
10
11 % Demand curve
12 alpha = 1 ; % c o n s t a n t
13 beta = 0 . 5 ; % slope
14
15 % Cost f u n c t i o n
16 gamma = 0 . 5 ; % q u a d r a t i c term
17 kappa = 0 . 9 ; % c o s t r e d u c t i o n term
18
19 % Discount f a c t o r
20 delta = 0.96;
21
22 % Time horizon f o r s i m u l a t i o n
23 T = 1 0 ; % Number o f p e r i o d s
24
25 % Compute steady − s t a t e l e v e l o f output
26 o s t a r = alpha /( b e t a + gamma * (1 − kappa ) * (1 − d e l t a * kappa ) ) ;
3 % S e t up t h e Quadratic Formulae f o r P s i
4 a = − d e l t a * gamma * kappa ; % C o e f f i c i e n t on squared term
deterministic dynamics 137
24 % I t e r a t e on d i f f e r e n c e e q u a t i o n f o r output
25 % S e t up v e c t o r t o s t o r e outputs f o r d e c i s i o n − r u l e approach
26 ovec = z e r o s ( T , 1 ) ;
27 time = ( 1 : T ) ’ ; % S e t up v e c t o r f o r time
28 % Note t h e t h e f i r s t element i n t h e s e v e c t o r s , ovec ( 1 , 1 ) and
time ( 1 , 1 ) ,
29 % a c t u a l l y correspond t o period zero
30 for t = 2:T
31 ovec ( t , 1 ) = e t a + p s i * ovec ( t − 1 , 1 ) ;
32 % S t a r t i n g value f o r loop i s ovec ( 1 , 1 ) = 0
33 end
34 pvec = alpha − b e t a * ovec / 2 ; % Compute p r i c e s over time
35
36 % Plot results
37 % Here a graph with 6 p a n e l s i s c o n s t r u c t e d i n t h e form o f a 3
by 2 matrix
38 % The l a s t number r e f e r s t o t h e p o s i t i o n o f t h e p l o t
39 % i n t h e matrix , running 1 t o 6
40 % Plot results
41 figure (2)
42 s u b p l o t ( 3 , 2 , 1 ) % Multipanel graph
43 p l o t ( time , ovec )
44 t i t l e ( ’ Outputd ecision r u l e approach ’ )
45 x l a b e l ( ’ Time ’ )
46 y l a b e l ( ’ Output ’ )
47 s u b p l o t ( 3 , 2 , 2 ) % Multipanel graph
48 p l o t ( time , pvec )
49 t i t l e ( ’ P r i c e d e c i s i o n r u l e approach ’ )
50 x l a b e l ( ’ Time ’ )
51 ylabel ( ’ Price ’ )
11 o2guess = . 5 * ovec ( 2 , 1 ) ;
12 o2 = f z e r o ( @multshooting , o2guess ) ;
13
14 % Check s o l u t i o n
15 errorms = abs ( multshooting ( o2 ) ) ; % a b s o l u t e value o f e r r o r i n
solution
16 i f errorms = 1 . E−9
17 disp ( ’ S o l u t i o n has not been found ’ )
18 disp ( errorms )% e r r o r
19 end
20 % Note t h a t t h e v e c t o r ovecms i s passed back from t h e f u n c t i o n
21 % multshooting using t h e g l o b a l s t a t e m e n t .
22 % The s t a t e m e n t multshooting ( o2 )
23 % e n s u r e s t h a t t h i s v e c t o r i s computed
24 % using MATLAB’ s f i n a l s o l u t i o n f o r o2
25 pvecms = alpha − b e t a * ovecms / 2 ; % Compute p r i c e s over time
26
27 % Plot results
28 subplot ( 3 , 2 , 3 )
29 p l o t ( time , ovecms )
30 t i t l e ( ’ Outputmultiple s h o o t i n g ’ )
31 x l a b e l ( ’ Time ’ )
32 y l a b e l ( ’ Output ’ )
33 subplot ( 3 , 2 , 4 )
34 p l o t ( time , pvecms )
35 t i t l e ( ’ Pricemultiple shooting ’ )
36 x l a b e l ( ’ Time ’ )
37 ylabel ( ’ Price ’ )
32 subplot ( 3 , 2 , 6 )
33 p l o t ( time , pvecms )
34 t i t l e ( ’ P r i c e e x t e n d e d path ’ )
35 x l a b e l ( ’ Time ’ )
36 ylabel ( ’ Price ’ )
16 % I t e r a t e on d i f f e r e n c e e q u a t i o n s t a r t i n g from period 3
17 % using s t a r t i n g v a l u e s f o r t h e f i r s t two p e r i o d s f o r output
18 for t =3:T
19 ovecms ( t , 1 ) = d + e * ovecms ( t − 1 , 1 ) + f * ovecms ( t − 2 , 1 ) ;
20 % Note t h a t t h e s t a r t i n g value f o r output , ovecms ( 1 , 1 ) = 0
21 % Likewise , t h e second s t a r t i n g value , ovecms ( 2 , 1 ) , i s equal
22 % t o t h e guess
23 end
24
13 r e v i s e d p a t h = z e r o s ( T , 1 ) ; % S t a r t i n g l e v e l o f output i s zero .
14 r e v i s e d p a t h ( T , 1 ) = o s t a r ; % Terminal output i s t h e steady −
state level .
15 % I t e r a t e on d i f f e r e n c e e q u a t i o n s t a r t i n g from period 2 using
starting
16 % value f o r t h e f i r s t period . There i s no need t o s o l v e f o r
the l a s t
17 % period output l e v e l s i n c e t h i s taken t o be t h e steady − s t a t e
l e v e l of
18 % output .
19
20 f o r t = 2 : T−1
21 r e v i s e d p a t h ( t , 1 ) = g + h * r e v i s e d p a t h ( t − 1 , 1 ) + i * guesspath (
t +1 ,1) ;
22 % Note t h a t t h e s t a r t i n g value f o r output or r e v i s e d p a t h
(1 ,1) = 0
23 % Likewise , t h e t e r m i n a l value , or r e v i s e d p a t h ( T , 1 ) , i s
equal t o
24 % ostar .
25 end
26
27 end
-0.2
-0.4
-0.6
0 0.2 0.4 0.6 0.8 1
Psi
Price
0.8
1
dynamic monopoly model. Ob-
0.6
0 serve that the three techniques
0 5 10 0 5 10
Time Time give the same solution for the
Output−−multiple shooting Price−−multiple shooting time path of output and prices.
2 1
Output
Price
0.8
1
0.6
0
0 5 10 0 5 10
Time Time
Output−−extended path Price−−extended path
2 1
Output
Price
0.8
1
0.6
0
0 5 10 0 5 10
Time Time
7 Malthus to Solow
“In October 1838, that is, fifteen months after I had begun my system-
atic inquiry, I happened to read for amusement Malthus on Population,
and being well prepared to appreciate the struggle for existence which
everywhere goes on from long- continued observation of the habits of
animals and plants, it at once struck me that under these circumstances
favourable variations would tend to be preserved, and unfavourable
ones to be destroyed. The results of this would be the formation of a
new species. Here, then I had at last got a theory by which to work.”
Charles Darwin (1876)
7.1 Introduction
The goal here is to model the transition from a world where living
conditions were stagnant over a long period of time to a world with
rising living standard.1 The analysis presumes the existence of two 1
This section is based on Hansen and
technologies: Malthus and Solow. The preindustrial era uses a land- Prescott (2002).
intensive technology. This constant-returns-to-scale technology also Thomas R. Malthus (1766-1834) was
employs capital and labor. Productivity for this technology grows at a an English cleric and economist.
very slow rate. Land is in fixed supply. The necessity to use land places He write the famous book An Essay
on the Principle of Population, which
a drag on growth. This technology is dubbed the Malthus technology.
postulated that the size of a
The modern era uses a constant-returns-to-scale technology employing population is limited by the
just capital and labor. Productivity for this technology grows at a faster productive capacity of land.
clip than the preindustrial one, although its productivity starts off from Charles Darwin credits Malthus’s
a very low level. This technology is labeled the Solow technology.- work as being instrumental in
Both technologies are always available. At low levels of development forming his theory of evolution.
it pays only to use the Malthus technology. As the economy develops Robert M. Solow (1923-) is an
it becomes profitable also to use the Solow technology. The Malthus American economist who is best
technology fades away asymptotically. known for his work on economic
growth. He broke down the sources
of economic growth into changes in
7.2 A Graphical Exposition of Malthusian Theory the labor supply, increases in the
capital stock, and technological
Malthusian theory states the size of the population will be regulated by progress. He won the Nobel Prize
in 1987.
the productive capacity of the economy. Figure 7.2.1 portrays the situ-
ation. Start with the lower panel. Income per worker, y, is negatively
related to the number of workers, n, as the curve in the lower panel
144 numerical methods for macroeconomists with julia and matlab codes
mortality rate
0
y* y y
n income per
person
n*
productivity
n
number
of people
7.3 Facts
The key factor in the Malthus technology is its use of land, lm . The ad-
vanced technology is dubbed the Solow technology. Under the Solow
technology production is governed by
ys = as kθs n1s −θ ,
1985 $US 12
40
10
30
8
20 6
4
10
2
0 0
1650 1700 1750 1800 1850 1900 1950 2000 2050
Year
1.5
0.5
0
1600 1700 1800 1900 2000 2100
-0.5
Year
148 numerical methods for macroeconomists with julia and matlab codes
Percentage
Figure 7.3.5: Value of farm land
100 relative to GDP, U.S., 1870-1990
90
80
70
60
50
40
30
20
10
0
1860 1880 1900 1920 1940 1960 1980 2000
and
max{ as kθs n1s −θ − wns − rk k s }, (7.4.2)
k s ,ns
where w is the wage rate and rk and rl are the rental rates on capital
and land.
c1 + k0 + ql 0 = w,
and
c20 = rk0 k0 + (rl0 + q0 )l 0 .
Here q is the current price for a unit of land. The first-period budget
constraint states that when young consumption, c1 , and savings in
malthus to solow 149
7.4.3 Demographics
Population growth is simply given by
7.4.4 Equilibrium
Let n represent the size of the today’s young population and n−1 the
size of the old population. An equilibrium for the above economy
must satisfy the following conditions.
(b) Labor
nm + ns = n,
(c) Land
n−1 l = 1,
(d) Goods
nc1 + n−1 c2 + nk0 = ym + ys ,
where aggregate consumption and investment are given by c =
nc1 + n−1 c2 and k0 = nk0 .
This cost function has the standard properties. Cost is increasing and
concave in both prices, rk and w, separately. It is homogenous of de-
gree one in both prices together. That is, if both prices are increased
by a factor λ, then costs will rise by this factor too. Cost also rises with
the level of output, ys , produced. Here marginal cost is
a− 1 −θ
s θ (1 − θ )
−(1−θ ) θ 1−θ
rk w .
1 −1/(φ+µ) φ µ/(φ+µ) φ
a− 1 −θ
s θ (1 − θ )
−(1−θ ) θ 1−θ
rk w > am [( ) + ( )−φ/(φ+µ) ]
(φ + µ) µ µ
φ/(φ+µ) 1/(φ+µ)−1
× rk wµ/(φ+µ) ym .
That is, the Solow sector will not operate at any aggregate output lev-
els, ys , where the Solow sector has higher marginal cost. Both sectors
will operate only when
1 −1/(φ+µ) φ µ/(φ+µ) φ
a− 1 −θ
s θ (1 − θ )
−(1−θ ) θ 1−θ
rk w = am [( ) + ( )−φ/(φ+µ) ]
(φ + µ) µ µ
φ/(φ+µ) 1/(φ+µ)−1
× rk wµ/(φ+µ) ym .
The Malthus sector will always operate since, as was mentioned, its
marginal cost goes to zero as output goes to zero. Figure 7.5.1 shows
the adoption point, at a given set of factor prices.
malthus to solow 151
MCs
ym
ym, ys
k s θ −1 r w θ −1
as θ ( ) = a s θ ( k )1− θ ( ) = rk .
ns θ 1−θ
152 numerical methods for macroeconomists with julia and matlab codes
c 1 = G − 1 ( γy m ) .
Third, the level of wages, w, can now be computed. From the con-
sumer’s problem it can be calculated that
c1 = w/(1 + β).
w = ( 1 + β ) c 1 = ( 1 + β ) G − 1 ( γy m ) . (7.6.1)
that is, the price of land is the discounted value of the (growth-adjusted)
rents that it will earn. It is immediate that rk = i since the gross return
on capital must equal the gross interest rate.
or
(w − c1 ) = β/(1 + β)w = γym k m /n + q/n. (7.6.3)
Think about (7.6.3) as determining the interest rate i. Observe that the
lefthand side is a constant, since w is just a function of γym . It will now
be shown that k m /n and q/n on the righthand side can be expressed
as functions of i. Given this, equation (7.6.3) determines i. [Condition
(7.6.3) is actually the same as the goods market-clearing condition. Try
to convince yourself that this is true.] Thus, in a steady state for an
overlapping generations model the interest rate, i, is not pinned down
in simple fashion by the discount factor, as in the representative agent
model.
By taking the ratio of the first-order conditions for labor and capital
in the firm’s optimization problem for the Malthus sector, it can be
shown that
km
= (w/i )(φ/µ).
nm
In a Malthus-only equilibrium nm is equal to the size of the young
population, n. Thus,
k m /n = (w/i )(φ/µ).
It’s clear from (7.6.4) that n, or the size of the young population, can
be written as a simple function of i–recall that w is pinned down by
154 numerical methods for macroeconomists with julia and matlab codes
(7.6.1). Specifically,
1 1/(φ+µ−1) (φ−1)/(φ+µ−1) (1−φ)/(φ+µ−1) i φ/(φ+µ−1)
n=( ) µ w ( ) .
am φ
7.7 Calibration
This function is plotted in Figure 7.7.1 below. The figure has three
interesting features. First, at the Malthusian level of living standards,
1/(1−µ−φ)
c1 = c1m , population grows at the same pace as output, γm ,
where γm is the rate of growth in am . Second, as living standards
double from the Malthusian level the population growth rate rises to
a point where it doubles every thirty-five (the period length) years.
Third, from this point to the point where living standards are 18 times
the Malthusian one (2c1m ≤ c1 ≤ 18c1m ) the rate of population growth
declines until a stationary level is attained.
the model gives reasonable values for the interest rate in the Malthus
and Solow eras. This is because the interest rate is not pinned down
by the discount factor, alone, in an overlapping generations model.
Remark 43. If both land and labor were fixed, then in an economy us-
1/(1−φ)
ing only the Malthus technology output would grow at rate γm ,
1/(1−θ )
while in a Solow one the growth rate would be γs . Thus, when
γm ≥ γs the Solow economy grows faster because θ > φ; i.e., the
reproducible factor, capital, has a larger share in the Solow economy.
7.8 Results
8.1 Introduction
F ( x + h) − F ( x − h)
F1 ( x ) = ,
2h
where h is some small number. Computing numerical derivatives is a
bit trickier than this formula suggests. Mathematically speaking, one
would like to make h as small as possible. But, note that the difference
between F ( x + h) and F ( x − h) will be rounding error if h is made too
small. This occurs because the numbers F ( x + h) and F ( x − h) will
be computed with small errors at the nth decimal where n is some
integer, say 10. If h is made too small the difference will just be this
error. Dividing this through by a very small h then blows this up.
Hence, there is a trade-off. Making h small improves mathematical
precision but increases numerical error. So, how small should h be? A
good lower bound on a PC would be about 1.0e-5.
To see the problem formally, take a first-order Taylor expansion of
the function F around the point x. (Chapter A reviews the concept of
a first-order Taylor expansion.) One gets
Subtracting the second equation from the first, while applying the in-
termediate value theorem, gives,
x-h x x+h x
F ( x − h) as F ( x − h) + ε −h . Therefore,
Thus, there are two types of error on the righthand side of the equa-
tion. The mathematical approximation error given by [ F11 (ξ ) − F11 (ζ )]h/4
and the machine error shown by (ε +h − ε −h )/(2h). The first gets
smaller when h is reduced while the latter becomes bigger. Note that
by computing the derivative in both the forward and backward direc-
tions (or centering the derivative around the point x) the mathematical
approximation error is reduced.
The second derivative is just the difference between two first deriva-
tives
[ F ( x + h) − F ( x )]/h − [ F ( x ) − F ( x − h)]/h
F11 ( x ) =
h
F ( x + h) − 2F ( x ) + F ( x − h)
= .
h2
This second derivative is automatically centered around the point x.
Last, suppose that the function is F ( x, y). From the above it is easy
to deduce that
F ( x + h, y + h) − F ( x + h, y − h)
F2 ( x + h, y) =
2h
and
F ( x − h, y + h) − F ( x − h, y − h)
F2 ( x − h, y) = .
2h
So, the centered cross derivative is1 1
It should be immediate that
F11 ( x, y) = [ F ( x + h, y) − 2F ( x, y) + F ( x − h), y]/h2
and
F22 ( x, y) = [ F ( x, y + h) − 2F ( x, y) + F ( x, y − h]/h2 .
160 numerical methods for macroeconomists with julia and matlab codes
[ F ( x + h, y + h) − F ( x + h, y − h)]/2h
F12 ( x, y) =
2h
[ F ( x − h, y + h) − F ( x − h, y − h)]/2h
−
2h
F ( x + h, y + h) − F ( x + h, y − h) − F ( x − h, y + h) + F ( x − h, y − h)
= .
4h2
step differentiation:
db φ db
and .
dφ b dφ
Recall the the failure rates for 1900 and 2000 are 72 and 30 percent.
The results are shown in the table below. Interestingly, an drop in the
failure rate for 1900 leads to a hike in non-marital births while for 2000
it causes a fall. I.e., an one percentage point drop in failure rate in 1900
leads to a 0.47 percentage point increase in the fraction of young women
having a non-marital birth, while in 2000 it causes a 0.37 percentage
point decrease. In elasticity terms a one percent drop in φ in 1900 leads
to 2.18 percent increase in b while for 2000 it causes a 0.48 percent
decrease.
Contraception and Non-Marital Births
db/dφ (φ/b)(db/dφ)
1900 -0.47 -2.18
2000 0.37 0.48
What explains this? A drop in the failure rate makes sex safer but
it also entices more women to engage in premarital sex. For 1900
the impact on b = φp from a rise in p is bigger than the effect from
a decline in φ. Since not many women were having premarital sex at
that time a reduction in the failure rate on non-marital births is modest.
For 2000 exactly the opposite is true since a large fraction of unmarried
young women were sexually active. As the failure rate approaches 0
so will the number of non-marital births to young women.
162 numerical methods for macroeconomists with julia and matlab codes
That is, the task is to compute the area under the function F on the
domain [ x0 , xn ], as shown in Figure 8.4.1. The classical way to do this
is to break up the distance between x0 to xn into a grid of n equally
spaced points { x0 , x1 , · · · , x j−1 , x j, · · · , xn } where x j − x j−1 = h for all
j = 1, · · · , n. Now, take any two adjacent points, say x j−1 and x j .
Over the interval [ x j−1 , x j ] the function F ( x ) will be approximated by
a trapezoid, T j−1 ( x ). Specifically,
where
y j−1 ≡ F ( x j−1 ) and y j ≡ F ( x j ),
and
µ = ( x − x j −1 ) / ( x j − x j −1 ).
The accuracy of this approximation will depend on how fine the grid,
{ x0 , x1 , · · · , x j−1 , x j, · · · , xn }, is. The more points, or the smaller h is,
the higher will be the approximation. Of course, the function F ( x )
doesn’t have to be approximated by a series of trapezoids. Other better
shapes could be used.
yj-1 = F(xj-1)
x0 x1 xj-1 xj xn x
The first PC to be successfully mass produced was the Apple II, which
was introduced in 1977. The computer sold for roughly $1,200. Its
microprocessor ran at 1MHz and the PC had 4 kb of random-access
memory (RAM). There was no hard disk. An audio cassette was used
for program loading and data storage. Now, the speed of PCs is mea-
sured in terms of gigaHZ, RAM in gigabytes, and hard drive storage
in terabytes. Peripherals such as monitors and speakers are also much
better. The quality adjusted price of computers dropped at 25 per-
cent per year between 1977 to 2004. Over that time period, spending
on computers and peripherals rose from 0 to 0.6 percent of personal
consumption spending.
Greenwood and Kopecky (2013) estimated a nonlinear demand curve
for computers of the following form for the period 1977 to 2004:
(y + pυ)
c = D ( p, y) = − υ, with υ, θ, ρ > 0,
p + θ pρ
a given level of income, y, the demand curve for computers has the
form shown in Figure 8.5.1. The price ph where the demand curve hits
the vertical axis is known as Hick’s virtual price. It solves the equation,
(y + ph υ)
ρ − υ = 0.
ph + θ ph
x mod m.
Observe that2 2
To understand the upper bound, ob-
0≤r=x mod m ≤ m − 1. serve that for any x the largest value
for the remainder must occur when
floor ( x/m) = 0, which gives r =
Example 44. (Remainder from 22 ÷ 7) 22 mod 7 = 22− floor (22/7) × x. But, this requires m > x. The
7 = 22− floor (3.1429) × 7 = 22 − 3 × 7 = 1. largest value of x compatible with this
inequality, m > x, is x = m −
Example 45. (Remainder from 11 ÷ 7) 11 mod 7 = 11− floor (11/7) × 1. The lower bound is easy to de-
duce. Clearly, r cannot be nega-
7 = 11− floor (1.5714) × 7 = 11 − 1 × 7 = 4.
tive because x ≥ floor ( x/m)m. But,
floor ( x/m)m = x whenever m = x. So
Example 46. (Remainder from 44 ÷ 7) 44 mod 7 = 44− floor (44/7) × 0 = x − floor ( x/m)m is an admissible
7 = 44− floor (6.2857) × 7 = 44 − 6 × 7 = 2. value for the remainder when m = x.
0 ≤ x j+1 ≤ m − 1.
If x j+1 ever returns a number that was generated before then the al-
gorithm will repeat itself. Since there are only m possible values of x
the generator must cycle after m periods. Define the pseudo-random
number, u j+1 , that is associated with this sequence by
x j +1
u j +1 = .
m
166 numerical methods for macroeconomists with julia and matlab codes
As can be seen from the above example, for small values of a and
m such a random number generator has very poor qualities. But, it
works reasonably well for large values. An earlier version of MATLAB
set m = 231 − 1 (a Mersenne prime number) and a = 75 = 16, 807. This
takes 231 − 2 periods before it cycles.
of two adjacent values for the business cycle index o, say ot and ot−1 ,
would have one random cause unique to itself, here xt and xt−10 , and 9
random causes in common, or xt−1 to xt−9 . Because the business cycle
index has events in common there appears to be a correlation between
them even though the series of causes are independent. Figure 8.7.1
shows the upshot of Slutsky’s Monte Carlo simulation. It exhibits the
same pattern of fluctuations as displayed in the business cycle data for
ninetieth century England. Think about how much effort and time it
since all the x’s are independently and identically distributed. This
implies that the sample’s standard deviation around the mean, σn , will
√
decline with n. Hence, to reduce the standard error by half, the
sample size needs to be quadrupled.
The righthand side of the above expression gives the lifetime utility
that the representative agent realizes when consumption and hours are
stabilized at their mean levels. So, the question being posed is what
fraction of consumption in each and every state would the person have
to be given to make him as well off as in a world without fluctuations.
Observe that there is no β in the formula because it will cancel out of
both sides of the equation. Last, despite being complicated looking,
this is only one equation in one unknown, e.
To come up with an estimate of the welfare cost of business cycles,
let utility be given by
so that
Z Z
e = exp {θ ln(c) + (1 − θ ) ln(1 − h) − [θ ln(c) + (1 − θ ) ln(1 − h)]dD (c, h)}/θ − 1.
For the analysis assume that ln c and ln h follow the bivariate nor-
2 , σ2 , σ
mal distribution N (µln c , µln h , σln c ln h ln c,ln h )–the bivariate normal
distribution is presented in Chapter A. As in the U.S. data, set the
variances and covariance as follows: σln 2 = 0.02172 , σ2 = 0.02572 ,and
c ln h
σln c,ln h = 0.6160 × (0.0217 × 0.0257). Without loss of generality, pick
µln c = −0.0002 and µln h = −1.0989, which imply that the means of c
and h are 1.0 and 0.3333. Call up 100, 000 random draws of (c, h) from
this distribution and then calculate
100,000
1
100, 000 i∑
[θ ln(ci ) + (1 − θ ) ln(1 − hi )],
=1
RR
which is a Monte Carlo approximation to the expectation [θ ln c +
(1 − θ ) ln(1 − h)]dD (c, h). Using the expectation in the formula for e re-
sults in 100 × e = 0.040. Thus, the representative agent needs be given
an extra 0.04 percent in terms of consumption to make him as well off
as in a world without business cycles! By comparison, Lucas (1987,
Table 2, p. 26)’s estimate of the cost of consumption fluctuations was
0.072 percent, at least when the utility function was logarithmic and
numerical approximations 171
The πij ’s are called transition probabilities. Since the odds of how
the system moves depend solely on where the system is currently, the
system is called memoryless. Now,
n
∑ πij = 1, for all i,
j =1
Example 49. (Two-State Markov Chain) Let z have two values, a low
value (state 1) and a high one (state 2) represented by z and z. Suppose
that the odds of going from the low value (state 1) to the high value
(state 2) are given by π12 and from the high value to the low value are
π21 . These are called transition probabilities. Note that π11 = 1 − π12
and π22 = 1 − π21 . Figure 8.10.1 illustrates the situation. Let π12 =
π21 . This implies π11 = π22 . Hence, the chain is symmetric, a common
assumption.
Now, load the transition probabilities, or the πij ’s, into a matrix, T,
as follows
π11 · · · π1n
. .. ..
T = .. . . .
|{z}
n×n πn1 · · · πnn
172 numerical methods for macroeconomists with julia and matlab codes
1 π 11 1
π 12
π 21
2 2
π 22
Each row of the matrix sums to one, since ∑nj=1 πij = 1, for all i. Sup-
pose that one is given some initial probability distribution, ρ0 , over the
position of the states, where
ρ1 = ρ0 × T .
|{z} |{z} |{z}
1× n 1× n n×n
If one knew with certainty that the time-0 state of the system is zi , then
just set ρ0i = 1 and ρ0j = 0 for all j 6= i. So, writing the time-0 state of
the system in the above manner is without loss of generality.
By expanding the above formula, it can be seen that
π11 ··· π1n
. n n
1 1 0 0 .. .. 0 0
(ρ1 , ..., ρn ) = (ρ1 , ..., ρn ) .. . . = ( ∑ ρi πi1 , · · · , ∑ ρi πin ).
i =1 i =1
πn1 ··· πnn
Take the probability of being in state j next period or ρ1j . This will be
n
given by ρ1j = ∑ ρ0i πij . In theory one can get to state j in period 1 by
i =1
starting out from any state i in period 0. The odds of initially being in
state i are given by ρ0i while the odds of then transiting from i to j are
πij . So, ρ0i πij represents the probability of starting out in i and then
moving to j.
numerical approximations 173
ρ m = ρ m −1 T = ρ m −2 T 2 = · · · = ρ 0 T m .
ρ∗ = ρ∗ T. (8.10.1)
ρ j+1 = ρ j T, (8.10.2)
or
ρ∗ = b ∗ [ I − T
b ] −1 . (8.10.3)
Since the matrix I − T does not have full rank, the last equation in
the system has (or the one for ρn ) been replaced by the equation
∑i ρi = 1, which can be written as ρn = 1 − ∑i6=n ρi . (To see that
I − T does not have full rank replace each πii with 1 − ∑ j6=i πij (=
πii ). The sum of the last n − 1 columns in I − T then equals the
negative of the first one.) This is resolved in the above system of
equations by writing the last equation as ρn = −ρ1 · · · − ρn−1 + 1.
eT = εe,
174 numerical methods for macroeconomists with julia and matlab codes
4. Last, one could conduct a Monte Carlo simulation to find the sta-
tionary distribution. To do this, the Markov chain is simulated for
a long time series of identically and independently distributed ran-
dom shocks drawn from a uniform distribution on [0, 1]. Suppose
in some time period that the Markov chain is in state i. The Markov
chain will move to state j next period if the shock for this period
j j +1
lies in the interval [∑l =1 πil , ∑l = j πil ].
One might guess that the existence of a unique, invariant long-run
distribution might be related to whether or not the operator T is a
contraction mapping. Let P n represent the space of n-dimensional
probability vectors. Think about the transition matrix as defining an
operator T : P n → P n .
Lemma 50. (Convergence to a unique, invariant long-run distribution) limm→∞ ρ0 T m =
ρ∗ for all ρ0 ∈ T n if and only if for some m it occurs that T m defines a con-
traction on P n .
Proof. See Stokey and Lucas (1986, chp 11).
Condition 51. (Mixing) For some j there exists a m such that mini πijm >
0.
That is, there exists a column where all the entries have non-zero ele-
ments. This condition implies that at interation m it is possible to get
into state j from any other state. In other words, the system can’t get
stuck with probability one in any other state i 6= j.
Some examples for two-state Markov chains are presented now.
must solve
" #
1 − π12 π12
( ρ1 , ρ2 ) = ( ρ1 , ρ2 ) .
π21 1 − π21
satisfy this equation. Or one could use the equations ρ1∗ = ρ1∗ (1 −
π12 ) + ρ2∗ π21 and ρ1∗ + ρ2∗ = 1, which is really just a restatement of
(8.10.3) for the 2 × 2 case.
current positions. The goal here is use a two-state Markov chain over
employment and unemployment to estimate what percentage of work-
ers are separated from their jobs every month and what percentage of
the unemployed find a job. To do this, data will be used on the aver-
age rate of unemployment and the average duration of unemployment.
This data will then be used to back out the job finding and separation
using the stationary distribution from the Markov chain.
Let the transition between employment, e, and unemployment, u,
be governed by the job-finding probability φ—how likely an unem-
ployed worker finds a job—and the separation probability σ—how
likely an employed worker becomes unemployed. Given this, the ag-
gregate rates of employment and unemployment will evolve according
to
et+1 = (1 − σ )et + φut
and
ut+1 = σet + (1 − φ)ut .
These equation are easy to explain. Take the one for unemployment.
In period t the employment rate is et . Out of this component of the
labor force the fraction σ will lose their job and enter unemployment in
period t + 1. Likewise, in period t the unemployment rate is ut . From
pool of unemployed the fraction 1 − φ will not find a job and therefore
will remain unemployed in period t + 1. An interesting statistics to
analyze is the average duration of unemployment, d. This is given by4 4
To go from the penultimate to the last
line, recall the formula for a geomet-
ric series, or 1/(1 − x ) = 1 + x + x2 +
x3 + · · · ,where 0 < x < 1. Differenti-
d = 1φ + 2(1 − φ)φ + 3(1 − φ)2 φ + · · · + n(1 − φ)n−1 φ + · · · ate both sides with respect to x to get
1/(1 − x )2 = 1 + 2x + 3x2 + · · · . Now,
= φ 1 + 2 (1 − φ ) + 3 (1 − φ )2 + · · · + n (1 − φ ) n −1 + · · ·
set x = 1 − φ so that 1 − x = φ.
= 1/φ.
From 1889-1978 the average return on equity from the Standard and
Poor 500 index was 7 percent. In contrast, the average yield on short
term debt was less than 1 percent. Can such a differential be explained
within the neoclassical growth model? The puzzle is that to get a low
risk-free interest rate in a growing economy you need a high elasticity
of intertemporal substitution. When future income is higher than cur-
rent income individuals would like to borrow. This operates to drive
up the interest rate. To mitigate this, people must be very willing
to postpone consumption in response to interest rate rises; in other
words, a high elasticity of intertemporal substitution is required. To
get a large equity premium, people must dislike risk. This requires
a high coefficient of relative risk aversion. But in a standard macro
model, with isoelastic preferences for consumption, the coefficient of
relative risk aversion (discussed below) is the reciprocal of the elastic-
ity of intertemporal substitution (discussed in Chapter 6). In a classic
paper, Mehra and Prescott (1985) show that the standard macro model
can only generate an equity premium that is 0.4 percentage points
higher than on short-term debt.
178 numerical methods for macroeconomists with julia and matlab codes
c 1− α − 1
U (c, α) = , 0 < α < ∞.
1−α
where c is consumption. Now, 1/α is the elasticity of intertemporal
substitution–see Chapter 6 for discussion about the elasticity of in-
tertemporal substitution. With this utility function α also represents
the coefficient of relative risk aversion, as is discussed below. So, α
has a double duty. This is at the heart of the equity premium puzzle.
The person discounts the future at rate β. Let the person’s income, y,
evolve according to the two-state Markov chain
y0 = zy,
z1 = 1 + µ + δ and z2 = 1 + µ − δ.
π11 = π22 ≡ π,
1 1 1 U1 (c)
φ= = =− .
θ α c U11 (c)
Equity
Suppose that an equity is a claim on the flow of income, y. Let the
price of a share be denoted by p. This price will be a function of the
state of the economy So, let p = P(y, zi ) represent the current price of
equity and p0 = P(y0 , z j ) = P(z j y, z j ) denote next period’s price. If the
individual buys a share in the current period, his consumption will
be reduced by P(y, zi ). The marginal utility of current consumption
is y−α , so his utility will be reduced by y−α P(y, zi ). Next period the
person gets a dividend in the random amount y0 and will be able to
sell the share at the price P(z j y, z j ). Thus, utility next period will be
increased by the random amount βy0−α [y0 + P(y0 , z j )] = β(z j y)−α [z j y +
P(z j y, z j )]. This event occurs with chance πi1 . So, the person’s Euler
equation is
y−α P(y, zi ) = πi1 β(z1 y)−α [z1 y + P(z1 y, z1 )] + πi2 β(z2 y)−α [z2 y + P(z2 y, z2 )],
or
P(y, zi ) = πi1 β(z1 )−α [z1 y + P(z1 y, z1 )] + πi2 β(z2 )−α [z2 y + P(z2 y, z2 )].
w = βΛw + γ,
where
" # " # " #
w1 π11 z11−α π12 z12−α β(π11 z11−α + π12 z12−α )
w= ,Λ = ,γ = .
wn π21 z11−α π22 z12−α β(π21 z11−α + π22 z12−α )
Thus,
w = [ I − βΛ]−1 γ,
P(z j y, z j ) + z j y − P(y, zi ) z j ( w j + 1)
rij = = − 1.
P(y, zi ) wi
Re = ρ1∗ R1 + ρ2∗ R2 .
Bonds
Next consider the price of a one-period discount bond in state i, or
f
pi = P f (y, zi ). Such a bond will pay off one unit of consumption next
period with certainty. Even so, the marginal utility of next period’s
consumption is a random variable dependent on the individual’s in-
come. The Euler equation for the one-period discount bond reads
so that
πi1 β(z1 y)−α + πi2 β(z2 y)−α
P f (y, zi ) = = β(πi1 z1−α + πi2 z2−α ).
y−α
The expected return on this risk free asset, conditional on that the
current state is i, is
f
Ri = 1/P f (y, zi ) − 1,
8.12.3 Findings
For the U.S. economy the mean annual growth rate in consumption
was 0.018. Its standard deviation and autocorrelation were 0.036 and
-0.14. Matching these facts necessitated setting µ = 0.018, δ = 0.036,
and π = 0.43. Now, clearly the discount factor, β, should lie between 0
and 1. Mehra and Prescott (1985) suggest that the coefficent of relative
risk aversion, α, is bounded between 0 and 10. So, they computed
the risk-free rate and the equity premium for values of α and β that
lie within these ranges subject to the condition that a solution for the
model exists or that | I − βΛ| 6= 0. In other words, think about risk-
free rate and the equity premium as being defined by two functions
R f = R(α, β) and Re − R f = P(α, β). They compute the values of these
two functions for parameter values that lie in the set X where
where πkl are the odds of going from state k to state l. The summa-
tion across any row equals 1; i.e., ∑nl=1 πkl = 1 for all k.
(a)
T j −1 0 0 T j −1
( j−1)×( j−1) ( j−1)×1 ( j−1)×1 ( j−1)×( j−1)
Tj = p + (1 − p )
00 0 0 00
j× j
1×( j−1) 1×1 1×1 1×( j−1)
00 0 0 00
1×( j−1) 1×1 1×1 1×( j−1)
+ (1 − p ) + p ,
T j −1 0 0 T j −1
( j−1)×( j−1) ( j−1)×1 ( j−1)×1 ( j−1)×( j−1)
where " #
2 p 1− p
T = ,
1− p p
0 is an ( j − 1) × 1 column vector of zeros, and p = (1 + ρ)/2.
(b) At the end of each iteration, all but the first and last rows of T j
should be divided by 2.
numerical approximations 183
The idea is that if you are in the upper left cell on iteration j − 1 then
you will stay there with probability p on iteration j or move to the
upper right cell with the complementary probability 1 − p. Note that
it is possible to get into the rows 2, · · · , n − 1 of T j from either the
upper or lower cells, which explains the division by 2; i.e., without the
division by 2, ∑nl πkl = 2 for k = 2, · · · , n − 1. By setting p = (1 + ρ)/2,
the Markov chain will have the same long-run variance and first-order
autocorrelation as the AR1.
8.14 Interpolation
Sh ( x ) = αh + β h ( x − xh ) + ψh ( x − xh )2 + δh ( x − xh )3 , for xh ≤ x ≤ xh+1 .
0.6
0.4
0.2
0.0
-0.2
-0.4
-0.6
-0.8
x
(b) S1 ( x1 ) = F1 ( x1 ) and S1 ( xn ) = F1 ( xn ) (clamped), where F ( x ) is
some function that is being approximated.
Observe that there are n − 1 intervals. Hence, there are 4(n − 1) param-
j j j j
eters that a solution is need for–the αh ’s, β h ’s, ψh ’s and δh ’s. Properties
2 to 6 imply that there will be exactly 4(n − 1) linear restrictions. The
Hodrick Prescott filter, discussed later, is a close cousin of cubic spline
interpolation.
Cubic spline functions are very flexible and can be used to approx-
imate many things. An artist’s ink drawing of a face is shown in
the upper panel of Figure 8.14.3. The lower panel shows a computer-
generated facsimile of the artist’s sketch using cubic spline functions.
To do this, the picture on the left was broken up into 4 regions: the
left eyebrow, the left eyeball, the profile, and the right eyelid. The pixel
coordinates for the lines in each of the regions were read off by placing
the cursor from a mouse on the parts of the lines in each region. The
pixel coordinates were then translated into (x,y) coordinates. There
are many programs that can read off pixel coordinates from a graph,
such as Windows Paint. The more points that are read off, the more
accurate will be the computer-generated rendition. Finally, 4 cubic
splines are fit to (x,y) coordinates in each of the 4 regions. The cubic
spline interpolation did a great job replicating the artist’s sketch. This
demonstrates the utility of cubic splines.
186 numerical methods for macroeconomists with julia and matlab codes
y1 = R ( x1 ) = ∑in=1 ωi φ(| x1 − xi |)
.. ..
. .
yn = R( xn ) = ∑in=1 ωi φ(| xn − xi |).
Figure 8.14.4 plots the Gaussian radial basis function for several values
of ε. The value of the function φ declines as one moves away from the
center, r = | x − xi | = 0. Therefore, less value will be attached to
points in the domain, x, further away from the point, xi , that is being
interpolated around. The shape parameter, ε, controls the speed of
the decay. The bigger the value of ε, the less weight will assigned to
distant points.
0.6 ε=1
φ(r)
0.4
0.2
ε=2
ε=5
0.0
T T −1
min { ∑ (yt − τt )2 + λ ∑ [(τt+1 − τt ) − (τt − τt−1 )]2 }
{τ }tT=1 t=1 t =2
T T −1
= min { ∑ (yt − τt )2 + λ ∑ (τt+1 − 2τt + τt−1 )2 },
{τ }tT=1 t=1 t =2
2. When λ → ∞ the trend becomes linear since this will set the last
term to zero. To see this, suppose that τt = a + bt. Then, τt+1 − τt =
b and τt − τt−1 = b.
3. For 0 < λ < ∞ solve the above problem to get {τt }tT=1 . Observe that
{τt }tT=1 6= {yt }tT=1 , because roughness in the resulting curve is being
penalized. The HP trend is obtained by fitting a cubic interpolating
spline to the points {τt }tT=1 . One can think about this procedure as
fitting a cubic interpolating spline, S(t), to the data points {yt }tT=1
while dropping the n restrictions that S(t) = yt . These restrictions
numerical approximations 189
GDP
8.4
ing parameter is increased the
8.5 HP trend becomes less flexible.
8.3
The right panel is merely a blow
8.2 up of the left one.
8
8.1
7.5 8
1940 1960 1980 2000 2020 1960 1965 1970 1975 1980
Year Year
Often for quarterly data λ is set to 1,600. For annual data a value
of 6.25 has been suggested (although values of 100 and 400 are also
used). Figure 8.15.1 plots postwar quarter real GDP together with its
H-P trend. H-P detrended is illustrated in Figure 8.15.2.
The generic first-order condition connected to the above minimiza-
tion problem is
and
λτT −2 − 2λτT −1 + λτT = y T .
Using these matrices the solution for the Hodrick Prescott reads
Tτ = y or
τ = T −1 y.
23 % C a l c u l a t e t h i s formula n u m e r i c a l l y s i n g complex s t e p
differentiation
192 numerical methods for macroeconomists with julia and matlab codes
24
25 h = 0 . 0 0 0 0 0 0 0 1 ; % Step s i z e f o r D e r i v a t i v e
26 MRSnumerical= imag ( u t i l i t y ( x+1 i * h , y ) ) /imag ( u t i l i t y ( x , y+1 i * h ) ) ;
27 disp ( [ MRSanalytical , MRSnumerical ] )
28 i f norm ( MRSanalytical −MRSnumerical ) = . 0 0 0 0 0 0 1
29 disp ( ’ D e r i v a t i v e s a r e not matching ’ )
30 end
1 % S l u t s k y .m
2 % This program g e n e r a t e s S l u t s k y ’ s b u s i n e s s c y c l e
3 c l e a r a l l % C l e a r memory
4 c l c % Clear screen
5 rng ( 1 ) % Seed t h e random number g e n e r a t o r
6 xvec = r a n d i ( 1 0 , 2 0 0 , 1 ) ; % C a l l up 200 uniform random numbers , 1
<= x <= 10
7 ovec = z e r o s ( 1 9 0 , 1 ) ; % The B u s i n e s s Cycle Index
8 for t = 1:190
9 ovec ( t ) = sum ( xvec ( t : t +9) ) + 5 ; % S l u t s k y ’ s formula
10 end
11
12 % P l o t S l u t s k y ’ s B u s i n e s s Cycle
13 figure (1)
14 p l o t ( ovec )
15 t i t l e ( ’ S l u t s k y s B u s i n e s s Cycle ’ )
16 x l a b e l ( ’ Period ’ )
17 y l a b e l ( ’ B u s i n e s s Cycle Index ’ )
75
70
65
Business Cycle Index
60
55
50
45
40
35
30
0 20 40 60 80 100 120 140 160 180 200
Period
9 Stochastic Dynamics
“Our task as I see it ... is to write a Fortran program that will accept
specific economic policy rules as ‘input’ and will generate as ‘output’
statistics describing the operating characteristics of time series we care
about, which are predicted to result from these policies ... It must be
taken for granted that simply attempting various policies that may be
proposed on actual economies and watching the outcome must not be
taken as a serious solution method: Social Experiments on the grand
scale may be instructive and admirable, but they are best admired at a
distance.” (Robert E. Lucas Jr, “Methods and Problems in Business Cycle
Theory,” Journal of Money,Credit and Banking, 1980).
9.1 Introduction
k t +1 = K ( k t , z t ),
the business cycle could be the result of random causes; this was dis-
cussed in Chapter 8.
The function K is modelled here as the outcome of a dynamic stochas-
tic optimization problem in conjunction with equilibrium conditions
and the government budget constraint. This dynamic stochastic opti-
mization problem is formulated as a dynamic programming problem
à la Bellman (1957). Dynamic programming problems can be solved
numerically in various ways. Three methods are covered here: dis-
crete state space dynamic problem, linearization, and policy-function
iteration. There different algorithms for policy function iteration are
discussed: the Coleman (1991) algorithm, the endogenous grid method
by Carroll (2006), and parameterized expectations introduced by den
Hann and Marcet (1990).
The above economy will never settle down to a deterministic steady
state because zt is always fluctuating. The best one can hope for is
that in the long run fluctuations in (k t , zt ) will be described by some
stationary probability distribution, S(k t , zt ). The above two equations
imply a joint probability distribution for (k t+1 , zt+1 ) as a function of
(k t , zt ). Write this as
T(k t+1 , zt+1 |k t , zt ) ≡ Pr[e
k t +1 ≤ k t +1 , e
zt+1 ≤ zt+1 |e
kt = kt , e
z t = z t ],
(9.1.1)
where T is often referred to as the transition operator. The long-run
distribution must solve the equation
Z Z
S ( k t +1 , z t +1 ) = T(k t+1 , zt+1 |k t , zt )dS(k t , zt ).
Note that the right-hand side of the above equation is counting the
ways you can move into a situation in period t + 1 where e k t +1 ≤ k t +1
and e zt+1 ≤ zt+1 from any of the possible (k t , zt ) combinations in pe-
riod t. In stochastic models one is interested in the statistical properties
of (k t , zt ), as opposed to characterizing a deterministic time path. This
will be done two ways here. First, by using a Markov chain for T and,
second, by simulating T using Monte Carlo techniques.
y t = z t F ( k t ),
stochastic dynamics 197
(9.2.1)
Problem (9.2.1) is a dynamic programming problem. It is presented us-
ing recursive notation, where the time subscripts have been dropped.
In this problem effectively there is only today and tomorrow, where a
0 is attached to variable to denote its value tomorrow. Today’s utility
can be shown the that the value function, V, exists and is unique, is
increasing and strictly concave in k, and is continuously differentiable
in k–the analysis proceeds along the lines of Section 6.8 in Chapter 6.1 1
The only additional assumption
V (k0 , z0 )dG (z0 |z) is
R
Let the solution for k0 that arises out of this problem be represented needed is that
continuous in k0 if V (k0 , z0 ) is. This is
by known as the Feller property. See Stokey
k0 = K (k, z). (9.2.2) and Lucas (1986) for the formalities.
The lefthand side of the above expression is the marginal cost of invest-
ing an extra unit of capital today. The righthand side is the discounted
expected benefit. To understand this, note that V1 (k0 , z0 ) is the ex-
pected benefit next period from having an extra unit of capital should
the technology shock be z0 . But, the value of next period’s technology
shock is unknown currently so Crusoe must take the expected value
of this benefit. Observe that the current value of the technology, z, is
useful for forecasting the value of z0 through the function G (z0 |z) [or
equivalently through g(z0 |z)]. Last, since this expected benefit occurs
next period it must be discounted by β. This first-order condition rep-
resents one equation in the one unknown, k0 , where k and z are known
exogenous variables in the current period. Hence, the solution for k0
will have the form shown by (9.2.2).
If the technology shock is a discrete random variable taking n val-
ues, so that z ∈ Z = {z1 , z2 , · · · , zn }, then the dynamic programming
problem (9.2.1) appears as
n
V (k, zr ) = max{U (zr F (k) + (1 − δ)k − k0 ) + β
k0
∑ πrs V (k0 , zs )}, (9.2.4)
s =1
k0 )[zF1 (k ) + 1 − δ]
V1 (k, z) = U1 (zF (k ) + (1 − δ)k − e
e0 k0
0 dk de
Z
−U1 (zF (k) + (1 − δ)k − k )
e + β V1 (ek0 , z0 )dG (z0 |z) ,
| dk
{z dk}
=0
where the term on the second line is zero from (9.2.3). The fact that per-
turbations in the choice variable, k0 , cancel out in the objective function
when evaluated at the optimal solution is called the envelope theorem.
Using this in the first-order condition (9.2.3) then leads to the following
stochastic Euler equation:
Z
U1 (zF (k ) + (1 − δ)k − k0 ) = β U1 (z0 F (k0 ) + (1 − δ)k0 − k00 )[z0 F1 (k0 ) + 1 − δ]dG (z0 |z)
= βE U1 (z0 F (k0 ) + (1 − δ)k0 − k00 ) × [z0 F1 (k0 ) + 1 − δ]|k, z .
(9.2.5)
Robinson Crusoe’s problem can also be cast in sequence space. Let the
technology shock, z, follow an N-state Markov chain, with the tran-
sition probability between states i and j being denoted by π ij , where
the states are now represented by superscripts. Suppose that the tech-
nology shock starts off in period 1 from the known state i, so that
z1 = z1i , where a superscript represents a state and a subscript the
time period. Let zt = (zt , zt−1 , · · · , z1 ) signify some realized sequence
of technology shocks between periods 1 and t. There are N t−1 possible
sequences, with the set of these sequences notated by Zt . Denote the
odds of the sequence zt occurring by ρ(zt ). The probabilities ρ(zt ) are
200 numerical methods for macroeconomists with julia and matlab codes
given recursively by
ρ(z1i ) = 1,
ρ[z2 = (z2 , z1i )] = Pr(z2 | z1 )ρ(z1i ),
ρ[z3 = (z3 , z2 )] = Pr(z3 | z2 )ρ(z2 ),
..
.
ρ[zt = (zt , zt−1 )] = Pr(zt | zt−1 )ρ(zt−1 ).
Think about Robinson Crusoe choosing a capital stock for every state-
time combination that can possibly occur. That is, for each period
t Robinson Crusoe chooses the capital stock for the next period, k t+1 ,
contingent upon the sequence of technology shocks that have occurred
up to that period, or zt . This choice variable is designated by k t+1 (zt ).
Note the capital stock chosen for period t + 1 will depend on the initial
capital stock, k1 , and the sequence of shocks that transpires between
period 1 and period t, or (zt , zt−1 , · · · , z1 ).
Robinson Crusoe’s maximization problem now appears as
∞
( )
max
{k t+1 (zt )}∞
∑ ∑ β t −1
ρ(zt )U ( F (k t+1 (zt−1 )) + (1 − δ)k t+1 (zt−1 ) − k t+1 (zt )) .
t =1 t=1 zt ∈Zt
Note that for each value of zt , the choice variable k t+1 (zt ) appears in
just two periods in the maximand, namely time t and t + 1. Specifically,
it shows up in the terms
j =1
j j
By writing ρ (zt+1 , zt ) = Pr(zt+1 | zt )ρ(zt ) the above can be ex-
pressed as
N
∑ Pr(zt+1 | zt )U (zt+1 F(kt+1 (zt )) + (1 − δ)kt+1 (zt ) − kt+2 (zt+1 ))
j
+ βt +··· .
j =1
Now, suppose that the current value of the technology shock in period
j
t is zit so that zt =(zit , zt−1 ). Then, Pr(zt+1 | zit ) = π ij and the generic
first-order condition reads
U1 zit F (k t (zt−1 )) + (1 − δ)k t+1 (zt−1 ) − k t+1 (zt )
N
= β ∑ π ij U1 zt+1 F (k t+1 (zt )) + (1 − δ)k t+1 (zt ) − k t+2 (zt+1 )
j
j =1
j
× [zt+1 F1 (k t+1 (zt )) + (1 − δ)],
stochastic dynamics 201
This is the same stochastic Euler equation that obtained from the dy-
namic programming formulation.
This method has two key steps. In the first step, a dynamic pro-
gramming problem is solved assuming that values for the capital stock
and the technology shock both lie in discrete sets. In the second step
the solution to this dynamics programming problem is represented as
Markov chain. Using this Markov chain representation, statistics for
any variable of interest can then be readily computed.
The capital stock in each period is constrained to be an element of
the finite time-invariant set, K. Thus,
k ∈ K = { k 1 , · · · , k n }.
πrs = Pr[z0 = zs |z = zr ].
2
V (k i , zr ) = max {U (zr F (k i ) + (1 − δ)k i − k0 ) + β
c>0,k0 ∈K
∑ πrs V (k0 , zs )}.
s =1
P(1)
Observe that V : K × Z → R is merely a list of 2n values, one for each
(k i , zr ) ∈ K × Z . The choice for k0 is restricted to lie in the discrete set
K. Discrete maximization was introduced in Chapter 3.
So, how can a solution V be obtained? Here’s an algorithm.
k 0 = K ( k i , zr ) ∈ K .
of the world, (k i , zr ):
o = O ( k i , zr ) = zr F ( k i ),
i = I ( k i , zr ) = K ( k i , zr ) − (1 − δ ) k i ,
and
c = C ( k i , z r ) = z r F ( k i ) + (1 − δ ) k i − K ( k i , z r ).
It is easy to add labor, h, into stochastic growth model along the
lines presented in Chapter 6. Now the production function would
read y = zF (k, h). The decision rule for labor would have the form
h = H (k i , zr ). Therefore, output in the current period can be expressed
as o = O(k i , zr ) = zr F (k i , H (k i , zr )).
Trivially, then
n
∑ Pr[k0 = k j | k = ki , z = zr ] = 1 for all (k, z) ∈ K × Z .
j =1
T = [ pir , js ].
| {z }
2n×2n
ρ1 = ρ0 × T .
1×2n 1×2n 2n×2n
ρ∗ = ρ∗ T, (9.4.2)
Computation of Moments
Once the long-run distribution, ρ∗ , has been obtained, it is easy to
compute any moment of interest.
2 n
∗
E[ln o ] = ∑ ∑ ρir ln O(k i , zr ), (9.4.3)
r = 1i = 1
2 n
∗
E[ln c ln o ] = ∑ ∑ ρir ln C (k i , zr ) ln O(k i ,zr ), (9.4.4)
r = 1i = 1
2 n 2 n
E[ln o 0 ln o ] = ∑ ∑ ∑ ∑ pir,js ρir
∗
ln O(k0j , z0s ) ln O(k i , zr ). (9.4.5)
s = 1 j = 1r = 1 i = 1
E[ln o 0 ln o ] − E[ln o ]2
ρln o0 , ln o = . (9.4.8)
(σln o )2
2. Use the decision rule for capital and the Markov process for the
technology shock to specify the Markov transition matrix (9.4.1).
3. Compute the stationary distribution for the capital stock and tech-
nology shock defined by equation (9.4.2) using one of the methods
discussed in Chapter 8.
One could alternatively use the Monte Carlo based algorithm dis-
cussed below. This is less desirable in general. But in some circum-
stances one might want to filter the business cycle data coming from
the model or perhaps the Markov transition matrix (9.4.1) is too big to
handle.
zt = zr , if ε < πrr ,
zt = zs , if ε ≥ πrr .
9.5 Linearization
ln k0 = a + b ln k + f ln z. (9.5.2)
One would expect that a > 0, 0 < b < 1, and f > 0. To solve for a, b,
and f , the above stochastic Euler equation (9.2.5) will linearized in ln k
and ln z. The resulting log-linear solution must be consistent with the
assumed one (9.5.2). This consistency requirement provides a solution
for the constants a, b, and f .
If one knew the constants a, b, and f , then one could use (9.5.1) and
(9.5.2) to compute sample paths for k and z denoted by {k t+1 }tT=1 and
{zt+1 }tT=1 , given a starting condition, k1 and z1 , and a sample path
208 numerical methods for macroeconomists with julia and matlab codes
for the error terms {ε t+1 }tT=1 . The sample path for the error terms,
{ε t+1 }tT=1 , can be drawn from a random number generator. Further-
more, if one has a sample path for k and z then it is easy to construct
ones for other variables, such as output, o, or consumption, c.
Therefore, the hardest part of the problem is computing a, b, and
f . These coefficients will be uncovered by linearizing the stochastic
Euler equation (9.2.5). Toward this end, represent the values for k and
z that would occur in a deterministic steady state by k∗ and z∗ . In the
absence of uncertainty, the above decision rule should converge to this
steady state implying
ln k∗ = a + b ln k∗ + f ln z∗ .
ln k0 − ln k∗ = b(ln k − ln k∗ ) + f (ln z − ln z∗ ),
k ≡ ln k − ln k∗ and b
b z ≡ ln z − ln z∗ ,
k0 = bb
b k + fb
z. (9.5.3)
Similarly,
z0 = ρb
b z + ε0 .
k and b
Last, the unconditional (long-run) expectations of b z are given
by
E[b
k ] = E [b
z] = 0,
z t + ε t + j + ρ 1 ε t + j −1 + · · · + ρ j −1 ε t +1 ,
zt+ j = ρ j b
b
so that
E [b zt ] = ρ j b
zt+ j |b zt .
Clearly,
zt+ j |b
lim E[b zt ] = 0,
j→∞
k t+ j = b jb
b k t + f (b z t + j −2 + · · · + b j −1 b
zt+ j−1 + bb zt )
= b jbk t + f (ρ j−1 b
zt + bρ j−2 b
z t + · · · + b j −1 b
zt ) + ε terms.
stochastic dynamics 209
Thus,
j
k t+ j |b
E[b zt ] = b jb
kt , b kt + f ∑ bi−1 ρ j−i bzt
i =1
Therefore,
k t+ j |b
lim E[b zt ] = 0,
kt , b
j→∞
z0 = zρ exp(ε0 ) (9.5.4)
Λ(k, k0 , k00 , z, ε0 )
= U1 (zF (k) + (1 − δ)k − k0 )
− βU1 (zρ exp(ε0 ) F (k0 ) + (1 − δ)k0 − k00 )[zρ exp(ε0 ) F1 (k0 ) + 1 − δ].
E[Λ(k, k0 , k00 , z, ε0 )] = 0.
Note that
x = eln x = exp(ln x ), (9.5.5)
which implies
dx
= eln x = x. (9.5.6)
d ln x
Hence, one can write
e (ln k, ln k0 , ln k00 , ln z, ε0 )]
E[Λ
= E[Λ(exp(ln k), exp(ln k0 ), exp(ln k00 ), exp(ln z), ε0 )]
| {z } | {z } | {z } | {z }
k k0 k00 z
= 0.
Λ e 2 = Λ2 k 0 , Λ
e 1 = Λ1 k, Λ e 3 = Λ3 k00 , Λ
e 4 = Λ4 z, and Λ
e 5 = Λ5 . (9.5.7)
ln k00 = a + b ln k0 + f ln z0
= a + b ln k0 + f (ρ ln z + ε0 ).
210 numerical methods for macroeconomists with julia and matlab codes
e (ln k, ln k0 , a + b ln k0 + f ρ ln z + f ε0 , ln z, ε0 )
Λ
| {z }
ln k00
where (9.5.7) has been used. The derivatives Λ1 (∗), Λ2 (∗), Λ3 (∗),
Λ4 (∗), and Λ5 (∗) are all just constant terms. In a deterministic steady-
state Λ(∗) = 0, because
Λ1 (∗)k∗b
k + [Λ2 (∗) + bΛ3 (∗)]k∗b
k0 + [Λ3 (∗)k∗ f ρ + Λ4 (∗)]b
z = 0.
In the above equation use has also been made of the fact that z∗ =
1. Note that expectation operator E has disappeared from the above
equation, because all terms are fully known. It is as if there is no
uncertainty in the economy. Thus, linearization imposes a certainty
equivalence property. Rewrite the above equation as
Λ1 (∗)
b= , (9.5.8)
−[Λ2 (∗) + bΛ3 (∗)]
stochastic dynamics 211
and
Λ3 (∗) f ρ + Λ4 (∗)/k∗
f = . (9.5.9)
−[Λ2 (∗) + bΛ3 (∗)]
Solving for b involves computing the solution to the quadratic equa-
tion
Λ3 (∗)b2 + Λ2 (∗)b + Λ1 (∗) = 0. (9.5.10)
This equation has two roots. As will be shown below, both will be
positive in value. One will be bigger than one, the other smaller. Pick
the smaller one. Note that by solving (9.5.9) for f it transpires that
Λ4 (∗)/k∗
f = . (9.5.11)
−[Λ2 (∗) + bΛ3 (∗) + Λ3 (∗)ρ]
Note these expressions involve the derivatives of Λ and not Λ.
e Last, a
will be given by
a = (1 − b) ln k∗ − f ln z∗ = (1 − b) ln k∗ . (9.5.12)
Λ2 (∗) = −U11 (∗) − βU11 (∗)[z∗ρ exp(ε∗0 ) F1 (∗) + 1 − δ][z∗ρ exp(ε∗0 ) F1 (∗) + 1 − δ]
− βU1 (∗)z∗ρ exp(ε∗0 ) F11 (∗)
= −U11 (∗) − U11 (∗)[ F1 (∗) + 1 − δ] − βU1 (∗) F11 (∗) > 0,
212 numerical methods for macroeconomists with julia and matlab codes
Λ4 (∗) = U11 (∗) F (∗) − βU11 (∗)[z∗ρ exp(ε∗0 ) F1 (∗) + 1 − δ] F (∗)ρz∗ρ−1 exp(ε∗0 )
− βU1 (∗) F1 (∗)ρz∗ρ−1 exp(ε∗0 )
= U11 (∗) F (∗)(1 − ρ) − βU1 (∗) F1 (∗)ρ < 0,
and
Λ5 (∗) = − βU11 (∗)[z∗ρ exp(ε∗0 ) F1 (∗) + 1 − δ]z∗ρ exp(ε∗0 ) F (∗) − βU1 (∗)z∗ρ F1 (∗) exp(ε∗0 )
= −U11 (∗) F (∗) − βU1 (∗) F1 (∗).
and Λ4 (∗) < 0. Last, note from (9.5.12) that a > 0 when ln k∗ > 0 and
a < 0 when ln k∗ < 1. Thus, the model’s local transitional dynamics
around its deterministic steady state have been characterized.
Unstable Root
Stable Root
Now, it has just been shown that 0 < b < 1 so that there exists a steady
state where 0 < dk t+1 /dk t < 1. The situation shown in Figure 6.7.2
therefore is apropos.
4. Pick some initial period-0 starting value for the capital stock and
214 numerical methods for macroeconomists with julia and matlab codes
ln k t+1 = a + b ln k t + f ln zt .
The technology shock for next period is computed using the rela-
tionship
ln zt+1 = ρ ln zt + ε t+1 .
The idea for policy-function iteration is to make a guess for the func-
tion K. Denote the guess to be used at stage j + 1 by k0 = K j (k, z).
One then solves the equation shown below to obtain a revised guess,
k0 = K j+1 (k, z).
To this end, let the shock process follow a m-state Markov chain
where z ∈ Z = {z1 , z2 , ..., zm }. Denote the odds of transition from
state i to state l by πil ≡ Pr[z0 = zl |z0 = zi ].
U1 ( F (k h , zi ) + (1 − δ)k h − k0 )
m
= β ∑ U1 ( F (k0 , zl ) + (1 − δ)k0 − K j (k0 , zl ))[ F1 (k0 , zl ) + 1 − δ]πil ,
l =1
s −1 s
zt = zs , if ε ∈ [ ∑ πr,u , ∑ πr,u ], for s = 1, · · · , n,
u =0 u =0
K0(k,zi)
0
k1 k2 k3 k4 k
The Coleman method defines an exogenous grid for the current stock
of capital k, which is then used to compute an interpolated decision
rule k0 = K (k, z) by calling a nonlinear equation solver to solve the
Euler equation. The endogenous grid method proposed by Carroll
(2006) runs the process in reverse. A fixed grid, K 0 , for next period’s
capital stock k0 is imposed. The values for k0 ∈ K are used to construct
an interpolated consumption function, C ( x, z), where x is the income
at the agent’s disposal. The levels of disposable income, x, that justify
the choice of k0 at each of these grid points are computed using the
Euler equation. This is the sense that Coleman’s process is run in
reverse. At each stage in an iteration of the algorithm, disposable
income and technology shocks are used to construct an interpolated
consumption function, c = C ( x, z). In sum, the current period shock,
next period’s capital stock, and an interpolated consumption function
are taken as given, and then a level of disposable income in the current
period is solved for that ensures the Euler equation holds. Depending
on the context, this may avoid the costly use of a nonlinear equation
solver by making disposable income x vary endogenously or at least
make the solution quicker to compute. One assumption is needed to
make the algorithm practicable: the marginal utility of consumption
must be invertible.
As above, define a grid for next period’s capital stock K 0 ≡ {k01 , k02 , ..., k0n }
spanning the interval [0, K ]. Let the shock process follow a first-order
m-state Markov chain, where z ∈ Z = {z1 , z2 , ..., zm } and πil ≡ Pr[z0 =
zl |z0 = zi ] corresponds to the probability of transitioning from state i
to state l. The following algorithm uses the interpolation schemes dis-
cussed above, but in some circumstances avoids the use of a nonlinear
equation solver in each iteration.
2. For each pair of next period’s capital stock and current technology
shock, (k h , zi ) ∈ K 0 ×Z , a value for current disposable income xk,zi
given current capital stock k and technology shock zi is recovered
stochastic dynamics 219
m
U1 xk,zi − k h = β ∑ U1 C j ( xk h zl , zl ) [ F1 (k h , zl ) + 1 − δ] πil .
l =1
Note, that for a given values of k h and zl , the level of next pe-
riod’s disposable income, xk h ,zl , is known from the formula xk h ,zl ≡
F (k h , zl ) + (1 − δ)k h . The advantage of defining the Euler equation
in this fashion is that often it can be solved analytically as long as
the marginal utility of consumption is invertible. Specifically,
!
m
xk,zi = k h + U1−1 β ∑ U1 C ( xk h zl , zl ) [ F1 (k h , zl ) + 1 − δ] πil
j
,
l =1
m
U1 (c(k h , zi )) = β ∑ U1 (c(k0 , zl ))[ F1 (k0 , zl ) + 1 − δ]πil (9.8.1)
l =1
as a function of k0 according to
m
U1 (c(k0 , zl ))
k0 = β ∑ [ F1 (k0 , zl ) + 1 − δ]k0 πil ,
l =1
U1 ( c ( k h , z i ))
| {z }
'P (k,z;φ)
where the righthand side of the equation is the conditional expectation
to be approximated as a function of k and z. The algorithm below uses
a stochastic simulation method to update the polynomial, P (k, z; φ),
on the righthand side. The two-stage procedure draws on Judd et al.
(2011).
k0 (k t , zt ) = P ( k t , z t ; φ j ),
c(k t , zt ) = F ( k t , z t ) − k 0 ( k t , z t ) + (1 − δ ) k t .
stochastic dynamics 221
k00 (k0 , zl ) = P P ( k t , z t ; φ j ), z l ; φ j ,
m
U1 (c(k0 , zl ))
ε0 = β ∑ [ F (k0 , zl ) + 1 − δ]k0 πtl − P (k t , zt ; φ).
U (c(k t , zt )) 1
l =1 1
m
U1 (c(e k0 , e
zl ))
zτ ) = β ∑
E (ek τ , e [ F1 (ek0 , e
zl ) + 1 − δ]πτl − 1.
U
l =1 1 ( c (ek ,
τ τz
e ))
If the mean of the errors are sufficiently small, the candidate φ? is
accepted.
222 numerical methods for macroeconomists with julia and matlab codes
αt = ραt−1 + ε t ,
with
ε t ∼ N ((1 − ρ)α, σ ).
Demand is decreasing in price, pt . The monopolist produces according
to the quadratic cost function
γ
ct = (ot − κot−1 )2 ,
2
where ct is period-t total cost and ot−1 is the monopolist’s level of out-
put in period t − 1. In this random world the above cost function im-
plies that the monopolist would like to smooth out fluctuations in his
output. Under the above formulation, the long-run or unconditional
expected level of the demand shifter is
E[α] = α.
1 − ρ t −1
E [ α t | α 1 ] = ρ t −1 α 1 + E[ε] = ρt−1 α1 + (1 − ρt )α.
1−ρ
Clearly, as t → ∞, this converges to α.
where o−1 is last period’s output and α0 is next period’s state of de-
mand. The first-order condition associated with this maximization
problem is
α − βo = γ(o − κo−1 ) − δE[V1 (o, α0 )],
| {z } | {z }
MR MC
which sets marginal revenue, MR, equal to expected marginal cost,
MC. By differentiating the both sides of the above dynamic program-
ming problem, while applying the envelope theorem, it is easy to de-
duce that
V1 (o−1 , α) = γκ (o − κo−1 ) > 0.
An increase in the past level of output, o−1 , is beneficial to the monop-
olist because it reduces his current costs. By updating the equation,
one obtains
V1 (o, α0 ) = γκ (o 0 − κo ).
Using this in the first-order condition for the above dynamic program-
ming problem gives
so that
As t become large E[αt |α1 ] = α and the above result obtains. Now, if
o−1 = o = E[o ] and α = α, then E[o 0 ] = E[o ].
Next, E[o ] will be calculated using the first-order condition. This
allows the constant η be determined. Toward this end, using the above
results in the first-order condition for the above dynamic programming
problem (9.9.1) gives
which implies
1 1
E[o ] = α= α.
β + γ(1 − κ ) − δγκ (1 − κ ) β + γ(1 − κ )(1 − δκ )
Therefore,
−δγκψ2 + (γ + β + δγκ 2 )ψ − γκ = 0.
1
λ= .
γ + β − δγκψ + δγκ 2 − δγκρ
Next, the decision rule is simulated using a simple for loop. The
last step is to present some output. Some means and standard devia-
tions are computed using the built-in MATLAB functions mean and
std. The probability distribution for output is plotted using the his-
togram command.
1 % main .m
2 % S t o c h a s t i c Monopoly ProblemMain Program
3 c l e a r a l l % C l e a r a l l numbers from p re vi o us runs
4 c l c % Clear screen
5
6 % S e t parameters f o r model
7
8 % Demand curve
9 malpha = 1 ; % mean o f i n t e r c e p t
10 beta = 0 . 5 ; % slope
11 rho = 0 . 5 ; % a u t o c o r r e l a t i o n
12 sdshock = . 1 0 ; % standard d e v i a t i o n
13
14 % Cost f u n c t i o n
15 gamma = 0 . 5 ; % q u a d r a t i c term
16 kappa = 0 . 9 ; % c o s t r e d u c t i o n term
17
18 % Discount f a c t o r
19 delta = 0.96;
20
21 % Time horizon f o r s i m u l a t i o n
22 T = 1 0 0 0 0 0 ; % Number o f p e r i o d s
23
24 % Compute steady − s t a t e l e v e l o f output
25 o s t a r = malpha /( b e t a + gamma * (1 − kappa ) * (1 − d e l t a * kappa ) ) ;
26
27 % Solve model t a k i n g t h e Decision −Rule Approach
28 % S e t up t h e Quadratic Formulae f o r P s i
29 a = − d e l t a * gamma * kappa ; % C o e f f i c i e n t on squared term
30 b = gamma + b e t a + d e l t a * gamma * kappa2 ; % L i n e a r term
31 c = −gamma * kappa ; % Constant term
32 p = [ a b c ] ; % C o e f f i c i e n t s on q u a d r a t i c
33
50 % I t e r a t e on d i f f e r e n c e e q u a t i o n f o r output
51 ovec = z e r o s ( T , 1 ) ; % S e t up v e c t o r t o s t o r e outputs
52 avec = z e r o s ( T , 1 ) ; % S e t up v e c t o r t o s t o r e a l p h a s
53 time = ( 0 : T−1) ’ ; % S e t up v e c t o r f o r time
226 numerical methods for macroeconomists with julia and matlab codes
54 a l p h a p a s t = malpha ;
55 for t = 2:T
56 alpha = rho * a l p h a p a s t + shocks ( t , 1 ) ;
57 avec ( t , 1 ) = alpha ;
58 ovec ( t , 1 ) = e t a + lam * alpha + p s i * ovec ( t − 1 , 1 ) ;
59 a l p h a p a s t = alpha ; % Update alpha
60 end
61 pvec = avec − b e t a * ovec / 2 ; % Compute p r i c e s over time
62
63 % Plot results
64 figure (1)
65 subplot ( 2 , 2 , 1 )
66 p l o t ( time ( 1 : 3 0 ) , ovec ( 1 : 3 0 ) )
67 t i t l e ( ’ S t a r t o f Time ’ )
68 x l a b e l ( ’ Time ’ )
69 y l a b e l ( ’ Output ’ )
70 subplot ( 2 , 2 , 2 )
71 p l o t ( time ( 1 : 3 0 ) , pvec ( 1 : 3 0 ) )
72 t i t l e ( ’ S t a r t o f Time ’ )
73 x l a b e l ( ’ Time ’ )
74 ylabel ( ’ Price ’ )
75 subplot ( 2 , 2 , 3 )
76 p l o t ( time ( 1 5 0 : 3 0 0 ) , ovec ( 1 5 0 : 3 0 0 ) )
77 t i t l e ( ’ Stationary Dist ’ )
78 x l a b e l ( ’ Time ’ )
79 y l a b e l ( ’ Quantity ’ )
80 a x i s ( [ 1 4 0 , 3 0 0 , min ( ovec ( 1 5 0 : 3 0 0 ) ) , max ( ovec ( 1 5 0 : 3 0 0 ) ) ] )
81 subplot ( 2 , 2 , 4 )
82 p l o t ( time ( 1 5 0 : 3 0 0 ) , pvec ( 1 5 0 : 3 0 0 ) )
83 t i t l e ( ’ Stationary Dist ’ )
84 x l a b e l ( ’ Time ’ )
85 ylabel ( ’ Price ’ )
86 a x i s ( [ 1 4 0 , 3 0 0 , min ( pvec ( 1 5 0 : 3 0 0 ) ) , max ( pvec ( 1 5 0 : 3 0 0 ) ) ] )
87 figure (2)
88 histogram ( ovec ( 1 5 0 : T ) , ’ Normalization ’ , ’ p r o b a b i l i t y ’ )
89 % Old MATLAB syntax , histogram with 20 b i n s
90 % h i s t ( ovec ( 1 5 0 : T ) , 20 )
91 t i t l e ( ’ Histogram o f Outputs ’ )
92 x l a b e l ( ’ Output ’ )
93 y l a b e l ( ’ Frequency ’ )
94
95 % Compute some d e s c r i p t i v e s t a t i s t i c s
96 disp ( ’ D e s c r i p t i v e S t a t i s t i c s ’ )
97 disp ( ’Mean Level o f Output ’ )
98 disp ( mean ( ovec ( 1 5 0 : T , 1 ) ) )
99 disp ( ’Mean Level o f Shock , Alpha ’ )
100 disp ( mean ( avec ( 1 5 0 : T , 1 ) ) )
101 disp ( ’ Standard D e v i a t i o n Ln Output ’ )
102 disp ( s t d ( l o g ( ovec ( 1 5 0 : T , 1 ) ) ) )
103 disp ( ’ Standard D e v i a t i o n Ln P r i c e ’ )
104 disp ( s t d ( l o g ( pvec ( 1 5 0 : T , 1 ) ) ) )
105 disp ( ’ C o r r e l a t i o n between Ln Output and Ln P r i c e s ’ )
106 x = c o r r c o e f ( l o g ( ovec ( 1 5 0 : T , 1 ) ) , l o g ( pvec ( 1 5 0 : T , 1 ) ) ) ;
107 disp ( x ( 2 , 1 ) )
108 disp ( ’ A u t o c o r r e l a t i o n f o r Ln Output ’ )
109 x = c o r r c o e f ( l o g ( ovec ( 1 5 1 : T , 1 ) ) , l o g ( ovec ( 1 5 0 : T − 1 , 1 ) ) ) ;
110 disp ( x ( 2 , 1 ) )
stochastic dynamics 227
5 % Descriptive S t a t i s t i c s
6 % Mean Level o f Output
7 1.9734
8 % Mean Level o f Shock , Alpha
9 1.0002
10 % Standard D e v i a t i o n Ln Output
11 0.0471
12 % Standard D e v i a t i o n Ln P r i c e
13 0.1942
14 % C o r r e l a t i o n between Ln Output and Ln P r i c e s
15 0.9118
16 % A u t o c o r r e l a t i o n f o r Ln Output
17 0.7285
10.1 Introduction
ct + at+1 = wlt + (1 + r ) at .
at+1 ≥ −φ.
y t = F ( k t , 1) ≡ Y ( k t ),
k t +1 = (1 − δ ) k t + i t ,
at+1 = at+1 + φ.
b (10.3.1)
ct + b
a t +1 = z t ,
and
at+1 ≥ 0.
b
An individual’s dynamic programming problem can be cast as
Z
V (zt , φ, w, r ) = max {U (zt − b
a t +1 ) + β V (zt+1 , φ, w, r )dL(lt+1 )},
a t +1 ≥0
b
subject to (10.3.1) and (10.3.2). The Euler equation connected with the
problem can have both an interior and a corner solution:
Z
U1 (zt − b
a t +1 ) = β (1 + r ) U1 (zt+1 − b
at+2 )dZ (zt+1 |zt ), if b
at+1 > 0,
and
Z
U1 (zt − b
a t +1 ) ≥ β (1 + r ) U1 (zt+1 − b
at+2 )dZ (zt+1 |zt ), if b
at+1 = 0.
Figure 10.3.1 plots the typical shapes for these functions, assuming
that the interest rate r lies below the rate of time preference, λ ≡
232 numerical methods for macroeconomists with julia and matlab codes
Proposition 59. Assume that β(1 + r ) < 1. Suppose that either U1 (0) < ∞
or zmin ≡ wlmin − rφ > 0. Then there is a b
z > zmin such that for all zt < b
z,
ct = zt and b
at+1 = 0.
Proof. First, note assume that U1 (zmin ) is finite. This implies that
V1 (zmin ) = U1 (zmin − b at+1 ) is finite also, a fact that will be estab-
lished later. The proof now proceeds by contradiction. Suppose to the
contrary that the borrowing constraint is not binding. Then V1 (zt ) =
U1 (zt − bat+1 ) = β(1 + r ) E[V1 (zt+1 )] < V1 (zmin ). As zt → zmin this re-
| {z }
<1
sults in a contradiction. So, there must be some neighborhood around
zm for which the proposition is true.
Second, it needs to be proved that V1 (zmin ) is finite. First, if U1 (0)
is finite then so will be V1 (zmin ) = U1 (zmin − bat+1 ) ≤ U1 (0). Sec-
ond, suppose alternatively that zmin ≡ wlmin − rφ > 0. Then, ex-
pected lifetime utility is bounded below by U (wlmin − rφ)/(1 − β).
Now, V1 (zmin ) = ∞ if and only if b at+1 = zmin . For this to be true,
it must transpire that U1 (0) ≤ β(1 + r ) E[V1 (wlt+1 + (1 + r )zmin −
the aiyagari model 233
Let E[ aw ] denote the long-run level of assets for the economy. Using
(10.3.1) and (10.3.3) this is given by
E[ aw ] = E[ A(z, φ, w, r )] − φ.
Some features of this function are shown in the lefthand panel of Fig-
ure 10.4.1. Here as the rate of interest approaches the rate of time
preference, λ = 1/β − 1, the economy’s holdings of assets grow with-
out bound. Suppose that r = λ. Then, in a world without uncertainty
it would be costless for a person to hang on to assets. In the world with
uncertainty there is a positive probability that the individual could get
a string of bad shocks. To insure against this, the person holds an
infinitely large amount of assets.
In equilibrium the marginal production of capital will be equal to
its user cost. Thus,
Y1 (k) = r + δ.
The capital-to-labor ratio is given by
K (r ) = Y1−1 (r + δ).
Since the aggregate supply of labor is one this is also the per-capita
demand for capital. Given the constant-returns-to-scale assumption,
the wage rate, w, can be expressed as
w = Y (K (r )) − rK (r ) ≡ W (r ).
Proof. Let C (z) denote an agent’s decision rule for c. In the presence
of borrowing constraints
Z
U1 (C (z)) > β(1 + r ) U1 (C (z0 ))dZ (z0 |z). (10.4.1)
234 numerical methods for macroeconomists with julia and matlab codes
This above equation will hold with equality for an individual who
isn’t borrowing constrained. The stationary distribution for z, Z (z0 ) is
defined by Z
Z (z0 ) = Z ( z 0 | z ) d Z ( z ).
1. Enter each iteration j with a guess for the interest rate, say r j <
1/β − 1.
10.5 Calibration
It’s time to pick functional forms for tastes, technology, and the stochas-
tic process for labor supply. The period length is taken to be one year,
so the discount factor β is set at 0.96. The model is simulated for
various configurations of parameters values for these functions. Let
momentary utility be represented by
c 1− µ − 1
U (c) = , where µ ∈ {1, 3, 5},
1−µ
In the analysis the model is computed for different values for the co-
efficient of relative risk aversion, µ. Suppose that labor income has the
following first-order autoregressive representation.
q
ln(lt ) = ρ ln(lt−1 ) + σ 1 − ρ2 ε t , with ε ∼ N (0, 1),
and
σ ∈ {0.2, 0.4} and
|{z} ρ ∈ {0, 0.3, 0.6, 0.9}.
|{z}
coef var. auto corr.
10.6 Results
Therefore,
1 dE[U (c)] 1 U11 (c̄) 1
= c̄ = µ.
cU 0 (c̄) dσc2 2 U1 (c̄) 2
| {z }
coef. rel. aver.
fixed at their per-capita amount to the current setting where assets may
be optimally accumulated and depleted. This lead to an increase in
welfare, measured in terms of consumption, of 3 × (0.352 − 0.172 )/2 '
0.14, which is about 8% of GNP.
1. Data:
(b) Gini coefficient for income and wealth are 0.40 and 0.80.
2. Model:
238 numerical methods for macroeconomists with julia and matlab codes
(a) Median income is over 90 percent of mean income for all pa-
rameterizations.
(b) When σ = 0.2, ρ = 0.6, and µ = 5, the Gini coefficients for
income and wealth are 0.12 and 0.32.
Line of Equality
2009 Lorenz Curve
2019 Lorenz Curve
Start the economy off from the steady-state wealth distribution in the
Aiyagari model. This is done using discrete-state-space dynamics pro-
gramming. Here it is important to create a grid of asset holdings
with more points at the lower end since the distribution will feature
a large mass of individuals close to the borrowing constraint.1 The 1
This can be achieved by defining a
steady-state wealth distribution is computed as the invariant distri- point in the asset grid as follows: a j =
j −1 α
a + ( a − a) n−1 , where a is the min-
bution of the Markov chain connected with the dynamic program-
imum level of asset holdings, a is the
ming problem–see Chapters 8 and 9 for a discussion of Markov chains. maximum level of asset holdings, n is
Then, do a one-time unforeseen shock to the innovation at the start of the total number of grid points, and α
is a curvature parameter (usuallly set to
time. In particular, let ε 1 = σ, where σ is one standard deviation.
7).
Hence, the sequence of innovations is just {ε 1 = σ, 0, 0, 0, · · · }. Thus, z
will jump up or down upon impact and then return to its steady-state
level (i.e. 1). In particular, ln zt will follow the time path ln zt = ρt−1 ε 1 .
Assume that the economy will converge back to its initial steady-state
level of capital by time T.
1. Enter an iteration with a guess for the time path of the aggregate
j
capital stock {k t }tT=1 , denoted by {k t }tT=1 . Note that a guess for the
time path for the aggregate capital stock will imply a guess for the
time paths for the interest and wage rates.
2. Given this guess path for aggregate capital stock, compute the rep-
resentative agent’s value functions and decision rules starting at pe-
riod T − 1 and then work backwards to period 1.
3. Now, use the obtained decision rule for savings and the idiosyn-
chratic transition probabilities to simulate forward in time the evo-
lution of the distribution of income. So, for example, in period 1 use
the decision rule for saving to compute the distribution of income
for period 2, and likewise in period t use the decision rule savings
to compute the wealth distribution for period t + 1. This is done us-
ing the transition matrix connected with the dynamic programming
problem.
(a) If so, exit the algorithm since a solution has been found.
j +1 T
(b) If not, set {k t } t =1 = {k t }tT=1 . Repeat step one using this new
guess.
5. Upon convergence save the time paths (or the impulse response
functions) for the variables of interest. The impulse reponse func-
tions never need to be computed again.
240 numerical methods for macroeconomists with julia and matlab codes
Let X ( j) represent the baseline impulse response function for the some
generic variable, x, that was obtained in the previous step. The vari-
able x is measured in logarithms as the deviation from the logarithm of
its steady-state value, x ? . That is, 100 × X ( j) gives the percentage de-
viation of the variable of interest relative to its steady-state level in the
j-th period following the one-shot innovation. The impulse response
function shifts up or down in proportion to the size of the unfore-
seen innovation. Hence, if the one-shot innovation was λε 1 instead of
ε 1 > 0, with λ > 1, then the impulse response function shifts up by
the factor λ.
J
ε t +1− j
xt = ∑ σ
X ( j ).
j =1
as follows
ε ε ε
J J −1
xJ = X (1) + X (2) + · · · + 1 X ( J ),
σ σ σ
ε ε ε
J +1 J 2
x J +1 = X (1) + X (2) + · · · + X ( J ),
σ σ σ
.. ..
.=.
ε ε ε
t +1− J
X (1 ) + t −1 X (2 ) + · · · +
t
xt = X ( J ).
σ σ σ
Finally, compute the desired descriptive statistics from the obtained
(logged) deviations from the steady state.
A Mathematical Appendix
Some of the basic mathematics used in the book is reviewed here. This
should make the book self-contained for those rusty or unfamiliar with
the mathematics used. The presentation is cookbook in style and is ori-
ented toward discussing the uses of mathematics in the main text. An
excellent gentle and gradual introduction to the mathematics used in
economics is contained in Chiang (2011). A good introduction to prob-
ability and statistics is DeGroot (1975). Last, Bryant (1985) provides a
heuristic approach to real analysis.
A.1 Notation
• F1 , F2 , F11 , F12 , and F22 . These denote various derivatives of the func-
tion F. Specifically, F1 is the partial derivative of F with respect to
its argument, x. Thus, F1 ≡ dF/dx. Likewise, F2 ≡ dF/dy. Next, F11
is the derivative with respect to x of the first derivative F1 so that
F11 ≡ d2 F/dx2 . Finally, F12 ≡ d2 F/(dxdy) and F22 ≡ d2 F/dy2 .
244 numerical methods for macroeconomists with julia and matlab codes
• 0 used to signify the value of a variable one period down the road.
dF ( x ) d2 F ( x ) dF ( x )
F1 ( x ) ≡ and F11 ( x ) ≡ 2
= 1 .
dx dx dx
The first derivative gives the impact that a small change in x will have
on y. The second derivative specifies how the first derivative changes
in response to a small shift in x. In other words, it says how the
change in y in response to a tiny shift in x, itself, changes with a small
movement in x.
Now, consider the unconstrained maximization problem
max F ( x ). (A.2.1)
x
F1 ( x ) = 0.
mathematical appendix 245
F11 ( x ) < 0.
F1 ( x ∗ ; α) = 0.
246 numerical methods for macroeconomists with julia and matlab codes
By definition,
V ( α ) = F ( x ∗ ; α ),
because x ∗ is the value of x that maximizes F ( x ). Differentiate both
sides of the above expression with respect to α to get
dV (α) dx ∗
= F1 ( x ∗ ; α) + F2 ( x ∗ ; α).
dα | {z } dα
=0
dV (α)
= F2 ( x ∗ ; α).
dα
This occurs because at the maximum a small change in x will have no
impact on the objective function. As can be seen from Figure A.2.1, at
the top of the function an infinitesimal step left or right won’t change
the value of the function. The envelope theorem is used in Chapters 6
and 9.
max F ( x ).
x ≥ xl
mathematical appendix 247
F1 ( x ) = 0.
F1 ( xl ) < 0.
This is shown by the right-hand panel of Figure A.2.2. Here, the peak
of the function cannot be attained because the lower bound on x has
been hit. The slope is negative at x = xl . Because F1 ( x ) < 0 at x = xl , a
small reduction in x would increase the value of the objective function,
F ( x ). This cannot be done due to the presence of the lower bound, xl .
Alternatively, x could be constrained by an upper bound, xu , requiring
that x ≤ xu . Now the corner solution happens when
F1 ( xu ) > 0.
dz dx dy
= F1 ( x, y) + F2 ( x, y) .
dt dt dt
Here, the derivatives dx/dt and dy/dt depend on the specified func-
tional dependencies of x and y on t.
F ( x, y) = 0.
F ( G (y), y) = 0.
and
F ( x, y) ' F ( x ∗ , y∗ ) + F1 ( x ∗ , y∗ )( x − x ∗ ) + F2 ( x ∗ , y∗ )(y − y∗ )
1
+ F11 ( x ∗ , y∗ )( x − x ∗ )2 + F21 ( x ∗ , y∗ )( x − x ∗ )(y − y∗ )
2
1
+ F22 ( x ∗ , y∗ )(y − y∗ )2 , (second order).
2
Lemma 62. (Euler’s theorem) Consider a function, F (k, h), which is homoge-
nous of degree one in k and h; i.e., exhibits constant returns to scale in k and
h. Then,
F (k, h) = F1 (k, h)k + F2 (k, h)h.
Te0 = εe0 ,
det( T − ε) = 0.
det( T − ε) = (ε − v1 )(ε − v2 ) · · · (ε − vn ) = 0.
The mean is just the average value of the data in a set. When the data is
ordered from the lowest to the highest value, the median is the middle
value.
Definition 64. (Mean) Let { xi }iN=1 be a data series. The mean for the
series, µ x , is defined by
N
1
µx =
N ∑ xi .
i =1
fall below its mean when x rises above its one. If y and x have a strictly
positive (negative) linear relationship then ρ will be 1 (-1).
x−x
Pr[ xe ≤ x ] = U ( x ) = ,
x−x
1
U1 ( x ) = .
x−x
−( xe − µ)2
Z x
1
Pr[ xe ≤ x ] = exp [ ]de
x,
−∞ σ (2π )1/2 2σ2
where
1 −( xe − µ)2
exp [ ],
σ(2π )1/2 2σ2
is the probability density function for a normal distribution. When the
logarithm of a variable is normally distributed the variable is said to
follow a log-normal distribution.
1
p
2πσx σy (1 − ρ2 )
1 ( xe − µ x )2 ( xe − µ x )(ye − µy ) (ye − µy )2
× exp{− [ − 2ρ + ]},
2(1 − ρ2 ) σx2 σx σy σy2
Here η > 0 is called the scale parameter and β > 0 is referred to as the
shape parameter. Depending on parameter values, the density func-
tion for the Weibull function can fall or rise and then fall. The Weibull
distribution has an easy formula for the median of the distribution:
η ln(2)1/β .
The formulae for the mean and variance are somewhat more compli-
cated:
ηΓ((1 + β)/β)
and
η 2 Γ((2 + β)/β) − ηΓ((1 + β)/β)2 ,
where Γ is the gamma function. The gamma function is built into most
numerical programming languages, such as MATLAB.
Pr[ lim xn = µ] = 1.
n→∞
In other words, as the sample sizes increases the mean of the sample
will approach the mean of the distribution with virtual certainty.
B Introduction to MATLAB
by Pengfei Han
Interface of MATLAB
The contents of the variables can be examined and edited in this sec-
tion.
Housekeeping
Prior to the main body of the coding work, there are three basic
commands commonly used for housekeeping:
clear all: removes all variables from the current workspace.
close all: deletes all figures whose handles are not hidden.
clc: clears all input and output from the command window, giving
you a "clean screen".
Loading Data
To import data into MATLAB, we can simply use the import wiz-
ard:
“HOME" → “Import Data", and choose the folder/file you wish to im-
port.
Alternatively, we can load the data by using the commands associated
with the type of file to be imported. For instance:
to import files with format “xls", use the command: xlsread(‘FileName.xls’);
to import files with format “csv", use the command: csvread(‘FileName.csv’);
A detailed example is given in the M-file “Ex_load_data.m".
Stopwatch Timer
Help
your executable statement will run over one line. Then, you must put
a continuation statement at the end of line in the Editor before you go
onto the next line. This way MATLAB knows that the next line is part
of the same executable statement. The continuation statement is just
three dots or . . . .
Concatenation of Matrices
In MATLAB the concatenation operator is “[ ]".
For example, we can join two matrices side by side as follows:
X = zeros(2, 2);
260 numerical methods for macroeconomists with julia and matlab codes
Y = ones(2, 2);
join = [X Y]
0011
0011
To stack the two matrices, we can use the command “[ ; ]":
stack = [X; Y]
00
00
11
11
Relational Operators
prod(X)
28 80 162
prod(X, 2)
6
120
504
norm(X) returns the Euclidean norm (or the square root of the sum of
the squares) for the matrix X.
[M, N] = size(X): returns the number of rows (M) and columns (N)
of the matrix X as separate output variables.
size(X, 1): returns the number of rows (M)
size(X, 2): returns the number of columns (N)
length(X) : returns the length of the vector X
B.3.1 If
The “if" statement –matched with the command “end" – executes a
group of statements when a logical expression is evaluated to be true.
The general form is:
if logical expression A
statements to be executed if A is true
end
In addition, we can enrich the logical evaluation process by adding the
command “else":
if logical expression A
statements to be executed if A is true
else
statements to be executed if A is false
end
Moreover, we can further augment the if-statement by adding a series
of the command “elseif":
if logical expression A1
statements to be executed if A1 is true
introduction to matlab 263
B.3.2 For
The for loop repeats a group of statements by a fixed and predeter-
mined number of times. The general form of the for loop is:
for n = 1 : N
statements to be repeated
end
In this loop, the variable “n" – which begins at 1 and ends at N – serves
as a counter, and the variable “N" controls the number of repetition.
B.3.3 While
The while loop repeats a group of statements an indefinite number
of times, under the control of a logical condition.
The general form of the while loop is:
while logical expression
statements to be repeated
end
13 k_old = k_new ;
14 k_new = s * ( k_old ) ( aalpha ) + ( 1 − d d e l t a ) * k_old ;
15 % e v a l u a t e t h e d i s t a n c e from converging t o t h e s t e a d y s t a t e
16 d i f f = abs ( k_new − k_old ) ;
17 end
B.5 Graphing
B.5.1 Plot
In MATLAB, two-dimensional graph can be created by the command
plot. For instance, to plot a vector y against a vector x, we can simply
use the command plot ( x, y).
To add further features into the figure, consider graphing for two func-
tions:
F ( x ) = log( x ) and g( x ) = 1 + e−cx .
1 % C r e a t e t h e Grid f o r Graphing
2 x _ l b = 2 ; % lower bound o f t h e g r i d
3 x_ub = 4 ; % upper bound o f t h e g r i d
4 nx = 1 0 1 ; % t h e number o f p o i n t s i n t h e g r i d f o r x
5
266 numerical methods for macroeconomists with julia and matlab codes
F ( xn ) + F1 ( xn )( xn+1 − xn ) = 0
=⇒ xn+1 = xn − [ F1 ( xn )]−1 F ( xn )
In general fsolve works well when the function F (·) is smooth. In
addition, as you can tell from the iteration rule above, the initial guess
is critical in governing the computation efficiency. The solution of
fsolve can be of any (finite) dimension. For the scenario of single-
variable nonlinear equation, an alternative is the command fzero,
with analogous procedure to implement as fsolve. fzero can only
solve one equation in one unknown.
fsolve: Implementation
The general form to implement fsolve is:
x_star = fsolve(@(x) objective(x, c), x_initial)
There are five ingredients to implement fsolve:
objective(x, c): the nonlinear equation F ( x; c) = 0 under consideration.
c: the exogenous parameter in this nonlinear equation.
x: the endogenous unknown to obtain.
x_star: the solution to this nonlinear equation, i.e., F ( xstar ; c) = 0.
x_initial: our initial guess for the solution x_star.
Of course, we can add a variety of options to the function fsolve.
For example, to control for the criterion of convergence, we can use
the command optimset:
opt = optimset(’Tolfun’, 1e-8)
x_star = fsolve(@(x) objective(x, c), x_initial, opt)
B.6.3 Bisection
Unfortunately, Newton’s method does not perform well when the ob-
jective function F (·) is not smooth, and a potential alternative solution
is the bisection method.
268 numerical methods for macroeconomists with julia and matlab codes
min { F ( x )}.
x≤x≤x
So, one would just need to put a negative sign in front of the objective
function.
an x n + an−1 x n−1 + · · · + a1 x1 + a0 = 0.
This can be done in MATLAB using the roots command. To use this
just specify the vector a = [ an , an−1 , · · · , a0 ], where the coefficients are
in descending order and type the command roots ( a).
introduction to matlab 269
B.10 Interpolation
Suppose one has two vectors of equal size, x and y, containing data.
To compute the standard deviation of x use the command std(x). To
compute the correlation between x and y just write corrcoef(x,y). To
plot a histogram for x use the command histogram(x). This com-
mand can be used in conjunction with title, xlabel, and ylabel to
generate a title and axis labels for the histogram.
It’s easy to write results to a table in MATLAB using the table com-
mand. Suppose one has two n × 1 vectors x and y. These can be
written to a table called T using the following command:
T = table( x, y);
This will create a n × 2 table with the headings x and y for each col-
umn. This table can be exported to an EXCEL file called Data using
the writetable command. Specifically, write
filename = ’Data.xlsx’
and
writetable(T, filename);
C Introduction to Julia
Julia has a built-in package manager that allows the user to install
packages and to call them when needed to run code. Packages are in-
stalled by typing “Alt ]” in the REPL and then “add package_name”
and enter. Alternatively, you can type “using Pkg” and then enter to
call the package manager, followed by “Pkg.add(“package_name”)”
and enter.
Key packages
Housekeeping
Prior to the main body of the coding work, there are two basic com-
mands commonly used for housekeeping:
Exit(): removes all packages, structures, and variables from the cur-
rent workspace.
clearconsole(): clears all input and output from the REPL.
Loading data
To import data
Stopwatch timer
To monitor the
Lines in a file
Julia does not require the use of three dots (“. . . “) continue state-
ments across lines.
introduction to julia 273
Distributions
References
Timo Boppart, Per Krusell, and Kurt Mitman. Exploiting mit shocks
in heterogeneous-agent economies: The impulse response as a nu-
merical derivative. Journal of Economic Dynamics & Control, 89:68–92,
2018.
Wouter J. den Hann and Albert. Marcet. Solving the stochastic growth
model by parameterizing expectations. Journal of Business and Eco-
nomic Statistics, 8(1):31–34, 1990.
Ray C. Fair and John B. Taylor. Solution and maximum likelihood es-
timation of dynamic nonlinear rational expectations models. Econo-
metrica, 51(4):1169–1185, 1983.
276 numerical methods for macroeconomists with julia and matlab codes
Jeremy Greenwood, Nezih Guner, and Ricardo Marto. The great tran-
sition: Kuznets facts for family economists. Handbook of Historical
Economics, 2021b.
Mark Hugget. The one sector growth model with idiosyncratic shocks:
Steady states and dynamics. Journal of Monetary Economics, 39(3):
385–403, 1997.
Eugen Slutsky. Sulla teoria del bilancio del consumatore. Giornale degli
Economisti e Rivista di Statistica, 51(1):1–26, 1915.