importpandasaspdimportnumpyasnpfromscipy.statsimportnormdefBlackScholes(S,K,r,sigma,T,type):ifS<=0:print('Stock Price should greater than 0')returnNoneelse:d1=(np.log(S/K)+(r+sigma**2/2)*(T))/(sigma*np.sqrt(T))d2=d1-sigma*np.sqrt(T)N_d1=norm.cdf(d1);N_d1m=norm.cdf(-d1)N_d2=norm.cdf(d2);N_d2m=norm.cdf(-d2)iftype=="Call":price=N_d1*S-N_d2*K*np.exp(-r*T)eliftype=="Put":price=N_d2m*K*np.exp(-r*T)-N_d1m*Selse:price=None;return(price)# Test
call_bs=BlackScholes(100,100,0.05,0.2,1,"Call")put_bs=BlackScholes(100,100,0.05,0.2,1,"Put")defBachelier(S,K,r,sigmaS,T,type):ifS<=0:print('Stock Price should greater than 0')returnNoneelse:d1=(S-K)/(sigmaS*np.sqrt(T))N_d1=norm.cdf(d1);N_d1m=norm.cdf(-d1)iftype=="Call":price=(S-K)*N_d1+sigmaS*np.sqrt(T)*norm.pdf(d1)eliftype=="Put":price=(K-S)*N_d1m+sigmaS*np.sqrt(T)*norm.pdf(d1)else:price=Nonereturn(price)# Test
call_bach=Bachelier(100,100,0.05,0.2*100,1,"Call")put_bach=Bachelier(100,100,0.05,0.2*100,1,"Put")defBachelierMod(S,K,r,sigmaS,T,type):ifS<=0:print('Stock Price should greater than 0')returnNoneelse:d1=(S-K)/(sigmaS*np.sqrt(T))N_d1=norm.cdf(d1);N_d1m=norm.cdf(-d1)iftype=="Call":price=S*N_d1-K*np.exp(-r*T)*N_d1+sigmaS*np.sqrt(T)*norm.pdf(d1)eliftype=="Put":price=K*np.exp(-r*T)*N_d1m-S*N_d1m+sigmaS*np.sqrt(T)*norm.pdf(d1)else:price=Nonereturn(price)