---
title: "Converting Sim.DiffProc Objects to LaTeX"
author: 
- A.C. Guidoum^[Department of Mathematics and Computer Science, Faculty of Sciences and Technology, University of Tamanghasset, Algeria, E-mail  (acguidoum@univ-tam.dz)] and K. Boukhetala^[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)]
date: "`r Sys.Date()`"
output: 
  knitr:::html_vignette:
    toc: yes
fontsize: 12pt
vignette: >
  %\VignetteIndexEntry{Converting Sim.DiffProc Objects to LaTeX}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, echo = F, message = F, results = 'hide',screenshot.force=FALSE}
library(Sim.DiffProc)
library(knitr)
knitr::opts_chunk$set(comment="",prompt=TRUE, fig.show='hold',warning=FALSE, message=FALSE)
options(prompt="R> ",scipen=16,digits=5,warning=FALSE, message=FALSE,width = 80)
```

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

```
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.")
```
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.

```{r echo=FALSE}
kable(mcm.mod2d$MC, format = "html",booktabs = TRUE,align = "r", caption ="LaTeX 
          table for Monte Carlo results generated by `TEX.sde()` method.")
```


## 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)
mem.oui
```
In R we create LaTeX mathematical expressions for this object using the following code:
```{r}
TEX.sde(object = mem.oui)

```

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)) 
g <- expression(sqrt(sigma1)*x*(1-y), abs(sigma2)*y*(1-x))  
TEX.sde(object=c(drift = f, diffusion = g))
```
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](snssde.html).
2. [`bridgesdekd()` & `dsdekd()` & `rsdekd()` - Constructs and Analysis of Bridges Stochastic Differential Equations](bridgesde.html).
3. [`fptsdekd()` & `dfptsdekd()` - Monte-Carlo Simulation and Kernel Density Estimation of First passage time](fptsde.html).
4. [`MCM.sde()` & `MEM.sde()` - Parallel Monte-Carlo and Moment Equations for SDEs](mcmsde.html).
5. [`TEX.sde()` - Converting Sim.DiffProc Objects to LaTeX](sdetotex.html).
6. [`fitsde()` - Parametric Estimation of 1-D Stochastic Differential Equation](fitsde.html).


# 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