# Converting Sim.DiffProc Objects to LaTeX

#### A.C. Guidoum1 and K. Boukhetala2

#### 2020-05-04

# The `TEX.sde()` function

`TEX.sde(object,...)` produces the related LATEX code (table and mathematic expression) for Sim.DiffProc environment, which can be copied and pasted in a scientific article.

- `object`: an objects from class `MCM.sde()` and `MEM.sde()`. Or an `R` vector of expressions of SDEs, i.e., drift and diffusion coefficients.
- `...`: arguments to be passed to `kable()` function available in knitr package (Xie, 2015), if `object` from class `MCM.sde()`.

## LaTeX table for object of class `MCM.sde`

The Monte Carlo results of `MCM.sde` class can be presented in terms of LaTeX tables.

\[\begin{equation}\label{eq01} \begin{cases} dX_t = -\frac{1}{\mu} X_t dt + \sqrt{\sigma} dW_t\\ dY_t = X_{t} dt \end{cases} \end{equation}\]

    R> mu=1;sigma=0.5;theta=2
    R> x0=0;y0=0;init=c(x0,y0)
    R> f <- expression(1/mu*(theta-x), x)
    R> g <- expression(sqrt(sigma),0)
    R> mod2d <- snssde2d(drift=f,diffusion=g,M=500,Dt=0.015,x0=c(x=0,y=0))
    R> ## true values of first and second moment at time 10
    R> Ex <- function(t) theta+(x0-theta)*exp(-t/mu)
    R> Vx <- function(t) 0.5*sigma*mu *(1-exp(-2*(t/mu)))
    R> Ey <- function(t) y0+theta*t+(x0-theta)*mu*(1-exp(-t/mu))
    R> Vy <- function(t) sigma*mu^3*((t/mu)-2*(1-exp(-t/mu))+0.5*(1-exp(-2*(t/mu))))
    R> covxy <- function(t) 0.5*sigma*mu^2 *(1-2*exp(-t/mu)+exp(-2*(t/mu)))
    R> tvalue = list(m1=Ex(15),m2=Ey(15),S1=Vx(15),S2=Vy(15),C12=covxy(15))
    R> ## function of the statistic(s) of interest.
    R> sde.fun2d <- function(data, i){
    +   d <- data[i,]
    +   return(c(mean(d$x),mean(d$y),var(d$x),var(d$y),cov(d$x,d$y)))
    + }
    R> ## Parallel Monte-Carlo of 'OUI' at time 10
    R> mcm.mod2d = MCM.sde(mod2d,statistic=sde.fun2d,time=15,R=10,exact=tvalue,parallel="snow",ncpus=2)
    R> mcm.mod2d$MC

        Exact Estimate     Bias Std.Error    RMSE    CI( 2.5 % , 97.5 % )
    m1   2.00  2.01036  0.01036   0.00582 0.02031   ( 1.99895 , 2.02177 )
    m2  28.00 27.98151 -0.01849   0.03814 0.11592 ( 27.90676 , 28.05626 )
    S1   0.25  0.25189  0.00189   0.00396 0.01203   ( 0.24413 , 0.25965 )
    S2   6.75  6.70796 -0.04204   0.12820 0.38688   ( 6.45669 , 6.95923 )
    C12  0.25  0.24826 -0.00174   0.01998 0.05997    ( 0.2091 , 0.28742 )

