It is a good idea to use the Monte Carlo simulation to present such parity. The following code is designed to achieve this. To make our simulation clearer, we deliberately choose just five simulations:
import p4f import scipy as sp import matplotlib.pyplot as plt # s =9.25 # stock price at time zero x =9.10 # exercise price barrier=10.5 # barrier T =0.5 # maturity date (in years) n_steps=30 # number of steps r =0.05 # expected annual return sigma = 0.2 # volatility (annualized) sp.random.seed(125) # seed() n_simulation = 5 # number of simulations # dt =T/n_steps S = sp.zeros([n_steps], dtype=float) time_= range(0, int(n_steps), 1) c=p4f.bs_call(s,x,T,r,sigma) sp.random.seed(124) outTotal, inTotal= 0.,0. n_out,n_in=0,0 for j in range(0, n_simulation): S[0]= s inStatus=False outStatus=True for i in time_[:-1]: e=sp.random.normal() ...