I have a numpy code to generate Merton jump.
poi_rv = np.multiply(np.random.poisson( lam*dt, size=size),
np.random.normal(m,v, size=size)).cumsum(axis=0)
geo = np.cumsum(((r - sigma**2/2 -lam*(m + v**2*0.5))*dt +\
sigma*np.sqrt(dt) * \
np.random.normal(size=size)), axis=0)
numpy_out = np.exp(geo+poi_rv)
Now, I want to do the smae function but with torch.tensor instead of numpy.
I wrote this code:
rates = torch.rand(steps,Npaths)
poisson = torch.poisson(rates)
poi_rv_torch = torch.mul(poisson, (torch.randn(size)*v).cumsum(dim=0))
geo_torch = torch.cumsum(((r - sigma_t ** 2 / 2 - lam_t * (m_t + v_t ** 2 * 0.5)) * \
dt_t +sigma_t * torch.sqrt(dt_t) * torch.randn(size)), dim=0)
torch_out = torch.exp(geo_torch + poi_rv_torch)
where:
S = 100 # current stock price
T = 1 # time to maturity
r = 0.02 # risk free rate
m = 0 # meean of jump size
v = 0.3 # standard deviation of jump
lam =1.0 # intensity of jump i.e. number of jumps per annum
steps =10000 # time steps
Npaths = 1 # number of paths to simulate
sigma = 0.2 # annaul standard deviation , for weiner process
r_t,m_t,v_t,lam_t,sigma_t = torch.tensor(r),torch.tensor(m),torch.tensor(v),torch.tensor(lam),torch.tensor(sigma)
But when I plot the outputs, it show big differences between both numpy_out and torch_out code as shown in the figures.
I use torch.manual_seed(4) and np.random.seed(4).