def impv_bs(spot, strike, r, d, expiry, price, optiontype): low = 0.000001 high = 0.3 if (optiontype != "EURO_CALL" and optiontype != "EURO_PUT"): return "NAN1" if (optiontype == "EURO_CALL"): ce = bs_call(spot, strike, r, d, high, expiry) else: ce = bs_put(spot, strike, r, d, high, expiry) while (ce < price): high *= 2.0 if (high > 1e10): return "NAN2" if (optiontype == "EURO_CALL"): ce = bs_call(spot, strike, r, d, high, expiry) else: ce = bs_put(spot, strike, r, d, high, expiry) if (optiontype == "EURO_CALL"): return brent(low, high, price, bs_call, None, None, spot, strike, r, d, expiry, 0, 0) else: return brent(low, high, price, bs_put, None, None, spot, strike, r, d, expiry, 0, 0)
def impv_bi(spot, strike, r, d, expiry, steps, price, optiontype): low = 0.001 high = 0.3 if (optiontype != "AMER_CALL" and optiontype != "AMER_PUT"): return "optiontype error" if (optiontype == "AMER_CALL"): ce = bi_amer_call(spot, strike, r, d, high, expiry, steps) else: ce = bi_amer_put(spot, strike, r, d, high, expiry, steps) while (ce < price): high *= 2.0 if (high > 1e10): return "can't find a high vol" if (optiontype == "AMER_CALL"): ce = bi_amer_call(spot, strike, r, d, high, expiry, steps) else: ce = bi_amer_put(spot, strike, r, d, high, expiry, steps) if (optiontype == "AMER_CALL"): return brent(low, high, price, None, bi_amer_call, None, spot, strike, r, d, expiry, 0, steps) else: return brent(low, high, price, None, bi_amer_put, None, spot, strike, r, d, expiry, 0, steps)
def eigenvals3(d, c, N): def f(x): # f(x) = |[A] - x[I]| p = sturmSeq(d, c, x) return p[len(p) - 1] lam = zeros((N), type=Float64) r = lamRange(d, c, N) # Bracket eigenvalues for i in range(N): # Solve by Brent's method lam[i] = brent(f, r[i], r[i + 1]) return lam
def eigenvals3(d,c,N): def f(x): # f(x) = |[A] - x[I]| p = sturmSeq(d,c,x) return p[len(p)-1] lam = zeros((N),type=Float64) r = lamRange(d,c,N) # Bracket eigenvalues for i in range(N): # Solve by Brent's method lam[i] = brent(f,r[i],r[i+1]) return lam
#!/usr/bin/python ## example4_5 from math import cos from brent import * def f(x): return x * abs(cos(x)) - 1.0 print "root =", brent(f, 0.0, 4.0) raw_input("Press return to exit")
#!/usr/bin/python ## example4_5 from math import cos from brent import * def f(x): return x*abs(cos(x)) - 1.0 print "root =",brent(f,0.0,4.0) raw_input("Press return to exit")
#!/usr/bin/python ## example8_1 from numarray import zeros,Float64,array from run_kut4 import * from brent import * from printSoln import * def initCond(u): # Init. values of [y, y’]; use ’u’ if unknown return array([0.0, u]) def r(u): # Boundary condition residual--see Eq. (8.3) X,Y = integrate(F,xStart,initCond(u),xStop,h) y = Y[len(Y) - 1] r = y[0] - 1.0 return r def F(x,y): # First-order differential equations F = zeros((2),type=Float64) F[0] = y[1] F[1] = -3.0*y[0]*y[1] return F xStart = 0.0 # Start of integration xStop = 2.0 # End of integration u1 = 1.0 # 1st trial value of unknown init. cond. u2 = 2.0 # 2nd trial value of unknown init. cond. h = 0.1 # Step size freq = 2 # Printout frequency u = brent(r,u1,u2) # Compute the correct initial condition X,Y = integrate(F,xStart,initCond(u),xStop,h) printSoln(X,Y,freq) raw_input(’’\nPress return to exit’’)
def initCond(u): # Init. values of [y,y']; use 'u' if unknown return array([0.0, u]) def r(u): # Boundary condition residual--see Eq. (8.3) X, Y = integrate(F, xStart, initCond(u), xStop, h) y = Y[len(Y) - 1] r = y[0] - 1.0 return r def F(x, y): # First-order differential equations F = zeros((2), type=Float64) F[0] = y[1] F[1] = -3.0 * y[0] * y[1] return F xStart = 0.0 # Start of integration xStop = 2.0 # End of integration u1 = 1.0 # 1st trial value of unknown init. cond. u2 = 2.0 # 2nd trial value of unknown init. cond. h = 0.1 # Step size freq = 2 # Printout frequency u = brent(r, u1, u2) # Compute the correct initial condition X, Y = integrate(F, xStart, initCond(u), xStop, h) printSoln(X, Y, freq) raw_input("\nPress return to exit")
return x * x * x - 2 * x - 10 def g(x): return (x * 2 + 10) / x / x # ------------------------------------------------------------ # buscar raices a, b = rootSearch(f, -10, 10, 0.5, printText=1) # bisection print "\nMetodo Bisection" print bisection(f, a, b, switch=1) # brent print "\nMetodo Brent" print brent(f, a, b, printText=0) # secant print "\nMetodo Secant" print secant(f, a, b, printText=0) # newton print "\nMetodo de Newton" print newton(f, d1f, a, printText=0) # newton raphson print "\nMetodo de Newton-Raphson" print newtonRaphson(f, d1f, a, b, printText=0) # posicion falsa print "\nMetodo Posicion Falsa o Falsa Regla" print falseRule(f, a, b, printText=0) # punto fijo print "\nMetodo Punto Fijo" print fixedPoint(g, a, printText=0)
from bisection import * from regula_falsi import * from ridder import * from brent import * def f(x): return x**3 - 10 * x**2 + 5.0 x1 = 0.0 x2 = 1.0 x, err = bisection(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("True err = %18.10e" % abs(f(x))) x, err = regula_falsi(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("True err = %18.10e" % abs(f(x))) x, err = ridder(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("True err = %18.10e" % abs(f(x))) x, err = brent(f, x1, x2, TOL=1e-10, verbose=True) print("Final root = %18.10f" % x) print("True err = %18.10e" % abs(f(x)))