# The fitsde() function

The Sim.DiffProc package implements pseudo-maximum likelihood via the fitsde() function. The interface and the output of the fitsde() function are made as similar as possible to those of the standard mle function in the stats4 package of the basic R system. The main arguments to fitsde consist:

• data a univariate time series (ts object).
• drift and diffusion indicate drift and diffusion coefficient of the SDE, is an expression of two variables t, x and theta names of the parameters, and must be nominated by a vector of theta = (theta[1], theta[2],..., theta[p]) for reasons of symbolic derived in approximation methods.
• start must be specified as a named list, where the names of the elements of the list correspond to the names of the parameters as they appear in the drift and diffusion coefficient.
• The pmle argument must be a character string specifying the method to use, can be either: "euler" Euler pseudo-likelihood, "ozaki" Ozaki pseudo-likelihood, "shoji" Shoji pseudo-likelihood and "kessler" Kessler pseudo-likelihood.
• optim.method select the optimization method ("L-BFGS-B" is used by default), and further arguments to pass to optim function.
• lower and upper bounds on the variables for the Brent or L-BFGS-B method.

The functions of type S3 method (as similar of the standard mle function in the stats4 package of the basic R system for the class fitsde are the following:

• coef: which extracts model coefficients from objects returned by fitsde.
• vcov: returns the variance-covariance matrix of the parameters of a fitted model objects.
• logLik: extract log-likelihood.
• AIC: calculating Akaike’s Information Criterion for fitted model objects.
• BIC: calculating Schwarz’s Bayesian Criterion for fitted model objects.
• confint: computes confidence intervals for one or more parameters in a fitted model objects.

The following we explain how to use this function to estimate a SDE with different approximation methods.

## Euler method

Consider a process solution of the general stochastic differential equation: $$$\label{eq02} dX_{t}= f(X_{t},\underline{\theta}) dt + g(X_{t},\underline{\theta}) dW_{t}, \quad \quad t \geq 0 \, , X_{0} = x_{0},$$$ The Euler scheme produces the discretization ($$\Delta t \rightarrow 0$$): $\begin{equation*} X_{t+\Delta t} - X_{t} = f(X_{t},\theta) \Delta t+ g(X_{t},\theta) (W_{t+\Delta t} -W_{t}), \end{equation*}$ The increments $$X_{t+\Delta t} - X_{t}$$ are then independent Gaussian random variables with mean: $$\text{E}_{x} = f(X_{t},\theta)\Delta t$$, and variance: $$\text{V}_{x} = g^{2}(X_{t},\theta) \Delta t$$. Therefore the transition density of the process can be written as: $\begin{equation*} p_{\theta}(t,y|x)=\frac{1}{\sqrt{2\pi t g^{2}(x,\theta)}} \exp\left(-\frac{\left(y-x-f(x,\theta)t\right)^2}{2tg^{2}(x,\theta)}\right) \end{equation*}$ Then the log-likelihood is: $$$\label{eq08} h_{n}(\theta|X_{1},X_{2},\dots,X_{n})=-\frac{1}{2}\left(\sum_{i=1}^{n} \frac{(X_{i}-X_{i-1}-f(X_{i-1},\theta)\Delta)^2}{\sigma^2 \Delta t} + n \log(2\pi \sigma^2 \Delta t)\right)$$$

As an example, we consider the Chan-Karolyi-Longstaff-Sanders (CKLS) model: $$$\label{eq09} dX_{t} = (\theta_{1}+\theta_{2} X_{t}) dt + \theta_{3} X_{t}^{\theta_{4}} dW_{t},\qquad X_{0}=2$$$ with $$\theta_{1}=1$$, $$\theta_{2}=2$$, $$\theta_{3}=0.5$$ and $$\theta_{4}=0.3$$. Before calling fitsde, we generate sampled data $$X_{t_{i}}$$, with $$\Delta t =10^{-4}$$, as following:

we set the initial values for the optimizer as $$\theta_{1}=\theta_{2}=\theta_{3}=\theta_{4}=1$$, and we specify the coefficients drift and diffusion as expressions. you can use the upper and lower limits of the search region used by the optimizer (here using the default method "L-BFGS-B"), and specifying the method to use with pmle="euler".

Call:
fitsde(data = mydata, drift = fx, diffusion = gx, start = list(theta1 = 1,
theta2 = 1, theta3 = 1, theta4 = 1), pmle = "euler")

Coefficients:
theta1  theta2  theta3  theta4
4.60550 1.36857 0.50377 0.29668

The estimated coefficients are extracted from the output object fitmod as follows:

theta1  theta2  theta3  theta4
4.60550 1.36857 0.50377 0.29668

We can use the summary function to produce result summaries of output object:

Pseudo maximum likelihood estimation

Method:  Euler
Call:
fitsde(data = mydata, drift = fx, diffusion = gx, start = list(theta1 = 1,
theta2 = 1, theta3 = 1, theta4 = 1), pmle = "euler")

Coefficients:
Estimate Std. Error
theta1  4.60550   1.656569
theta2  1.36857   0.214030
theta3  0.50377   0.011932
theta4  0.29668   0.011744

-2 log L: -66015

vcov for variance-covariance matrice, and extract log-likelihood by logLik:

theta1       theta2       theta3       theta4
theta1  2.74422217 -0.302654626 -0.000218033  0.000224417
theta2 -0.30265463  0.045808855  0.000028446 -0.000029289
theta3 -0.00021803  0.000028446  0.000142373 -0.000133736
theta4  0.00022442 -0.000029289 -0.000133736  0.000137913
[1] 33007
[1] -66007
[1] -65997

Computes confidence intervals for one or more parameters in a fitted SDE:

2.5 %  97.5 %
theta1 1.35868 7.85232
theta2 0.94908 1.78806
theta3 0.48038 0.52716
theta4 0.27366 0.31970

## Ozaki method

The second approach we present is the Ozaki method, and it works for homogeneous stochastic differential equations. Consider the stochastic differential equation: $$$\label{eq10} dX_{t}= f(X_{t},\underline{\theta}) dt + \sigma dW_{t}, \quad \quad t \geq 0 \, , X_{0} = x_{0},$$$ where $$\sigma$$ is supposed to be constant. We just recall that the transition density for the Ozaki method is Gaussian, we have that: $$X_{t+\Delta t}|X_{t} = x \sim \mathcal{N}(\text{E}_{x},\text{V}_{x})$$, where: \begin{align}\label{eq11} \text{E}_{x} &= x + \frac{f(x)}{\partial_{x} f(x)} \left( e^{\partial_{x} f(x) \Delta t} - 1 \right), \quad\text{and}\quad \text{V}_{x} &= \sigma^{2} \frac{e^{2K_{x} \Delta t} -1}{2K_{x}}, \end{align} with: $\begin{equation*} K_{x} = \frac{1}{\Delta t} \log \left(1+\frac{f(x)}{x\partial_{x}f(x)}\left(e^{\partial_{x}f(x) \Delta t}-1\right) \right) \end{equation*}$ It is always possible to transform process $$X_t$$ with a constant diffusion coefficient using the Lamperti transform.

Now we consider the Vasicek model, with $$f(x,\theta) = \theta_{1} (\theta_{2}- x)$$ and constant volatility $$g(x,\theta) = \theta_{3}$$, $$$\label{eq12} dX_{t} = \theta_{1} (\theta_{2}- X_{t}) dt + \theta_{3} dW_{t},\qquad X_{0}=5$$$ with $$\theta_{1}=3$$, $$\theta_{2}=2$$ and $$\theta_{3}=0.5$$, we generate sampled data $$X_{t_{i}}$$, with $$\Delta t =10^{-2}$$, as following:

we set the initial values for the optimizer as $$\theta_{1}=\theta_{2}=\theta_{3}=1$$, and we specify the coefficients drift and diffusion as expressions. Specifying the method to use with pmle="ozaki", which can easily be implemented in R as follows:

Pseudo maximum likelihood estimation

Method:  Ozaki
Call:
fitsde(data = HWV, drift = fx, diffusion = gx, start = list(theta1 = 1,
theta2 = 1, theta3 = 1), pmle = "ozaki")

Coefficients:
Estimate Std. Error
theta1  3.30068   0.386644
theta2  1.90711   0.050529
theta3  0.50643   0.011326

-2 log L: -3128.6

If you want to have confidence intervals $$\theta_{1}$$ and $$\theta_{2}$$ parameters only, using the command confint as following:

2.5 % 97.5 %
theta1 2.5429 4.0585
theta2 1.8081 2.0061

## Shoji-Ozaki method

An extension of the method to Ozaki the more general case where the drift is allowed to depend on the time variable $$t$$, and also the diffusion coefficient can be varied is the method Shoji and Ozaki. Consider the stochastic differential equation: $$$\label{eq13} dX_{t}= f(t,X_{t},\underline{\theta}) dt + g(X_{t},\underline{\theta}) dW_{t}, \quad \quad t \geq 0 \, , X_{0} = x_{0},$$$ the transition density for the Shoji-Ozaki method is Gaussian, we have that: $$X_{t+\Delta t}|X_{t} = x \sim \mathcal{N}\left(\mathrm{A}_{(t,x)}x,\mathrm{B}^{2}_{(t,x)}\right)$$, where: \begin{align}\label{eq14} \mathrm{A}_{(t,x)} &= 1+ \frac{f(t,x)}{x\mathrm{L}_{t}} \left(e^{\mathrm{L}_{t}\Delta t }-1\right)+\frac{\mathrm{M}_{t}}{x\mathrm{L}^{2}_{t}} \left(e^{\mathrm{L}_{t} \Delta t}-1-\mathrm{L}_{t}\Delta t\right), \\ \mathrm{B}_{(t,x)} &= g(x) \sqrt{\frac{e^{2\mathrm{L}_{t} \Delta t}-1}{2\mathrm{L}_{t}}}, \end{align} with: $\begin{equation*} \mathrm{L}_{t} = \partial_{x} f(t,x) \quad\text{and}\quad \mathrm{M}_{t} = \frac{g^{2}(x)}{2} \partial_{xx} f(t,x)+ \partial_{t} f(t,x). \end{equation*}$

As an example, we consider the following model: $$$\label{eq15} dX_{t} = a(t)X_{t} dt + \theta_{2}X_{t} dW_{t},\qquad X_{0}=10$$$ with: $$a(t) = \theta_{1}t$$, and we generate sampled data $$X_{t_{i}}$$, with $$\theta_{1}=-2$$, $$\theta_{2}=0.2$$ and time step $$\Delta t =10^{-3}$$, as following:

we set the initial values for the optimizer as $$\theta_{1}=\theta_{2}=1$$, and we specify the method to use with pmle="shoji":

Pseudo maximum likelihood estimation

Method:  Shoji
Call:
fitsde(data = mydata, drift = fx, diffusion = gx, start = list(theta1 = 1,
theta2 = 1), pmle = "shoji", lower = c(-3, 0), upper = c(-1,
1))

Coefficients:
Estimate Std. Error
theta1 -1.48890  0.3350703
theta2  0.19336  0.0043256

-2 log L: -3205

## Kessler method

Kessler (1997) proposed to use a higher-order Ito-Taylor expansion to approximate the mean and variance in a conditional Gaussian density. Consider the stochastic differential equation $$dX_{t}= f(X_{t},\underline{\theta}) dt + g(X_{t},\underline{\theta}) dW_{t}$$ , the transition density by Kessler method is: $$X_{t+\Delta t}|X_{t} = x \sim \mathcal{N}\left(\text{E}_{x},\text{V}_{x}\right)$$, where: \begin{align}\label{eq16} \text{E}_{x} &= x + f(t,x) \Delta t+\left(f(t,x)\partial_{x}f(t,x) + \frac{1}{2} g^{2}(t,x) \partial_{xx}g(t,x)\right)\frac{(\Delta t)^2}{2}, \\ \text{V}_{x} &= x^2 +(2f(t,x)x+g^{2}(t,x)) \Delta t +\bigg(2f(t,x)\left(\partial_{x}f(t,x)x+f(t,x)+g(t,x)\partial_{x}g(t,x)\right) \nonumber\\ &\quad+g^{2}(t,x)\left(\partial_{xx}f(t,x)x+2\partial_{x}f(t,x)+\partial_{x}g^{2}(t,x)+g(t,x)\partial_{xx}g(t,x)\right)\bigg)\frac{(\Delta t)^2}{2}-\text{E}^{2}_{x}. \end{align}

We consider the following Hull-White (extended Vasicek) model: $$$\label{eq17} dX_{t} = a(t)(b(t)-X_{t}) dt + \sigma(t) dW_{t},\qquad X_{0}=2$$$ with: $$a(t) = \theta_{1}t$$ and $$b(t)=\theta_{2}\sqrt{t}$$, the volatility depends on time: $$\sigma(t)=\theta_{3}t$$. We generate sampled data of $$X_t$$, with $$\theta_{1}=3$$, $$\theta_{2}=1$$ and $$\theta_{3}=0.3$$, time step $$\Delta t =10^{-3}$$, as following:

we set the initial values for the optimizer as $$\theta_{1}=\theta_{2}=\theta_{3}=1$$, and we specify the method to use with pmle="kessler":

Pseudo maximum likelihood estimation

Method:  Kessler
Call:
fitsde(data = mydata, drift = fx, diffusion = gx, start = list(theta1 = 1,
theta2 = 1, theta3 = 1), pmle = "kessler")

Coefficients:
Estimate Std. Error
theta1  3.63927  0.3589406
theta2  0.99058  0.1437718
theta3  0.30710  0.0068709

-2 log L: -8417.6

# The fitsde() in practice

## Model selection via AIC

Let the following models: \begin{align*} % \nonumber to remove numbering (before each equation) dX_{t} &= \theta_{1} X_{t} dt + \theta_{2} X_{t}^{\theta_{3}} dW_{t}, &\text{(true model)}\\ dX_{t} &= (\theta_{1}+\theta_{2} X_{t}) dt + \theta_{3} X_{t}^{\theta_{4}} dW_{t},&\text{(competing model 1)}\\ dX_{t} &= (\theta_{1}+\theta_{2} X_{t}) dt + \theta_{3} \sqrt{X_{t}} dW_{t}, &\text{(competing model 2)}\\ dX_{t} &= \theta_{1} dt + \theta_{2} X_{t}^{\theta_{3}} dW_{t}, &\text{(competing model 3)} \end{align*} We generate data from true model with parameters $$\underline{\theta}=(2,0.3,0.5)$$, initial value $$X_{0}=2$$ and $$\Delta t =10^{-4}$$, as following:

We test the performance of the AIC statistics for the four competing models, we can proceed as follows:

[1] "True mod"

the estimates under the different models:

Theta1  Theta2  Theta3  Theta4
True mod   2.0900 0.30873 0.48926
Comp mod1  1.1268 1.93234 0.30878 0.48915
Comp mod2  1.1289 1.93204 0.30292
Comp mod3 10.3956 0.30873 0.49200

## Application to real data

We make use of real data of the U.S. Interest Rates monthly form $$06/1964$$ to $$12/1989$$ (see Figure 1) available in package Ecdat, and we estimate the parameters $$\underline{\theta}=(\theta_{1},\theta_{2},\theta_{3},\theta_{4})$$ of CKLS model. These data have been analyzed by Brouste et all (2014) with yuima package, here we confirm the results of the estimates by several approximation methods.

we can now use all previous methods by fitsde function to estimate the parameters of CKLS model as follows:

euler  kessler    ozaki    shoji
theta1  2.07695  2.14335  2.11532  2.10150
theta2 -0.26319 -0.27434 -0.26905 -0.26647
theta3  0.13022  0.12598  0.12652  0.13167
theta4  1.45132  1.46917  1.46491  1.45131
logLik    AIC    BIC
euler   -237.88 483.76 487.15
kessler -237.78 483.57 486.96
ozaki   -237.84 483.67 487.07
shoji   -237.88 483.76 487.15

In Figure 2 we reports the sample mean of the solution of the CKLS model with the estimated parameters and real data, their empirical $$95\%$$ confidence bands (from the $$2.5th$$ to the $$97.5th$$ percentile), we can proceed as follows:

Itô Sde 1D:
| dX(t) = (2.076 - 0.263 * X(t)) * dt + 0.13 * X(t)^1.451 * dW(t)
Method:
| Euler scheme with order 0.5
Summary:
| Size of process   | N  = 299.
| Number of simulation  | M  = 500.
| Initial value     | x0 = 3.317.
| Time of process   | t in [1964.5,1989.3].
| Discretization    | Dt = 0.08343.

# References

1. Brouste A, Fukasawa M, Hino H, Iacus SM, Kamatani K, Koike Y, Masuda H, Nomura R,Ogihara T, Shimuzu Y, Uchida M, Yoshida N (2014). The YUIMA Project: A ComputationalFramework for Simulation and Inference of Stochastic Differential Equations." Journal of Statistical Software, 57(4), 1-51. URL http://www.jstatsoft.org/v57/i04.

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

3. Iacus SM (2008). Simulation and Inference for Stochastic Differential Equations: With R Examples. Springer-Verlag, New York.

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 ()

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