matlab programming and quantitative economic...
TRANSCRIPT
Matlab Programming and Quantitative Economic
Theory
Patrick Bunk and Hong Lan
SFB C7
Humboldt University of Berlin
June 4, 2010
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 1 / 69
Quantitative Economic Theory
one utility maximizing representative agent (HH)
one profit maximizing firm
market structure
equilibrium, s.t.
HH optimize given their BC
firms maximize profits
markets clear
approximate the system around an equilibrium
–> find out the numerical solution to this class of modelsPatrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 2 / 69
A real business cycle model with labor choice
max{ct ,nt ,kt+1}∞t=0
Et
∞∑t=0
βt[log ct − φnt
]s.t. yt = ztkαt n1−α
t (1)
yt = ct + it (2)
kt+1 = (1− δ)kt + it (3)
zt+1 = ρzt + σεt+1 εt ∼ i .i .d Etεt+1 = 0 (4)
k0 is given
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 3 / 69
Bells and Whistles
certain level of heterogeneity in a an economy
goods
capital stocks
HH
firms
market structures
production technologies
matching markets (labor)
idea: add complications might help to get it right
hard to evaluate the quality of approximationsPatrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 4 / 69
1 Recursive Form
2 The Curse of Dimensionality
3 Sparse Grids
4 Parallelization
5 Perturbation
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 5 / 69
Computational Difficulties
Many economic models are of high dimension
DSGE: multiple kinds of capital stocks, agents, firms, countries...
Games: multiple players and states
Bayesian analyses: compute high-dimensional integrals
Bootstrapping: analyze many n-dimensional samples from n data
points
Simulation of large Markov processes - MCMC, Gibbs sampling...
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 6 / 69
Computational Difficulties
If we are interested in the globally accurate solution of a high
dimensional model
Global methods are cursed by dimensionality
or circumvent the curse by local approximation
Perturbation method: Schmitt-Grohe and Uribe (2004)
Linear methods: Blanchard and Kahn (1980)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 7 / 69
Recursive Form
A real business cycle model with labor choice
max{ct ,nt ,kt+1}∞t=0
Et
∞∑t=0
βt[log ct − φnt
]s.t. yt = ztkαt n1−α
t (5)
yt = ct + it (6)
kt+1 = (1− δ)kt + it (7)
zt+1 = ρzt + σεt+1 εt ∼ i .i .d Etεt+1 = 0 (8)
k0 is given
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 8 / 69
Recursive Form
The recursive formulation of the optimization problem
The stochastic case
V (k , z) = maxc,n{log(c)− φn + βEV (k ′, z ′)} (9)
s.t. c + k ′ = zkαn1−α + (1− δ)k (10)
z ′ = ρz + σε′ (11)
The deterministic case
V (k) = maxc,n{log(c)− φn + βV (k ′)} (12)
s.t. c + k ′ = zkαn1−α + (1− δ)k (13)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 9 / 69
Recursive Form
The deterministic Bellman equation
V (k) = maxc,n
{log(c)− φn + βV (k ′)
}(14)
s.t. c + k ′ = zkαn1−α + (1− δ)k (15)
The Bellman equation implicitly defines three policy functions
c = c(k) (16)
n = n(k) (17)
k ′ = k(k) (18)
Computational task: Find out the parameterization of these policy functions
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 10 / 69
Recursive Form
Result
6 8 10 12 14 16 18 206
8
10
12
14
16
18
20
k
k p
rim
epolicy function for capital
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 11 / 69
Recursive Form
Characterization
FOCs
1c
= βVk (k ′) (19)
φ = (1− α)βyn
Vk (k ′) (20)
Envelope condition
Vk (k) =1c
R where R = αyk
+ 1− δ (21)
one step forward of the envelope,
Vk (k ′) =1c′
R′ (22)
Euler equations
1 = β
[cc′
R′]
(23)
φc = (1− α)yn
(24)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 12 / 69
Recursive Form
Solving for the stationary steady state
Steady state represents the long-term equilibrium relationship of the model,
It is the constant solution of Euler equations and all constraints, and it is the constant
solution of the Bellman equation!
Need to use parameter values, δ = 0.025 α = 0.36 β = 0.99 φ = 2.5 z = 1
An example, the consumption Euler equation in steady state, 1 = βR, using β = 0.99, we
obtain R = 1/0.99
The steady state values of the model are
consumption c 0.9482
output y 1.2752
capital k 13.079
labor supply n 0.3443
These values are the ’best’ guesses to initialize the value function iteration
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 13 / 69
Recursive Form
The discretized state space algorithm (Deterministic case)
Make grids for both state variable (k): k1 < k2 < ... < kkg , and for the control variable (n):
n1 < n2 < ... < nhg
Calculate u(ni , kj , kn) for all state-control variable pairs (3 dimensional combinations),
(ni , kj , kn)
Find the new iteration of the value function as,
V 1(kn) = maxkj ,n
{u(ni , kj , kn) + βV 0(kj )
}
Check for convergence, i.e. check norm(V 1(kn)− V 0(kn)) ≤ tol for all grid values kn
If not, go back to step 2
If so, done
Use the indices from the maximum step 4 to recover the policy function
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 14 / 69
Recursive Form
Discretized state space - domain of the value function
5
10
15
20
5
10
15
200.1
0.2
0.3
0.4
0.5
0.6
capital in period tcapital in period t+1
lab
or
in p
erio
d t
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 15 / 69
Recursive Form
Discretized state space - domain of the value function
Matlab
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 16 / 69
Recursive Form
1 Recursive Form
2 The Curse of Dimensionality
3 Sparse Grids
4 Parallelization
5 Perturbation
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 17 / 69
The Curse of Dimensionality
Curse of dimensionality
The computational cost and storage requirements for a n-dimensional value function
approximation with a prescribed tolerance of error
‖Vn − V̂n‖ = O(n)
depends exponentially on the dimension n
In the stochastic version of the model, the computational cost and storage requirements of
integrating conditional expectation of q states depends exponentially on the number of
states q
Increasing the number of points in the discretized state space will dramatically increase
computational burden, even on the most powerful machine
There is some good news !
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 18 / 69
The Curse of Dimensionality
Mitigate the Curse
Judd (1992), (1998), Gaspar and Judd (1997)
a large domain creates a high computational burden
carefully choose the domain of the value function
choose the shape of the domain
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 19 / 69
The Curse of Dimensionality
Judd’s suggestion I
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 20 / 69
The Curse of Dimensionality
Judd’s suggestion I
Get rid of kinks in the model (smoothness improves computation)
Use finite-dimensional states
Use continuous time formulation
–> reduces state space by proper modeling
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 21 / 69
The Curse of Dimensionality
Judd’s suggestion II
Reduce state space a bit further
–> Use spheres instead of cubes
Spheres are much more compact
hyper sphere and circumscribed hypercube
as dimension gets large, most of the mass is in the corners
Ratio of n-sphere to n-cube volume for n even
(√π
2 )n((n/2)!)−1
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 22 / 69
The Curse of Dimensionality
Judd’s suggestion II
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 23 / 69
The Curse of Dimensionality
1 Recursive Form
2 The Curse of Dimensionality
3 Sparse Grids
4 Parallelization
5 Perturbation
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 24 / 69
Sparse Grids
Sparse Grid
Sparse grids have been used in several context
Numerical integration
Projection methods for DSGE models
Barthelmann, Novak and Ritter (2000), Computational Mathematics
Kubler and Krueger (2004), JEDC
Malin, Krueger and Kubler (2007), JEDC project report
Solution to partial differential equations
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 25 / 69
Sparse Grids
A sparse grid
0
0.5
1
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
0 0.2 0.4 0.6 0.8 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 26 / 69
Sparse Grids
Sparse Grid
To construct the sparse grid for a DSGE model, we need to
Specify the basis function, i.e. Chebyshev nodes or Gauss-Lobotto
nodes, both of them are defined on [−1,1]
Construct the sparse grid using Smolyak’s method
Convert the domain into the actual domain of the state variables in the
model
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 27 / 69
Sparse Grids
An example from Malin, Krueger and Kubler (2007)
The first level, three dimensional sparse grid
compute the basis for the grid of points
G1 = {0}
For n = 2, ..., Gn = {ζ1, ...ζn} where ζj =
−cos(π(j−1)
n−1
)j = 1, ....n
Define a sequence of positive integers by m(1) = 1 and m(i) = 2i−1 + 1 for i = 2, 3, ....
This leads to
Gm(1) = G1 = {0} and Gm(2) = G3 = {−1,0,1}
Key property of this Smolyak’s construction: Gm(i) ⊂ Gm(i+1)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 28 / 69
Sparse Grids
An example from Malin, Krueger and Kubler (2007)
The first level, three dimensional sparse grid
The grid: H3,1 = Gm(2)×Gm(1)×Gm(1)⋃
Gm(1)×Gm(2)×Gm(1)⋃
Gm(1)×Gm(1)×Gm(2)
The first level grid consists of 7 points:x1, ..., x7
(−1, 0, 0), (0, 0, 0), (1, 0, 0), (0,−1, 0), (0, 1, 0), (0, 0,−1) and (0, 0, 1)
Use (once per dimension): xi,1 = 2si,1−si,1s̄i,1−si,1
− 1 to convert the ’standard sparse grid’ into
the state variable sparse grid
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 29 / 69
Sparse Grids
An example from Malin, Krueger and Kubler (2007)
5
10
15
20
5
10
15
200.1
0.2
0.3
0.4
0.5
0.6
labor
in p
eriod t
State Sparse Grid
capital in period tcapital in period t+1 −1
−0.5
0
0.5
1
−1
−0.5
0
0.5
1−1
−0.5
0
0.5
1
Standard Sparse Grid
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 30 / 69
Sparse Grids
1 Recursive Form
2 The Curse of Dimensionality
3 Sparse Grids
4 Parallelization
5 Perturbation
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 31 / 69
Parallelization
Bound the State Space
What if all this is still not enough?
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 32 / 69
Parallelization
Getting Things Done
Standard strategy
–> 1. calculate computing time (back on the envelope)
2. optimize the code
3. Throw money at the problem (buy a faster computer)
3a. wait for better computers
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 33 / 69
Parallelization
Calculate the State Space Size and Computing Time
State Space:
capital kpt , k
ht
choices:
consumption ct
labor lt
investment kpt+1, k
ht+1
payoff: flow utility function u(c,l), discount factor β
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 34 / 69
Parallelization
Calculate the State Space Size and Computing Time
domains and discretization: [0, 1000] for kp, kh, [0, 100] for l
# choices: 100 ∗ 1000 ∗ 1000 = 108
# states: 10002 = 106
full enumeration: (#choices)#states = (108)(106)
DP: #choices ∗ (#States)2 = 108 ∗ 1012 = 1020
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 35 / 69
Parallelization
3 Curses of Dimensionality
state space
choice space
domains and discretization steps
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 36 / 69
Parallelization
Calculate the State Space Size and Computing Time
How long does it take?
how many computations are needed per point? ≥ 1
1020 = 1011 GFLOPS (109 operations per second)
3171yearsCPU GFLOPS
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 37 / 69
Parallelization
Superfluous space
kt+1 ≤ yt (kt ) + (1− δ)kt , kt+1 ≥ (1− δ)kt
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 38 / 69
Parallelization
Judd’s suggestion I
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 39 / 69
Parallelization
Calculate the State Space Size and Computing Time
How long does it take?
how many computations are needed per point? ≈ 1
1020 = 1011 GFLOPS (109 operations per second)
3171yearsCPU GFLOPS
superfluous space (-90%)
Judd I (-99%) (hard choice)
Judd II (-70%)
–> down to 347 days/ 23 GFLOPS ≈ 15 days on a decent PC
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 40 / 69
Parallelization
Calculate the State Space Size and Computing Time
How long does it take?
how many computations are needed per point? ≈ 1
1020 = 1011 GFLOPS (109 operations per second)
3171yearsCPU GFLOPS
superfluous space (-90%)
Judd I (-99%) (hard choice)
Judd II (-70%)
–> down to 347 days/ 23 GFLOPS ≈ 15 days on a decent PC
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 40 / 69
Parallelization
Getting Things Done
Standard strategy
1. calculate computing time (back on the envelope)
Is the problem large?
–> 2. optimize the code (parallelize)
3. Throw money at the problem (buy a faster computer)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 41 / 69
Parallelization
Getting Things Done
parallelize code
one of the hardest tasks in computer science
one of the main challenges in the past 20 years
sequential processing reached some physical limits (6 GHz?)
speed of light limiting factor 30 ∗ 109 cms –> 5cm/calculation
prize just got bigger!
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 42 / 69
Parallelization
Getting Things Done
parallelize code
one of the hardest tasks in computer science
one of the main challenges in the past 20 years
sequential processing reached some physical limits (6 GHz?)
speed of light limiting factor 30 ∗ 109 cms –> 5cm/calculation
prize just got bigger!
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 42 / 69
Parallelization
Divide & Conquer
algorithm design paradigm
1. break down a problem recursively
2. keep track of branches
beautiful source code (recursion)
applications:
sorting (quicksort)
multiplication (Karatsuba)
Fourier transformation DFT/FFT (Cooley-Tukey)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 43 / 69
Parallelization
Example of D&C - Value Iteration
V (s) := maxa
{∑s′
P(s, s′|a)[Ra(s, s′) + βV (s′)]
}(25)
Bellman equation as an update rule
take any starting guess: V0(s) := 0,∀s ∈ S ≈ O(S)
update V1(s) := maxa{∑
s′ P(s, s′|a)[Ra(s, s′) + βV0(s′)]}∀s ∈ S
≈ O(S2C) or O(SC)
compute ∆t := maxs∈S |Vt (s)− Vt−1(s)| ≈ O(S)
loop until ∆t ≤ ε ≈ O(P(S))
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 44 / 69
Parallelization
Example of D&C - Value Iteration
V (s) := maxa
{∑s′
P(s, s′|a)[Ra(s, s′) + βV (s′)]
}(25)
Bellman equation as an update rule
take any starting guess: V0(s) := 0,∀s ∈ S ≈ O(S)
update V1(s) := maxa{∑
s′ P(s, s′|a)[Ra(s, s′) + βV0(s′)]}∀s ∈ S
≈ O(S2C) or O(SC)
compute ∆t := maxs∈S |Vt (s)− Vt−1(s)| ≈ O(S)
loop until ∆t ≤ ε ≈ O(P(S))
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 44 / 69
Parallelization
Example of D&C - Value Iteration
V (s) := maxa
{∑s′
P(s, s′|a)[Ra(s, s′) + βV (s′)]
}(25)
Bellman equation as an update rule
take any starting guess: V0(s) := 0,∀s ∈ S ≈ O(S)
update V1(s) := maxa{∑
s′ P(s, s′|a)[Ra(s, s′) + βV0(s′)]}∀s ∈ S
≈ O(S2C) or O(SC)
compute ∆t := maxs∈S |Vt (s)− Vt−1(s)| ≈ O(S)
loop until ∆t ≤ ε ≈ O(P(S))
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 44 / 69
Parallelization
Example of D&C - Value Iteration
V (s) := maxa
{∑s′
P(s, s′|a)[Ra(s, s′) + βV (s′)]
}(25)
Bellman equation as an update rule
take any starting guess: V0(s) := 0,∀s ∈ S ≈ O(S)
update V1(s) := maxa{∑
s′ P(s, s′|a)[Ra(s, s′) + βV0(s′)]}∀s ∈ S
≈ O(S2C) or O(SC)
compute ∆t := maxs∈S |Vt (s)− Vt−1(s)| ≈ O(S)
loop until ∆t ≤ ε ≈ O(P(S))
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 44 / 69
Parallelization
Example of D&C - Value Iteration
V (s) := maxa
{∑s′
P(s, s′|a)[Ra(s, s′) + βV (s′)]
}(25)
Bellman equation as an update rule
take any starting guess: V0(s) := 0,∀s ∈ S ≈ O(S)
update V1(s) := maxa{∑
s′ P(s, s′|a)[Ra(s, s′) + βV0(s′)]}∀s ∈ S
≈ O(S2C) or O(SC)
compute ∆t := maxs∈S |Vt (s)− Vt−1(s)| ≈ O(S)
loop until ∆t ≤ ε ≈ O(P(S))
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 44 / 69
Parallelization
Getting Things Done
Standard strategy
1. calculate computing time (back on the envelope)
2. optimize the code (parallelize)
–> 3. Throw money at the problem (buy a faster computer)
3a. wait for better computers
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 45 / 69
Parallelization
Speed things up
Name CPU Price GFLOPS ratio Top
Dell OptiPlex780 C2D7500 900 23 40 1992
Dell Workstation 2xXeon5680 7000 215 32 1996
Table: Currently available computers
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 46 / 69
Parallelization
Speed things up II
new trend: General Purpose processing with GPUs
GPUs calculate parts of the image –> branches
specialized early in multi-core processing
last years development of interface to tap into that power
CUDA by Nvidia 2007
OpenCL Standard 2009
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 47 / 69
Parallelization
Speed things up III
Name CPU Price GFLOPS ratio Top
Dell OptiPlex780 C2D7500 900 23 40 1992
Dell Workstation 2xXeon5680 7000 215 32 1996
600 EUR PC NVIDIA GT240 700 385 1.8 1996
Table: Currently available processing power
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 48 / 69
Parallelization
Speed things up III
Name CPU Price GFLOPS ratio Top
Dell OptiPlex780 C2D7500 900 23 40 1992
Dell Workstation 2xXeon5680 7000 215 32 1996
600 EUR PC NVIDIA GT240 700 385 1.8 1996
600 EUR PC NVIDIA GTX 295 1000 1788 0.56 1998
Table: Currently available processing power
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 49 / 69
Parallelization
Speed things up III
Name CPU Price GFLOPS ratio Top
Dell OptiPlex780 C2D7500 900 23 40 1992
Dell Workstation 2xXeon5680 7000 215 32 1996
600 EUR PC NVIDIA GT240 700 385 1.8 1996
600 EUR PC NVIDIA GTX 295 1000 1788 0.56 1998
600 EUR PC ATI 5970 1200 4640 0.26 2001
1200 EUR PC 3xATI 5970 3000 13920 0.20 2002
Table: Currently available processing power
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 50 / 69
Parallelization
net performance gains
Test N CPU time GPU time Speed Up
exp(A) 1600x1600 0.2911 0.0029 100
A.*B 1600x1600 0.0778 0.0026 30
Black-Scholes 18.1 0.13 13
A*B 1600x1600 2.81 0.24 12
FFT(A) 1600x1600 0.1710 0.0189 9
Table: average performance gains GTX8800 (512) vs C2D (23)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 51 / 69
Parallelization
CUDA
CUDA is a C SDK
CUDA Framework is written and accessed in C
works on all supported graphics cards without code changes
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 52 / 69
Parallelization
CUDA - How does it look?
Look at the code for vector addition
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 53 / 69
Parallelization
Why Matlab?
C (90 % of code on how to do things, 10% on what to do)
–> CS’ are better at this than Economists!
Matlab (0% of code on how, 100% on what to do)
my requirements:
hide CUDA-API
take care of low level stuff
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 54 / 69
Parallelization
Why Matlab?
Matlab GPU plugins requirements:
tap into GPU power w/o GPU knowledge
execute Matlab code on the GPU transparently
ability to port code
construct new functions that work on the GPU w/o deep CUDA
knowledge
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 55 / 69
Parallelization
GPUmat and Alternatives
MathWorks is working on it (closed registration)
AcceleratorEyes Jacket - closed source (hard to fix) ($350+)
GPUlib - open source, tedious memory management
GPUmat - free for academic use, early development stage
Mathematica CUDA plugin (alpha)
R (R+GPU) plugin (beta)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 56 / 69
Parallelization
How to tap into the GPU-Power
adjust your algorithms (sequential to parallel)
check your graphics card (5min)
install CUDA Framework (5min)
install CUDA Matlab Plugin (3min)
unzip GPUmat Framework (2min)
(private beta, public end of June)
run GPUstart.m
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 57 / 69
Parallelization
How to use GPUmat
Matlab
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 58 / 69
Parallelization
Laptop
CPU Core i5m ≈ 21 GFLOPS
GPU GT330M ≈ 182 GFLOPS
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 59 / 69
Parallelization
net performance gains
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 60 / 69
Parallelization
Backup Slides
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 61 / 69
Perturbation
Perturbation
Assume we know the solution, plug the assumed solution back
into the system, then
’perturbate’ the system using total differentials.
The previous step will impose some restrictions on the system
Use those restrictions to parameterize the assumed solution.
Done!
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 62 / 69
Perturbation
The model in equilibrium
Consumption Euler equation: 1ct
= βEt
[1
ct+1
(αzt+1kα−1
t+1 n1−αt+1 + 1− δ
)]Labor Euler equation: ct = 1−α
φ ztkαt n−αt
Budget constraint: ct + kt+1 = ztkαt n1−αt + (1− δ)kt
Productivity shock: zt+1 = ρzt + σεt+1
Endogenous state variable, kt , exogenous state variable, zt ,
endogenous variables, ct ,nt
There are four equations, four unknowns
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 63 / 69
Perturbation
The assumed solution
Assume the following solution
ct = c(kt , zt , σ)
nt = n(kt , zt , σ)
kt+1 = k(kt , zt , σ)
The productivity shock implies zt+1 = z(zt , σ), so that
ct+1 = c(k(kt , zt , σ), z(zt , σ), σ)
nt+1 = n(k(kt , zt , σ)︸ ︷︷ ︸kt+1
, z(zt , σ)︸ ︷︷ ︸zt+1
, σ)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 64 / 69
Perturbation
Solving for steady state
The assumed solution in steady state takes form of
c = c(k , z, 0)
n = n(k , z, 0)
k = k(k , z, 0)
They can be solved using given parameter values, all Euler equations and constraints in
steady state
σ = 0 in steady state, meaning there is no uncertainty
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 65 / 69
Perturbation
Solving for dynamics
Example: ct + kt+1 − zt kαt n1−αt − (1− δ)kt = 0
Plug in the solution c(kt , zt , σ) + k(kt , zt , σ)− zt kαt n(kt , zt , σ)1−α − (1− δ)kt = 0
In general F (kt , zt , σ) = 0
First order total differential DF = D0 = 0, or Fk dkt + Fzdzt + Fσdσ = 0
Equivalently,[Fk Fz Fσ
]dkt
dzt
dσ
= 0
To allow arbitrary change in kt , zt , σ (equivalently, to allow dkt , dzt , dσ to take any value),
we require Fk = Fz = Fσ = 0
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 66 / 69
Perturbation
Solving for dynamics
Euqation c(kt , zt , σ) + k(kt , zt , σ)− zt kαt n(kt , zt , σ)1−α − (1− δ)kt = 0
To find Fk , first find Fkt , then evaluate Fkt at steady state,
Fkt = ck (kt , zt , σ) + kk (kt , zt , σ)− αzt kα−1t n(kt , zt , σ)1−α
− (1− α)zt kαt n(kt , zt , σ)−αnk (kt , zt , σ)− (1− δ)
Evaluate Fkt at steady state, using Fk = 0,
Fk = ck + kk − αzkα−1n1−α − (1− α)zkαn−αnk − (1− δ) = 0
Similarly, for Fz and Fσ , we have
Fz = cz + kz − kαn1−α − (1− α)zkαn−αnz = 0
Fσ = cσ + kσ − (1− α)zkαn−αnσ = 0
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 67 / 69
Perturbation
Solving for dynamics
Apply the same procedure to consumption and labor Euler equations, we can altogether
obtain 9 equations for 9 unknowns
9 unknowns are (ck , cz , cσ , kk , kz , kσ , nk , nz , nσ)
In first order perturbation, (cσ , kσ , nσ) are always zero, because by requiring
Fk = Fz = Fσ = 0, we only allow changes to happen in these three basic directions, σ has
no impact until we allow changes to happen along some ’composite’ directions, i.e.
Fkk ,Fzz .
The 9 unknowns can be used to construct the first order approximation of the assumed
solution
ct = c + ck (ct − c) + cz (zt − z)
nt = n + nk (ct − c) + nz (zt − z)
kt+1 = k + kk (ct − c) + kz (zt − z)
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 68 / 69
Perturbation
A short summary
Perturbation is still a local approximation method
Using total differential and derivatives means we observe the model’s behavior in the
neighborhood of the equilibrium
Using higher order perturbation, we can observe the model’s behavior when changes
happen along ’all’ directions around the equilibrium
Schmitt-Grohe and Uribe (2004)’s code of perturbation is efficient, because it takes
analytic derivatives first
Early version of Dynare takes numerical derivatives, that is why it crashes often
Patrick Bunk and Hong Lan (SFB C7 Humboldt University of Berlin)Matlab Programming and Quantitative Economic Theory June 4, 2010 69 / 69