└── Merton Jump Diffusion model.py /Merton Jump Diffusion model.py: -------------------------------------------------------------------------------- 1 | 2 | #Merton Jump Diffusion- Analytical and Simulation vs Black Scholes 3 | 4 | import math as math 5 | import numpy as np 6 | 7 | def sn_cdf(x): 8 | a1 = 0.254829592 9 | a2 = -0.284496736 10 | a3 = 1.421413741 11 | a4 = -1.453152027 12 | a5 = 1.061405429 13 | p = 0.3275911 14 | 15 | sign = 1 16 | if x < 0: 17 | sign = -1 18 | x = abs(x)/math.sqrt(2.0) 19 | 20 | t = 1.0/(1.0 + p*x) 21 | y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x) 22 | 23 | return 0.5*(1.0 + sign*y) 24 | 25 | 26 | def BS_call(S0,sig, tau,r, K): 27 | d1=math.log(S0/K)+(r+0.5*sig*sig)*tau 28 | d1/=(sig*math.sqrt(tau)) 29 | d2=d1-sig*math.sqrt(tau) 30 | 31 | price=S0*sn_cdf(d1)-K*math.exp(-r*tau)*sn_cdf(d2) 32 | return price 33 | 34 | def merton_call(S0,sig, tau,r, K,lam,mu_y,sig_y, N): 35 | price=0.0 36 | k=math.exp(mu_y+0.5*sig_y*sig_y)-1 37 | for n in range(N): 38 | sig_n=math.sqrt( sig*sig+n*sig_y*sig_y/tau) 39 | S0_n=S0*math.exp(-lam*k*tau+n*mu_y+0.5*n*sig_y*sig_y) 40 | prob_n=(lam*tau)**n/math.factorial(n)*math.exp(-lam*tau) 41 | price+=BS_call(S0_n,sig_n, tau,r, K)*prob_n 42 | return price 43 | 44 | def merton_call_alt(S0,sig, tau,r, K,lam,mu_y,sig_y, N): 45 | price=0.0 46 | k=math.exp(mu_y+0.5*sig_y*sig_y)-1 47 | lam_b=lam*(1.0+k) 48 | for n in range(N): 49 | sig_n=math.sqrt( sig*sig+n*sig_y*sig_y/tau) 50 | r_n=r-lam*k+n*(mu_y+0.5*sig_y*sig_y)/tau 51 | prob_n=(lam_b*tau)**n/math.factorial(n)*math.exp(-lam_b*tau) 52 | price+=BS_call(S0,sig_n, tau,r_n, K)*prob_n 53 | return price 54 | 55 | def merton_call_simulation(S0,sig, tau,r, K,lam,mu_y,sig_y): 56 | nsim=100000 57 | k=math.exp(mu_y+0.5*sig_y*sig_y)-1 58 | price=0.0 59 | for i in range(nsim): 60 | jump_N=np.random.poisson(lam*tau) 61 | jump_normal=np.random.normal(mu_y,sig_y,jump_N) 62 | jump_sum=np.sum(jump_normal) 63 | S_tau=S0*math.exp((r-lam*k-0.5*sig*sig)*tau+sig*np.random.normal(0,math.sqrt(tau))+jump_sum) 64 | price+=max(S_tau-K,0) 65 | price*=math.exp(-r*tau) 66 | price/=nsim 67 | return price 68 | 69 | 70 | print(BS_call(100,0.4, 1,0.05, 100)) 71 | print(merton_call(100,0.4, 1,0.05, 100,0.5, 0.1,0.1, 20)) 72 | print(merton_call_alt(100,0.4, 1,0.05, 100,0.5, 0.1,0.1, 20)) 73 | print(merton_call_simulation(100,0.4, 1,0.05, 100,0.5, 0.1,0.1)) --------------------------------------------------------------------------------