خرید بک لینک

Vote count: 0

I transformed Blacks Scholes equation to a Heat equation. I try to use explicit finite difference method to solve this PDE and get the price of a call option. I also solve for this by using black schols equation "analytically".

The problem is that I caot get more accurate in the numerical result. Here is my Python code.

import math
import numpy as np
from scipy.stats import norm

s0 = 15
sigma = 0.2
r = 0.01
t = 1
Xmax = 10

def Bull_Spread_Call_Payoff(st):
    if st <= 10:
        payoff = -5
    elif st <= 20:
        payoff = st - 15
    else:
        payoff = 5
    retu payoff

'''B-S price''' 

def C(s,k,t):
    d1 = (math.log(s/k)+(r+sigma*sigma/2)*t)/(sigma*math.sqrt(t))
    d2 = (math.log(s/k)+(r-sigma*sigma/2)*t)/(sigma*math.sqrt(t))
    retu s*norm.cdf(d1)-math.exp(-r*t)*k*norm.cdf(d2)  

print('B-S',  C(s0,10,t))

'''Explicit_finite_difference'''

EFD_n_x = 500
EFD_n_t = 100
EFD_k = Xmax/EFD_n_x
EFD_h = t/EFD_n_t
EFD_xx = np.linspace(Xmax,-Xmax, 2 * EFD_n_x + 1)
EFD_xx = EFD_xx[1:2 * EFD_n_x]

def EFD_T0_Bound(x):
    retu max(math.exp(x)-10*math.exp(-r*t),0) 
def EFD_U_Bound(tao):
    retu math.exp(Xmax)-10*math.exp(-r*(t-tao))
def EFD_L_Bound(tao):
    retu 0
EFD_T0bound = np.vectorize(EFD_T0_Bound)
EFD_lambda = EFD_h*sigma*sigma/2/EFD_k/EFD_k

EFD_A = (np.eye(2 * EFD_n_x - 1) * (1-2*EFD_lambda)
        + np.eye(2 * EFD_n_x - 1, k=1)*EFD_lambda
        + np.eye(2 * EFD_n_x - 1, k=-1)*EFD_lambda)

EFD_Y = np.zeros(2 * EFD_n_x - 1)
EFD_U = EFD_T0bound(EFD_xx)

for i in range(EFD_n_t):
    EFD_Y[0] = EFD_lambda*EFD_U_Bound(EFD_h*i)
    EFD_Y[2 * EFD_n_x - 2] = EFD_lambda*EFD_L_Bound(EFD_h*i)
    EFD_U = np.dot(EFD_A,EFD_U) + EFD_Y       #U_t_i+1 = A * U_t_i + Y

print('Explicit_finite_difference',EFD_U[EFD_n_x - 1 - round(math.log(s0)/EFD_k)])

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 21 فروردين 1395 ساعت: 9:17

صفحه بندی