-
Notifications
You must be signed in to change notification settings - Fork 131
/
InsideLoop.cpp
51 lines (35 loc) · 1.65 KB
/
InsideLoop.cpp
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
#include <Rcpp.h>
#include <math.h> // power
#include <cmath>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix InsideLoop(NumericVector vGridCapital, NumericMatrix mOutput, NumericMatrix expectedValueFunction){
const double bbeta = 0.95;
const int nGridCapital = 17820, nGridProductivity = 5;
NumericMatrix results(nGridCapital,2*nGridProductivity);
double valueProvisional, valueHighSoFar, consumption, capitalChoice;
int nProductivity, nCapital, nCapitalNextPeriod, gridCapitalNextPeriod;
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
}
results(nCapital,nProductivity) = valueHighSoFar;
results(nCapital,nGridProductivity+nProductivity) = capitalChoice;
}
}
}
return results;
}