def __init__(self, valuefunction_prename=None, load_existing_list=False, c_name=None, grad_c_name=None): self.t_vec_p = np.load('t_vec_p.npy') self.V = [] if valuefunction_prename == None: V_load = pickle.load(open('V_new', 'rb')) for i0 in range(len(self.t_vec_p)): self.V.append(1 * V_load) self.c_add_fun_list = np.zeros(len(self.V)) self.c_add_fun_list[-1] = 1 self.c_add_fun_list[-2] = 1 # self.V= [V_load for i0 in range(len(self.t_vec_p))] else: if load_existing_list: for i0 in range(len(self.t_vec_p)): self.V.append( pickle.load(open(valuefunction_prename + str(i0), 'rb'))) self.c_add_fun_list = np.load(c_name) else: V_load = pickle.load(open(valuefunction_prename, 'rb')) for i0 in range(len(self.t_vec_p)): self.V.append(1 * V_load) self.c_add_fun_list = np.zeros(len(self.V)) self.c_add_fun_list[-1] = 1 self.c_add_fun_list[-2] = 1 # self.V= [V_load for i0 in range(len(self.t_vec_p))] self.r = self.V[0].order() load_me = np.load('save_me.npy') self.tau = self.t_vec_p[1] - self.t_vec_p[0] self.integrate_min = load_me[2] self.integrate_max = load_me[3] self.maxrank = int(load_me[6]) self.pol_deg = np.max(self.V[0].dimensions) usepol = True # usepol = False self.ddpol = [] if not usepol: self.pol, self.dpol, self.ddpol = polynomials(self.pol_deg) else: self.pol, self.dpol = orth_pol.calc_pol(self.integrate_max, self.integrate_min, self.pol_deg - 1) for i0 in range(self.pol_deg): self.ddpol.append(np.polyder(self.dpol[i0])) # print('self.pol_deg', self.pol_deg) self.add_fun = lambda t, x: np.zeros(x.shape[-1]) self.grad_add_fun = lambda t, x: np.zeros(x.shape[-1]) self.hess_add_fun = lambda t, x: np.zeros(x.shape[-1]) self.laplacian_add_fun = lambda t, x: np.zeros(x.shape[-1])
def __init__(self, valuefunction_prename=None, load_existing_list=False, c_name=None): self.t_vec_p = np.load('t_vec_p.npy') self.V = [] if valuefunction_prename == None: V_load = pickle.load(open('V_new', 'rb')) for i0 in range(len(self.t_vec_p)): self.V.append(1 * V_load) self.c_add_fun_list = np.zeros(len(self.V)) self.c_add_fun_list[-1] = 1 # self.V= [V_load for i0 in range(len(self.t_vec_p))] else: if load_existing_list: for i0 in range(len(self.t_vec_p)): self.V.append( pickle.load(open(valuefunction_prename + str(i0), 'rb'))) self.c_add_fun_list = np.load(c_name) else: V_load = pickle.load(open(valuefunction_prename, 'rb')) for i0 in range(len(self.t_vec_p)): self.V.append(1 * V_load) self.c_add_fun_list = np.zeros(len(self.V)) self.c_add_fun_list[-1] = 1 # self.V= [V_load for i0 in range(len(self.t_vec_p))] self.r = self.V[0].order() load_me = np.load('save_me.npy') self.tau = self.t_vec_p[1] - self.t_vec_p[0] self.integrate_min = load_me[2] self.integrate_max = load_me[3] self.pol_deg = np.max(self.V[0].dimensions) # self.pol, self.dpol = polynomials(self.pol_deg) self.pol, self.dpol = orth_pol.calc_pol(self.integrate_max, self.integrate_min, self.pol_deg - 1) # self.replace_last_pol() self.add_fun = lambda t, x: np.zeros(x.shape[-1]) self.grad_add_fun = lambda t, x: np.zeros(x.shape[-1])
def set_dynamics(pol_deg = None, num_valuefunctions = None): # num_valuefunctions = 3 # num_valuefunctions = 6 # num_valuefunctions = 11 # num_valuefunctions = 21 if pol_deg is None: pol_deg = 2 if num_valuefunctions is None: num_valuefunctions = 4 # num_valuefunctions = 9 # num_valuefunctions = 16 # print('pol_deg, num_valuefunctions', pol_deg, num_valuefunctions) T = 3 n = 5 # spacial discretization points that are considered sigma = .2 interest = 0.05 increase = 0 strike_price = 100 interval_min = 10 # integration area of HJB equation is [interval_min, interval_max]**n interval_max = 620 x0 = np.ones(n)*100 rho = 0 # T = 1 # n = 1 # spacial discretization points that are considered # sigma = .2 # interest = 0.1 # increase = 0 # strike_price = 110 # interval_min = 0 # integration area of HJB equation is [interval_min, interval_max]**n # interval_max = strike_price/n # x0 = np.ones(n)*100 # rho = 0.2 # print('x0', x0) rank = 3 payoff_vec = 1/n*np.ones(n) # print('payoff_vec', payoff_vec) Gamma_mat = rho*np.ones((n,n)) np.fill_diagonal(Gamma_mat, 1) # print('gammamat/covariance', Gamma_mat) L = la.sqrtm(Gamma_mat) # print('L', L) t_vec_p = np.linspace(0, T, num_valuefunctions) # tau = 1e-2 # time step size tau = t_vec_p[1] - t_vec_p[0] # time step size num_timesteps = int(np.round((T - 0) / tau)) t_vec_s = np.linspace(0, T, num_timesteps+1) # print('t_vec_p', t_vec_p) # print('t_vec_s', t_vec_s) b = 1 # left end of Domain a = -1 # right end of Domain nu = 1 # diffusion constant lambd = 0.1 # cost parameter gamma = 0 # discount factor, 0 for no discount boundary = 'Neumann' # use 'Neumann' or "Dirichlet use_full_model = True # if False, model is reduced to r Dimensions r = n # Model is reduced to r dimensions, only if use_full_model == False load = np.zeros([11]) load[0] = lambd; load[1] = gamma; load[2] = interval_min; load[3] = interval_max; load[4] = tau; load[5] = n; load[6] = sigma; load[7] = interest; load[8] = strike_price; load[9] = increase; load[10] = rho np.save("x0", x0) np.save('payoff_vec.npy', payoff_vec) np.save('L', L) np.save("save_me", load) np.save('t_vec_p', t_vec_p) np.save('t_vec_s', t_vec_s) # 'delete from here if you do not want to use xerus' set_V_new = True # print(set_V_new) import orth_pol load_me = np.load('save_me.npy') pol, dpol = orth_pol.calc_pol(interval_max, interval_min, 2) desired_ranks = [rank]*(n-1) V_setranks = xe.TTTensor.random([pol_deg+1]*n, desired_ranks) desired_ranks = V_setranks.ranks() load_prev = False if load_prev == True: # V_prev = xe.load_from_file('V_optimized.txt') # load as initial guess V_prev = xe.load_from_file('V_99.txt') # load as initial guess if V_prev.order() < r: # print('increase dimensions') # print((V_prev.ranks() + [1])) V_increased = xe.TTTensor.random(V_prev.dimensions + [pol_deg], V_prev.ranks() + [1]) for i0 in range(V_prev.order()): V_increased.set_component(i0, V_prev.get_component(i0)) V_increased.set_component(V_prev.order(), xe.Tensor.dirac((1,pol_deg,1), [0,0,0])) V_prev = V_increased else: V_prev = 0 if type(V_prev) == xe.TTTensor: # print("type(V_prev) == xe.TTTensor") V_new = V_prev new = pol_deg+1 prev = V_prev.dimensions # print("prev dim:", prev) for iter_0 in range(r): comp = V_prev.get_component(iter_0) # print(comp.dimensions) comp.resize_mode(mode=1, newDim=new, cutPos=prev[iter_0]) if iter_0 != 0: if comp.dimensions[0] != desired_ranks[iter_0-1]: comp.resize_mode(mode=0, newDim=desired_ranks[iter_0-1], cutPos=comp.dimensions[0]) comp = comp + 0.000000000001*xe.Tensor.random(comp.dimensions) if iter_0 != r-1: if comp.dimensions[2] != desired_ranks[iter_0]: comp.resize_mode(mode=2, newDim=desired_ranks[iter_0], cutPos=comp.dimensions[2]) comp = comp + 0.000000000001*xe.Tensor.random(comp.dimensions) V_new.set_component(iter_0, comp) V_new.canonicalize_left() else: V_new = 0.0000000001*xe.TTTensor.random([pol_deg + 1]*n, desired_ranks) pickle.dump(V_new, open("V_new", 'wb'))