def main(): #valList = [] start_time = time.clock() output = dict() output[0] = [] for shock in range(5): paramL['curr_theta'] = paramL['theta_list'][shock] valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol.sparse_grid(paramL) valold = TasmanianSG.TasmanianSparseGrid() valold = valnew output[0].append(valold) valnew.write("valnew_1." + str(paramL['numstart']) + ".s{}".format(shock) + ".txt") #write file to disk for restart for i in range(paramL['numstart'] + 1, paramL['numits'] + 1): output[i] = list() for shock in range(5): paramL['curr_theta'] = paramL['theta_list'][shock] valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol_iter.sparse_grid_iter(paramL, list(output[i - 1])) valold = TasmanianSG.TasmanianSparseGrid() valold = valnew output[i].append(valold) valnew.write("valnew_1." + str(i) + ".s{}".format(shock) + ".txt") avg_err = post.ls_error(paramL) end_time = time.clock() - start_time print('END') print(end_time) return output
def main(): start_time = time.clock() paramL['curr_theta'] = paramL['theta_list'][rank] valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol.sparse_grid(paramL) valold = TasmanianSG.TasmanianSparseGrid() valold = valnew valnew.write("valnew_1." + str(paramL['numstart']) + ".s{}".format(rank) + ".txt") #write file to disk for restart comm.barrier() for i in range(paramL['numstart'] + 1, paramL['numits'] + 1): print('Size: ', size, ' rank ', rank) paramL['curr_theta'] = paramL['theta_list'][rank] valnew = TasmanianSG.TasmanianSparseGrid() output = get_iteration_list(i - 1) valnew = interpol_iter.sparse_grid_iter(paramL, list(output)) valold = TasmanianSG.TasmanianSparseGrid() valold = valnew valnew.write("valnew_1." + str(i) + ".s{}".format(rank) + ".txt") comm.barrier() end_time = time.clock() - start_time print('END') if rank == 0: avg_err = post.ls_error(paramL) print(end_time)
def test_sg(n_agents, iDepth, num_points=[]): grid=interpol.sparse_grid(n_agents, iDepth) k_sample=grid.getPoints() v_interpol=grid.evaluateBatch(k_sample) v_func=np.empty((len(k_sample),1)) for i in range(len(k_sample)): v_func[i][0]=solver.initial(k_sample[i], n_agents)[0] res=np.fabs(v_interpol-v_func) return res
def test_sg(n_agents, iDepth, num_points=[]): #unif=np.random.rand(num_points, n_agents) #k_sample=k_bar+(unif)*(k_up-k_bar) grid = interpol.sparse_grid(n_agents, iDepth) k_sample = grid.getPoints() v_interpol = grid.evaluateBatch(k_sample) v_func = np.empty((len(k_sample), 1)) for i in range(len(k_sample)): v_func[i][0] = solver.initial(k_sample[i], n_agents, method)[0] res = np.fabs(v_interpol - v_func) return res
def run_all(n_agents): valnew=TasmanianSG.TasmanianSparseGrid() if (numstart==0): valnew=interpol.sparse_grid(n_agents, iDepth) valnew.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart # value function during iteration else: valnew.read("valnew_1." + str(numstart) + ".txt") #write file to disk for restart valold=TasmanianSG.TasmanianSparseGrid() valold=valnew for i in range(numstart, numits): valnew=TasmanianSG.TasmanianSparseGrid() valnew=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold) valold=TasmanianSG.TasmanianSparseGrid() valold=valnew valnew.write("valnew_1." + str(i+1) + ".txt") #====================================================================== print "===============================================================" print " " print " Computation of a growth model of dimension ", n_agents ," finished after ", numits, " steps" print " " print "===============================================================" #====================================================================== # compute errors avg_err, max_err =post.ls_error(n_agents, numstart, numits, No_samples) #====================================================================== print "===============================================================" print " " print " Errors are computed -- see errors.txt" print " " print "===============================================================" #====================================================================== return avg_err, max_err
import interpolation_iter_adap as interpol_iter_adap import TasmanianSG #sparse grid library import numpy as np #====================================================================== # Start with Value Function Iteration shocks = np.array([.9, .95, 1, 1.05, 1.10]) # terminal value function storeVal = [] if (numstart == 0): for jj in range(5): valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol.sparse_grid(n_agents, iDepth, jj) #print(valnew) valnew.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart storeVal.append(valnew) # value function during iteration else: valnew.read("valnew_1." + str(numstart) + ".txt") #write file to disk for restart storeValOld = [] for jj in range(5): valold = TasmanianSG.TasmanianSparseGrid() valold = storeVal[jj] storeValOld.append(valold)
def run_all(n_agents): valnew=TasmanianSG.TasmanianSparseGrid() if (numstart==0): gridlist = [] for tT in range(ntheta): valnew=TasmanianSG.TasmanianSparseGrid() valnew=interpol.sparse_grid(n_agents, iDepth, theta[tT]) gridlist.append(valnew) valnew.write("valnew_1." + str(numstart) + "theta_" + str(tT) + ".txt") #write file to disk for restart # value function during iteration else: gridlist = [] for tT in range(ntheta): valnew.read("valnew_1." + str(numstart) + "theta_" + str(tT) + ".txt") #write file to disk for restart gridlist.append(valnew) #valold=TasmanianSG.TasmanianSparseGrid() #valold=valnew # avals_list = [] for i in range(numstart, numits): print " ================================================= " print " Iteration", i print " ================================================= " for tT in range(ntheta): thet = theta[tT] valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol_iter.sparse_grid_iter(n_agents, iDepth, gridlist, thet) valnew.write("valnew_1." + str(i+1) + "theta_" + str(tT) + ".txt") gridlist[tT].copyGrid(valnew) # valnew0=TasmanianSG.TasmanianSparseGrid() # valnew1=TasmanianSG.TasmanianSparseGrid() # valnew2=TasmanianSG.TasmanianSparseGrid() # valnew3=TasmanianSG.TasmanianSparseGrid() # valnew4=TasmanianSG.TasmanianSparseGrid() # # valnew0=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta[0]) # valnew1=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta[1]) # valnew2=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta[2]) # valnew3=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta[3]) # valnew4=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta[4]) # evaluate all grids at the same points # chosen arbitrarily to be the points of the third grid where theta=1 # eval_points = valnew2.getPoints() # # aVals0 = valnew0.evaluateBatch(eval_points)[:,0] # aVals1 = valnew1.evaluateBatch(eval_points)[:,0] # aVals2 = valnew2.evaluateBatch(eval_points)[:,0] # aVals3 = valnew3.evaluateBatch(eval_points)[:,0] # aVals4 = valnew4.evaluateBatch(eval_points)[:,0] # print aVals0, aVals4 # aVals_new = 0.2 *(aVals0 + aVals1 + aVals2 + aVals3 + aVals4) # aVals_new = np.reshape(aVals_new, (eval_points.shape[0], 1)) #f=open("aVals_new.txt", 'a') #np.savetxt(f, aVals_new, fmt='% 2.16f') #f.close() # print "===================================================================" # print " print avals shape here :" # print aVals_new.shape # # valold=TasmanianSG.TasmanianSparseGrid() # valold.copyGrid(valnew2) # valold.loadNeededPoints(aVals_new) #valold=valnew # valold.write("valnew_1." + str(i+1) + "theta_" + str(tT) + ".txt") #====================================================================== print "===============================================================" print " " print " Computation of a growth model of dimension ", n_agents ," finished after ", numits, " steps" print " " print "===============================================================" #====================================================================== # compute errors post.ls_error(n_agents, numstart, numits, No_samples) #====================================================================== print "===============================================================" print " " print " Errors are computed -- see errors_theta_NUM.txt" print " " print " Groupwork of Max, Clint and Ben at OSM Lab 2017" print "==============================================================="
# Policy function function endvars=TasmanianSG.TasmanianSparseGrid() if (numstart==0): endvars=initval.initialize(n_dims, iDepth) endvars.write("endvars_1." + str(numstart) + ".txt") #write file to disk for restart # Policy function during iteration else: endvars.read("endvars_1." + str(numstart) + ".txt") #read file from disk endvarsold=TasmanianSG.TasmanianSparseGrid() endvarsold=endvars for i in range(numstart, numits): endvars=TasmanianSG.TasmanianSparseGrid() endvars=interpol.sparse_grid(n_dims, iDepth, endvarsold) endvarsold=TasmanianSG.TasmanianSparseGrid() endvarsold=endvars endvars.write("endvars_1." + str(i+1) + ".txt") print " step ", i, " computed " print "========================" #====================================================================== print "===============================================================" print " " print " Computation of RBC model of dimension ", n_dims ," finished after ", numits , " steps" print " " print "===============================================================" #====================================================================== # compute errors
import nonlinear_solver_iterate as solviter #solves opt. problems during VFI from parameters import * #parameters of model import interpolation as interpol #interface to sparse grid library/terminal VF import interpolation_iter as interpol_iter #interface to sparse grid library/iteration import postprocessing as post #computes the L2 and Linfinity error of the model import TasmanianSG #sparse grid library import numpy as np #====================================================================== # Start with Value Function Iteration # terminal/initial value function valnew = TasmanianSG.TasmanianSparseGrid() if (numstart == 0): valnew = interpol.sparse_grid(n_agents, iDepth, refinement_level, fTol) valnew.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart # value function during iteration else: valnew.read("valnew_1." + str(numstart) + ".txt") #write file to disk for restart valold = TasmanianSG.TasmanianSparseGrid() valold = valnew for i in range(numstart, numits): valnew = TasmanianSG.TasmanianSparseGrid() valnew = interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, refinement_level, fTol)
paramL['l_up']=1.0 paramL['inv_bar']=1e-2 paramL['inv_up']=10000.0 # Stochastic paramL['theta_list'] = [0.90, 0.95, 1.00, 1.05, 1.10] paramL['curr_theta'] = 1.00 #====================================================================== # terminal value function valList = [] if (paramL['numstart']==0): for state in range(5): valnew=TasmanianSG.TasmanianSparseGrid() valnew=interpol.sparse_grid(paramL) valList.append(valnew) valnew.write("valnew_1." + str(paramL['numstart']) + ".txt") #write file to disk for restart # value function during iteration else: valnew.read("valnew_1." + str(paramL['numstart']) + ".txt") #write file to disk for restart valListold = [] for state in range(5): valold=TasmanianSG.TasmanianSparseGrid() valold=valList[state] valListold.append(valold) for i in range(paramL['numstart'], paramL['numits']): for state in range(5):
import interpolation_iter as interpol_iter #interface to sparse grid library/iteration import postprocessing as post #computes the L2 and Linfinity error of the model import TasmanianSG #sparse grid library import numpy as np #====================================================================== # Start with Value Function Iteration # terminal value function valnew=[TasmanianSG.TasmanianSparseGrid()]*5 if (numstart==0): for itheta in range(ntheta): valnew[itheta]=interpol.sparse_grid(n_agents, iDepth, 1) valnew[itheta].write("valnew_" + str(theta_range[itheta]) + '_'+str(numstart)+".txt") #write file to disk for restart # value function during iteration else: for itheta in range(ntheta): valnew[itheta].read("valnew_" + str(theta_range[itheta]) + '_'+str(numstart)+".txt") #write file to disk for restart valold=[TasmanianSG.TasmanianSparseGrid()]*5 valold=valnew for i in range(numstart, numits): valnew=[TasmanianSG.TasmanianSparseGrid()]*5 for itheta in range(ntheta): valnew[itheta]=interpol_iter.sparse_grid_iter(n_agents, iDepth, valold, theta_range[itheta]) valold[itheta]=valnew[itheta]
import interpolation_exp as interpol_exp #interface to sparse grid library/terminal VF import interpolation_iter_exp as interpol_iter_exp #interface to sparse grid library/iteration import postprocessing as post #computes the L2 and Linfinity error of the model import TasmanianSG #sparse grid library import numpy as np #====================================================================== # Start with Value Function Iteration # terminal/initial value function valnew_sub = TasmanianSG.TasmanianSparseGrid() for iT, theta in enumerate(theta_vec): if (numstart == 0): valnew_sub = interpol.sparse_grid(n_agents, iDepth, refinement_level, fTol, theta) valnew_sub.write("valnew_1." + str(numstart) + ".shock" + str(iT + 1) + ".txt") #write file to disk for restart #valnew = interpol.sparse_grid(n_agents, iDepth , refinement_level, fTol, theta) #valnew.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart # value function during iteration else: valnew_sub.read("valnew_1." + str(numstart) + ".shock" + str(iT + 1) + ".txt") #write file to disk for restart valnew = TasmanianSG.TasmanianSparseGrid( ) # we might need to calculate 5 different Valnew now valnew = interpol_exp.sparse_grid(n_agents, iDepth, refinement_level, fTol, theta_vec, theta_prob)
import numpy as np from mpi4py import MPI #====================================================================== comm = MPI.COMM_WORLD rank = comm.Get_rank() # Start with Value Function Iteration valold = TasmanianSG.TasmanianSparseGrid() valnew = TasmanianSG.TasmanianSparseGrid() t1 = MPI.Wtime() if numstart == 0: valnew = interpol.sparse_grid(n_agents) if rank == 0: valnew.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart comm.Barrier() if rank != 0: valnew.read("valnew_1." + str(numstart) + ".txt") else: valnew.read("valnew_1." + str(numstart) + ".txt") # value function during iteration valold = valnew comm.Barrier() for i in range(numstart, numits):
#====================================================================== # Start with Value Function Iteration # terminal value function valnew_list = np.array([ TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid() ]) if (numstart == 0): for i in range(len(valnew_list)): valnew_list[i] = interpol.sparse_grid(n_agents, iDepth) valnew_list.write("valnew_1." + str(numstart) + ".txt") #write file to disk for restart # value function during iteration else: valnew_list.read("valnew_1." + str(numstart) + ".txt") #write file to disk for restart valold_list = np.array([ TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid(), TasmanianSG.TasmanianSparseGrid() ])