# -*- coding: utf-8 -*- """ Weak EM scheme for Ornstein-Uhlenbeck, EM scheme for log normal. @author: turinici """ import numpy as np import matplotlib.pyplot as plt T,N=1,500 h=T/N M=1000 trange=np.linspace(0,T,N+1,endpoint=True) theta=1 mu=10 sigma=3. print('weak Euler Maruyama for Ornstein-Uhlenbeck') X=np.zeros((N+1,M)) for ii in range(N): xi=2*np.random.binomial(1,0.5,M)-1 X[ii+1,:] =X[ii,:]+ theta*(mu-X[ii,:])*h+np.sqrt(h)*sigma*xi plt.hist(X[-1,:]) plt.plot(X) print('Black-Scholes with Euler-Maruyama') mu=0.25 sigma=0.15 X=np.zeros((N+1,M)) X[0,:]=100 for ii in range(N): X[ii+1,:] =X[ii,:]+ mu*X[ii,:]*h+np.sqrt(h)*sigma*X[ii,:]*np.random.randn(M) plt.hist(X[-1,:]) plt.plot(X)