ECON 4310
Fall 2008
Espen Henriksen
Problem set 3
Solutions
1. Theory
We are asked to verify that the value function solving the function
equation is of the following form
v (k) = a + b ln k.
(1)
The Bellman equation (the functional equation) can then be written
as
v (k) = max
u γk α − k 0 + βv k 0
(2)
0
k
and the first order condition u0 (c) = βv 0 (k 0 ) is for our guess for the
value function equal to
b
1
= β 0.
c
k
Given the resource constraint, we eliminate c and solve for k 0
βb
0
γk α .
(3)
k =
1 + βb
So if our initial guess is right, then this is our decision rule.1
Given this decision rule we can verify whether the initial guess for v (k)
satisfies the functional equation
1
βb
a + b ln k = ln
γk α + β a + b ln
γk α
1 + βb
1 + βb
1
βb
= ln
γ + α ln k + βa + βb ln
γ + βbα ln k
1 + βb
1 + βb
γ
γ
+ β a + b ln
+ b ln βb + α ln k + βbα ln k
= ln
1 + βb
1 + βb
γ
= βa + (1 + βb) ln
+ βb ln (βb) + α (1 + βb) ln k.
1 + βb
1
We are not doing “policy”.
1
So we see that this expression only depends on the current state k.
Since this expression must hold for all k, we group the terms and
solve for the unknown coefficients of the value function in terms of the
structural parameters (here: α, β and γ).
a =
b =
1
γ
(1 + βb) ln
+ βb ln (βb) ,
1−β
1 + βb
α
.
1 − βα
(4)
(5)
So the decision rules with respect to our two control variables k 0 and
c are
k 0 = βαγk α ,
and
c = g (k) = (1 − βα) γk α .
2. Computations
Matlab m-files attached.
(a) Deterministic value function iteration (dp1.m)
clear all;
close all;
% Define the structural parameters of the model,
% i.e. policy invariant preference and technology parameters
% alpha : capital’s share of output
% beta : time discount factor
% delta : depreciation rate
% sigma : risk-aversion parameter, also intertemp. subst. param.
% gamma : unconditional expectation of the technology parameter
alpha = .35;
beta = .98;
delta = .025;
sigma = 2;
gamma = 5;
% Find the steady-state level of capital as a function of
%
the structural parameters
kstar = ((1/beta - 1 + delta)/(alpha*gamma))^(1/(alpha-1));
% Define the number of discrete values k can take
gk = 501;
k = linspace(0.95*kstar,1.05*kstar,gk);
% Compute a (gk x gk) dimensional consumption matrix c
%
for all the (gk x gk) values of k_t, k_t+1
for h = 1 : gk
for i = 1 : gk
c(h,i) = gamma*k(h)^alpha + (1-delta)*k(h) - k(i);
if c(h,i) < 0
c(h,i) = 0;
end
2
% h is the counter for the endogenous state variable k_t
% i is the counter for the control variable k_t+1
end
end
% Compute a (gk x gk) dimensional consumption matrix u
%
for all the (gk x gk) values of k_t, k_t+1
for h = 1 : gk
for i = 1 : gk
if sigma == 1
u(h,i) = log(c(h,i));
else
u(h,i) = (c(h,i)^(1-sigma) - 1)/(1-sigma);
end
% h is the counter for the endogenous state variable k_t
% i is the counter for the control variable k_t+1
end
end
% Define the initial matrix v as a matrix of zeros (could be anything)
v = zeros(gk,1);
% Set parameters for the loop
convcrit = 1E-9; % chosen convergence criterion
diff = 1;
% arbitrary initial value greater than convcrit
iter = 0;
% iterations counter
while diff > convcrit
% for each k_t
%
find the k_t+1 that maximizes the sum of instantenous utility and
%
discounted continuation utility
for h = 1 : gk
Tv(h,1) = max( u(h,:) + beta*v’);
end
iter = iter + 1;
diff = norm(Tv - v);
v = Tv;
end
disp(iter)
figure
plot(k,v)
xlabel(’k_t’)
ylabel(’v_t’)
print -depsc2 valuefn.eps
% Find what gridpoint of the k_t+1 vector which gave the optimal value
for h = 1 : gk
[Tv,gridpoint] = max(u(h,:) + beta*v’);
kgridrule(h) = gridpoint;
end
% Find what element of k_t+1 which gave the optimal value
kdecrule = k(kgridrule);
figure
plot(k,kdecrule,k,k);
xlabel(’k_t’)
ylabel(’k_{t+1}’)
print -depsc2 kdecrule.eps
% Compute the optimal decision rule for c as a function of the state
% variable
for h = 1 : gk
cdecrule(h) = gamma*k(h)^alpha + (1-delta)*k(h) - kdecrule(h);
end
figure
plot(k,cdecrule)
xlabel(’k_t’)
ylabel(’c_t’)
print -depsc2 cdecrule.eps
3
(b) Stochastic value function iteration, iid shocks (dp2.m)
clear all;
close all;
% Define the structural parameters of the model,
% i.e. policy invariant preference and technology parameters
% alpha : capital’s share of output
% beta : time discount factor
% delta : depreciation rate
% sigma : risk-aversion parameter, also intertemp. subst. param.
alpha = .35;
beta = .98;
delta = .025;
sigma = 2;
% Number of exogenous states
gz = 2;
% Values of z
z = [4.95
5.05];
% Probabilites for z
prh = .5;
prl = 1-prh;
% Expected value of z
zbar = prh*z(1) + prl*z(2);
% Find the steady-state level of capital as a function of
%
the structural parameters
kstar = ((1/beta - 1 + delta)/(alpha*zbar))^(1/(alpha-1));
% Define the number of discrete values k can take
gk = 101;
k = linspace(0.90*kstar,1.10*kstar,gk);
% Compute a (gk x gk x gz) dimensional consumption matrix c
%
for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.
for h = 1 : gk
for i = 1 : gk
for j = 1 : gz
c(h,i,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - k(i);
if c(h,i,j) < 0
c(h,i,j) = 0;
end
% h is the counter for the endogenous state variable k_t
% i is the counter for the control variable k_t+1
% j is the counter for the exogenous state variable z_t
end
end
end
% Compute a (gk x gk x gz) dimensional consumption matrix u
%
for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.
for h = 1 : gk
for i = 1 : gk
for j = 1 : gz
if sigma == 1
u(h,i,j) = log(c(h,i,j))
else
u(h,i,j) = (c(h,i,j)^(1-sigma) - 1)/(1-sigma);
end
% h is the counter for the endogenous state variable k_t
% i is the counter for the control variable k_t+1
% j is the counter for the exogenous state variable z_t
end
end
end
% Define the initial matrix v as a matrix of zeros (could be anything)
v = zeros(gk,gz);
% Set parameters for the loop
4
convcrit = 1E-6;
diff = 1;
iter = 0;
% chosen convergence criterion
% arbitrary initial value greater than convcrit
% iterations counter
while diff > convcrit
% for each combination of k_t and gamma_t
%
find the k_t+1 that maximizes the sum of instantenous utility and
%
discounted continuation utility
for h = 1 : gk
for j = 1 : gz
Tv(h,j) = max( u(h,:,j) + beta*(prh*v(:,1)’ + prl*v(:,2)’) );
end
end
iter = iter + 1;
diff = norm(Tv - v);
if mod(iter,50) == 0
fprintf(’\t iteration = %3.0f \t diff = %1.8f \n’, [iter diff]);
end
v = Tv;
end
% Find the implicit decision rule for k_t+1 as a function of the state
%
variables k_t and z_t
for h = 1 : gk
for j = 1 : gz
% Using the [ ] syntax for max does not only give the value, but
%
also the element chosen.
[Tv,gridpoint] = max(u(h,:,j) + beta*(prh*v(:,1)’ + prl*v(:,2)’));
% Find what grid point of the k vector which is the optimal decision
kgridrule(h,j) = gridpoint;
% Find what value for k_t+1 which is the optimal decision
kdecrule(h,j) = k(gridpoint);
end
end
% Plot it and save it as a jpg-file
figure
plot(k,kdecrule,k,k);
xlabel(’k_t’)
ylabel(’k_{t+1}’)
print -djpeg kdecrule.jpg
% Compute the optimal decision rule for c as a function of the state
%
variables
for h = 1 : gk
% counter for k_t
for j = 1 : gz
% counter for z_t
cdecrule(h,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - kdecrule(h,j);
end
end
figure
plot(k,cdecrule)
xlabel(’k_t’)
ylabel(’c_t’)
print -djpeg decrulec.jpg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulation
% Arbitrary starting point
enostate = round(gk/2);
ksim(1) = k(enostate);
% Draw a sequence of 100 random variables and use the optimal decision rule
for i = 1 : 100
draw = rand;
if draw < prh
exostate = 1;
else
exostate = 2;
end
5
kprimegrid = kgridrule(enostate,exostate);
kprime = k(kprimegrid);
zsim(i) =
ksim(i+1)
ysim(i) =
isim(i) =
csim(i) =
z(exostate);
= kprime;
z(exostate)*ksim(i)^alpha;
ksim(i+1) - (1-delta)*ksim(i);
ysim(i) - isim(i);
enostate = kprimegrid;
end
figure
plot(1:100,ysim/mean(ysim),1:100,csim/mean(csim),1:100,isim/mean(isim),1:100,ksim(1:100)/mean(ksim(1:100)))
legend(’y’,’c’,’i’,’k’)
title(’Simulation 100 draws, devations from mean’)
print -djpeg simulation.jpg
(c) Stochastic value function iteration, Markov shocks (dp3.m)
% Solving a stochastic neoclassical growth model with elastic labor supply
% ECON 4310, University of Oslo
% by Espen Henriksen
clear all;
% clears all variables from the memory
close all;
% closes all figures
tic % starts the "stop watch"
alpha
beta
delta
epsilon
kappa
psi
sigma
= .36;
= .97;
= .006;
= .022;
= .975;
= 1.78;
= 1;
z = [-epsilon, epsilon];
Xi = (1/beta - (1 - delta))/alpha;
Pi = [kappa
1-kappa
1-kappa kappa];
dim = 2;
hg = 51;
kg = 101;
% Number of values the exogenous state variable might take
% Number of grid points for the control variable h
% Number of grid points for both the control var and state var k
khratio = ((1/beta - 1 + delta)/alpha)^(1/(alpha-1));
chratio = khratio^alpha - delta*khratio;
hstar = 1/(1 + psi*chratio/((1-alpha)*khratio^alpha));
kstar = khratio*hstar;
cstar = chratio*hstar;
clear khratio chratio;
% if you won’t need ’em delete ’em
kgrid = linspace(.85*kstar,1.15*kstar,kg)’;
hgrid = linspace(.8*hstar,1.2*hstar,hg)’;
c = zeros(hg,kg,dim,kg);
u = zeros(hg,kg,dim,kg);
%
%
%
%
i
j
m
n
is
is
is
is
a
a
a
a
counter
counter
counter
counter
for
for
for
for
the
the
the
the
control variable h
control variable k’
exog. state variable z
endogenous state variable k
warning off; % disable warning for taking log of zero (just annoying)
for i = 1 : hg
for j = 1 : kg
for m = 1 : dim
for n = 1 : kg
c(i,j,m,n) = exp(z(m))*(kgrid(n)^alpha)*hgrid(i)^(1-alpha) + (1-delta)*kgrid(n) - kgrid(j);
6
if c(i,j,m,n) < 0
c(i,j,m,n) = 0;
end
u(i,j,m,n) = log(c(i,j,m,n)) + psi*log(1-hgrid(i));
end
end
end
end
warning on;
% turn warnings on again
clear c
% free up memory
clear i j m n % clean up
v = zeros(kg,dim);
convcrit = 1E-11;
diff = 1;
iter = 0;
% chosen convergence criterion
% arbitrary initial value greate
% iterations counter
while diff > convcrit
diff = 0;
for m = 1 : dim
for n = 1 : kg
objfn(:,:,m,n) = u(:,:,m,n) + beta*(Pi(m,1)*(v(:,1)*ones(1,hg))’+Pi(m,2)*(v(:,2)*ones(1,hg))’);
Tv(n,m) = max(max(objfn(:,:,m,n)));
end
end
diff = norm(v-Tv);
v = Tv;
iter = iter + 1;
end
for m = 1 : dim
for n = 1 : kg
objfn(:,:,m,n) = u(:,:,m,n) + beta*(Pi(m,1)*(v(:,1)*ones(1,hg))’+Pi(m,2)*(v(:,2)*ones(1,hg))’);
[tmp1,x1] = max(objfn(:,:,m,n),[],1);
[tmp2,x2] = max(tmp1,[],2);
kgridrule(m,n) = x2;
hgridrule(m,n) = x1(x2);
kdecrule(m,n) = kgrid(kgridrule(m,n));
hdecrule(m,n) = hgrid(hgridrule(m,n));
cdecrule(m,n) = exp(z(m))*(kgrid(n)^alpha)*hdecrule(m,n)^(1-alpha) + (1-delta)*kgrid(n) - kdecrule(m,n);
end
end
% If you won’t need ’em -- delete ’em
clear tmp1 tmp2 x1 x2
clear diff convcrit iter m n
clear objfn Tv
clear u
figure;plot(kgrid,v);
title(’Value function’)
figure;
plot(kgrid,kgrid,kgrid,kdecrule);
title(’Decision rules for capital’);
figure;
plot(kgrid,cdecrule)
title(’Decision rules for consumption’);
figure;
plot(kgrid,hdecrule)
title(’Decision rules for labor supply’);
save termpaperq3
load termpaperq3
if mod(kg,2) == 0
endostate = kg/2;
else
endostate = (kg-1)/2 + 1;
end
exostate = 1;
% modulus after division,in
7
kprime = kgrid(endostate);
for ctr = 1 : 10000
kcurr = kprime;
draw = rand;
if exostate == 1
if draw < Pi(1,1)
exostate = 1;
else
exostate = 2;
end
else
if draw < Pi(2,2)
exostate = 2;
else
exostate = 1;
end
end
kprime = kdecrule(exostate,endostate);
h(ctr,1) = hdecrule(exostate,endostate);
k(ctr,1)
i(ctr,1)
y(ctr,1)
c(ctr,1)
r(ctr,1)
w(ctr,1)
=
=
=
=
=
=
kcurr;
kprime - (1-delta)*kcurr;
exp(z(exostate))*kcurr^alpha*h(ctr,1)^(1-alpha);
y(ctr,1) - i(ctr,1);
exp(z(exostate))*alpha*(kcurr/h(ctr,1))^(alpha-1);
exp(z(exostate))*(1-alpha)*(kcurr/h(ctr,1))^(alpha);
endostate = kgridrule(exostate,endostate);
end
clear ctr draw
y
i
h
k
c
r
w
=
=
=
=
=
=
=
y(1000:10000);
i(1000:10000);
h(1000:10000);
k(1000:10000);
c(1000:10000);
r(1000:10000);
w(1000:10000);
ystd
istd
hstd
kstd
cstd
rstd
wstd
=
=
=
=
=
=
=
ystdy
istdy
hstdy
kstdy
cstdy
rstdy
wstdy
std(y)/mean(y);
std(i)/mean(i);
std(h)/mean(h);
std(k)/mean(k);
std(c)/mean(c);
std(r)/mean(r);
std(w)/mean(w);
ycorry
icorry
hcorry
kcorry
ccorry
rcorry
wcorry
=
=
=
=
=
=
=
ystd/ystd;
istd/ystd;
hstd/ystd;
kstd/ystd;
cstd/ystd;
rstd/ystd;
wstd/ystd;
=
=
=
=
=
=
=
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
corrcoef(y,y);
corrcoef(y,i);
corrcoef(y,h);
corrcoef(y,k);
corrcoef(y,c);
corrcoef(y,r);
corrcoef(w,i);
\n’)
\t Mean
y \t %1.5f
c \t %1.5f
i \t %1.5f
h \t %1.5f
k \t %1.5f
r \t %1.5f
w \t %1.5f
\n’)
\t
\t
\t
\t
\t
\t
\t
\t
St.dev.
%1.5f
%1.5f
%1.5f
%1.5f
%1.5f
%1.5f
%1.5f
\t
\t
\t
\t
\t
\t
\t
\t
Std rel y \t Cont.corr y \n’);
%1.5f
\t %1.5f
\n’, [mean(y) ystd ystdy ycorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(c) cstd cstdy ccorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(i) istd istdy icorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(h) hstd hstdy hcorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(k) kstd kstdy kcorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(r) rstd rstdy rcorry(1,2)])
%1.5f
\t %1.5f
\n’, [mean(w) wstd wstdy wcorry(1,2)])
8
fprintf(’ \n’)
fprintf(’ \n’)
for ctr = 1 : 9
tmp = corrcoef(y(ctr:8990+ctr,1),y(5:8995,1));
corrtable(1,ctr) = tmp(2,1);
tmp = corrcoef(c(ctr:8990+ctr,1),y(5:8995,1));
corrtable(2,ctr) = tmp(2,1);
tmp = corrcoef(i(ctr:8990+ctr,1),y(5:8995,1));
corrtable(3,ctr) = tmp(2,1);
tmp = corrcoef(h(ctr:8990+ctr,1),y(5:8995,1));
corrtable(4,ctr) = tmp(2,1);
end
clear tmp ctr
fprintf(’
fprintf(’
fprintf(’
fprintf(’
fprintf(’
y
c
i
h
t-4
%1.4f
%1.4f
%1.4f
%1.4f
t-3
%1.4f
%1.4f
%1.4f
%1.4f
t-2
t-1
%1.4f %1.4f %1.4f
%1.4f %1.4f %1.4f
%1.4f %1.4f %1.4f
%1.4f %1.4f %1.4f
% because matlab rather stupidly returns the correlation matrix
% because matlab rather stupidly returns the correlation matrix
% because matlab rather stupidly returns the correlation matrix
% because matlab rather stupidly returns the correlation matrix
t
%1.4f
%1.4f
%1.4f
%1.4f
t+1
%1.4f
%1.4f
%1.4f
%1.4f
t+2
%1.4f
%1.4f
%1.4f
%1.4f
t+3
%1.4f
%1.4f
%1.4f
%1.4f
%for i = 1 : 5000
3. Measurements
Stata do- and dta-files attached.
* Trend and business cycle analysis on annual Norwegian accounts 1865-2005
* Prepared for UiO 4310 Fall 2007 by Espen Henriksen
* Datasource: Statistics Norway
clear
use histdata.dta
tsset year
sort year
twoway (line y year if year<1943) (line y year if year>1943), legend(off)
graph export graph1.wmf, replace
more
gen logy = log(y)
label var logy "log(Gross national product)"
gen logc = log(c)
label var logc "log(Private consumption)"
gen logg = log(g)
label var logg "log(Government consumption)"
gen logi = log(i)
label var logi "log(Gross real investment)"
twoway (line logy year if year<1946) (line
graph export graph2.wmf, replace
more
logy year if year>1941), legend(off)
gen cyratio = c/y
label var cyratio "Private consumption to GNP ratio"
gen gyratio = g/y
label var gyratio "Government consumption to GNP ratio"
gen iyratio = i/y
label var iyratio "Gross real investment to GNP ratio"
gen neyratio = (exp - imp)/y
label var neyratio "Net export to GNP ratio"
9
t+4
\n’,
\n’,
\n’,
\n’,
\n’)
[corrtable(1,:)])
[corrtable(2,:)])
[corrtable(3,:)])
[corrtable(4,:)])
twoway (line cyratio year) (line gyratio year) (line iyratio year) (line neyratio year), legend(col(1))
graph export graph3.wmf, replace
more
* Trends and business cycles after WW2
preserve
drop if year<1943
regress logy year
predict lt_logy
predict lt_logy_resid, resid
label var lt_logy "loglinear trend GNP"
twoway (line logy year) (line lt_logy year)
graph export graph4.wmf, replace
more
twoway (line lt_logy_resid year)
graph export graph5.wmf, replace
more
hprescott
hprescott
hprescott
hprescott
label
label
label
label
label
var
var
var
var
var
logy,
logc,
logg,
logi,
stub(hp)
stub(hp)
stub(hp)
stub(hp)
hp_logy_1
hp_logc_1
hp_logg_1
hp_logi_1
hp_logy_sm_1
smooth(6.25)
smooth(6.25)
smooth(6.25)
smooth(6.25)
"Deviations from HP trend
"Deviations from HP trend
"Deviations from HP trend
"Deviations from HP trend
"HP trend of log(GNP)"
of
of
of
of
log(Y)"
log(C)"
log(G)"
log(I)"
twoway line hp_logy_1 year
graph export graph6.wmf, replace
more
twoway (line logy year) (line hp_logy_sm_1 year), legend(col(1))
graph export graph7.wmf, replace
more
twoway (line hp_logy_1 year) (line hp_logi_1 year), legend(col(1))
graph export graph8.wmf, replace
more
rename hp_logy_1 hp_logy_postwar
rename hp_logc_1 hp_logc_postwar
rename hp_logg_1 hp_logg_postwar
rename hp_logi_1 hp_logi_postwar
keep year hp_logy_postwar hp_logc_postwar hp_logg_postwar hp_logi_postwar
save postwardata.dta, replace
twoway (line hp_logy_postwar year ) (line
graph export graph9.wmf, replace
more
hp_logi_postwar year ) (line
restore
* Trends and business cycles before WW2
preserve
keep if year<1943
regress logy year
predict lt_logy
predict lt_logy_resid, resid
label var lt_logy "loglinear trend GNP"
twoway (line logy year) (line lt_logy year), legend(col(1))
10
hp_logg_postwar year )
(line
hp_logc_postwar year ), legend(col(2
graph export graph10.wmf, replace
more
twoway (line lt_logy_resid year)
graph export graph11.wmf, replace
more
hprescott
hprescott
hprescott
hprescott
label
label
label
label
label
var
var
var
var
var
logy,
logc,
logg,
logi,
stub(hp)
stub(hp)
stub(hp)
stub(hp)
hp_logy_1
hp_logc_1
hp_logg_1
hp_logi_1
hp_logy_sm_1
smooth(6.25)
smooth(6.25)
smooth(6.25)
smooth(6.25)
"Deviations from HP trend
"Deviations from HP trend
"Deviations from HP trend
"Deviations from HP trend
"HP trend of log(GNP)"
of
of
of
of
log(Y)"
log(C)"
log(G)"
log(I)"
twoway (line logy year) (line hp_logy_sm_1 year), legend(col(1))
graph export graph12.wmf, replace
more
twoway line hp_logy_1 year
graph export graph13.wmf, replace
more
twoway (line hp_logy_1 year) (line hp_logi_1 year), legend(col(1))
graph export graph14.wmf, replace
more
rename hp_logy_1 hp_logy_prewar
rename hp_logc_1 hp_logc_prewar
rename hp_logg_1 hp_logg_prewar
rename hp_logi_1 hp_logi_prewar
keep year hp_logy_prewar hp_logc_prewar hp_logg_prewar hp_logi_prewar
save prewardata.dta, replace
restore
tsset year
sort year
merge year using prewardata
drop _merge
erase prewardata.dta
sort year
merge year using postwardata
drop _merge
erase postwardata.dta
sort year
* Plot the results
twoway (line hp_logy_prewar year) (line
graph export graph15.wmf, replace
hp_logy_postwar year), legend(off)
11
© Copyright 2026 Paperzz