-
Notifications
You must be signed in to change notification settings - Fork 131
/
RBC_C2.c
272 lines (221 loc) · 9.69 KB
/
RBC_C2.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//============================================================================
// Name : RBC2_C.c
// Description : Basic RBC model with full depreciation
// Date : August 23, 2014
// Adapted from C++ to C by Luke Hartigan, [email protected]
//============================================================================
#include <stdio.h>
#include <stdlib.h> /* malloc() and free() */
#include <math.h> /* log(), fabs() */
// The next few lines are just for counting time
// Windows
#ifdef _WIN32
#include <Windows.h>
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
// Posix/Linux
#else
#include <time.h>
double get_cpu_time(){
return (double)clock() / CLOCKS_PER_SEC;
}
#endif
int main(void) {
double cpu0 = get_cpu_time();
///////////////////////////////////////////////////////////////////////////////////////////
// 1. Calibration
///////////////////////////////////////////////////////////////////////////////////////////
const double aalpha = 0.33333333333; // Elasticity of output w.r.t. capital
const double bbeta = 0.95; // Discount factor;
// Productivity values
double vProductivity[5] = {0.9792, 0.9896, 1.0000, 1.0106, 1.0212};
// Transition matrix
double mTransition[5][5] = {
{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
///////////////////////////////////////////////////////////////////////////////////////////
double capitalSteadyState = pow(aalpha*bbeta, 1 / (1 - aalpha));
double outputSteadyState = pow(capitalSteadyState, aalpha);
double consumptionSteadyState = outputSteadyState - capitalSteadyState;
printf("Output = %g, Capital = %g, Consumption = %g\n", outputSteadyState, capitalSteadyState, consumptionSteadyState);
printf("\n");
// We generate the grid of capital
int nCapital, nCapitalNextPeriod, gridCapitalNextPeriod, nProductivity, nProductivityNextPeriod;
const int nGridCapital = 17820, nGridProductivity = 5;
double* vGridCapital = malloc(nGridCapital * sizeof *vGridCapital);
if (vGridCapital == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
for (nCapital = 0; nCapital < nGridCapital; ++nCapital){
vGridCapital[nCapital] = 0.5*capitalSteadyState + 0.00001*nCapital;
}
// 3. Required matrices and vectors
/* mOutput */
/* Allocate memory for the row pointers */
double** mOutput = malloc(nGridCapital * sizeof *mOutput);
if (mOutput == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
mOutput[0] = malloc(nGridCapital * nGridProductivity * sizeof *mOutput[0]);
if (mOutput[0] == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* mValueFunction */
/* Allocate memory for the row pointers */
double** mValueFunction = malloc(nGridCapital * sizeof *mValueFunction);
if (mValueFunction == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* Allocate memory for the whole array */
mValueFunction[0] = malloc(nGridCapital * nGridProductivity * sizeof *mValueFunction[0]);
if (mValueFunction[0] == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* mValueFunctionNew */
/* Allocate memory for the row pointers */
double** mValueFunctionNew = malloc(nGridCapital * sizeof *mValueFunctionNew);
if (mValueFunctionNew == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* Allocate memory for the whole array */
mValueFunctionNew[0] = malloc(nGridCapital * nGridProductivity * sizeof *mValueFunctionNew[0]);
if (mValueFunctionNew[0] == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* mPolicyFunction */
/* Allocate memory for the row pointers */
double** mPolicyFunction = malloc(nGridCapital * sizeof *mPolicyFunction);
if (mPolicyFunction == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* Allocate memory for the whole array */
mPolicyFunction[0] = malloc(nGridCapital * nGridProductivity * sizeof *mPolicyFunction[0]);
if (mPolicyFunction[0] == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* expectedValueFunction */
/* Allocate memory for the row pointers */
double** expectedValueFunction = malloc(nGridCapital * sizeof *expectedValueFunction);
if (expectedValueFunction == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* Allocate memory for the whole array */
expectedValueFunction[0] = malloc(nGridCapital * nGridProductivity * sizeof *expectedValueFunction[0]);
if (expectedValueFunction[0] == NULL) {
fprintf(stderr,"Error allocating memory\n");
exit(1);
}
/* Point the nGridCapital pointers to array elements with stride nGridProductivity */
int k;
for (k = 0; k < nGridCapital; ++k) {
mOutput[k] = mOutput[0] + (k * nGridProductivity);
mValueFunction[k] = mValueFunction[0] + (k * nGridProductivity);
mValueFunctionNew[k] = mValueFunctionNew[0] + (k * nGridProductivity);
mPolicyFunction[k] = mPolicyFunction[0] + (k * nGridProductivity);
expectedValueFunction[k] = expectedValueFunction[0] + (k * nGridProductivity);
}
// 4. We pre-build output for each point in the grid
for (nProductivity = 0; nProductivity < nGridProductivity; ++nProductivity){
for (nCapital = 0; nCapital < nGridCapital; ++nCapital){
mOutput[nCapital][nProductivity] = vProductivity[nProductivity]*pow(vGridCapital[nCapital], aalpha);
}
}
// 5. Main iteration
double maxDifference = 10.0, diff, diffHighSoFar;
double tolerance = 1.0E-07;
double valueHighSoFar, valueProvisional, consumption, capitalChoice;
int iteration = 0;
while (maxDifference > tolerance){
for (nProductivity = 0; nProductivity < nGridProductivity; ++nProductivity){
for (nCapital = 0; nCapital < nGridCapital; ++nCapital){
expectedValueFunction[nCapital][nProductivity] = 0.0;
for (nProductivityNextPeriod = 0; nProductivityNextPeriod < nGridProductivity; ++nProductivityNextPeriod){
expectedValueFunction[nCapital][nProductivity] += mTransition[nProductivity][nProductivityNextPeriod]*
mValueFunction[nCapital][nProductivityNextPeriod];
}
}
}
for (nProductivity = 0; nProductivity < nGridProductivity; ++nProductivity){
// We start from previous choice (monotonicity of policy function)
gridCapitalNextPeriod = 0;
for (nCapital = 0; nCapital < nGridCapital; ++nCapital){
valueHighSoFar = -100000.0;
capitalChoice = vGridCapital[0];
for (nCapitalNextPeriod = gridCapitalNextPeriod; nCapitalNextPeriod < nGridCapital; ++nCapitalNextPeriod){
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
}
mValueFunctionNew[nCapital][nProductivity] = valueHighSoFar;
mPolicyFunction[nCapital][nProductivity] = capitalChoice;
}
}
}
diffHighSoFar = -100000.0;
for (nProductivity = 0; nProductivity < nGridProductivity; ++nProductivity){
for (nCapital = 0; nCapital < nGridCapital; ++nCapital){
diff = fabs(mValueFunction[nCapital][nProductivity] - mValueFunctionNew[nCapital][nProductivity]);
if (diff > diffHighSoFar){
diffHighSoFar = diff;
}
mValueFunction[nCapital][nProductivity] = mValueFunctionNew[nCapital][nProductivity];
}
}
maxDifference = diffHighSoFar;
iteration++;
if (iteration % 10 == 0 || iteration == 1){
printf("Iteration = %d, Sup Diff = %g\n", iteration, maxDifference);
}
}
printf("Iteration = %d, Sup Diff = %g\n", iteration, maxDifference);
printf("\n");
printf("My check = %g\n", mPolicyFunction[999][2]);
printf("\n");
double cpu1 = get_cpu_time();
printf("Elapsed time is = %g\n", cpu1 - cpu0);
printf("\n");
/* Free memory */
free(mOutput[0]);
free(mOutput);
free(mValueFunction[0]);
free(mValueFunction);
free(mValueFunctionNew[0]);
free(mValueFunctionNew);
free(mPolicyFunction[0]);
free(mPolicyFunction);
free(expectedValueFunction[0]);
free(expectedValueFunction);
return 0;
}