def european_call(self, r, sigma, T, Bu, m, n, Bl=0.0, barrier=None, method=None, isAmerican=False): """Compute prices for a European-style call option.""" X = linspace(0.0, B, n + 2) X = X[1:-1] Fp = clip(X - K, 0.0, 1e600) if barrier is None: Fu = B - K * exp(-r * linspace(0.0, T, m + 1)) Fl = zeros((m + 1, )) elif barrier == 'up-and-out': Fu = Fl = zeros((m + 1, )) bss = BSSolver(r, sigma, T, Bl, Bu, Fl, Fu, Fp, m, n, isAmerican) return X, bss.solve(method)
def schwarz_christoffel_coeff(points): a = exp(2j*pi*linspace(0, 1, len(points), endpoint=False)) a.shape = (1, -1) p = [points[-1]] + points + points[0:1] b = array([ (angle( (p[k-1]-p[k])/(p[k+1]-p[k]) )/pi)%2.0 - 1.0 for k in xrange(1, len(p)-1) ]) b.shape = (1,-1) return (a,b)
def european_call(r, sigma, T, Smax, m, n, Smin=0.0, barrier=None): X = linspace(0.0, Smax, n+2) X = X[1:-1] Fp = clip(X-K, 0.0, 1e600) if barrier is None: Fu = Smax - K*exp(-r * linspace(0.0, T, m+1)) Fl = zeros((m+1, )) elif barrier == 'up-and-out': Fu = Fl = zeros((m+1,)) bss = BS_FDM_cn(r, sigma, T, Smin, Smax, Fl, Fu, Fp, m, n) return X, bss.solve()
def european_put(r, sigma, T, Bu, m, n, Bl=0.0, barrier=None, method=None): """Compute prices for a European-style put option.""" X = linspace(0.0, B, n+2) X = X[1:-1] Fp = clip(K-X, 0.0, 1e600) if barrier is None: Fu = zeros((m+1,)) Fl = K*exp(-r * linspace(0.0, T, m+1)) elif barrier == 'up-and-out': Fu = Fl = zeros((m+1,)) bss = BlackScholesSolver(r, sigma, T, Bl, Bu, Fl, Fu, Fp, m, n) return X, bss.solve(method)
def schwarz_christoffel_integrand(t, a, b, z, dz): return dz*exp(inner(log( (z+dz*t.reshape(-1,1)-a)/(a*1j) ), b))
figure = figures[opts.figure] a, b = schwarz_christoffel_coeff(figure) print a print b+1.0 # Set up discretization points for the grid M = opts.M N = opts.N * len(figure) R = 1.0 - 2**linspace(0, -M, M+1, endpoint=True) Theta = linspace(0, 2*pi, N, endpoint=False) if not opts.show_domain: # Compute f(z) over grid W = zeros(shape=(M, N), dtype=complex) for v in xrange(0, N): Z = R * exp(1j*Theta[v]) for u in xrange(0, M): W[u,v] = gauss_quad32(schwarz_christoffel_integrand, (a, b, Z[u], Z[u+1]-Z[u])) W = cumsum(W, axis=0) else: # Domain contours are just concentric circles W = 36.0 * R[1:].reshape(-1,1) * exp(1j*Theta).reshape(1,-1) # Start vector drawing with PyX unit.set(uscale=0.075) cvs = canvas.canvas() if opts.show_wavefronts: if opts.curved_contours: for u in xrange(0, M-1):