import scipy import math import numpy as np
S0=100;K=100;R=0.03;T=1;N=100;M=10000;sigma=0.3 np.random.seed(1)
paths=np.zeros((M,N),dtype=float,order='c') for j in range(M): paths[j,0]=np.random.normal(loc=0,scale=1,size=1)*np.sqrt(T/N)*sigma*S0 + T/N *R*S0 +S0 for i in range(1,N): for j in range(M): paths[j,i]=np.random.normal(loc=0,scale=1,size=1)*np.sqrt(T/N)*sigma*paths[j,i-1] + T/N *R*paths[j,i-1] +paths[j,i-1]
def asian_call_option(S0,K,R,T,N,M,sigma): c_asian_call = math.exp(-R * T) * np.sum(np.maximum(np.sum(paths,axis=1)/N - K, 0))/M print("c_asian_call=",c_asian_call) return asian_call_option(S0=100,K=100,R=0.03,T=1,N=100,M=10000,sigma=0.3)
def fixed_lookback_call_option(S0,K,R,T,N,M,sigma): c_fixed_lookback_call = math.exp(-R * T) * np.sum(np.maximum(np.max(paths,axis=1) - K, 0))/M print("c_fixed_lookback_call=",c_fixed_lookback_call) return fixed_lookback_call_option(S0=100,K=100,R=0.03,T=1,N=100,M=10000,sigma=0.3)
def knock_out_call_option(S0,K,R,T,N,M,sigma,knock_price): paths_barrier=np.zeros((M,N),dtype=float,order='c') paths_barrier[:,0]=paths[:,0] for j in range(M): for i in range(1,N): if paths[j,i]>knock_price: paths_barrier[j,i]=paths[j,i] else: break c_barrier_call = math.exp(-R * T) * np.sum(np.maximum(paths_barrier[:,-1] - K, 0))/M print("c_barrier_call=",c_barrier_call) return knock_out_call_option(S0=100,K=100,R=0.03,T=1,N=100,M=10000,sigma=0.3,knock_price=80)
|