forked from rncarpio/bellman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ponzi_params.cpp
342 lines (305 loc) · 10.5 KB
/
ponzi_params.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// 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 ponzi problem
// global parameters
typedef struct globalParams {
PyFileObject *pOutputFileObj;
FILE *pOutputFile;
double theta;
double beta;
double gamma;
PyArrayObject *pGrid1, *pGrid2;
PyArrayObjectPtr_array gridPtrArray; // for arbitrary dimensions, use a vector. must update the ref count when reassigning!
PyArrayObject *pZVals, *pZProbs;
ddFn *pU;
ddFn2 *pFS;
double depositorSlope;
double bankruptcyPenalty;
} _globalParams;
typedef struct eu_params0 {
double M, D;
PyArrayObject *pW;
bool bPrint;
} _eu_params;
class eu_params {
public:
eu_params(double M, double D, PyArrayObject *w, int bPrint=0)
{
m_M = M;
m_D = D;
m_stateVars.resize(2);
m_stateVars[0] = M;
m_stateVars[1] = D;
m_pW = w;
m_bPrint = (bPrint != 0);
}
eu_params(PyObject *pArgList, PyArrayObject *w, int bPrint=0) {
m_pW = w;
int result = getDoublesFromPySequence(pArgList, m_stateVars);
m_M = m_stateVars[0];
m_D = m_stateVars[1];
assert(result >= 0);
m_bPrint = (bPrint != 0);
}
double m_M, m_D;
DoubleArray m_stateVars;
PyArrayObject *m_pW;
bool m_bPrint;
};
struct globalParams g_Params;
// fraction of income deposited into bank
double fs(double r) {
return 1.0 - exp(1.0 - r);
}
// f only
// depositors have linear mean-SD preferences, i.e. U(c) = E(c) - m*SD(c)
// k = d + D - M
// assume Z has 2 possible values
// r is gross, i.e. > 1
double f(double k, double r, void* pArg) {
assert(ARRAYLEN1D(g_Params.pZVals) == 2);
double zLow = *ARRAYPTR1D(g_Params.pZVals, 0);
double zHigh = *ARRAYPTR1D(g_Params.pZVals, 1);
double pHigh = *ARRAYPTR1D(g_Params.pZProbs, 1);
assert(zLow < zHigh);
if (k > zHigh) {
return 0.0;
} else if (k <= zHigh && k > zLow) {
// slope of feasible region line
// in this case, bank survives if zHigh occurs, so probability = pHigh
double slope = (r*pHigh - 1.0)/(r*sqrt(pHigh * (1.0-pHigh)));
if (g_Params.depositorSlope > slope) {
return 0.0;
} else {
return 1.0;
}
} else if (k <= zLow && k > 0.0) {
return 1.0;
} else {
// k < 0
return 1.0;
}
}
PyObject *testf(PyObject *self, PyObject *args) {
double k, r;
if (!PyArg_ParseTuple(args, "dd:testf", &k, &r)) {
return NULL;
}
double result = f(k,r, NULL);
return Py_BuildValue("d", result);
}
// default values. can be overridden later
void initGlobalParams() {
g_Params.pOutputFileObj = NULL;
g_Params.pOutputFile = PySys_GetFile("stdout", stdout);
g_Params.beta = 0.9;
g_Params.theta = 0.5;
g_Params.gamma = 2.0;
g_Params.pGrid1 = g_Params.pGrid2 = NULL;
g_Params.pZVals = g_Params.pZProbs = NULL;
g_Params.pU = &U_linear;
g_Params.pFS = &f;
g_Params.depositorSlope = 1.0;
g_Params.bankruptcyPenalty = 0.0;
}
PyObject* setGlobalParams(PyObject *self, PyObject *args) {
double theta, beta, gamma;
const char* pcUFnName = NULL;
PyArrayObject *pGrid1=NULL, *pGrid2=NULL;
PyArrayObject *pZVals=NULL, *pZProbs=NULL;
double depositorSlope, bankruptcyPenalty;
PyObject *pGridList = NULL;
uint i;
PyArrayPtrPtr src[] = {&pGrid1, &pGrid2, &pZVals, &pZProbs};
PyArrayPtrPtr dest[] = {&g_Params.pGrid1, &g_Params.pGrid2,
&g_Params.pZVals, &g_Params.pZProbs};
if (!PyArg_ParseTuple(args, "dddsO!O!O!O!ddO:setGlobalParams", &theta, &beta, &gamma, &pcUFnName,
&PyArray_Type, &pGrid1, &PyArray_Type, &pGrid2,
&PyArray_Type, &pZVals, &PyArray_Type, &pZProbs,
&depositorSlope, &bankruptcyPenalty,
&pGridList)) {
return NULL;
}
g_Params.beta = beta;
g_Params.theta = theta;
g_Params.gamma = gamma;
// set pointer values
for (i=0; i<CARRAYLEN(src); i++) {
if (*(dest[i]) != NULL) {
Py_DECREF(*(dest[i]));
}
Py_INCREF(*(src[i]));
*(dest[i]) = *(src[i]);
}
// for multidimensional grids, copy from list to array, take care of ref counts
int nGrids = PyObject_Length(pGridList);
PyArrayObjectPtr_array tempArray(nGrids);
if (nGrids < 0) {
PyErr_SetString(PyExc_ValueError, "gridList has length < 0");
return NULL;
}
if (getPyArrayFromPySequence(pGridList, tempArray) <= 0) {
PyErr_SetString(PyExc_ValueError, "could not read items from gridList");
return NULL;
}
// decr ref counts for old object
for (i=0; i<g_Params.gridPtrArray.size(); i++) {
Py_DECREF(g_Params.gridPtrArray[i]);
}
g_Params.gridPtrArray = tempArray;
for (i=0; i<g_Params.gridPtrArray.size(); i++) {
Py_INCREF(g_Params.gridPtrArray[i]);
}
char *fnNames[] = {"crra", "exponential", "linear"};
ddFn *u_functions[] = {&U_crra, &U_exponential, &U_linear};
// set utility function
bool bFoundMatch = false;
for (uint i=0; i<CARRAYLEN(fnNames); i++) {
if (strcmp(pcUFnName, fnNames[i]) == 0) {
g_Params.pU = u_functions[i];
DebugMsg("setGlobalParams: setting utility to %s\n", pcUFnName);
bFoundMatch = true;
}
}
char pcTemp[1024];
sprintf(pcTemp, "unknown utility fn name: %s", pcUFnName);
if (bFoundMatch == false) {
PyErr_SetString(PyExc_ValueError, pcTemp);
return NULL;
}
// check that zProbs sum up to 1
double sum = 0;
for (uint i=0; i<ARRAYLEN1D(pZProbs); i++) {
sum += * ARRAYPTR1D(pZProbs, i);
}
if (sum != 1.0) {
char pcTemp[1024];
//printf("diff: %.30f", sum - 1.0);
std::string sErr("z probs don't add up to 1: ");
sprintf(pcTemp, "%.30f = ", sum);
sErr += pcTemp;
for (uint i=0; i<ARRAYLEN1D(pZProbs)-1; i++) {
sprintf(pcTemp, "%f +", * ARRAYPTR1D(pZProbs, i));
sErr += pcTemp;
}
sprintf(pcTemp, "%f", * ARRAYPTR1D(pZProbs, ARRAYLEN1D(pZProbs)-1));
sErr += pcTemp;
PyErr_SetString(PyExc_ValueError, sErr.c_str());
return NULL;
}
g_Params.depositorSlope = depositorSlope;
g_Params.bankruptcyPenalty = bankruptcyPenalty;
return Py_BuildValue("i", 1);
}
// w_array is a 3D array
// grid_M, grid_D, grid_N are 1d arrays
// M, D, N are the state values
// d, r are the controls
// z_vals, z_probs are shock distribution values
// fs is a function that takes r, returns the fraction of income invested in bank (a double)
double expected_next_v
(PyArrayObject *pWArray,
PyArrayObject *pMGrid, PyArrayObject *pDGrid,
double M, double D, double d, double r, double bankruptcyPenalty,
PyArrayObject *pZVals, PyArrayObject *pZProbs,
ddFn2 *pFSFn, bool bPrint=false) {
int zi, zlen;
double *pZ, fs, probZ, nextM, nextD;
double sum;
assert(pZVals->nd == pZProbs->nd);
assert(pZVals->dimensions[0] == pZProbs->dimensions[0]);
zlen = pZVals->dimensions[0];
// calculate expectation -> for each possible shock z...
if (bPrint) {
DebugMsg("expected_next_v M=%f D=%f d=%f r=%f\n", M,D,d,r);
}
sum = 0.0;
for (zi=0; zi<zlen; zi++) {
pZ = (double*) ARRAYPTR1D(pZVals, zi);
probZ = * (double*) ARRAYPTR1D(pZProbs, zi);
fs = (*pFSFn)(d + D - M, r, NULL);
nextM = (M + fs * (*pZ) - D - d) / (*pZ);
nextD = r * fs;
if (nextM <= 0.0) {
//sum += 0.0;
// bankruptcyPenalty should be a negative number.
double incr = probZ * (*pZ) * bankruptcyPenalty * (-nextM); // multiply by *pZ because we changed the problem to use per-customer (divided by N_t) variables
//double incr = probZ * bankruptcyPenalty * (-nextM); // multiply by *pZ because we changed the problem to use per-customer (divided by N_t) variables
sum += incr;
if (bPrint) {
DebugMsg(" probZ=%f z=%f nextM=%f nextD=%f: +%f\n", probZ, *pZ, nextM, nextD, incr);
}
} else {
double interpVal = interp2d_grid(pMGrid, pDGrid, pWArray, nextM, nextD); // multiply by *pZ because we changed the problem to use per-customer (divided by N_t) variables
double incr = probZ * (*pZ) * interpVal;
//double incr = probZ * interpVal;
sum += incr;
if (bPrint) {
DebugMsg(" probZ=%f z=%f nextM=%f nextD=%f: w=%f +%f\n", probZ, *pZ, nextM, nextD, interpVal, incr);
}
}
}
return sum;
}
// args points to double[3], containing pW,M,D,N
double calc_exp_util_orig(double d, double r, void *pArgs) {
eu_params *pParams = (eu_params *) pArgs;
double M = pParams->m_M;
double D = pParams->m_D;
PyArrayObject *pW = pParams->m_pW;
bool bPrint = pParams->m_bPrint;
double ev = expected_next_v(pW, g_Params.pGrid1, g_Params.pGrid2,
M, D, d, r, g_Params.bankruptcyPenalty, g_Params.pZVals, g_Params.pZProbs, g_Params.pFS, bPrint);
double result = (*g_Params.pU)(d) + g_Params.beta * ev;
if (bPrint) {
DebugMsg("calc_exp_util: M=%f D=%f d=%f r=%f\n", M, D, d, r);
DebugMsg(" %f + %f * %f = %f\n", (*g_Params.pU)(d), g_Params.beta, ev, result);
}
return result;
}
double calc_exp_util(double d, double r, void *pArgs) {
eu_params *pParams = (eu_params *) pArgs;
double M = pParams->m_M;
double D = pParams->m_D;
PyArrayObject *pW = pParams->m_pW;
bool bPrint = pParams->m_bPrint;
double ev = expected_next_v(pW, g_Params.pGrid1, g_Params.pGrid2,
M, D, d, r, g_Params.bankruptcyPenalty, g_Params.pZVals, g_Params.pZProbs, g_Params.pFS, bPrint);
double result = (*g_Params.pU)(d) + g_Params.beta * ev;
if (bPrint) {
DebugMsg("calc_exp_util: M=%f D=%f d=%f r=%f\n", M, D, d, r);
DebugMsg(" %f + %f * %f = %f\n", (*g_Params.pU)(d), g_Params.beta, ev, result);
}
return result;
}
double calc_exp_util2(DoubleArray const &argArray, void *pArgs) {
return calc_exp_util(argArray[0], argArray[1], pArgs);
}
PyObject *expUtil(PyObject *self, PyObject *args) {
double M, D, d, r;
PyArrayObject *pW=NULL;
int bPrint = 0;
if (!PyArg_ParseTuple(args, "O!dddd|i:expUtil",
&PyArray_Type, &pW, &M, &D, &d, &r, &bPrint)) {
return NULL;
}
if (pW->nd != 2 ||
pW->dimensions[0] != g_Params.pGrid1->dimensions[0] ||
pW->dimensions[1] != g_Params.pGrid2->dimensions[0]) {
PyErr_SetString(PyExc_ValueError, "w dimensions don't match grid");
return NULL;
}
eu_params params(M, D, pW);
params.m_bPrint = (bPrint != 0);
double result = calc_exp_util(d, r, (void*) ¶ms);
return Py_BuildValue("d", result);
}