def main(): opt = read_args() print opt.scheme, opt.nspots, opt.nvols, opt.n print if opt.cpu: utils.tic("CPU Create:") F = create_cpu(nspots=opt.nspots, nvols=opt.nvols) utils.toc() idx = F.idx if opt.run and opt.npaths: mc = F.option.monte_carlo() print mc if opt.run: print run(1.0/opt.n, F, opt.scheme)[idx] F.grid.reset() if opt.gpu: utils.tic("GPU Create 1:") FG = create_gpu(nspots=opt.nspots, nvols=opt.nvols) utils.toc() idx = FG.idx if opt.run: print run(1.0/opt.n, FG, opt.scheme, FG.grid.domain[0])[idx], print
def main(): opt = read_args() print opt.scheme, opt.nspots, opt.nvols, opt.n print if opt.cpu: utils.tic("CPU Create:") F = create_cpu(nspots=opt.nspots, nvols=opt.nvols) utils.toc() idx = F.idx if opt.run and opt.npaths: mc = F.option.monte_carlo() print mc if opt.run: print run(1.0 / opt.n, F, opt.scheme)[idx] F.grid.reset() if opt.gpu: utils.tic("GPU Create 1:") FG = create_gpu(nspots=opt.nspots, nvols=opt.nvols) utils.toc() idx = FG.idx if opt.run: print run(1.0 / opt.n, FG, opt.scheme, FG.grid.domain[0])[idx], print
def impl(V, L1, R1x, L2, R2x, dt, n, initial=None, callback=None): V = V.copy() # L1i = flatten_tensor(L1) L1i = L1.copy() R1 = np.array(R1x).T # L2i = flatten_tensor(L2) L2i = L2.copy() R2 = np.array(R2x) m = 2 # L = (As + Ass - H.interest_rate*np.eye(nspots))*-dt + np.eye(nspots) L1i.data *= -dt L1i.data[m, :] += 1 R1 *= -dt L2i.data *= -dt L2i.data[m, :] += 1 R2 *= -dt offsets1 = (abs(min(L1i.offsets)), abs(max(L1i.offsets))) offsets2 = (abs(min(L2i.offsets)), abs(max(L2i.offsets))) dx = np.gradient(spots)[:, np.newaxis] dy = np.gradient(vars) Y, X = np.meshgrid(vars, spots) gradgrid = dt * coeffs[(0, 1)](0, X, Y) / (dx * dy) gradgrid[:, 0] = 0 gradgrid[:, -1] = 0 gradgrid[0, :] = 0 gradgrid[-1, :] = 0 print_step = max(1, int(n / 10)) to_percent = 100.0 / n utils.tic("Impl:\t") for k in xrange(n): if not k % print_step: if np.isnan(V).any(): print "Impl fail @ t = %f (%i steps)" % (dt * k, k) return crumbs print int(k * to_percent), if callback is not None: callback(V, ((n - k) * dt)) Vsv = np.gradient(np.gradient(V)[0])[1] * gradgrid # Vsv = 0.0 V = spl.solve_banded(offsets2, L2i.data, (V + Vsv - R2).flat, overwrite_b=True).reshape(V.shape) V = spl.solve_banded(offsets1, L1i.data, (V - R1).T.flat, overwrite_b=True).reshape(V.shape[::-1]).T crumbs.append(V.copy()) utils.toc(": \t") return crumbs
def douglas(V, L1, R1x, L2, R2x, dt, n, initial=None, callback=None): V = V.copy() theta = 0.5 # dt *= 0.5 # L1e = flatten_tensor(L1) L1e = L1.copy() L1i = L1e.copy() R1 = np.array(R1x).T # L2e = flatten_tensor(L2) L2e = L2.copy() L2i = L2e.copy() R2 = np.array(R2x) # print "L var" # fp(L2e.data, 2) # print "FDE op var" # fp(F.operators[1].data, 2) # print "diff" # fp(F.operators[1].data - L2e.data, 2, 'f') # assert np.allclose(F.operators[1].data, L2e.data) m = 2 # L = (As + Ass - H.interest_rate*np.eye(nspots))*-dt + np.eye(nspots) L1i.data *= -theta * dt L1i.data[m, :] += 1 # R1 *= dt L2i.data *= -theta * dt L2i.data[m, :] += 1 # R2 *= dt offsets1 = (abs(min(L1i.offsets)), abs(max(L1i.offsets))) offsets2 = (abs(min(L2i.offsets)), abs(max(L2i.offsets))) dx = np.gradient(spots)[:, np.newaxis] dy = np.gradient(vars) X, Y = [dim.T for dim in np.meshgrid(spots, vars)] gradgrid = coeffs[(0, 1)](0, X, Y) / (dx * dy) gradgrid[:, 0] = 0 gradgrid[:, -1] = 0 gradgrid[0, :] = 0 gradgrid[-1, :] = 0 print_step = max(1, int(n / 10)) to_percent = 100.0 / n utils.tic("Douglas:\t") R = R1 + R2 normal_shape = V.shape transposed_shape = V.T.shape for k in xrange(n): if not k % print_step: if np.isnan(V).any(): print "Douglas fail @ t = %f (%i steps)" % (dt * k, k) return crumbs print int(k * to_percent), if callback is not None: callback(V, ((n - k) * dt)) Vsv = np.gradient(np.gradient(V)[0])[1] * gradgrid # V12 = (V # + Vsv # + (1-theta)*dt*L1e.dot(V.T.flat).reshape(transposed_shape).T # + (1-theta)*dt*L2e.dot(V.flat).reshape(normal_shape) # + dt * R) # V1 = spl.solve_banded(offsets2, L2i.data, V12.flat, overwrite_b=True).reshape(normal_shape) # V = spl.solve_banded(offsets1, L1i.data, V1.T.flat, overwrite_b=True).reshape(transposed_shape).T V1 = (L1e.dot(V.T.flat).reshape(transposed_shape)).T V2 = L2e.dot(V.flat).reshape(normal_shape) Y0 = V + dt * (Vsv + V1 + V2 + R) V1 = Y0 - theta * dt * L1e.dot(V.T.flat).reshape(transposed_shape).T Y1 = spl.solve_banded(offsets1, L1i.data, V1.T.flat, overwrite_b=True).reshape(transposed_shape).T V2 = Y1 - theta * dt * L2e.dot(V.flat).reshape(normal_shape) Y2 = spl.solve_banded(offsets2, L2i.data, V2.flat, overwrite_b=True).reshape(normal_shape) V = Y2 crumbs.append(V.copy()) utils.toc(": \t") return crumbs
tenor=H.tenor, ).analytical utils.tic("Heston Analytical:\t") hs = hs_call_vector( spots, H.strike, H.interest_rate.value, np.sqrt(vars), H.tenor, H.mean_reversion, H.mean_variance, H.vol_of_variance, H.correlation, ) utils.toc() hs = np.nan_to_num(hs) if max(hs.flat) > spots[-1] * 2: BADANALYTICAL = True print "Warning: Analytical solution looks like trash." if len(sys.argv) > 1: if sys.argv[1] == "0": print "Bail out with arg 0." sys.exit() def mu_s(t, *dim): return H.interest_rate.value * dim[0]
def implicit_manual(V, L1, R1x, L2, R2x, dt, n, spots, vars, coeffs, crumbs=[], callback=None): V = V.copy() # L1i = flatten_tensor(L1) L1i = L1.copy() R1 = np.array(R1x) # L2i = flatten_tensor(L2) L2i = L2.copy() R2 = np.array(R2x) m = 2 # L = (As + Ass - H.interest_rate*np.eye(nspots))*-dt + np.eye(nspots) L1i.data *= -dt L1i.data[m, :] += 1 R1 *= dt L2i.data *= -dt L2i.data[m, :] += 1 R2 *= dt offsets1 = (abs(min(L1i.offsets)), abs(max(L1i.offsets))) offsets2 = (abs(min(L2i.offsets)), abs(max(L2i.offsets))) dx = np.gradient(spots)[:, np.newaxis] dy = np.gradient(vars) X, Y = [dim.T for dim in np.meshgrid(spots, vars)] gradgrid = dt * coeffs[(0, 1)](0, X, Y) / (dx * dy) gradgrid[:, 0] = 0 gradgrid[:, -1] = 0 gradgrid[0, :] = 0 gradgrid[-1, :] = 0 print_step = max(1, int(n / 10)) to_percent = 100.0 / n utils.tic("Impl:") for k in xrange(n): if not k % print_step: if np.isnan(V).any(): print "Impl fail @ t = %f (%i steps)" % (dt * k, k) return crumbs print int(k * to_percent), if callback is not None: callback(V, ((n - k) * dt)) Vsv = np.gradient(np.gradient(V)[0])[1] * gradgrid V = spl.solve_banded(offsets2, L2i.data, (V + Vsv + R2).flat, overwrite_b=True).reshape(V.shape) V = spl.solve_banded(offsets1, L1i.data, (V + R1).T.flat, overwrite_b=True).reshape(V.shape[::-1]).T crumbs.append(V.copy()) utils.toc() return crumbs