def iterate1(n=None, **kwargs): localvars = {} def postIterCallbackFn(nIter, currentVArray, newVArray, optControls, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', optControls[0]) bellman.setInLastIter('opt_r', optControls[1]) v0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for value function bellman.resetIters() bellman.appendIter() bellman.setInLastIter('v', v0_array) bellman.setInLastIter('opt_d', None) bellman.setInLastIter('opt_r', None) params = PonziParams() time1 = time.time() result = bellman.grid_valueIteration([g.grid_M, g.grid_D], v0_array, params, postIterCallbackFn=postIterCallbackFn) (nIter, currentVArray, newVArray, optControls) = result time2 = time.time() nIters = localvars[0] print("total time: %f, avg time: %f" % (time2-time1, (time2-time1)/nIters))
def iterate2(n=None, **kwargs): localvars = {} def postIterCallbackFn(nIter, newVArray, currentPolicyArrayList, greedyPolicyList, stoppingResult): (stoppingDecision, diff) = stoppingResult print("iter %d, diff %f" % (nIter, diff)) localvars[0] = nIter bellman.appendIter() bellman.setInLastIter('v', newVArray) bellman.setInLastIter('opt_d', currentPolicyArrayList[0]) bellman.setInLastIter('opt_r', currentPolicyArrayList[1]) v0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for value function d0_array = scipy.zeros((len(g.grid_M), len(g.grid_D))); # initial guess for policy function for d r0_array = scipy.ones((len(g.grid_M), len(g.grid_D))); # initial guess for policy function for r bellman.resetIters() bellman.appendIter() bellman.setInLastIter('v', v0_array) bellman.setInLastIter('opt_d', None) bellman.setInLastIter('opt_r', None) params = PonziParams() time1 = time.time() result = bellman.grid_policyIteration([g.grid_M, g.grid_D], [d0_array, r0_array], v0_array, params, postIterCallbackFn=postIterCallbackFn, parallel=True) (nIter, currentVArray, currentPolicyArrayList, greedyPolicyList) = result time2 = time.time() nIters = localvars[0] print("total time: %f, avg time: %f" % (time2-time1, (time2-time1)/nIters))
itemList.append(("%s=%f" % (paramName, paramVal), imgList)) plt.ion() page = markup.page() page.init(title=title) page.br( ) for item in itemList: (caption, imgList) = item page.p(caption) for imageName in imgList: page.img(src=imageName) filename = os.path.join(outputDirname, "index.html") f = open(filename, 'w') f.write(str(page)) f.close() ######################################################################3 # testing and generating the output. # # init global parameters, c library # default values are: beta=0.9, m=0.1, bankruptcyPenalty=0, zLow=1.0, zHigh=1.2, pHigh=0.95 #g.setGlobalParams(True) g.setGlobalParams(True, beta=0.8, zHigh=1.2, zLow=0.8, pHigh=0.9) # zLow's effect... ? # beta's effect is to make blue "vertical", i.e. bank becomes impatient, so would rather take more out of M and go to blue, rather than have a higher M and go to green bellman.resetIters()