-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbankProblem.cpp
304 lines (260 loc) · 10.8 KB
/
bankProblem.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Copyright (c) 2011 Ronaldo Carpio
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. The authors make no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//
// data structures specific to the bank problem
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <float.h>
#include <iostream>
#include <boost/python.hpp>
#include <boost/python/dict.hpp>
#include "bankProblem.h"
#include "maximizer.h"
#include "debugMsg.h"
namespace bpl = boost::python;
using namespace pyublas;
BankParams3::BankParams3(double beta, double rFast, double rSlow,
DoublePyArray const &SlowOutFrac,
DoublePyArray const &FastOutFrac, DoublePyArray const &FastInFrac,
DoublePyArray const &ProbSpace, DoublePyArray const &BankruptcyPenalty) {
m_beta = beta;
m_pPrevIter = NULL;
m_rFast = rFast;
m_rSlow = rSlow;
// check random variable probabilities sum to 1.0
if (std::accumulate(ProbSpace.begin(), ProbSpace.end(), 0.0) != 1.0) {
#ifdef FP_STRICT
throw std::invalid_argument("ProbSpace doesn't sum to 1.0");
#else
printf("warning: ProbSpace doesn't sum to 1.0\n");
#endif //FP_STRICT
}
// check array sizes match
if (ProbSpace.size() != SlowOutFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
if (ProbSpace.size() != FastOutFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
if (ProbSpace.size() != FastInFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
m_SlowOutFrac = SlowOutFrac;
m_FastOutFrac = FastOutFrac;
m_FastInFrac = FastInFrac;
m_ProbSpace = ProbSpace;
if (BankruptcyPenalty.size() != 3) { throw std::invalid_argument("bankruptcy penalty must have size=3"); }
for (int i=0; i<3; i++) {
m_BankruptcyPenalty[i] = BankruptcyPenalty.sub(i);
}
}
void BankParams3::setPrevIteration(bpl::list const &stateGridList, DoublePyArray const &WArray) {
m_StateGrid1 = bpl::extract<DoublePyArray>(stateGridList[0]);
m_StateGrid2 = bpl::extract<DoublePyArray>(stateGridList[1]);
m_pStateGrid1 = (PyArrayObject const*) m_StateGrid1.data().handle().get();
m_pStateGrid2 = (PyArrayObject const*) m_StateGrid2.data().handle().get();
m_PrevIterArray = WArray;
m_pPrevIter = (PyArrayObject const*) m_PrevIterArray.data().handle().get();
m_pPrevIterInterp.reset(new Interp2D(m_StateGrid1, m_StateGrid2, DoublePyMatrix(m_StateGrid1.size(), m_StateGrid2.size(), WArray)));
}
double BankParams3::objectiveFunction(DoubleVector const &controlVars) const {
double d = controlVars[0];
double slowInFrac = controlVars[1];
double EV = calc_EV(d, slowInFrac);
double result = d + m_beta * EV;
return result;
}
// M is actually M/F
// S is actually S/F
// M, S, F are all positive, but F is actually a short quantity, so an outflow of S is positive for M, while an outflow
// of F is negative
double BankParams3::calc_EV(double d, double slowInFrac, DoubleVector *pNextM, DoubleVector *pNextS) const {
double M = m_M;
double S = m_S;
assert(M >= 0.0);
double rFast = m_rFast;
double rSlow = m_rSlow;
double V;
double sum = 0.0;
DoublePyArray::const_iterator i1, i2, i3, i5;
DoubleVector::iterator i_nextM, i_nextS;
// save the computed values for next period's state, if necessary
if (pNextM != NULL) {
pNextM->resize(m_ProbSpace.size());
i_nextM = pNextM->begin();
}
if (pNextS != NULL) {
pNextS->resize(m_ProbSpace.size());
i_nextS = pNextS->begin();
}
for (i1=m_FastOutFrac.begin(), i2=m_FastInFrac.begin(), i3=m_SlowOutFrac.begin(), i5=m_ProbSpace.begin();
i5 != m_ProbSpace.end(); i1++, i2++, i3++, i5++) {
double fast_out_frac = *i1;
double fast_in_frac = *i2;
double slow_out_frac = *i3;
double slow_in_frac = slowInFrac;
double prob_space = *i5;
double fast_growth = (1.0 + fast_in_frac - fast_out_frac) * (1.0 + rFast);
double nextM = (M - d + slow_out_frac*S - slow_in_frac*S - fast_out_frac + fast_in_frac)/fast_growth;
double nextS = (1.0 + slow_in_frac - slow_out_frac) * S * (1.0 + rSlow) / fast_growth;
if (nextM <= 0.0) {
//V = 0.0;
V = (m_BankruptcyPenalty[0] * nextM) + (m_BankruptcyPenalty[1] * nextS) + m_BankruptcyPenalty[2];
nextS = -1.0;
} else {
//V = interp2d_grid(m_pStateGrid1, m_pStateGrid2, m_pPrevIter, nextM, nextS);
V = m_pPrevIterInterp->interp(nextM, nextS);
}
sum += prob_space * fast_growth * V; // need to multiply by fast_growth since state variables are divided by F
if (pNextM != NULL) {
(*i_nextM) = nextM;
i_nextM++;
}
if (pNextS != NULL) {
(*i_nextS) = nextS;
i_nextS++;
}
}
return sum;
}
bpl::tuple BankParams3::calc_EV_wrap(double d, double slowInFrac) {
DoubleVector nextM, nextS;
double EV = calc_EV(d, slowInFrac, &nextM, &nextS);
return bpl::make_tuple(EV, nextM, nextS);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// BankParams4 begin
////////////////////////////////////////////////////////////////////////////////////////////////
BankParams4::BankParams4(double beta, double rFast, double rSlow,
DoublePyArray const &SlowOutFrac,
DoublePyArray const &FastOutFrac, DoublePyArray const &FastInFrac,
DoublePyArray const &ProbSpace, DoublePyArray const &BankruptcyPenalty, double PopGrowth) {
m_beta = beta;
m_pPrevIter = NULL;
m_rFast = rFast;
m_rSlow = rSlow;
m_PopGrowth = PopGrowth;
// check random variable probabilities sum to 1.0
if (std::accumulate(ProbSpace.begin(), ProbSpace.end(), 0.0) != 1.0) {
#ifdef FP_STRICT
throw std::invalid_argument("ProbSpace doesn't sum to 1.0");
#else
printf("warning: ProbSpace doesn't sum to 1.0\n");
#endif //FP_STRICT
}
// check array sizes match
if (ProbSpace.size() != SlowOutFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
if (ProbSpace.size() != FastOutFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
if (ProbSpace.size() != FastInFrac.size()) { throw std::invalid_argument("array sizes don't match"); }
m_SlowOutFrac = SlowOutFrac;
m_FastOutFrac = FastOutFrac;
m_FastInFrac = FastInFrac;
m_ProbSpace = ProbSpace;
if (BankruptcyPenalty.size() != 3) { throw std::invalid_argument("bankruptcy penalty must have size=3"); }
for (int i=0; i<3; i++) {
m_BankruptcyPenalty[i] = BankruptcyPenalty.sub(i);
}
}
void BankParams4::setPrevIteration(bpl::list const &stateGridList, DoublePyArray const &WArray) {
m_StateGrid1 = bpl::extract<DoublePyArray>(stateGridList[0]);
m_StateGrid2 = bpl::extract<DoublePyArray>(stateGridList[1]);
m_StateGrid3 = bpl::extract<DoublePyArray>(stateGridList[2]);
m_pStateGrid1 = (PyArrayObject const*) m_StateGrid1.data().handle().get();
m_pStateGrid2 = (PyArrayObject const*) m_StateGrid2.data().handle().get();
m_pStateGrid3 = (PyArrayObject const*) m_StateGrid3.data().handle().get();
m_PrevIterArray = WArray;
m_pPrevIter = (PyArrayObject const*) m_PrevIterArray.data().handle().get();
//m_pPrevIterInterp.reset(new Interp2D(m_StateGrid1, m_StateGrid2, DoublePyMatrix(m_StateGrid1.size(), m_StateGrid2.size(), WArray)));
}
double BankParams4::objectiveFunction(DoubleVector const &controlVars) const {
double d = controlVars[0];
double slowInFrac = controlVars[1];
double EV = calc_EV(d, slowInFrac);
double result = d + m_beta * EV;
return result;
}
// M is actually M/F
// S is actually S/F
// M, S, F are all positive, but F is actually a short quantity, so an outflow of S is positive for M, while an outflow
// of F is negative
double BankParams4::calc_EV(double d, double slowInFrac, DoubleVector *pNextM, DoubleVector *pNextS, DoubleVector *pNextP) const {
double M = m_M;
double S = m_S;
double P = m_P;
assert(M >= 0.0);
double rFast = m_rFast;
double rSlow = m_rSlow;
double pop_growth = m_PopGrowth;
double V;
double sum = 0.0;
DoublePyArray::const_iterator i1, i2, i3, i5;
DoubleVector::iterator i_nextM, i_nextS, i_nextP;
// save the computed values for next period's state, if necessary
if (pNextM != NULL) {
pNextM->resize(m_ProbSpace.size());
i_nextM = pNextM->begin();
}
if (pNextS != NULL) {
pNextS->resize(m_ProbSpace.size());
i_nextS = pNextS->begin();
}
if (pNextP != NULL) {
pNextP->resize(m_ProbSpace.size());
i_nextP = pNextP->begin();
}
for (i1=m_FastOutFrac.begin(), i2=m_FastInFrac.begin(), i3=m_SlowOutFrac.begin(), i5=m_ProbSpace.begin();
i5 != m_ProbSpace.end(); i1++, i2++, i3++, i5++) {
double fast_out_frac = *i1;
double fast_in_frac = *i2;
double slow_out_frac = *i3;
double slow_in_frac = slowInFrac;
double prob_space = *i5;
double fast_growth = (1.0 + fast_in_frac*P - fast_out_frac) * (1.0 + rFast);
double nextM = (M - d + slow_out_frac*S - slow_in_frac*S - fast_out_frac + fast_in_frac*P)/fast_growth;
double nextS = (1.0 + slow_in_frac - slow_out_frac) * S * (1.0 + rSlow) / fast_growth;
double nextP = (pop_growth * P) / fast_growth;
if (nextM <= 0.0) {
//V = 0.0;
V = (m_BankruptcyPenalty[0] * nextM) + (m_BankruptcyPenalty[1] * nextS) + m_BankruptcyPenalty[2];
nextS = -1.0;
} else {
V = interp3d_grid(m_pStateGrid1, m_pStateGrid2, m_pStateGrid3, m_pPrevIter, nextM, nextS, nextP);
//V = m_pPrevIterInterp->interp(nextM, nextS);
}
sum += prob_space * fast_growth * V; // need to multiply by fast_growth since state variables are divided by F
if (pNextM != NULL) {
(*i_nextM) = nextM;
i_nextM++;
}
if (pNextS != NULL) {
(*i_nextS) = nextS;
i_nextS++;
}
if (pNextP != NULL) {
(*i_nextP) = nextP;
i_nextP++;
}
}
return sum;
}
bpl::tuple BankParams4::calc_EV_wrap(double d, double slowInFrac) {
DoubleVector nextM, nextS, nextP;
double EV = calc_EV(d, slowInFrac, &nextM, &nextS, &nextP);
return bpl::make_tuple(EV, nextM, nextS, nextP);
}
BOOST_PYTHON_MODULE(_bankProblem)
{
bpl::class_<BankParams3, bpl::bases<BellmanParams>>("BankParams3", bpl::init<double, double, double,
DoublePyArray, DoublePyArray, DoublePyArray,
DoublePyArray, DoublePyArray>())
.def("calc_EV", &BankParams3::calc_EV_wrap)
;
bpl::class_<BankParams4, bpl::bases<BellmanParams>>("BankParams4", bpl::init<double, double, double,
DoublePyArray, DoublePyArray, DoublePyArray,
DoublePyArray, DoublePyArray, double>())
.def("calc_EV", &BankParams4::calc_EV_wrap)
;
}