def down_and_in_put(s0,x,T,r,sigma,n_simulation,barrier): n_steps=100. dt=T/n_steps total=0 for j in range(0, n_simulation): sT=s0 in_=False for i in range(0,int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if sT<barrier: in_=True #print 'sT=',sT #print 'j=',j ,'out=',out if in_==True: total+=p4f.bs_put(s0,x,T,r,sigma) return total/n_simulation
def down_and_in_put(s0, x, T, r, sigma, n_simulation, barrier): n_steps = 100. dt = T / n_steps total = 0 for j in range(0, n_simulation): sT = s0 in_ = False for i in range(0, int(n_steps)): e = sp.random.normal() sT *= sp.exp((r - 0.5 * sigma * sigma) * dt + sigma * e * sp.sqrt(dt)) if sT < barrier: in_ = True #print 'sT=',sT #print 'j=',j ,'out=',out if in_ == True: total += p4f.bs_put(s0, x, T, r, sigma) return total / n_simulation
""" Name : 4375OS_10_23_if_one_loop_and_break_implied_vol_using_put.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import p4f S=40;K=40;T=0.5;r=0.05;sigma=0.2;p=1.77 diff=1;i=1 sigma_old=0.005 sign_1=sign(p-p4f.bs_put(S,K,T,r,sigma_old)) while(1==1): sigma=0.0001*(i+1) sign_2=sign(p-p4f.bs_put(S,K,T,r,sigma)) i+=1 if sign_1*sign_2<0: break else: sigma_old=sigma print('i, implied-vol, diff') print(i,(sigma_old+sigma)/2, diff)
""" Name : 4375OS_10_25_impliws_col_using_put.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import p4f S = 40 K = 40 T = 0.5 r = 0.05 sigma = 0.2 p = 1.77 diff = 1 i = 1 while abs(diff) > 0.01: sigma = 0.005 * (i + 1) diff = p - p4f.bs_put(S, K, T, r, sigma) i += 1 print('i, implied-vol, diff') print(i, sigma, diff)
""" Name : 4375OS_10_25_impliws_col_using_put.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import p4f S=40;K=40;T=0.5;r=0.05;sigma=0.2;p=1.77 diff=1;i=1 while abs(diff)>0.01: sigma=0.005*(i+1) diff=p-p4f.bs_put(S,K,T,r,sigma) i+=1 print('i, implied-vol, diff') print(i,sigma, diff)