def test_conv_rate_5dDig(self): dim = 6 T = 1 strike = 40 init_price_vec = np.full(dim, 40) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 euro = Digital(strike, init_price_vec, T, ir, vol, dividend, corr, 1) result = FFTConvExperiment(0.45, 2, 7, euro, 10, True) hdpPickle.dump(result, 'FFTconv_rate_5dDig.pickle') print(result)
def test_conv_rate_6dBas(self): dim = 6 T = 1 strike = 40 init_price_vec = np.full(dim, 40) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 euro = BasEuro(strike, init_price_vec, T, ir, vol, dividend, corr, -1) result = FFTConvExperiment(1.50600, 2, 6, euro, 10, True) hdpPickle.dump(result, 'FFTconv_rate_6dBas.pickle') print(result)
def test_delta_4d(self): dim = 4 T = 1 strike = 40 init_price_vec = np.full(4, 40, dtype=np.float64) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 euro = GAEuro(strike, init_price_vec, T, ir, vol, dividend, corr, 1) analy = AnalyticalGA(dim, init_price_vec, strike, T, ir, vol, dividend, corr).delta() result = FFTConvDeltaExperiment(analy[0], 3, 8, euro, 10) hdpPickle.dump(result, 'FFTconv_delta_4dGA.pickle') print(result)
def test_conv_rate_4dGA(self): dim = 4 T = 1 strike = 40 init_price_vec = np.full(4, 40, dtype=np.float64) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 euro = GAEuro(strike, init_price_vec, T, ir, vol, dividend, corr, 1) analy = AnalyticalGA(dim, init_price_vec, strike, T, ir, vol, dividend, corr).european_option_price() result = FFTConvExperiment(analy, 3, 7, euro, 10) hdpPickle.dump(result, 'FFTconv_rate_4dGA.pickle') print(result)
def test_amer(self): # although this is not a euro experiment... T = 1 strike = 50 asset_num = 1 init_price_vec = 50*np.ones(asset_num) vol_vec = 0.5*np.ones(asset_num) ir = 0.05 dividend_vec = np.zeros(asset_num) corr_mat = np.eye(asset_num) nTime = 365 random_walk = GBM(T, nTime, init_price_vec, ir, vol_vec, dividend_vec, corr_mat) def test_payoff(*l): return max(strike - np.sum(l), 0) opt = American(test_payoff, random_walk) analy = 8.723336355455928 np.random.seed(1) result = MCEuroExperiment(analy, 10, 16, opt, "V1") hdpPickle.dump(result, 'MCAmer_1d.pickle') print(result)
def test_conv_rate_6d_control_sobol(self): dim = 6 T = 1 strike = 40 init_price_vec = np.full(dim, 40) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 vol_vec = np.full(dim, vol) dividend_vec = np.full(dim, dividend) corr_mat = np.full((dim, dim), corr) np.fill_diagonal(corr_mat, 1) payoff_func = lambda x: np.maximum(strike - np.mean(x, axis=1), np.zeros(len(x))) random_walk = GBM(T, 400, init_price_vec, ir, vol_vec, dividend_vec, corr_mat) opt = Euro(payoff_func, random_walk) analy = 1.50600 np.random.seed(1) result = MCEuroExperiment(analy, 14, 21, opt, "V8") hdpPickle.dump(result, 'MCEuro_6d_control_sobol.pickle') print(result)
def test_conv_rate_4dGA_anti_sol(self): from scipy.stats.mstats import gmean dim = 4 T = 1 strike = 40 init_price_vec = np.full(dim, 40) vol = 0.2 ir = 0.06 dividend = 0.04 corr = 0.25 vol_vec = np.full(dim, vol) dividend_vec = np.full(dim, dividend) corr_mat = np.full((dim, dim), corr) np.fill_diagonal(corr_mat, 1) payoff_func = lambda x: np.maximum((gmean(x, axis=1) - strike), np.zeros(len(x))) random_walk = GBM(T, 400, init_price_vec, ir, vol_vec, dividend_vec, corr_mat) opt = Euro(payoff_func, random_walk) analy = 2.165238512096621 np.random.seed(1) result = MCEuroExperiment(analy, 20, 24, opt, "V6") hdpPickle.dump(result, 'MCEuro_4dGA_AntiSol.pickle') print(result)
def run(self, n_samples, steps_per_sample, n_layers=3, layer_width=50, n_interior=32,\ n_terminal=32, saved_name=None, use_fd_hessian=False, use_L2_err=True): if not saved_name: pickle_dir = DIR_LOC + "/saved_models/{}_Euro".format( time.strftime("%Y%m%d")) saved_name = "{}_Euro.ckpt".format(time.strftime("%Y%m%d")) else: pickle_dir = DIR_LOC + "/saved_models/{}_{}".format( time.strftime("%Y%m%d"), saved_name) saved_name = time.strftime("%Y%m%d") + "_" + saved_name + ".ckpt" model = DGMNet(n_layers, layer_width, input_dim=self.dim) self.model = model S_interior_tnsr = tf.placeholder(tf.float64, [None, self.dim]) t_interior_tnsr = tf.placeholder(tf.float64, [None, 1]) S_terminal_tnsr = tf.placeholder(tf.float64, [None, self.dim]) t_terminal_tnsr = tf.placeholder(tf.float64, [None, 1]) L1_tnsr, L3_tnsr = self.loss_func(model, S_interior_tnsr, t_interior_tnsr,\ S_terminal_tnsr, t_terminal_tnsr, use_fd_hessian=use_fd_hessian, use_L2_err=use_L2_err) loss_tnsr = L1_tnsr + L3_tnsr global_step = tf.Variable(0, trainable=False) boundaries = [5000, 10000, 20000, 30000, 40000, 45000] values = [1e-4, 5e-5, 1e-5, 5e-6, 1e-6, 5e-7, 1e-7] learning_rate = tf.train.piecewise_constant(global_step, boundaries, values) optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(loss_tnsr) model_saver = tf.train.Saver() self.loss_vec, self.L1_vec, self.L3_vec = [], [], [] with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(n_samples): S_interior, t_interior, S_terminal, t_terminal = self.sampler.run( n_interior, n_terminal) for _ in range(steps_per_sample): loss, L1, L3, _ = sess.run([loss_tnsr, L1_tnsr, L3_tnsr, optimizer],\ feed_dict={S_interior_tnsr: S_interior, t_interior_tnsr: t_interior,\ S_terminal_tnsr: S_terminal, t_terminal_tnsr: t_terminal}) self.loss_vec.append(loss) self.L1_vec.append(L1) self.L3_vec.append(L3) print("Iteration {}: Loss: {}; L1: {}; L3: {}".format( i, loss, L1, L3)) model_saver.save(sess, DIR_LOC + "/saved_models/" + saved_name) pickle.dump(self.loss_vec, pickle_dir + "_lossvec.pickle") pickle.dump(self.L1_vec, pickle_dir + "_l1vec.pickle") # pickle.dump(self.L2_vec, pickle_dir+"_l2vec.pickle") pickle.dump(self.L3_vec, pickle_dir + "_l3vec.pickle")