Assume that we want to describe the following SDE:
Ito form1:
\[\begin{equation}\label{eq:05} dX_{t} = \frac{1}{2}\theta^{2} X_{t} dt + \theta X_{t} dW_{t},\qquad X_{0}=x_{0} > 0 \end{equation}\] Stratonovich form: \[\begin{equation}\label{eq:06} dX_{t} = \frac{1}{2}\theta^{2} X_{t} dt +\theta X_{t} \circ dW_{t},\qquad X_{0}=x_{0} > 0 \end{equation}\]In the above \(f(t,x)=\frac{1}{2}\theta^{2} x\) and \(g(t,x)= \theta x\) (\(\theta > 0\)), \(W_{t}\) is a standard Wiener process. To simulate this models using snssde1d() function we need to specify:
drift and diffusion coefficients as R expressions that depend on the state variable x and time variable t.N=1000 (by default: N=1000).M=500 (by default: M=1).t0=0, x0=10 and end time T=1 (by default: t0=0, x0=0 and T=1).Dt=0.001 (by default: Dt=(T-t0)/N).type="ito" for Ito or type="str" for Stratonovich (by default type="ito").method (by default method="euler").theta = 0.5
f <- expression( (0.5*theta^2*x) )
g <- expression( theta*x )
mod1 <- snssde1d(drift=f,diffusion=g,x0=10,M=500,type="ito") # Using Ito
mod2 <- snssde1d(drift=f,diffusion=g,x0=10,M=500,type="str") # Using Stratonovich
mod1## Ito Sde 1D:
## | dX(t) = (0.5 * theta^2 * X(t)) * dt + theta * X(t) * dW(t)
## Method:
## | Euler scheme of order 0.5
## Summary:
## | Size of process | N = 1000.
## | Number of simulation | M = 500.
## | Initial value | x0 = 10.
## | Time of process | t in [0,1].
## | Discretization | Dt = 0.001.
mod2## Stratonovich Sde 1D:
## | dX(t) = (0.5 * theta^2 * X(t)) * dt + theta * X(t) o dW(t)
## Method:
## | Euler scheme of order 0.5
## Summary:
## | Size of process | N = 1000.
## | Number of simulation | M = 500.
## | Initial value | x0 = 10.
## | Time of process | t in [0,1].
## | Discretization | Dt = 0.001.
Using Monte-Carlo simulations, the following statistical measures (S3 method) for class snssde1d() can be approximated for the \(X_{t}\) process at any time \(t\):
mean.median.quantile.skewness and kurtosis.moment.bconfint.The summary of the results of mod1 and mod2 at time \(t=1\) of class snssde1d() is given by:
summary(mod1, at = 1)##
## Monte-Carlo Statistics for X(t) at time t = 1
##
## Mean 11.40353
## Variance 34.13210
## Median 10.25887
## First quartile 7.25841
## Third quartile 14.08331
## Skewness 2.46228
## Kurtosis 18.19171
## Moment of order 3 491.00057
## Moment of order 4 21193.33999
## Moment of order 5 976221.18991
## Int.conf Inf (95%) 4.12423
## Int.conf Sup (95%) 23.59981
summary(mod2, at = 1)##
## Monte-Carlo Statistics for X(t) at time t = 1
##
## Mean 10.10946
## Variance 30.84606
## Median 8.73994
## First quartile 6.40033
## Third quartile 12.43298
## Skewness 1.96516
## Kurtosis 9.70802
## Moment of order 3 336.66411
## Moment of order 4 9236.98164
## Moment of order 5 254177.41724
## Int.conf Inf (95%) 3.62197
## Int.conf Sup (95%) 24.37419
Hence we can just make use of the rsde1d() function to build our random number generator for the conditional density of the \(X_{t}|X_{0}\) (\(X_{t}^{\text{mod1}}| X_{0}\) and \(X_{t}^{\text{mod2}}|X_{0}\)) at time \(t = 1\).
x1 <- rsde1d(object = mod1, at = 1) # X(t=1) | X(0)=x0 (Itô SDE)
x2 <- rsde1d(object = mod2, at = 1) # X(t=1) | X(0)=x0 (Stratonovich SDE)
summary(data.frame(x1,x2))## x1 x2
## Min. : 2.245 Min. : 1.403
## 1st Qu.: 7.258 1st Qu.: 6.400
## Median :10.259 Median : 8.740
## Mean :11.404 Mean :10.109
## 3rd Qu.:14.083 3rd Qu.:12.433
## Max. :64.986 Max. :48.100
The function dsde1d() can be used to show the kernel density estimation for \(X_{t}|X_{0}\) at time \(t=1\) with log-normal curves:
mu1 = log(10); sigma1= sqrt(theta^2) # log mean and log variance for mod1
mu2 = log(10)-0.5*theta^2 ; sigma2 = sqrt(theta^2) # log mean and log variance for mod2
AppdensI <- dsde1d(mod1, at = 1)
AppdensS <- dsde1d(mod2, at = 1)
plot(AppdensI , dens = function(x) dlnorm(x,meanlog=mu1,sdlog = sigma1))
plot(AppdensS , dens = function(x) dlnorm(x,meanlog=mu2,sdlog = sigma2))
In Figure 2, we present the flow of trajectories, the mean path (red lines) of solution of and , with their empirical \(95\%\) confidence bands, that is to say from the \(2.5th\) to the \(97.5th\) percentile for each observation at time \(t\) (blue lines):
plot(mod1,plot.type="single",ylab=expression(X^mod1))
lines(time(mod1),mean(mod1),col=2,lwd=2)
lines(time(mod1),bconfint(mod1,level=0.95)[,1],col=4,lwd=2)
lines(time(mod1),bconfint(mod1,level=0.95)[,2],col=4,lwd=2)
legend("topleft",c("mean path",paste("bound of",95,"% confidence")),col=c(2,4),lwd=2,cex=0.8)
plot(mod2,plot.type="single",ylab=expression(X^mod2))
lines(time(mod2),mean(mod2),col=2,lwd=2)
lines(time(mod2),bconfint(mod2,level=0.95)[,1],col=4,lwd=2)
lines(time(mod2),bconfint(mod2,level=0.95)[,2],col=4,lwd=2)
legend("topleft",c("mean path",paste("bound of",95,"% confidence")),col=c(2,4),lwd=2,cex=0.8)
The following \(2\)-dimensional SDE’s with a vector of drift and a diagonal matrix of diffusion coefficients:
Ito form: \[\begin{equation}\label{eq:09} \begin{cases} dX_t = f_{x}(t,X_{t},Y_{t}) dt + g_{x}(t,X_{t},Y_{t}) dW_{1,t}\\ dY_t = f_{y}(t,X_{t},Y_{t}) dt + g_{y}(t,X_{t},Y_{t}) dW_{2,t} \end{cases} \end{equation}\] Stratonovich form: \[\begin{equation}\label{eq:10} \begin{cases} dX_t = f_{x}(t,X_{t},Y_{t}) dt + g_{x}(t,X_{t},Y_{t}) \circ dW_{1,t}\\ dY_t = f_{y}(t,X_{t},Y_{t}) dt + g_{y}(t,X_{t},Y_{t}) \circ dW_{2,t} \end{cases} \end{equation}\]\(W_{1,t}\) and \(W_{2,t}\) is a two independent standard Wiener process. To simulate \(2d\) models using snssde2d() function we need to specify:
drift (2d) and diffusion (2d) coefficients as R expressions that depend on the state variable x, y and time variable t.N (default: N=1000).M (default: M=1).t0, x0 and end time T (default: t0=0, x0=c(0,0) and T=1).Dt (default: Dt=(T-t0)/N).type="ito" for Ito or type="str" for Stratonovich (default type="ito").method (default method="euler").We simulate a flow of \(500\) trajectories of \((X_{t},Y_{t})\), with integration step size \(\Delta t = 0.01\), and using second Milstein method.
x=5;y=0
mu=3;sigma=0.5
fx <- expression(-(x/mu),x)
gx <- expression(sqrt(sigma),0)
mod2d <- snssde2d(drift=fx,diffusion=gx,Dt=0.01,M=500,x0=c(x,y),method="smilstein")
mod2d## Ito Sde 2D:
## | dX(t) = -(X(t)/mu) * dt + sqrt(sigma) * dW1(t)
## | dY(t) = X(t) * dt + 0 * dW2(t)
## Method:
## | Second Milstein scheme of order 1.5
## Summary:
## | Size of process | N = 1000.
## | Number of simulation | M = 500.
## | Initial values | (x0,y0) = (5,0).
## | Time of process | t in [0,10].
## | Discretization | Dt = 0.01.
summary(mod2d)##
## Monte-Carlo Statistics for (X(t),Y(t)) at time t = 10
## X Y
## Mean 0.15038 14.03061
## Variance 0.76853 26.45717
## Median 0.12574 13.97368
## First quartile -0.43664 10.27947
## Third quartile 0.72965 17.67226
## Skewness 0.04405 0.01798
## Kurtosis 2.85390 2.94868
## Moment of order 3 0.02968 2.44685
## Moment of order 4 1.68563 2064.02471
## Moment of order 5 0.12083 -1315.88866
## Int.conf Inf (95%) -1.56840 4.59106
## Int.conf Sup (95%) 1.84745 24.27626
For plotting (back in time) using the command plot, the results of the simulation are shown in Figure 3.
plot(mod2d)
Take note of the well known result, which can be derived from either this equations. That for any \(t > 0\) the OU process \(X_t\) and its integral \(Y_t\) will be the normal distribution with mean and variance given by: \[ \begin{cases} \text{E}(X_{t}) =x_{0} e^{-t/\mu} &\text{and}\quad\text{Var}(X_{t})=\frac{\sigma \mu}{2} \left (1-e^{-2t/\mu}\right )\\ \text{E}(Y_{t}) = y_{0}+x_{0}\mu \left (1-e^{-t/\mu}\right ) &\text{and}\quad\text{Var}(Y_{t})=\sigma\mu^{3}\left (\frac{t}{\mu}-2\left (1-e^{-t/\mu}\right )+\frac{1}{2}\left (1-e^{-2t/\mu}\right )\right ) \end{cases} \]
Hence we can just make use of the rsde2d() function to build our random number for \((X_{t},Y_{t})\) at time \(t = 10\).
out <- rsde2d(object = mod2d, at = 10)
summary(out)## x y
## Min. :-2.4036 Min. :-2.885
## 1st Qu.:-0.4366 1st Qu.:10.279
## Median : 0.1257 Median :13.974
## Mean : 0.1504 Mean :14.031
## 3rd Qu.: 0.7296 3rd Qu.:17.672
## Max. : 2.9342 Max. :29.632
For each SDE type and for each numerical scheme, the density of \(X_t\) and \(Y_t\) at time \(t=10\) are reported using dsde2d() function, see e.g. Figure 4: the marginal density of \(X_t\) and \(Y_t\) at time \(t=10\).
denM <- dsde2d(mod2d,pdf="M",at =10)
denM##
## Marginal density for the conditional law of X(t)|X(0) at time t = 10
##
## Data: x (500 obs.); Bandwidth 'bw' = 0.226
##
## x f(x)
## Min. :-3.081710 Min. :0.0000398
## 1st Qu.:-1.408215 1st Qu.:0.0077504
## Median : 0.265281 Median :0.0836797
## Mean : 0.265281 Mean :0.1492413
## 3rd Qu.: 1.938777 3rd Qu.:0.2912292
## Max. : 3.612272 Max. :0.4404050
##
## Marginal density for the conditional law of Y(t)|Y(0) at time t = 10
##
## Data: y (500 obs.); Bandwidth 'bw' = 1.336
##
## y f(y)
## Min. :-6.89196 Min. :0.00000785
## 1st Qu.: 3.24084 1st Qu.:0.00139667
## Median :13.37364 Median :0.01218128
## Mean :13.37364 Mean :0.02464810
## 3rd Qu.:23.50644 3rd Qu.:0.04939563
## Max. :33.63924 Max. :0.07406468
plot(denM, main="Marginal Density")
Created using dsde2d() plotted in (x, y)-space with dim = 2. A contour and image plot of density obtained from a realization of system \((X_{t},Y_{t})\) at time t=10.
denJ <- dsde2d(mod2d,pdf="J",at =10)
denJ##
## Joint density for the conditional law of X(t),Y(t)|X(0),Y(0) at time t = 10
##
## Data: (x,y) (2 x 500 obs.);
##
## x y f(x,y)
## Min. :-2.403643 Min. :-2.884757 Min. :0.00000000
## 1st Qu.:-1.069181 1st Qu.: 5.244442 1st Qu.:0.00011450
## Median : 0.265281 Median :13.373641 Median :0.00145132
## Mean : 0.265281 Mean :13.373641 Mean :0.00561250
## 3rd Qu.: 1.599743 3rd Qu.:21.502839 3rd Qu.:0.00739674
## Max. : 2.934206 Max. :29.632038 Max. :0.03511061
plot(denJ,display="contour",main="Bivariate Density")
plot(denJ,display="image",drawpoints=TRUE,col.pt="green",cex=0.25,pch=19,main="Bivariate Density")
A \(3\)D plot of the density obtained with:
plot(denJ,display="rgl",main="Bivariate Density")You must enable Javascript to view this page properly.
Implemente in R as follows, with integration step size \(\Delta t = 0.01\) and using stochastic Runge-Kutta methods 1-stage.
mu = 4; sigma=0.1
fx <- expression( y , (mu*( 1-x^2 )* y - x))
gx <- expression( 0 ,2*sigma)
mod2d <- snssde2d(drift=fx,diffusion=gx,N=10000,Dt=0.01,type="str",method="rk1")
mod2d## Stratonovich Sde 2D:
## | dX(t) = Y(t) * dt + 0 o dW1(t)
## | dY(t) = (mu * (1 - X(t)^2) * Y(t) - X(t)) * dt + 2 * sigma o dW2(t)
## Method:
## | Runge-Kutta method of order 1
## Summary:
## | Size of process | N = 10000.
## | Number of simulation | M = 1.
## | Initial values | (x0,y0) = (0,0).
## | Time of process | t in [0,100].
## | Discretization | Dt = 0.01.
plot2d(mod2d) ## in plane (O,X,Y)
plot(mod2d) ## back in time
The following \(3\)-dimensional SDE’s with a vector of drift and a diagonal matrix of diffusion coefficients:
Ito form: \[\begin{equation}\label{eq17} \begin{cases} dX_t = f_{x}(t,X_{t},Y_{t},Z_{t}) dt + g_{x}(t,X_{t},Y_{t},Z_{t}) dW_{1,t}\\ dY_t = f_{y}(t,X_{t},Y_{t},Z_{t}) dt + g_{y}(t,X_{t},Y_{t},Z_{t}) dW_{2,t}\\ dZ_t = f_{z}(t,X_{t},Y_{t},Z_{t}) dt + g_{z}(t,X_{t},Y_{t},Z_{t}) dW_{3,t} \end{cases} \end{equation}\] Stratonovich form: \[\begin{equation}\label{eq18} \begin{cases} dX_t = f_{x}(t,X_{t},Y_{t},Z_{t}) dt + g_{x}(t,X_{t},Y_{t},Z_{t}) \circ dW_{1,t}\\ dY_t = f_{y}(t,X_{t},Y_{t},Z_{t}) dt + g_{y}(t,X_{t},Y_{t},Z_{t}) \circ dW_{2,t}\\ dZ_t = f_{z}(t,X_{t},Y_{t},Z_{t}) dt + g_{z}(t,X_{t},Y_{t},Z_{t}) \circ dW_{3,t} \end{cases} \end{equation}\]\(W_{1,t}\), \(W_{2,t}\) and \(W_{3,t}\) is a 3 independent standard Wiener process. To simulate this system using snssde3d() function we need to specify:
drift (3d) and diffusion (3d) coefficients as R expressions that depend on the state variables x, y , z and time variable t.N (default: N=1000).M (default: M=1).t0, x0 and end time T (default: t0=0, x0=c(0,0,0) and T=1).Dt (default: Dt=(T-t0)/N).type="ito" for Ito or type="str" for Stratonovich (default type="ito").method (default method="euler").We simulate a flow of 500 trajectories, with integration step size \(\Delta t = 0.001\).
fx <- expression(4*(-1-x)*y , 4*(1-y)*x , 4*(1-z)*y)
gx <- rep(expression(0.2),3)
mod3d <- snssde3d(x0=c(x=2,y=-2,z=-2),drift=fx,diffusion=gx,N=1000,M=500)
mod3d## Ito Sde 3D:
## | dX(t) = 4 * (-1 - X(t)) * Y(t) * dt + 0.2 * dW1(t)
## | dY(t) = 4 * (1 - Y(t)) * X(t) * dt + 0.2 * dW2(t)
## | dZ(t) = 4 * (1 - Z(t)) * Y(t) * dt + 0.2 * dW3(t)
## Method:
## | Euler scheme of order 0.5
## Summary:
## | Size of process | N = 1000.
## | Number of simulation | M = 500.
## | Initial values | (x0,y0,z0) = (2,-2,-2).
## | Time of process | t in [0,1].
## | Discretization | Dt = 0.001.
summary(mod3d)##
## Monte-Carlo Statistics for (X(t),Y(t),Z(t)) at time t = 1
## X Y Z
## Mean -0.79363 0.89824 0.79638
## Variance 0.01017 0.10923 0.00896
## Median -0.80195 0.87184 0.80025
## First quartile -0.86826 0.66836 0.73532
## Third quartile -0.73147 1.12565 0.86637
## Skewness 0.26669 0.24028 -0.34912
## Kurtosis 2.74278 3.18534 2.83584
## Moment of order 3 0.00027 0.00867 -0.00030
## Moment of order 4 0.00028 0.03801 0.00023
## Moment of order 5 0.00002 0.00945 -0.00002
## Int.conf Inf (95%) -0.97932 0.27582 0.59075
## Int.conf Sup (95%) -0.58567 1.59040 0.96242
plot(mod3d,union = TRUE) ## back in time
plot3D(mod3d,display="persp") ## in space (O,X,Y,Z)
For each SDE type and for each numerical scheme, the marginal density of \(X_t\), \(Y_t\) and \(Z_t\) at time \(t=1\) are reported using dsde3d() function, see e.g. Figure 8.
den <- dsde3d(mod3d,at =1)
den##
## Marginal density for the conditional law of X(t)|X(0) at time t = 1
##
## Data: x (500 obs.); Bandwidth 'bw' = 0.02619
##
## x f(x)
## Min. :-1.1148777 Min. :0.000402
## 1st Qu.:-0.9431618 1st Qu.:0.199276
## Median :-0.7714459 Median :1.067292
## Mean :-0.7714459 Mean :1.454461
## 3rd Qu.:-0.5997300 3rd Qu.:2.717240
## Max. :-0.4280140 Max. :3.923853
##
## Marginal density for the conditional law of Y(t)|Y(0) at time t = 1
##
## Data: y (500 obs.); Bandwidth 'bw' = 0.08583
##
## y f(y)
## Min. :-0.3775533 Min. :0.0001050
## 1st Qu.: 0.3028136 1st Qu.:0.0142478
## Median : 0.9831806 Median :0.1460117
## Mean : 0.9831806 Mean :0.3670882
## 3rd Qu.: 1.6635476 3rd Qu.:0.7306455
## Max. : 2.3439145 Max. :1.2486437
##
## Marginal density for the conditional law of Z(t)|Z(0) at time t = 1
##
## Data: z (500 obs.); Bandwidth 'bw' = 0.02458
##
## z f(z)
## Min. :0.4333812 Min. :0.000405
## 1st Qu.:0.5966982 1st Qu.:0.188503
## Median :0.7600151 Median :0.955669
## Mean :0.7600151 Mean :1.529261
## 3rd Qu.:0.9233321 3rd Qu.:2.865271
## Max. :1.0866490 Max. :3.911343
plot(den, main="Marginal Density")
For Joint density for \((X_t,Y_t,Z_t)\) see package sm or ks.
out <- rsde3d(mod3d,at =1)
library(sm)
sm.density(out,display="rgl")
##
library(ks)
fhat <- kde(x=out)
plot(fhat, drawpoints=TRUE)with initial conditions \((X_{0},Y_{0},Z_{0})=(1,1,1)\), by specifying the drift and diffusion coefficients of three processes \(X_{t}\), \(Y_{t}\) and \(Z_{t}\) as R expressions which depends on the three state variables (x,y,z) and time variable t, with integration step size Dt=0.0001.
K = 4; s = 1; sigma = 0.2
fx <- expression( (-K*x/sqrt(x^2+y^2+z^2)) , (-K*y/sqrt(x^2+y^2+z^2)) , (-K*z/sqrt(x^2+y^2+z^2)) )
gx <- rep(expression(sigma),3)
mod3d <- snssde3d(drift=fx,diffusion=gx,N=10000,x0=c(x=1,y=1,z=1))
mod3d## Ito Sde 3D:
## | dX(t) = (-K * X(t)/sqrt(X(t)^2 + Y(t)^2 + Z(t)^2)) * dt + sigma * dW1(t)
## | dY(t) = (-K * Y(t)/sqrt(X(t)^2 + Y(t)^2 + Z(t)^2)) * dt + sigma * dW2(t)
## | dZ(t) = (-K * Z(t)/sqrt(X(t)^2 + Y(t)^2 + Z(t)^2)) * dt + sigma * dW3(t)
## Method:
## | Euler scheme of order 0.5
## Summary:
## | Size of process | N = 10000.
## | Number of simulation | M = 1.
## | Initial values | (x0,y0,z0) = (1,1,1).
## | Time of process | t in [0,1].
## | Discretization | Dt = 1e-04.
The results of simulation are shown:
plot3D(mod3d,display="rgl",col="blue")You must enable Javascript to view this page properly.
run by calling the function snssde3d() to produce a simulation of the solution, with \(\mu = 1\) and \(\sigma = 1\).
fx <- expression(y,0,0)
gx <- expression(z,1,1)
modtra <- snssde3d(drift=fx,diffusion=gx,M=500)
modtra## Ito Sde 3D:
## | dX(t) = Y(t) * dt + Z(t) * dW1(t)
## | dY(t) = 0 * dt + 1 * dW2(t)
## | dZ(t) = 0 * dt + 1 * dW3(t)
## Method:
## | Euler scheme of order 0.5
## Summary:
## | Size of process | N = 1000.
## | Number of simulation | M = 500.
## | Initial values | (x0,y0,z0) = (0,0,0).
## | Time of process | t in [0,1].
## | Discretization | Dt = 0.001.
summary(modtra)##
## Monte-Carlo Statistics for (X(t),Y(t),Z(t)) at time t = 1
## X Y Z
## Mean 0.03918 0.02304 -0.08398
## Variance 0.86747 1.01719 1.01050
## Median 0.00060 0.07058 -0.12287
## First quartile -0.57320 -0.69130 -0.74918
## Third quartile 0.58203 0.69016 0.58294
## Skewness 0.32959 -0.05206 0.17379
## Kurtosis 4.15533 2.97478 3.31916
## Moment of order 3 0.26629 -0.05341 0.17653
## Moment of order 4 3.12690 3.07794 3.38920
## Moment of order 5 3.79900 -0.22436 1.66325
## Int.conf Inf (95%) -1.70857 -2.00373 -2.05616
## Int.conf Sup (95%) 2.04740 1.90640 2.05156
the following code produces the result in Figure 9.
plot(modtra$X,plot.type="single",ylab="X")
lines(time(modtra),mean(modtra)$X,col=2,lwd=2)
lines(time(modtra),bconfint(modtra,level=0.95)$X[,1],col=4,lwd=2)
lines(time(modtra),bconfint(modtra,level=0.95)$X[,2],col=4,lwd=2)
legend("topleft",c("mean path",paste("bound of",95,"% confidence")),col=c(2,4),lwd=2,cex=0.8)
The histogram and kernel density of \(X_t\) at time \(t=1\) are reported using dsde3d() function, see e.g. Figure 10.
den <- dsde3d(modtra,at=1)
den$resx##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (500 obs.); Bandwidth 'bw' = 0.2239
##
## x y
## Min. :-3.5243 Min. :0.0000404
## 1st Qu.:-1.4024 1st Qu.:0.0035571
## Median : 0.7194 Median :0.0359903
## Mean : 0.7194 Mean :0.1177059
## 3rd Qu.: 2.8413 3rd Qu.:0.2143911
## Max. : 4.9631 Max. :0.4566161
MASS::truehist(den$ech$x,xlab = expression(X[t==1]));box()
lines(den$resx,col="red",lwd=2)
legend("topleft",c("Distribution histogram","Kernel Density"),inset =.01,pch=c(15,NA),lty=c(NA,1),col=c("cyan","red"),lwd=2,cex=0.8)
The equivalently of \(X_{t}^{\text{mod1}}\) the following Stratonovich SDE: \(dX_{t} = \theta X_{t} \circ dW_{t}\).↩