# Converting Sim.DiffProc Objects to LaTeX

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

#### 2024-03-05

# 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  1.99996 -0.00004   0.00578 0.01735  ( 1.98863 , 2.01129 )
    m2  28.00 27.98526 -0.01474   0.04641 0.14000 ( 27.8943 , 28.07622 )
    S1   0.25  0.24766 -0.00234   0.00383 0.01173  ( 0.24015 , 0.25517 )
    S2   6.75  6.69702 -0.05298   0.14770 0.44625  ( 6.40753 , 6.98651 )
    C12  0.25  0.27466  0.02466   0.02525 0.07966  ( 0.22517 , 0.32415 )

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.3.3 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 & 1.99996 & -0.00004 & 0.00578 & 0.01735 & ( 1.98863 , 2.01129 )\\
    $m_{2}(t)$ & 28.00 & 27.98526 & -0.01474 & 0.04641 & 0.14000 & ( 27.8943 , 28.07622 )\\
    $S_{1}(t)$ & 0.25 & 0.24766 & -0.00234 & 0.00383 & 0.01173 & ( 0.24015 , 0.25517 )\\
    $S_{2}(t)$ & 6.75 & 6.69702 & -0.05298 & 0.14770 & 0.44625 & ( 6.40753 , 6.98651 )\\
    $C_{12}(t)$ & 0.25 & 0.27466 & 0.02466 & 0.02525 & 0.07966 & ( 0.22517 , 0.32415 )\\
    \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 | 1.99996 | -0.00004 | 0.00578 | 0.01735 | ( 1.98863 , 2.01129 )
m2 | 28.00 | 27.98526 | -0.01474 | 0.04641 | 0.14000 | ( 27.8943 , 28.07622 )
S1 | 0.25 | 0.24766 | -0.00234 | 0.00383 | 0.01173 | ( 0.24015 , 0.25517 )
S2 | 6.75 | 6.69702 | -0.05298 | 0.14770 | 0.44625 | ( 6.40753 , 6.98651 )
C12 | 0.25 | 0.27466 | 0.02466 | 0.02525 | 0.07966 | ( 0.22517 , 0.32415 )

## 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.3.3 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.3.3 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.org/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 Itô and Stratonovich Stochastic Differential Systems: R Package Sim.DiffProc”. Journal of Statistical Software, 96(2), 1–82. https://doi.org/10.18637/jss.v096.i02

1. Department of Mathematics and Computer Science, Faculty of Sciences and Technology, University of Tamanghasset, Algeria, E-mail (acguidoum@univ-tam.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)↩︎