期權
屏障選項程式碼說明
我想知道是否有人可以解釋障礙選項定價背後的程式碼(特別是 def(up_and_out_call) 部分。我發現循環概念中的循環非常混亂。謝謝。
import numpy as np from scipy import stats from math import log,exp,sqrt s0=30. # today stock price K=30 # exercise price barrier=32 # barrier level T=6./12. # maturity in years r=0.05 # risk-free rate sig=0.2 # volatility (annualized) n_sim=100 # number of simulations np.random.seed(12) # fix a seed # def bsCall(s0,K,T,r,sig): d1=(log(s0/K)+(r+sig*sig/2.)*T)/(sig*sqrt(T)) d2= d1-sig*sqrt(T) return s0*stats.norm.cdf(d1)-K*exp(-r*T)*stats.norm.cdf(d2) # def up_and_out_call(s0,K,T,r,sig,n_sim,barrier): n_steps=100 # for each simulation there are 100 steps dt=T/n_steps #time increment total=0 for j in np.arange(0, n_sim): x=s0 #sT=s0 out=False for i in range(0,int(n_steps)): n=np.random.normal() x*=exp((r-0.5*sig*sig)*dt+sig*n*sqrt(dt)) if x>barrier: #for all paths greater than the barrier level, they expire worthless out=True if out==False: total+=bsCall(s0,K,T,r,sig) return total/n_sim # result=up_and_out_call(s0,K,T,r,sig,n_sim,barrier) print('up-and-out-call = ', round(result,5)) #prints 'up-and-out-call = result up to 5 decimal places.