In R we create simple LaTeX table for this object using the following code:

    R> TEX.sde(object = mcm.mod2d, booktabs = TRUE, align = "r", caption ="LaTeX
    +           table for Monte Carlo results generated by `TEX.sde()` method.")

    %%% LaTeX table generated in R 4.0.0 by TEX.sde() method
    %%% Copy and paste the following output in your LaTeX file

    \begin{table}

    \caption{\label{tab:unnamed-chunk-2}LaTeX
              table for Monte Carlo results generated by `TEX.sde()` method.}
    \centering
    \begin{tabular}[t]{lrrrrrr}
    \toprule
      & Exact & Estimate & Bias & Std.Error & RMSE & CI( 2.5 \% , 97.5 \% )\\
    \midrule
    $m_{1}(t)$ & 2.00 & 2.01036 & 0.01036 & 0.00582 & 0.02031 & ( 1.99895 , 2.02177 )\\
    $m_{2}(t)$ & 28.00 & 27.98151 & -0.01849 & 0.03814 & 0.11592 & ( 27.90676 , 28.05626 )\\
    $S_{1}(t)$ & 0.25 & 0.25189 & 0.00189 & 0.00396 & 0.01203 & ( 0.24413 , 0.25965 )\\
    $S_{2}(t)$ & 6.75 & 6.70796 & -0.04204 & 0.12820 & 0.38688 & ( 6.45669 , 6.95923 )\\
    $C_{12}(t)$ & 0.25 & 0.24826 & -0.00174 & 0.01998 & 0.05997 & ( 0.2091 , 0.28742 )\\
    \bottomrule
    \end{tabular}
    \end{table}

For inclusion in LaTeX documents, and optionally if we use `booktabs = TRUE` in the previous function, the LaTeX add-on package `booktabs` must be loaded into the `.tex` document.

LaTeX table for Monte Carlo results generated by `TEX.sde()` method.
| Exact | Estimate | Bias | Std.Error | RMSE | CI( 2.5 % , 97.5 % )
--- | --- | --- | --- | --- | --- | ---
m1 | 2.00 | 2.01036 | 0.01036 | 0.00582 | 0.02031 | ( 1.99895 , 2.02177 )
m2 | 28.00 | 27.98151 | -0.01849 | 0.03814 | 0.11592 | ( 27.90676 , 28.05626 )
S1 | 0.25 | 0.25189 | 0.00189 | 0.00396 | 0.01203 | ( 0.24413 , 0.25965 )
S2 | 6.75 | 6.70796 | -0.04204 | 0.12820 | 0.38688 | ( 6.45669 , 6.95923 )
C12 | 0.25 | 0.24826 | -0.00174 | 0.01998 | 0.05997 | ( 0.2091 , 0.28742 )

## LaTeX mathematic for object of class `MEM.sde`

we want to automatically generate the LaTeX code appropriate to moment equations obtained from the previous model using `TEX.sde()` method.

    R> mem.oui <- MEM.sde(drift = f, diffusion = g)
    R> mem.oui

    Itô Sde 2D:
     | dX(t) = 1/mu * (theta - X(t)) * dt + sqrt(sigma) * dW1(t)
     | dY(t) = X(t) * dt + 0 * dW2(t)
     | t in [t0,T].

    Moment equations:
     | dm1(t)  = (theta - m1(t))/mu
     | dm2(t)  = m1(t)
     | dS1(t)  = sigma - 2 * (S1(t)/mu)
     | dS2(t)  = 2 * C12(t)
     | dC12(t) = S1(t) - C12(t)/mu

In R we create LaTeX mathematical expressions for this object using the following code:

    R> TEX.sde(object = mem.oui)

    %%% LaTeX equation generated in R 4.0.0 by TEX.sde() method
    %%% Copy and paste the following output in your LaTeX file

    \begin{equation}\label{eq:}
    \begin{cases}
    \begin{split}
    \frac{d}{dt} m_{1}(t) ~&= \frac{\left( \theta - m_{1}(t) \right)}{\mu} \\
    \frac{d}{dt} m_{2}(t) ~&= m_{1}(t) \\
    \frac{d}{dt} S_{1}(t) ~&= \sigma - 2 \, \left( \frac{S_{1}(t)}{\mu} \right) \\
    \frac{d}{dt} S_{2}(t) ~&= 2 \, C_{12}(t) \\
    \frac{d}{dt} C_{12}(t) &= S_{1}(t) - \frac{C_{12}(t)}{\mu}
    \end{split}
    \end{cases}
    \end{equation}

that can be typed with LaTeX to produce a system:

\[\begin{equation} \begin{cases} \begin{split} \frac{d}{dt} m_{1}(t) ~&= \frac{\left( \theta - m_{1}(t) \right)}{\mu} \\ \frac{d}{dt} m_{2}(t) ~&= m_{1}(t) \\ \frac{d}{dt} S_{1}(t) ~&= \sigma - 2 \, \left( \frac{S_{1}(t)}{\mu} \right) \\ \frac{d}{dt} S_{2}(t) ~&= 2 \, C_{12}(t) \\ \frac{d}{dt} C_{12}(t) &= S_{1}(t) - \frac{C_{12}(t)}{\mu} \end{split} \end{cases} \end{equation}\]

Note that it is obvious the LaTeX package `amsmath` must be loaded into the `.tex` document.

## LaTeX mathematic for an R expression of SDEs

In this section, we will convert the R expressions of a SDEs, i.e., drift and diffusion coefficients into their LaTeX mathematical equivalents with the same procedures previous. An example sophisticated that will make this clear.

    R> f <- expression((alpha*x *(1 - x / beta)- delta * x^2 * y / (kappa + x^2)),
    +                 (gamma * x^2 * y / (kappa + x^2) - mu * y^2))
    R> g <- expression(sqrt(sigma1)*x*(1-y), abs(sigma2)*y*(1-x))
    R> TEX.sde(object=c(drift = f, diffusion = g))

    %%% LaTeX equation generated in R 4.0.0 by TEX.sde() method
    %%% Copy and paste the following output in your LaTeX file

    \begin{equation}\label{eq:}
    \begin{cases}
    \begin{split}
    dX_{t} &= \left( \alpha \, X_{t} \, \left( 1 - \frac{X_{t}}{\beta} \right) - \frac{\delta \, X_{t}^2 \, Y_{t}}{\left( \kappa + X_{t}^2 \right)} \right) \:dt +  \sqrt{\sigma_{1}} \, X_{t} \, \left( 1 - Y_{t} \right) \:dW_{1,t} \\
    dY_{t} &= \left( \frac{\gamma \, X_{t}^2 \, Y_{t}}{\left( \kappa + X_{t}^2 \right)} - \mu \, Y_{t}^2 \right) \:dt +  \left| \sigma_{2}\right|  \, Y_{t} \, \left( 1 - X_{t} \right) \:dW_{2,t}
    \end{split}
    \end{cases}
    \end{equation}

under LaTeX will create this system:

\[\begin{equation*} \begin{cases} \begin{split} dX_{t} &= \left( \alpha \, X_{t} \, \left( 1 - \frac{X_{t}}{\beta} \right) - \frac{\delta \, X_{t}^2 \, Y_{t}}{\left( \kappa + X_{t}^2 \right)} \right) \:dt + \sqrt{\sigma_{1}} \, X_{t} \, \left( 1 - Y_{t} \right) \:dW_{1,t} \\ dY_{t} &= \left( \frac{\gamma \, X_{t}^2 \, Y_{t}}{\left( \kappa + X_{t}^2 \right)} - \mu \, Y_{t}^2 \right) \:dt + \left| \sigma_{2}\right| \, Y_{t} \, \left( 1 - X_{t} \right) \:dW_{2,t} \end{split} \end{cases} \end{equation*}\]

# Further reading

1. `snssdekd()` & `dsdekd()` & `rsdekd()`- Monte-Carlo Simulation and Analysis of Stochastic Differential Equations.
2. `bridgesdekd()` & `dsdekd()` & `rsdekd()` - Constructs and Analysis of Bridges Stochastic Differential Equations.
3. `fptsdekd()` & `dfptsdekd()` - Monte-Carlo Simulation and Kernel Density Estimation of First passage time.
4. `MCM.sde()` & `MEM.sde()` - Parallel Monte-Carlo and Moment Equations for SDEs.
5. `TEX.sde()` - Converting Sim.DiffProc Objects to LaTeX.
6. `fitsde()` - Parametric Estimation of 1-D Stochastic Differential Equation.

# References

1. Xie Y (2015). Dynamic Documents with R and knitr. 2nd edition. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 978-1498716963, URL https://yihui.name/knitr/.

2. Wickham H (2015). Advanced R. Chapman & Hall/CRC The R Series. CRC Press. ISBN 9781498759809.

3. Guidoum AC, Boukhetala K (2020). Performing Parallel Monte Carlo and Moment Equations Methods for Ito and Stratonovich Stochastic Differential Systems: R Package Sim.DiffProc. Accept Submission to Journal of Statistical Software.

4. Guidoum AC, Boukhetala K (2020). Sim.DiffProc: Simulation of Diffusion Processes. R package version 4.6, URL https://cran.r-project.org/package=Sim.DiffProc.

1. Department of Probabilities & Statistics, Faculty of Mathematics, University of Science and Technology Houari Boumediene, BP 32 El-Alia, U.S.T.H.B, Algeria, E-mail (acguidoum@usthb.dz)↩

2. Faculty of Mathematics, University of Science and Technology Houari Boumediene, BP 32 El-Alia, U.S.T.H.B, Algeria, E-mail (kboukhetala@usthb.dz)↩