-
Notifications
You must be signed in to change notification settings - Fork 131
/
RBC_Matlab.m
137 lines (93 loc) · 4.02 KB
/
RBC_Matlab.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%% Basic RBC model with full depreciation
%
% Jesus Fernandez-Villaverde
% Haverford, July 3, 2013
%% 0. Housekeeping
clear all
close all
clc
tic
%% 1. Calibration
aalpha = 1/3; % Elasticity of output w.r.t. capital
bbeta = 0.95; % Discount factor
% Productivity values
vProductivity = [0.9792; 0.9896; 1.0000; 1.0106; 1.0212]';
% Transition matrix
mTransition = [0.9727, 0.0273, 0.0000, 0.0000, 0.0000;
0.0041, 0.9806, 0.0153, 0.0000, 0.0000;
0.0000, 0.0082, 0.9837, 0.0082, 0.0000;
0.0000, 0.0000, 0.0153, 0.9806, 0.0041;
0.0000, 0.0000, 0.0000, 0.0273, 0.9727];
%% 2. Steady State
capitalSteadyState = (aalpha*bbeta)^(1/(1-aalpha));
outputSteadyState = capitalSteadyState^aalpha;
consumptionSteadyState = outputSteadyState-capitalSteadyState;
fprintf(' Output = %2.6f, Capital = %2.6f, Consumption = %2.6f\n', outputSteadyState, capitalSteadyState, consumptionSteadyState);
fprintf('\n')
% We generate the grid of capital
vGridCapital = 0.5*capitalSteadyState:0.00001:1.5*capitalSteadyState;
nGridCapital = length(vGridCapital);
nGridProductivity = length(vProductivity);
%% 3. Required matrices and vectors
mOutput = zeros(nGridCapital,nGridProductivity);
mValueFunction = zeros(nGridCapital,nGridProductivity);
mValueFunctionNew = zeros(nGridCapital,nGridProductivity);
mPolicyFunction = zeros(nGridCapital,nGridProductivity);
expectedValueFunction = zeros(nGridCapital,nGridProductivity);
%% 4. We pre-build output for each point in the grid
mOutput = (vGridCapital'.^aalpha)*vProductivity;
%% 5. Main iteration
maxDifference = 10.0;
tolerance = 0.0000001;
iteration = 0;
while (maxDifference>tolerance)
expectedValueFunction = mValueFunction*mTransition';
for nProductivity = 1:nGridProductivity
% We start from previous choice (monotonicity of policy function)
gridCapitalNextPeriod = 1;
for nCapital = 1:nGridCapital
valueHighSoFar = -1000.0;
capitalChoice = vGridCapital(1);
for nCapitalNextPeriod = gridCapitalNextPeriod:nGridCapital
consumption = mOutput(nCapital,nProductivity)-vGridCapital(nCapitalNextPeriod);
valueProvisional = (1-bbeta)*log(consumption)+bbeta*expectedValueFunction(nCapitalNextPeriod,nProductivity);
if (valueProvisional>valueHighSoFar)
valueHighSoFar = valueProvisional;
capitalChoice = vGridCapital(nCapitalNextPeriod);
gridCapitalNextPeriod = nCapitalNextPeriod;
else
break; % We break when we have achieved the max
end
end
mValueFunctionNew(nCapital,nProductivity) = valueHighSoFar;
mPolicyFunction(nCapital,nProductivity) = capitalChoice;
end
end
maxDifference = max(max(abs(mValueFunctionNew-mValueFunction)));
mValueFunction = mValueFunctionNew;
iteration = iteration+1;
if (mod(iteration,10)==0 || iteration ==1)
fprintf(' Iteration = %d, Sup Diff = %2.8f\n', iteration, maxDifference);
end
end
fprintf(' Iteration = %d, Sup Diff = %2.8f\n', iteration, maxDifference);
fprintf('\n')
fprintf(' My check = %2.6f\n', mPolicyFunction(1000,3));
fprintf('\n')
toc
%% 6. Plotting results
figure(1)
subplot(3,1,1)
plot(vGridCapital,mValueFunction)
xlim([vGridCapital(1) vGridCapital(nGridCapital)])
title('Value Function')
subplot(3,1,2)
plot(vGridCapital,mPolicyFunction)
xlim([vGridCapital(1) vGridCapital(nGridCapital)])
title('Policy Function')
vExactPolicyFunction = aalpha*bbeta.*(vGridCapital.^aalpha);
subplot(3,1,3)
plot((100.*(vExactPolicyFunction'-mPolicyFunction(:,3))./mPolicyFunction(:,3)))
title('Comparison of Exact and Approximated Policy Function')
%set(gcf,'PaperOrientation','landscape','PaperPosition',[-0.9 -0.5 12.75 9])
%print('-dpdf','Figure1.pdf')