-
Notifications
You must be signed in to change notification settings - Fork 131
/
RBC_Mathematica_Imperative.nb
113 lines (76 loc) · 3.18 KB
/
RBC_Mathematica_Imperative.nb
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
Clear["Global`*"]
Timing[
(* 1. Calibration*)
\[Alpha] = 0.333333333333;
\[Beta] = 0.95;
(* 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*)
Subscript[k, ss] = (\[Alpha]*\[Beta])^(1/(1 - \[Alpha]));
Subscript[y, ss] = Subscript[k, ss]^\[Alpha];
Subscript[c, ss] = Subscript[y, ss] - Subscript[k, ss];
(* We generate the grid of capital*)
vGridCapital =
Range[0.089099143696263, 1.5*Subscript[k, ss], 0.00001];
nGridCapital = Length[vGridCapital];
nGridProductivity = Length[vProductivity];
(*3. Required matrices and vectors*)
mOutput = ConstantArray[0, {nGridCapital, nGridProductivity}];
mValueFunction =
ConstantArray[0, {nGridCapital, nGridProductivity}];
mValueFunctionNew =
ConstantArray[0, {nGridCapital, nGridProductivity}];
mPolicyFunction =
ConstantArray[0, {nGridCapital, nGridProductivity}];
expectedValueFunction =
ConstantArray[0, {nGridCapital, nGridProductivity}];
(*4. We pre-build output for each point in the grid*)
mOutput = Transpose[{vGridCapital^\[Alpha]}].{vProductivity};
(* 5. Main iteration*)
maxDifference = 10.0;
tolerance = 0.0000001;
iteration = 0;
While [maxDifference > tolerance,
expectedValueFunction = mValueFunction.Transpose[mTransition];
For[nProductivity = 1, nProductivity <= nGridProductivity,
nProductivity++,
(*We start from previous choice (monotonicity of policy function)*)
gridCapitalNextPeriod = 1;
For [nCapital = 1, nCapital <= nGridCapital, nCapital++,
valueHighSoFar = -1000.0;
capitalChoice = vGridCapital[[1]];
For [nCapitalNextPeriod = gridCapitalNextPeriod,
nCapitalNextPeriod <= nGridCapital, nCapitalNextPeriod++,
consumption =
mOutput[[nCapital, nProductivity]] -
vGridCapital[[nCapitalNextPeriod]];
valueProvisional = (1 - \[Beta])*Log[consumption] + \[Beta]*
expectedValueFunction[[nCapitalNextPeriod, nProductivity]];
If[valueProvisional > valueHighSoFar,
valueHighSoFar = valueProvisional;
capitalChoice = vGridCapital[[nCapitalNextPeriod]];
gridCapitalNextPeriod = nCapitalNextPeriod;,
Break[] (*We break when we have achieved the max*)
];
];
mValueFunctionNew[[nCapital, nProductivity]] = valueHighSoFar;
mPolicyFunction[[nCapital, nProductivity]] = capitalChoice;
];
];
maxDifference = Max[Abs[mValueFunctionNew - mValueFunction]];
mValueFunction = mValueFunctionNew;
iteration = iteration + 1;
If[Mod[iteration, 10] == 0 || iteration == 1,
Print[StringForm["Iteration = ``, Sup Diff = ``", iteration,
maxDifference]]]
];
Print[StringForm["Iteration = ``, Sup Diff = ``", iteration,
maxDifference]];
Print[StringForm["My check = ``", mPolicyFunction[[1000, 3]]]];
]