You are on page 1of 128

Statistical data analysis of financial time series

and option pricing in R

S.M. Iacus (University of Milan)

Chicago, R/Finance 2011, April 29th

S.M. Iacus 2011 R in Finance – 1 / 113


Explorative Data
Analysis
NYSE data
Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation
Explorative Data Analysis
Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 2 / 113


Cluster Analysis

Explorative Data
Analysis
Cluster analysis is concerned with discovering group structure among n
NYSE data observations, for example time series.
Estimation of Financial
Models
Such methods are based on the notion of dissimilarity. A dissimilarity
Likelihood approach
measure d is a symmetric: d(A, B) = d(B, A), non-negative, and such
Two stage least squares
estimation that d(A, A) = 0.
Model selection

Numerical Evidence Dissimilarities can, but not necessarily be, a metric, i.e.
Application to real data d(A, C) ≤ d(A, B) + d(B, C).
The change point
problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 3 / 113


Cluster Analysis

Explorative Data
Analysis
A clustering method is an algorithm that works on a n × n dissimilarity
NYSE data matrix by aggregating (or, viceversa, splitting) individuals into groups.
Estimation of Financial
Models
Agglomerative algorithms start from n individuals and, at each step,
Likelihood approach
aggregate two of them or move an individual in a group obtained at some
Two stage least squares
estimation previous step. Such an algorithm stops when only one group with all the n
Model selection individuals is formed.
Numerical Evidence

Application to real data Divisive methods start from a unique group of all individuals by splitting
The change point it, at each step, in subgroups till the case when n singletons are formed.
problem

Overview of the yuima


package
These methods are mainly intended for exploratory data analysis and each
Option Pricing with R
method is influenced by the dissimilarity measure d used.

R offers a variety to clustering algorithm and distances to play with but,


up to date, not so much towards clustering of time series.

S.M. Iacus 2011 R in Finance – 4 / 113


Real Data from NYSE

Next: time series of daily closing quotes, from 2006-01-03 to 2007-12-31, for the
following 20 financial assets:
Microsoft Corporation (MSOFT in the plots) Advanced Micro Devices Inc. (AMD)
Dell Inc. (DELL) Intel Corporation (INTEL)
Hewlett-Packard Co. (HP) Sony Corp. (SONY)
Motorola Inc. (MOTO) Nokia Corp. (NOKIA)
Electronic Arts Inc. (EA) LG Display Co., Ltd. (LG)
Borland Software Corp. (BORL) Koninklijke Philips Electronics NV (PHILIPS)
Symantec Corporation (SYMATEC) JPMorgan Chase & Co (JMP)
Merrill Lynch & Co., Inc. (MLINCH) Deutsche Bank AG (DB)
Citigroup Inc. (CITI) Bank of America Corporation (BAC)
Goldman Sachs Group Inc. (GSACHS) Exxon Mobil Corp. (EXXON)

Quotes come from NYSE/NASDAQ. Source Yahoo.com.

S.M. Iacus 2011 R in Finance – 5 / 113


LG EA NOKIA MOTO SONY HP INTEL DELL AMD MSOFT

15 25 45 55 20 35 16 22 40 50 60
30 45 18 24 20 26 3210 30 25 35

2006

S.M. Iacus 2011


2007

Index
2008
quotes

EXXON GSACHS BAC CITI DB MLINCH JPM SYMATEC PHILIPS BORL

60 80 140 220 42 50 30 45 100 140 50 80 40 50 15 19 30 40 3 5

2006
2007

Index
2008
Real Data from NYSE. How to cluster them?

R in Finance – 6 / 113
How to measure the distance between two time series?

Explorative Data
Analysis
The are several ways to measure the distance between two time series
NYSE data
Estimation of Financial  the Euclidean distance dEU C
Models

Likelihood approach  the Short-Time-Series distance dST S


Two stage least squares
estimation
 the Dynamic Time Warping distance dDT W
Model selection

Numerical Evidence  the Markov Operator distance dM O


Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 7 / 113


the Euclidean distance dEU C

Explorative Data
Analysis
Let X and Y be two time series, then the usual Euclidean distance
NYSE data v
Estimation of Financial
uN
uX
Models
dEU C (X, Y ) = t (Xi − Yi )2
Likelihood approach
i=1
Two stage least squares
estimation
is one of the most used in the applied literature. We use it only for
Model selection
comparison purposes.
Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 8 / 113


the Short-Time-Series distance dST S

Explorative Data
Analysis
Let X and Y be two time series, then the usual Euclidean distance
NYSE data v
uN 
Estimation of Financial uX Xi − Xi−1 Yi − Yi−1 2
Models
dST S (X, Y ) = t − .
Likelihood approach ∆ ∆
i=1
Two stage least squares
estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 9 / 113


Dynamic Time Warping distance dDT W

Explorative Data
Analysis
Let X and Y be two time series, then
NYSE data v
uN 
Estimation of Financial uX Xi − Xi−1 Yi − Yi−1 2
Models
dDT W (X, Y ) = t − .
Likelihood approach ∆ ∆
i=1
Two stage least squares
estimation
DTW allows for non-linear alignments between time series not
Model selection
necessarily of the same length.
Numerical Evidence

Application to real data Essentially, all shiftings between two time series are attempted and each
The change point time a cost function is applied (e.g. a weighted Euclidean distance
problem
between the shifted series). The minimum of the cost function over all
Overview of the yuima
package possible shiftings is the dynamic time warping distance dDT W .
Option Pricing with R

S.M. Iacus 2011 R in Finance – 10 / 113


The Markov operator

Consider
dXt = b(Xt )dt + σ(Xt )dWt
therefore, the discretized observations Xi form a Markov process and all the mathematical
properties are embodied in the so-called transition operator

P∆ f (x) = E{f (Xi )|Xi−1 = x}

with f is a generic function, e.g. f (x) = xk .

Notice that P∆ depends on the transition density between Xi and Xi−1 , so we put
explicitly the dependence on ∆ in the notation.

Luckily, there is no need to deal with the transition density, we can estimate P∆ directly
and easily.

S.M. Iacus 2011 R in Finance – 11 / 113


The Markov Operator distance dM O

Explorative Data
Analysis
Let X and Y be two time series, then
NYSE data X
dM O (X, Y) = (P̂∆ )j,k (X) − (P̂∆ )j,k (Y) , (1)

Estimation of Financial
Models
j,k∈J
Likelihood approach

Two stage least squares


estimation where (P̂∆ )j,k (·) is a matrix and it is calculated as in (2) separately for X
Model selection and Y.
Numerical Evidence

Application to real data N


1 X
The change point
(P̂∆ )j,k (X) = {φj (Xi−1 )φk (Xi ) + φk (Xi−1 )φj (Xi )} (2)
problem
2N
Overview of the yuima
i=1
package

Option Pricing with R The terms (P̂∆ )j,k are approximations of the scalar product
< P∆ φj , φk >b,σ where b and σ are the unknown drift and diffusion
coefficient of the model. Thus, (P̂∆ )j,k contains all the information of the
Markovian structure of X.

S.M. Iacus 2011 R in Finance – 12 / 113


Simulations

We simulate 10 paths Xi , i = 1, . . . , 10, according to the combinations of drift bi and


diffusion coefficients σi , i = 1, . . . , 4 presented in the following table

σ1 (x) σ2 (x) σ3 (x) σ4 (x)


b1 (x) X10, X1 X5
b2 (x) X2,X3 X4
b3 (x) X6, X7
b4 (x) X8
where

b1 (x) = 1 − 2x, b2 (x) = 1.5(0.9 − x), b3 (x) = 1.5(0.5 − x), b4 (x) = 5(0.05 − x)
p
σ1 (x) = 0.5 + 2x(1 − x), σ2 (x) = 0.55x(1 − x)
p p
σ3 (x) = 0.1x(1 − x), σ4 (x) = 0.8x(1 − x)
The process X9=1-X1, hence it has drift −b1 (x) and the same quadratic variation of X1
and X10.
S.M. Iacus 2011 R in Finance – 13 / 113
X5 X4 X3 X2 X1

0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8

S.M. Iacus 2011


2
3

Time
4
5
6
X10 X9 X8 X7 X6

0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8 0.0 0.4 0.8
Simulated diffusions

1
2
3

Time
4
5
6
Trajectories

R in Finance – 14 / 113
Dendrograms

S.M. Iacus 2011 R in Finance – 15 / 113


Multidimensional scaling

S.M. Iacus 2011 R in Finance – 16 / 113


Software

Explorative Data
Analysis
The package sde for the R statistical environment is freely available at
NYSE data http://cran.R-Project.org.
Estimation of Financial
Models
It contains the function MOdist which calculates the Markov Operator
Likelihood approach
distance and returns a dist object.
Two stage least squares
estimation

Model selection

Numerical Evidence data(quotes)


Application to real data

The change point d <- MOdist(quotes)


problem
cl <- hclust( d )
Overview of the yuima
package groups <- cutree(cl, k=4)
Option Pricing with R plot(quotes, col=groups)

cmd <- cmdscale(d)


plot( cmd, col=groups)
text( cmd, labels(d) , col=groups)
S.M. Iacus 2011 R in Finance – 17 / 113
Multidimensional scaling. Clustering of NYSE data.

S.M. Iacus 2011 R in Finance – 18 / 113


Explorative Data
Analysis

Estimation of Financial
Models
Examples

Likelihood approach

Two stage least squares


estimation
Estimation of Financial Models
Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 19 / 113


Statistical models

We focus on the general approach to statistical inference for the parameter θ of models in
the wide class of diffusion processes solutions to stochastic differential equations

dXt = b(θ, Xt )dt + σ(θ, Xt )dWt

with initial condition X0 and θ the p-dimensional parameter of interest.

S.M. Iacus 2011 R in Finance – 20 / 113


Examples

Explorative Data
Analysis  geometric Brownian motion (gBm)
Estimation of Financial
Models
Examples
dXt = µXt dt + σXt dWt
Likelihood approach

Two stage least squares


estimation

Model selection
 Cox-Ingersoll-Ross (CIR)
Numerical Evidence
p
dXt = (θ1 + θ2 Xt )dt + θ3 Xt dWt
Application to real data

The change point


problem

Overview of the yuima


package  Chan-Karolyi-Longstaff-Sanders (CKLS)
Option Pricing with R
dXt = (θ1 + θ2 Xt )dt + θ3 Xtθ4 dWt

S.M. Iacus 2011 R in Finance – 21 / 113


Examples

Explorative Data
Analysis  nonlinear mean reversion (Aït-Sahalia)
Estimation of Financial
Models
Examples
dXt = (α−1 Xt−1 + α0 + α1 Xt + α2 Xt2 )dt + β1 Xtρ dWt
Likelihood approach

Two stage least squares


estimation

Model selection
 double Well potential (bimodal behaviour, highly nonlinear)

dXt = (Xt − Xt3 )dt + dWt


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package  Jacobi diffusion (political polarization)
Option Pricing with R  
1 p
dXt = −θ Xt − dt + θXt (1 − Xt )dWt
2

S.M. Iacus 2011 R in Finance – 22 / 113


Examples

Explorative Data
Analysis  Ornstein-Uhlenbeck (OU)
Estimation of Financial
Models
Examples
dXt = θXt dt + dWt
Likelihood approach

Two stage least squares


estimation

Model selection
 radial Ornstein-Uhlenbeck

dXt = (θXt−1 − Xt )dt + dWt


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package  hyperbolic diffusion (dynamics of sand)
Option Pricing with R " #
σ2 Xt
dXt = β −γp dt + σdWt
2 2
δ + (Xt − µ) 2

...and so on.
S.M. Iacus 2011 R in Finance – 23 / 113
Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach
MLE
QMLE

Two stage least squares


estimation
Likelihood approach
Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 24 / 113


Likelihood in discrete time

Consider the model


dXt = b(θ, Xt )dt + σ(θ, Xt )dWt
with initial condition X0 and θ the p-dimensional parameter of interest. By Markov
property of diffusion processes, the likelihood has this form
n
Y
Ln (θ) = pθ (∆, Xi |Xi−1 ) pθ (X0 )
i=1

and the log-likelihood is


n
X
ℓn (θ) = log Ln (θ) = log pθ (∆, Xi |Xi−1 ) + log(pθ (X0 )) .
i=1

Assumption pθ (X0 ) irrelevant, hence pθ (X0 ) = 1


As usual, in most cases we don’t know the conditional distribution pθ (∆, Xi |Xi−1 )

S.M. Iacus 2011 R in Finance – 25 / 113


Maximum Likelihood Estimators (MLE)

Explorative Data
Analysis If we study the likelihood Ln (θ) as a function of θ given the n numbers
Estimation of Financial (X1 = x1 , . . . , Xn = xn ) and we find that this function has a maximum,
Models
we can use this maximum value as an estimate of θ.
Likelihood approach
MLE
QMLE
In general we define maximum likelihood estimator of θ, and we
Two stage least squares
abbreviate this with MLE, the following estimator
estimation

Model selection θ̂n = arg max Ln (θ)


θ∈Θ
Numerical Evidence

Application to real data


= arg max Ln (θ|X1 , X2 , . . . , Xn )
θ∈Θ
The change point
problem
provided that the maximum exists.
Overview of the yuima
package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 26 / 113


MLE with R

Explorative Data
Analysis
It is not always the case, that maximum likelihood estimators can be
Estimation of Financial
obtained in explicit form.
Models

Likelihood approach For what concerns applications to real data, it is important to know that
MLE
QMLE
MLE are optimal in many respect from the statistical point of view,
Two stage least squares
compared to, e.g. estimators obtained via the method of the moments
estimation (GMM) which can produce fairly biased estimates of the models.
Model selection

Numerical Evidence R offers a prebuilt generic function called mle in the package stats4
Application to real data which can be used to maximize a likelihood.
The change point
problem
The mle function actually minimizes the negative log-likelihood −ℓ(θ) as
Overview of the yuima
package a function of the parameter θ where ℓ(θ) = log L(θ).
Option Pricing with R

S.M. Iacus 2011 R in Finance – 27 / 113


QUASI - MLE

Consider the multidimensional diffusion process

dXt = b(θ2 , Xt )dt + σ(θ1 , Xt )dWt

where Wt is an r-dimensional standard Wiener process independent of the initial value


X0 = x0 . Quasi-MLE assumes the following approximation of the true log-likelihood for
multidimensional diffusions
n  
1X 1
ℓn (Xn , θ) = − log det(Σi−1 (θ1 )) + (∆Xi − ∆n bi−1 (θ2 ))T Σ−1
i−1 (θ1 )(∆Xi − ∆n bi−1 (θ2 )) (3)
2 i=1 ∆n

where θ = (θ1 , θ2 ), ∆Xi = Xti − Xti−1 , Σi (θ1 ) = Σ(θ1 , Xti ), bi (θ2 ) = b(θ2 , Xti ),
Σ = σ ⊗2 , A⊗2 = AT A and A−1 the inverse of A. Then the QML estimator of θ is

θ̃n = arg min ℓn (Xn , θ)


θ

S.M. Iacus 2011 R in Finance – 28 / 113


QUASI - MLE

Consider the matrix  1 


n∆n Ip 0
ϕ(n) = 1
0 n Iq
where Ip and Iq are respectively the identity matrix of order p and q. Then, is possible to
show that
d
ϕ(n)−1/2 (θ̂n − θ) → N (0, I(θ)−1 ).
where I(θ) is the Fisher information matrix.

The above result means that the estimator of the parameter of the drift converges slowly to
the true value because n∆n = T → ∞ slower than n, as n → ∞.

S.M. Iacus 2011 R in Finance – 29 / 113


QUASI - MLE using yuima package

To estimate a model we make use of the qmle function in the yuima package, whose
interface is similar to mle. Consider the model

dXt = −θ2 Xt dt + θ1 dWt


with θ1 = 0.3 and θ2 = 0.1

R> library(yuima)

R> ymodel <- setModel(drift = c("(-1)*theta2*x"),


+ diffusion = matrix(c("theta1"), 1, 1) )
R> n <- 100
R> ysamp <- setSampling(Terminal = (n)^(1/3), n = n)
R> yuima <- setYuima(model = ymodel, sampling = ysamp)
R> set.seed(123)
R> yuima <- simulate(yuima, xinit = 1, true.parameter = list(theta1 = 0.3,
+ theta2 = 0.1))

S.M. Iacus 2011 R in Finance – 30 / 113


QUASI - MLE. True values: θ1 = 0.3, θ2 = 0.1

We can now try to estimate the parameters

R> mle1 <- qmle(yuima, start = list(theta1 = 0.8, theta2 = 0.7),


+ lower = list(theta1=0.05, theta2=0.05),
+ upper = list(theta1=0.5, theta2=0.5), method = "L-BFGS-B")
R> coef(mle1)
theta1 theta2
0.30766981 0.05007788
R> summary(mle1)
Maximum likelihood estimation

Coefficients:
Estimate Std. Error
theta1 0.30766981 0.02629925
theta2 0.05007788 0.15144393

-2 log L: -280.0784

not very good estimate of the drift so now we consider a longer time series.

S.M. Iacus 2011 R in Finance – 31 / 113


QUASI - MLE. True values: θ1 = 0.3, θ2 = 0.1

R> n <- 1000


R> ysamp <- setSampling(Terminal = (n)^(1/3), n = n)
R> yuima <- setYuima(model = ymodel, sampling = ysamp)
R> set.seed(123)
R> yuima <- simulate(yuima, xinit = 1, true.parameter = list(theta1 = 0.3,
+ theta2 = 0.1))
R> mle1 <- qmle(yuima, start = list(theta1 = 0.8, theta2 = 0.7),
+ lower = list(theta1=0.05, theta2=0.05),
+ upper = list(theta1=0.5, theta2=0.5), method = "L-BFGS-B")
R> coef(mle1)
theta1 theta2
0.3015202 0.1029822
R> summary(mle1)
Maximum likelihood estimation

Coefficients:
Estimate Std. Error
theta1 0.3015202 0.006879348
theta2 0.1029822 0.114539931

-2 log L: -4192.279

S.M. Iacus 2011 R in Finance – 32 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Two stage least squares estimation


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 33 / 113


Aït-Sahalia’s interest rates model

Explorative Data
Analysis
Aït-Sahalia in 1996 proposed a model much more sophisticated than the
Estimation of Financial
CKLS to include other polynomial terms.
Models

Likelihood approach dXt = (α−1 Xt−1 + α0 + α1 Xt + α2 Xt2 )dt + β1 Xtρ dBt


Two stage least squares
estimation
He proposed to use the two stage least squares approach in the following
Model selection
manner.
Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 34 / 113


Aït-Sahalia’s interest rates model

Explorative Data
Analysis
Aït-Sahalia in 1996 proposed a model much more sophisticated than the
Estimation of Financial
CKLS to include other polynomial terms.
Models

Likelihood approach dXt = (α−1 Xt−1 + α0 + α1 Xt + α2 Xt2 )dt + β1 Xtρ dBt


Two stage least squares
estimation
He proposed to use the two stage least squares approach in the following
Model selection
manner. First estimate the drift coefficients with a simple linear regression
Numerical Evidence
like
Application to real data

The change point


problem
E(Xt+1 − Xt |Xt ) = α−1 Xt−1 + α0 + (α1 − 1)Xt + α2 Xt2
Overview of the yuima
package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 34 / 113


Aït-Sahalia’s interest rates model

Explorative Data
Analysis
Aït-Sahalia in 1996 proposed a model much more sophisticated than the
Estimation of Financial
CKLS to include other polynomial terms.
Models

Likelihood approach dXt = (α−1 Xt−1 + α0 + α1 Xt + α2 Xt2 )dt + β1 Xtρ dBt


Two stage least squares
estimation
He proposed to use the two stage least squares approach in the following
Model selection
manner. First estimate the drift coefficients with a simple linear regression
Numerical Evidence
like
Application to real data

The change point


problem
E(Xt+1 − Xt |Xt ) = α−1 Xt−1 + α0 + (α1 − 1)Xt + α2 Xt2
Overview of the yuima
package then, regress the residuals ǫ2t+1 from the first regression to obtain the
Option Pricing with R estimates of the coefficients in the diffusion term with

E(ǫ2t+1 |Xt ) = β0 + β1 Xt + β2 Xtβ3

S.M. Iacus 2011 R in Finance – 34 / 113


Aït-Sahalia’s interest rates model

Explorative Data
Analysis
Aït-Sahalia in 1996 proposed a model much more sophisticated than the
Estimation of Financial
CKLS to include other polynomial terms.
Models

Likelihood approach dXt = (α−1 Xt−1 + α0 + α1 Xt + α2 Xt2 )dt + β1 Xtρ dBt


Two stage least squares
estimation
He proposed to use the two stage least squares approach in the following
Model selection
manner. First estimate the drift coefficients with a simple linear regression
Numerical Evidence
like
Application to real data

The change point


problem
E(Xt+1 − Xt |Xt ) = α−1 Xt−1 + α0 + (α1 − 1)Xt + α2 Xt2
Overview of the yuima
package then, regress the residuals ǫ2t+1 from the first regression to obtain the
Option Pricing with R estimates of the coefficients in the diffusion term with

E(ǫ2t+1 |Xt ) = β0 + β1 Xt + β2 Xtβ3

and finally, use the fitted values from the last regression to set the weights
in the second stage regression for the drift.
S.M. Iacus 2011 R in Finance – 34 / 113
Example of 2SLS estimation

Stage one: regression

E(Xt+1 − Xt |Xt ) = α−1 Xt−1 + α0 + (α1 − 1)Xt + α2 Xt2

R> library(Ecdat)
R> data(Irates)
R> X <- Irates[, "r1"]
R> Y <- X[-1]
R> stage1 <- lm(diff(X) ~ I(1/Y) + Y + I(Y^2))
R> coef(stage1)
(Intercept) I(1/Y) Y I(Y^2)
0.253478884 -0.135520883 -0.094028341 0.007892979

S.M. Iacus 2011 R in Finance – 35 / 113


Example of 2SLS estimation

Stage regression now we extract the squared residuals and run a regression on those

E(ǫ2t+1 |Xt ) = β0 + β1 Xt + β2 Xtβ3

R> eps2 <- residuals(stage1)^2


R> mod <- nls(eps2 ~ b0 + b1 * Y + b2 * Y^b3, start = list(b0 = 1,
+ b1 = 1, b2 = 1, b3 = 0.5), lower = rep(1e-05, 4), upper = rep(2,
+ 4), algorithm = "port")
R> w <- predict(mod)

and we run a second stage regression

R> stage2 <- lm(diff(X) ~ I(1/Y) + Y + I(Y^2), weights = 1/w)


R> coef(stage2)
(Intercept) I(1/Y) Y I(Y^2)
0.189414678 -0.104711262 -0.073227254 0.006442481
R> coef(mod)
b0 b1 b2 b3
0.00001000 0.09347354 0.00001000 0.00001000

S.M. Iacus 2011 R in Finance – 36 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Model selection


Idea
AIC example
Lasso

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 37 / 113


Idea

The aim is to try to identify the underlying continuous model on the basis of discrete
observations using AIC (Akaike Information Criterion) statistics defined as (Akaike
1973,1974)  
AIC = −2ℓn θ̂n(M L) + 2 dim(Θ),
(M L)
where θ̂n is the true maximum likelihood estimator and ℓn (θ) is the log-likelihood

S.M. Iacus 2011 R in Finance – 38 / 113


Model selection via AIC

 
(M L)
Akaike’s index idea AIC = −2ℓn θ̂n + 2 dim(Θ) is to penalize this value
 
−2ℓn θ̂n(M L)

with the dimension of the parameter space

2 dim(Θ)
 
(M L)
Thus, as the number of parameter increases, the fit may be better, i.e. −2ℓn θ̂n
decreases, at the cost of overspecification and dim(Θ) compensate for this effect.

When comparing several models for a given data set, the models such that the AIC is
lower is preferred.

S.M. Iacus 2011 R in Finance – 39 / 113


Model selection via AIC

In order to calculate  
AIC = −2ℓn θ̂n(M L) + 2 dim(Θ),
(M L)
we need to evaluate the exact value of the log-likelihood ℓn (·) at point θ̂n .

Problem: for discretely observed diffusion processes the true likelihood function is not
known in most cases

Uchida and Yoshida (2005) develop the AIC statistics defined as


 
(QM L)
AIC = −2ℓ̃n θ̂n + 2 dim(Θ),

(QM L)
where θ̂n is the quasi maximum likelihood estimator and ℓ̃n the local Gaussian
approximation of the true log-likelihood.

S.M. Iacus 2011 R in Finance – 40 / 113


A trivial example

Explorative Data
Analysis
We compare three models
Estimation of Financial p
Models dXt = −α1 (Xt − α2 )dt + β1 Xt dWt (true model),
Likelihood approach
p
dXt = −α1 (Xt − α2 )dt + β1 + β2 Xt dWt (competing model 1),
Two stage least squares
estimation
dXt = −α1 (Xt − α2 )dt + (β1 + β2 Xt )β3 dWt (competing model 2),
Model selection
Idea
We call the above models Mod1, Mod2 and Mod3.
AIC example
Lasso
We generate data from Mod1 with parameters
Numerical Evidence
p
Application to real data dXt = −(Xt − 10)dt + 2 Xt dWt ,
The change point
problem
and initial value X0 = 8. We use n = 1000 and ∆ = 0.1.
Overview of the yuima
package
We test the performance of the AIC statistics for the three competing
Option Pricing with R
models

S.M. Iacus 2011 R in Finance – 41 / 113


Simulation results. 1000 Monte Carlo replications

Explorative Data p
Analysis dXt = −(Xt − 10)dt + 2 Xt dWt (true model),
Estimation of Financial
p
Models dXt = −α1 (Xt − α2 )dt + β1 Xt dWt (Model 1)
p
Likelihood approach dXt = −α1 (Xt − α2 )dt + β1 + β2 Xt dWt (Model 2)
Two stage least squares
estimation dXt = −α1 (Xt − α2 )dt + (β1 + β2 Xt )β3 dWt (Model 3)
Model selection
Idea Model selection via AIC
AIC example Model 1 Model 2 Model 3
Lasso
(true)
Numerical Evidence
99.2 % 0.6 % 0.2 %
Application to real data

The change point


problem
QMLE estimates under the different models
Overview of the yuima
package
α1 α2 β1 β2 β3
Option Pricing with R Model 1 1.10 8.05 0.90
Model 2 1.12 8.07 2.02 0.54
Model 3 1.12 9.03 7.06 7.26 0.61
S.M. Iacus 2011 R in Finance – 42 / 113
LASSO estimation

The Least Absolute Shrinkage and Selection Operator (LASSO) is a useful and well
studied approach to the problem of model selection and its major advantage is the
simultaneous execution of both parameter estimation and variable selection (see
Tibshirani, 1996; Knight and Fu, 2000, Efron et al., 2004).
To simplify the idea: take a full specified regression model

Y = θ0 + θ1 X1 + θ2 X2 + · · · + θk Xk

perform least squares estimation under L1 constraints, i.e.


k
( )
X
θ̂ = arg min (Y − θX)T (Y − θX) + |θi |
θ
i=1

model selection occurs when some of the θi are estimated as zeros.

S.M. Iacus 2011 R in Finance – 43 / 113


The SDE model

Explorative Data
Analysis
Let Xt be a diffusion process solution to
Estimation of Financial
Models dXt = b(α, Xt )dt + σ(β, Xt )dWt
Likelihood approach

Two stage least squares α = (α1 , ..., αp )′ ∈ Θp ⊂ Rp , p≥1


estimation

Model selection β = (β1 , ..., βq )′ ∈ Θq ⊂ Rq , q≥1


Idea
AIC example b : Θp × Rd → Rd , σ : Θq × Rd → Rd × Rm and Wt , t ∈ [0, T ], is a
Lasso standard Brownian motion in Rm .
Numerical Evidence

Application to real data We assume that the functions b and σ are known up to α and β.
The change point
problem
We denote by θ = (α, β) ∈ Θp × Θq = Θ the parametric vector and with
Overview of the yuima
package θ0 = (α0 , β0 ) its unknown true value.
Option Pricing with R
Let Hn (Xn , θ) = −ℓn (θ) from the local Gaussian approximation.

S.M. Iacus 2011 R in Finance – 44 / 113


Quasi-likelihood function

The classical adaptive LASSO objective function for the present model is then
 
 X p Xq 
min Hn (α, β) + λn,j |αj | + γn,k |βk |
α,β  
j=1 k=1

λn,j and γn,k are appropriate sequences representing an adaptive amount of shrinkage for
each element of α and β.

S.M. Iacus 2011 R in Finance – 45 / 113


Idea of Quadratic Approximation

By Taylor expansion of the original LASSO objective function, for θ around θ̃n (the
QMLE estimator)
1
Hn (Xn , θ) = Hn (Xn , θ̃n ) + (θ − θ̃n )′ Ḣn (Xn , θ̃n ) + (θ − θ̃n )′ Ḧn (Xn , θ̃n )(θ − θ̃n )
2
+op (1)
1
= Hn (Xn , θ̃n ) + (θ − θ̃n )′ Ḧn (Xn , θ̃n )(θ − θ̃n ) + op (1)
2

with Ḣn and Ḧn the gradient and Hessian of Hn with respect to θ.

S.M. Iacus 2011 R in Finance – 46 / 113


The Adaptive LASSO estimator

Explorative Data
Analysis
Finally, the adaptive LASSO estimator is defined as the solution to the
Estimation of Financial
quadratic problem under L1 constraints
Models

Likelihood approach θ̂n = (α̂n , β̂n ) = arg min F (θ).


θ
Two stage least squares
estimation

Model selection
with
Idea p
X q
X
AIC example
Lasso
F (θ) = (θ − θ̃n )Ḧn (Xn , θ̃n )(θ − θ̃n )′ + λn,j |αj | + γn,k |βk |
j=1 k=1
Numerical Evidence

Application to real data The lasso method is implemented in the yuima package.
The change point
problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 47 / 113


How to choose the adaptive sequences

Explorative Data
Analysis
Clearly, the theoretical and practical implications of our method rely to
Estimation of Financial
the specification of the tuning parameter λn,j and γn,k .
Models

Likelihood approach
The tuning parameters should be chosen as is Zou (2006) in the following
Two stage least squares
way
estimation λn,j = λ0 |α̃n,j |−δ1 , γn,k = γ0 |β̃n,j |−δ2 (4)
Model selection
Idea where α̃n,j and β̃n,k are the unpenalized QML estimator of αj and βk
AIC example respectively, δ1 , δ2 > 0 and usually taken unitary.
Lasso

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 48 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Numerical Evidence


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 49 / 113


A multidimensional example

Explorative Data
Analysis
We consider this two dimensional geometric Brownian motion process
Estimation of Financial
solution to the stochastic differential equation
Models
      
Likelihood approach dXt 1 − µ11 Xt + µ12 Yt σ11 Xt −σ12 Yt dWt
= dt+
Two stage least squares dYt 2 + µ21 Xt − µ22 Yt σ21 Xt σ22 Yt dBt
estimation

Model selection
with initial condition (X0 = 1, Y0 = 1) and Wt , t ∈ [0, T ], and
Numerical Evidence
Bt , t ∈ [0, T ], are two independent Brownian motions.
Application to real data

The change point


This model is a classical model for pricing of basket options in
problem
mathematical finance.
Overview of the yuima
package We assume that α = (µ11 = 0.9, µ12 = 0, µ21 = 0, µ22 = 0.7)′ and
Option Pricing with R β = (σ11 = 0.3, σ12 = 0, σ21 = 0, σ22 = 0.2)′ , θ = (α, β).

S.M. Iacus 2011 R in Finance – 50 / 113


Results

µ11 µ12 µ21 µ22 σ11 σ12 σ21 σ22


True 0.9 0.0 0.0 0.7 0.3 0.0 0.0 0.2
Qmle: n = 100 0.96 0.05 0.25 0.81 0.30 0.04 0.01 0.20
(0.08) (0.06) (0.27) (0.15) (0.03) (0.05) (0.02) (0.02)

Lasso: λ0 = γ0 = 1, n = 100 0.86 0.00 0.05 0.71 0.30 0.02 0.01 0.20
(0.12) (0.00) (0.13) (0.09) (0.03) (0.05) (0.02) (0.02)
% of times θi = 0 0.0 99.9 80.2 0.0 0.3 67.2 66.7 0.1

Lasso: λ0 = γ0 = 5, n = 100 0.82 0.00 0.00 0.66 0.29 0.01 0.00 0.20
(0.12) (0.00) (0.00) (0.09) (0.03) (0.03) (0.01) (0.02)
% of times θi = 0 0.0 100.0 99.9 0.0 0.4 86.9 89.7 0.2
Qmle: n = 1000 0.95 0.03 0.21 0.79 0.30 0.04 0.01 0.20
(0.07) (0.04) (0.25) (0.13) (0.03) (0.06) (0.02) (0.02)

Lasso: λ0 = γ0 = 1, n = 1000 0.88 0.00 0.08 0.73 0.30 0.02 0.01 0.20
(0.08) (0.00) (0.16) (0.09) (0.03) (0.05) (0.01) (0.02)
% of times θi = 0 0.0 99.7 72.1 0.0 0.1 67.5 66.6 0.1

Lasso: λ0 = γ0 = 5, n = 1000 0.86 0.00 0.00 0.68 0.29 0.01 0.00 0.20
(0.09) (0.00) (0.01) (0.06) (0.03) (0.04) (0.01) (0.02)
% of times θi = 0 0.0 100.0 99.4 0.0 0.2 87.8 89.9 0.2

S.M. Iacus 2011 R in Finance – 51 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Application to real data


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 52 / 113


Interest rates LASSO estimation examples

Explorative Data
Analysis

Estimation of Financial
Models

15
Likelihood approach

Two stage least squares


estimation

Model selection
10

Numerical Evidence
rates

Application to real data

The change point


problem

Overview of the yuima


5

package

Option Pricing with R


0

1950 1960 1970 1980 1990


S.M. Iacus 2011 R in Finance – 53 / 113
Interest rates LASSO estimation examples

LASSO estimation of the U.S. Interest Rates monthly data from 06/1964 to 12/1989.
These data have been analyzed by many author including Nowman (1997), Aït-Sahalia
(1996), Yu and Phillips (2001) and it is a nice application of LASSO.

Reference Model α β γ
Merton (1973) dXt = αdt + σdWt 0 0
Vasicek (1977) dXt = (α + βXt )dt + σdW
√ t 0
Cox, Ingersoll and Ross (1985) dXt = (α + βXt )dt + σ Xt dWt 1/2
Dothan (1978) dXt = σXt dWt 0 0 1
Geometric Brownian Motion dXt = βXt dt + σXt dWt 0 1
Brennan and Schwartz (1980) dXt = (α + βXt )dt + σXt dWt 1
3/2
Cox, Ingersoll and Ross (1980) dXt = σXt dWt 0 0 3/2
Constant Elasticity Variance dXt = βXt dt + σXtγ dWt 0
CKLS (1992) dXt = (α + βXt )dt + σXtγ dWt

S.M. Iacus 2011 R in Finance – 54 / 113


Interest rates LASSO estimation examples

Explorative Data Model Estimation Method α β σ γ


Analysis
Vasicek MLE 4.1889 -0.6072 0.8096 –
Estimation of Financial
Models
CKLS Nowman 2.4272 -0.3277 0.1741 1.3610
Likelihood approach

Two stage least squares


CKLS Exact Gaussian 2.0069 -0.3330 0.1741 1.3610
estimation (Yu & Phillips) (0.5216) (0.0677)
Model selection
CKLS QMLE 2.0822 -0.2756 0.1322 1.4392
Numerical Evidence (0.9635) (0.1895) (0.0253) (0.1018)
Application to real data
CKLS QMLE + LASSO 1.5435 -0.1687 0.1306 1.4452
The change point with mild penalization (0.6813) (0.1340) (0.0179) (0.0720)
problem

Overview of the yuima CKLS QMLE + LASSO 0.5412 0.0001 0.1178 1.4944
package
with strong penalization (0.2076) (0.0054) (0.0179) (0.0720)
Option Pricing with R
LASSO selected: Cox, Ingersoll and Ross (1980) model
1 3/2
dXt = dt + 0.12 · Xt dWt
2

S.M. Iacus 2011 R in Finance – 55 / 113


Lasso in practice

An example of Lasso estimation using yuima package. We make use of real data with
CKLS model
dXt = (α + βXt )dt + σXtγ dWt

R> library(Ecdat)
R> data(Irates)
R> rates <- Irates[,"r1"]
R> plot(rates)
R> require(yuima)
R> X <- window(rates, start=1964.471, end=1989.333)
R> mod <- setModel(drift="alpha+beta*x", diffusion=matrix("sigma*x^gamma",1,1))
R> yuima <- setYuima(data=setData(X), model=mod)
15
10
rates

5
0

1950 1960 1970 1980 1990


S.M. Iacus 2011 R in Finance – 56 / 113
Example of Lasso estimation

R> lambda10 <- list(alpha=10, beta =10, sigma =10, gamma =10)
R> start <- list(alpha=1, beta =-.1, sigma =.1, gamma =1)
R> low <- list(alpha=-5, beta =-5, sigma =-5, gamma =-5)
R> upp <- list(alpha=8, beta =8, sigma =8, gamma =8)
R> lasso10 <- lasso(yuima, lambda10, start=start, lower=low, upper=upp,
method="L-BFGS-B")

Looking for MLE estimates...


Performing LASSO estimation...

R> round(lasso10$mle, 3) # QMLE


sigma gamma alpha beta
0.133 1.443 2.076 -0.263

R> round(lasso10$lasso, 3) # LASSO


sigma gamma alpha beta
0.117 1.503 0.591 0.000

dXt = (α + βXt )dt + σXtγ dWt


3
dXt = 0.6dt + 0.12Xt dWt
2
S.M. Iacus 2011 R in Finance – 57 / 113
Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection The change point problem


Numerical Evidence

Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 58 / 113


Change point problem

Explorative Data
Analysis
Discover from the data a structural change in the generating model.
Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 59 / 113


Change point problem

Explorative Data
Analysis
Discover from the data a structural change in the generating model.
Estimation of Financial
Next famous example shows historical change points for the Dow-Jones
Models
index.
Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 59 / 113


Change point problem

Explorative Data
Analysis
Discover from the data a structural change in the generating model.
Estimation of Financial
Next famous example shows historical change points for the Dow-Jones
Models
index.
Likelihood approach Dow-Jones closings
950 1050

Two stage least squares


estimation

Model selection
850

Numerical Evidence
750

Application to real data

The change point 1972 1973 1974


problem
Least Squares Dow-Jones returns
QMLE
0.00 0.04

Overview of the yuima


package

Option Pricing with R


-0.06

1972 1973 1974

S.M. Iacus 2011


break of gold-US$ linkage (left) WatergateR in
scandal (right)
Finance – 59 / 113
Least squares approach

Explorative Data
Analysis
De Gregorio and I. (2008) considered the change point problem for the
Estimation of Financial
ergodic model
Models

Likelihood approach dXt = b(Xt )dt + θσ(Xt )dWt , 0 ≤ t ≤ T, X0 = x0 ,
Two stage least squares
estimation
observed at discrete time instants. The change point problem is
Model selection
formulated as follows
Numerical Evidence
( Rt √ Rt
Application to real data X0 + 0 b(Xs )ds + θ1 0 σ(Xs )dWs , 0 ≤ t ≤ τ ∗
Xt = Rt √ Rt
The change point
problem Xτ ∗ + τ ∗ b(Xs )ds + θ2 τ ∗ σ(Xs )dWs , τ ∗ < t ≤ T
Least Squares
QMLE where τ ∗ ∈ (0, T ) is the change point and θ1 , θ2 two parameters to be
Overview of the yuima
package
estimated.
Option Pricing with R

S.M. Iacus 2011 R in Finance – 60 / 113


Least squares approach

Explorative Data
Analysis Xi+1 − Xi − b(Xi )∆n
Let Zi = √ be the standardized residuals. Then the
Estimation of Financial ∆n σ(Xi )
Models
LS estimator of τ is τ̂n = k̂0 /n where k̂0 is solution to
Likelihood approach

Two stage least squares k


estimation k Sk X
k̂0 = arg max −
, Sk = Zi2 .
Model selection k n Sn
i=1
Numerical Evidence

Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 61 / 113


Least squares approach

Explorative Data
Analysis Xi+1 − Xi − b(Xi )∆n
Let Zi = √ be the standardized residuals. Then the
Estimation of Financial ∆n σ(Xi )
Models
LS estimator of τ is τ̂n = k̂0 /n where k̂0 is solution to
Likelihood approach

Two stage least squares k


estimation k Sk X
k̂0 = arg max −
, Sk = Zi2 .
Model selection k n Sn
i=1
Numerical Evidence

Application to real data If we assume to observe the following SDE with unknown drift b(·)
The change point
problem

Least Squares
dXt = b(Xt )dt + θdWt ,
QMLE

Overview of the yuima


we can still estimate b(·) non parametrically and obtain a good change
package
point estimator.
Option Pricing with R

This approach has been used in Smaldone (2009) to re-analyze the recent
global financial crisis using data from different markets.

S.M. Iacus 2011 R in Finance – 61 / 113


Time

June 30 July 7 July 14 August 18 August 25 Sept. 1 Sept. 8 Sept. 15 Sept. 22 Sept. 29 Oct. 6 Oct. 20
July 4 July 11 July18 August 22 August 29 Sept. 5 Sept. 12 Sept. 19 Sept. 26 Oct. 3 Oct. 10 Oct 24

Lehman DJ Stoxx Global FTSE Lehman DJ Stoxx Goldman Deutsche Bank Dj Stoxx Asia Pacific
Brothers 1800 Banks DJ Stoxx 600 Brothers 600 Banks Sachs HSBC Nasdaq 600 Banks
JP Morgan Chase

DJ Stoxx America 600 Banks Nyse DJ Stoxx Global 1800


S&P MIB
DJ Stoxx 600 Banks Dow Jones MCSI World
Nikkei 225
Deutsche Bank S&P 500 Morgan Stanley
HBSC FTSE Bank of America
Barclays DAX Barclays
Deutsche Bank (GER) S&P MIB RBS
CAC CAC Unicredit
IBEX Intesa Sanpaolo
SMI Deutsche Bank (GER)
Nikkei 225 Commerzbank
DJ Stoxx 600 Banks
cpoint function in the sde package

S [2004−07−23/2005−05−05]
log returns
Last 20.45
Bollinger Bands (20,2) [Upper/Lower]: 21.731/20.100
0.04

22
0.02
0.00

20
X

−0.02

18
−0.04

16
0 50 100 150 200

Index Lug 23 Ott 01 Dic 01 Feb 01 Apr 01


2004 2004 2004 2005 2005

> S <- get.hist.quote("ATL.MI", start = "2004-07-23", end = "2005-05-05")$Close


> require(sde)
> cpoint(S)
$k0
[1] 123
$tau0
[1] "2005-01-11"
$theta1
2005-01-12
0.1020001
$theta2
[1] 0.3770111
attr(,"index")
[1] "2005-01-12"

S.M. Iacus 2011 R in Finance – 63 / 113


Volatility Change-Point Estimation

Explorative Data
Analysis
The theory works for SDEs of the form
Estimation of Financial
Models dYt = bt dt + σ(Xt , θ)dWt , t ∈ [0, T ],
Likelihood approach

Two stage least squares where Wt a r-dimensional Wiener process and bt and Xt are
estimation
multidimensional processes and σ is the diffusion coefficient (volatility)
Model selection
matrix.
Numerical Evidence

Application to real data


When Y = X the problem is a diffusion model.
The change point
problem
Least Squares The process bt may have jumps but should not explode and it is treated as
QMLE a nuisance in this model and it is completely unspecified.
Overview of the yuima
package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 64 / 113


Change-point analysis

Explorative Data
Analysis
The change-point problem for the volatility is formalized as follows
Estimation of Financial ( Rt Rt
Models Y0 + 0 bs ds + 0 σ(Xs , θ1∗ )dWs for t ∈ [0, τ ∗ )
Yt = Rt Rt
Likelihood approach
Yτ ∗ + τ ∗ bs ds + τ ∗ σ(Xs , θ2∗ )dWs for t ∈ [τ ∗ , T ].
Two stage least squares
estimation

Model selection
The change point τ ∗ instant is unknown and is to be estimated, along with
Numerical Evidence
θ1∗ and θ2∗ , from the observations sampled from the path of (X, Y ).
Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 65 / 113


Example of Volatility Change-Point Estimation

Consider the 2-dimensional stochastic differential equation


′ 
dXt1 Xt1 θ1.1 · Xt1 Xt1 dWt1
     
1− 0·
= dt +
dXt2 3− Xt2 0 · Xt2 θ2.2 · Xt2 dWt2

X01 = 1.0, X02 = 1.0,


with change point instant at time τ = 4
1.5
x1

7 0.5
5
x2

3
1

S.M. Iacus 2011 0 2 4 6 8 R in Finance – 66


10/ 113
the CPoint function in the yuima package

Explorative Data
Analysis
the object yuima contains the model and the data from the previous slide
so we can call CPoint on this yuima object
Estimation of Financial
Models

Likelihood approach > t.est <- CPoint(yuima,param1=t1,param2=t2, plot=TRUE)


Two stage least squares >
estimation > t.est$tau
[1] 3.99
Model selection

Numerical Evidence

Application to real data

The change point


problem
Least Squares
QMLE

Overview of the yuima


package

Option Pricing with R

S.M. Iacus 2011 R in Finance – 67 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Overview of the yuima package


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 68 / 113


The yuima object

Explorative Data
Analysis
The main object is the yuima object which allows to describe the model
Estimation of Financial
in a mathematically sound way.
Models

Likelihood approach Then the data and the sampling structure can be included as well or, just
Two stage least squares
estimation
the sampling scheme from which data can be generated according to the
Model selection
model.
Numerical Evidence
The package exposes very few generic functions like simulate, qmle,
Application to real data
plot, etc. and some other specific functions for special tasks.
The change point
problem

Overview of the yuima Before looking at the details, let us see an overview of the main object.
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 69 / 113


univariate ts, xts, ...
Data
multivariate

multigrid
diffusion fractional
asynch.
Lévy BM

regular
irregular

Sampling Yuima Model

tick times
space disc.
...
Markov
random Switching HMM
deterministic
univariate ts, xts, ...
Data
multivariate

multigrid
diffusion fractional
asynch.
Lévy BM

regular
irregular

Sampling Yuima Model

tick times
space disc.
...
Markov
random Switching HMM
deterministic
univariate ts, xts, ...
Data
multivariate

multigrid
diffusion fractional
asynch.
Lévy BM

regular
irregular

Sampling Yuima Model

tick times
space disc.
...
Markov
random Switching HMM
deterministic
univariate ts, xts, ...
Data
multivariate

multigrid
diffusion fractional
asynch.
Lévy BM

regular
irregular

Sampling Yuima Model

tick times
space disc.
...
Markov
random Switching HMM
deterministic
yuima
yuima-class

yuima-class
model
data
sampling
characteristic
functional

model sampling data functional


yuima.model yuima.sampling yuima.data yuima.functional

yuima.model yuima.sampling yuima.data yuima.functional


drift Initial original.data F
diffusion Terminal zoo.data f
hurst n xinit
measure delta e
measure.type grid
state.variable random
parameter characteristic
regular
state.variable sdelta yuima.characteristic
jump.variable sgrid
time.variable oindex yuima.characteristic
noise.number interpolation equation.number
equation.number time.scale
dimension
solve.variable
xinit
J.flag
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
Covariation Nonparametrics p-variation
Euler-
Maruyama
Quasi
MLE
Space High freq. Diff,
discr. Low freq. Jumps,
Exact Simulation fBM

Parametric
Yuima Inference
Asymptotic
expansion
Option
pricing Adaptive
Bayes Change
Monte Akaike’s MCMC point
Carlo

LASSO- Hypotheses
type Model Testing
selection
The model specification

Explorative Data
Analysis
We consider here the three main classes of SDE’s which can be easily
Estimation of Financial
specified. All multidimensional and eventually parametric models.
Models

Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 81 / 113


The model specification

Explorative Data
Analysis
We consider here the three main classes of SDE’s which can be easily
Estimation of Financial
specified. All multidimensional and eventually parametric models.
Models

Likelihood approach  Diffusions dXt = a(t, Xt )dt + b(t, Xt )dWt


Two stage least squares
estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 81 / 113


The model specification

Explorative Data
Analysis
We consider here the three main classes of SDE’s which can be easily
Estimation of Financial
specified. All multidimensional and eventually parametric models.
Models

Likelihood approach  Diffusions dXt = a(t, Xt )dt + b(t, Xt )dWt


Two stage least squares
estimation  Fractional Gaussian Noise, with H the Hurst parameter
Model selection

Numerical Evidence dXt = a(t, Xt )dt + b(t, Xt )dWtH


Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 81 / 113


The model specification

Explorative Data
Analysis
We consider here the three main classes of SDE’s which can be easily
Estimation of Financial
specified. All multidimensional and eventually parametric models.
Models

Likelihood approach  Diffusions dXt = a(t, Xt )dt + b(t, Xt )dWt


Two stage least squares
estimation  Fractional Gaussian Noise, with H the Hurst parameter
Model selection

Numerical Evidence dXt = a(t, Xt )dt + b(t, Xt )dWtH


Application to real data

The change point


problem

Overview of the yuima  Diffusions with jumps, Lévy


package
yuima object
Z
functionalities dXt = a(Xt )dt + b(Xt )dWt + c(Xt− , z)µ(dt, dz)
how to use it
|z|>1
Option Pricing with R Z
+ c(Xt− , z){µ(dt, dz) − ν(dz)dt}
0<|z|≤1
S.M. Iacus 2011 R in Finance – 81 / 113
1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis > mod1 <- setModel(drift = "-3*x", diffusion = "1/(1+x^2)")
Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 82 / 113


1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis > mod1 <- setModel(drift = "-3*x", diffusion = "1/(1+x^2)")
Estimation of Financial
Models yuima
yuima-class
Likelihood approach
yuima-class
Two stage least squares model
estimation data
sampling
characteristic
Model selection functional

Numerical Evidence

Application to real data model sampling data functional


yuima.model yuima.sampling yuima.data yuima.functional
The change point
problem yuima.model yuima.sampling yuima.data yuima.functional
drift Initial original.data F
Overview of the yuima diffusion Terminal zoo.data f
hurst n xinit
package measure e
delta
yuima object measure.type grid
state.variable random
characteristic
functionalities parameter regular
state.variable sdelta yuima.characteristic
how to use it jump.variable sgrid
time.variable oindex yuima.characteristic
noise.number interpolation equation.number
Option Pricing with R
equation.number time.scale
dimension
solve.variable
xinit
J.flag

S.M. Iacus 2011 R in Finance – 82 / 113


1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis > mod1 <- setModel(drift = "-3*x", diffusion = "1/(1+x^2)")
Estimation of Financial
Models

Likelihood approach > str(mod1)


Formal class ’yuima.model’ [package "yuima"] with 16 slots
Two stage least squares
..@ drift : expression((-3 * x))
estimation
..@ diffusion :List of 1
.. ..$ : expression(1/(1 + x^2))
Model selection
..@ hurst : num 0.5
..@ jump.coeff : expression()
Numerical Evidence
..@ measure : list()
Application to real data ..@ measure.type : chr(0)
..@ parameter :Formal class ’model.parameter’ [package "yuima"] with 6 slots
The change point .. .. ..@ all : chr(0)
problem .. .. ..@ common : chr(0)
.. .. ..@ diffusion: chr(0)
Overview of the yuima .. .. ..@ drift : chr(0)
package .. .. ..@ jump : chr(0)
.. .. ..@ measure : chr(0)
yuima object ..@ state.variable : chr "x"
functionalities ..@ jump.variable : chr(0)
..@ time.variable : chr "t"
how to use it
..@ noise.number : num 1
..@ equation.number: int 1
Option Pricing with R
..@ dimension : int [1:6] 0 0 0 0 0 0
..@ solve.variable : chr "x"
..@ xinit : num 0
..@ J.flag : logi FALSE

S.M. Iacus 2011 R in Finance – 83 / 113


1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis
And we can easily simulate and plot the model like
Estimation of Financial
Models
> set.seed(123)
Likelihood approach > X <- simulate(mod1)
Two stage least squares
> plot(X)
estimation

Model selection

Numerical Evidence
0.2

Application to real data


0.0

The change point


problem
−0.2

Overview of the yuima


x

package
−0.4

yuima object
−0.6

functionalities
how to use it
−0.8

Option Pricing with R


0.0 0.2 0.4 0.6 0.8 1.0

S.M. Iacus 2011 R in Finance – 84 / 113


1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis
The simulate function fills the slots data and sampling
Estimation of Financial
Models
> str(X)
Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 85 / 113


1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis
The simulate function fills the slots data and sampling
Estimation of Financial
Models
> str(X)
yuima
Likelihood approach
yuima-class

Two stage least squares yuima-class


estimation model
data
Model selection sampling
characteristic
functional
Numerical Evidence

Application to real data


model sampling data functional
The change point yuima.model yuima.data yuima.functional
yuima.sampling
problem
yuima.model yuima.sampling yuima.data yuima.functional
Overview of the yuima drift Initial original.data F
package diffusion Terminal zoo.data f
hurst n xinit
yuima object measure delta e
measure.type grid
functionalities state.variable random
parameter characteristic
how to use it regular
state.variable sdelta yuima.characteristic
jump.variable sgrid
Option Pricing with R time.variable oindex yuima.characteristic
noise.number interpolation equation.number
equation.number time.scale
dimension
solve.variable
xinit
J.flag
S.M. Iacus 2011 R in Finance – 85 / 113
1
dXt = −3Xt dt + 1+Xt2
dWt

Explorative Data
Analysis
The simulate function fills the slots data and sampling
Estimation of Financial
Models
> str(X)
Likelihood approach

Two stage least squares Formal class ’yuima’ [package "yuima"] with 5 slots
estimation ..@ data :Formal class ’yuima.data’ [package "yuima"] with 2 slots
.. .. ..@ original.data: ts [1:101, 1] 0 -0.217 -0.186 -0.308 -0.27 ...
Model selection .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : NULL
Numerical Evidence .. .. .. .. ..$ : chr "Series 1"
.. .. .. ..- attr(*, "tsp")= num [1:3] 0 1 100
Application to real data .. .. ..@ zoo.data :List of 1
.. .. .. ..$ Series 1:’zooreg’ series from 0 to 1
The change point ..@ model :Formal class ’yuima.model’ [package "yuima"] with 16 slots
problem
(...) output dropped
Overview of the yuima
package ..@ sampling :Formal class ’yuima.sampling’ [package "yuima"] with 11 slots
yuima object .. .. ..@ Initial : num 0
.. .. ..@ Terminal : num 1
functionalities .. .. ..@ n : num 100
how to use it .. .. ..@ delta : num 0.1
.. .. ..@ grid : num(0)
Option Pricing with R .. .. ..@ random : logi FALSE
.. .. ..@ regular : logi TRUE
.. .. ..@ sdelta : num(0)
.. .. ..@ sgrid : num(0)
.. .. ..@ oindex : num(0)
.. .. ..@ interpolation: chr "none"
S.M. Iacus 2011 R in Finance – 86 / 113
1
Parametric model: dXt = −θXt dt + 1+Xtγ
dWt

Explorative Data
Analysis > mod2 <- setModel(drift = "-theta*x", diffusion = "1/(1+x^gamma)")
Estimation of Financial
Models Automatic extraction of the parameters for further inference
Likelihood approach
> str(mod2)
Two stage least squares
Formal class ’yuima.model’ [package "yuima"] with 16 slots
estimation
..@ drift : expression((-theta * x))
..@ diffusion :List of 1
Model selection
.. ..$ : expression(1/(1 + x^gamma))
..@ hurst : num 0.5
Numerical Evidence
..@ jump.coeff : expression()
..@ measure : list()
Application to real data
..@ measure.type : chr(0)
The change point ..@ parameter :Formal class ’model.parameter’ [package "yuima"] with 6 slots
problem .. .. ..@ all : chr [1:2] "theta" "gamma"
.. .. ..@ common : chr(0)
Overview of the yuima .. .. ..@ diffusion: chr "gamma"
package .. .. ..@ drift : chr "theta"
.. .. ..@ jump : chr(0)
yuima object .. .. ..@ measure : chr(0)
functionalities ..@ state.variable : chr "x"
..@ jump.variable : chr(0)
how to use it ..@ time.variable : chr "t"
..@ noise.number : num 1
Option Pricing with R
..@ equation.number: int 1
..@ dimension : int [1:6] 2 0 1 1 0 0
..@ solve.variable : chr "x"
..@ xinit : num 0
..@ J.flag : logi FALSE
S.M. Iacus 2011 R in Finance – 87 / 113
1
Parametric model: dXt = −θXt dt + 1+Xtγ
dWt

Explorative Data
Analysis
And this can be simulated specifying the parameters
Estimation of Financial
Models
> simulate(mod2,true.param=list(theta=1,gamma=3))
Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 88 / 113


2-dimensional diffusions with 3 noises

Explorative Data
Analysis dXt1 = −3Xt1 dt + dWt1 + Xt2 dWt3
Estimation of Financial
Models dXt2 = −(Xt1 + 2Xt2 )dt + Xt1 dWt1 + 3dWt2
Likelihood approach has to be organized into matrix form
Two stage least squares
estimation
dWt1
 
dXt1 −3Xt1 1 0 Xt2
     
Model selection
= dt + dWt2
dXt2 −Xt1 − 2Xt2 Xt1 3 0
 
Numerical Evidence
dWt3
Application to real data

The change point


problem
> sol <- c("x1","x2") # variable for numerical solution
Overview of the yuima
package
> a <- c("-3*x1","-x1-2*x2") # drift vector
> b <- matrix(c("1","x1","0","3","x2","0"),2,3) # diffusion matrix
yuima object
> mod3 <- setModel(drift = a, diffusion = b, solve.variable = sol)
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 89 / 113


2-dimensional diffusions with 3 noises

Explorative Data
Analysis dXt1 = −3Xt1 dt + dWt1 + Xt2 dWt3
Estimation of Financial
Models dXt2 = −(Xt1 + 2Xt2 )dt + Xt1 dWt1 + 3dWt2
Likelihood approach

Two stage least squares > str(mod3)


estimation Formal class ’yuima.model’ [package "yuima"] with 16 slots
..@ drift : expression((-3 * x1), (-x1 - 2 * x2))
Model selection ..@ diffusion :List of 2
.. ..$ : expression(1, 0, x2)
Numerical Evidence .. ..$ : expression(x1, 3, 0)
..@ hurst : num 0.5
Application to real data ..@ jump.coeff : expression()
..@ measure : list()
The change point ..@ measure.type : chr(0)
problem ..@ parameter :Formal class ’model.parameter’ [package "yuima"] with 6 slots
.. .. ..@ all : chr(0)
Overview of the yuima .. .. ..@ common : chr(0)
package .. .. ..@ diffusion: chr(0)
yuima object .. .. ..@ drift : chr(0)
.. .. ..@ jump : chr(0)
functionalities .. .. ..@ measure : chr(0)
how to use it ..@ state.variable : chr "x"
..@ jump.variable : chr(0)
Option Pricing with R ..@ time.variable : chr "t"
..@ noise.number : int 3
..@ equation.number: int 2
..@ dimension : int [1:6] 0 0 0 0 0 0
..@ solve.variable : chr [1:2] "x1" "x2"
..@ xinit : num [1:2] 0 0
S.M. Iacus 2011 ..@ J.flag : logi FALSE R in Finance – 90 / 113
Plot methods inherited by zoo

Explorative Data
Analysis > set.seed(123)
> X <- simulate(mod3)
Estimation of Financial
Models > plot(X,plot.type="single",col=c("red","blue"))

Likelihood approach

Two stage least squares


estimation
1

Model selection

Numerical Evidence
0

Application to real data


−1

The change point


x1

problem

Overview of the yuima


−2

package
yuima object
functionalities
−3

how to use it

Option Pricing with R 0.0 0.2 0.4 0.6 0.8 1.0

S.M. Iacus 2011 R in Finance – 91 / 113


Multidimensional SDE

Explorative Data
Analysis
Also models likes this can be specified
Estimation of Financial  1 2/3
Models dX 1 = X 2 X dW 1 ,

 t t t t
Likelihood approach
dXt2 = g(t)dXt3 , ,
Two stage least squares  p
3 3 1
dXt = Xt (µdt + σ(ρdWt + 1 − ρ2 dWt2 ))
estimation

Model selection

Numerical Evidence where g(t) = 0.4 + (0.1 + 0.2t)e−2t


Application to real data

The change point The above is an example of parametric SDE with more equations than
problem
noises.
Overview of the yuima
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 92 / 113


Fractional Gaussian Noise dYt = 3Yt dt + dWtH

Explorative Data > mod4 <- setModel(drift=3*y, diffusion=1, hurst=0.3, solve.var=y)


Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 93 / 113


Fractional Gaussian Noise dYt = 3Yt dt + dWtH

Explorative Data > mod4 <- setModel(drift=3*y, diffusion=1, hurst=0.3, solve.var=y)


Analysis
The hurst slot is filled
Estimation of Financial
Models
> str(mod4)
Likelihood approach Formal class ’yuima.model’ [package "yuima"] with 16 slots
..@ drift : expression((3 * y))
Two stage least squares ..@ diffusion :List of 1
estimation .. ..$ : expression(1)
..@ hurst : num 0.3
Model selection ..@ jump.coeff : expression()
..@ measure : list()
Numerical Evidence ..@ measure.type : chr(0)
..@ parameter :Formal class ’model.parameter’ [package "yuima"] with 6 slots
Application to real data .. .. ..@ all : chr(0)
.. .. ..@ common : chr(0)
The change point .. .. ..@ diffusion: chr(0)
problem .. .. ..@ drift : chr(0)
.. .. ..@ jump : chr(0)
Overview of the yuima
.. .. ..@ measure : chr(0)
package
..@ state.variable : chr "x"
yuima object ..@ jump.variable : chr(0)
..@ time.variable : chr "t"
functionalities
..@ noise.number : num 1
how to use it ..@ equation.number: int 1
..@ dimension : int [1:6] 0 0 0 0 0 0
Option Pricing with R ..@ solve.variable : chr "y"
..@ xinit : num 0
..@ J.flag : logi FALSE

S.M. Iacus 2011 R in Finance – 93 / 113


Fractional Gaussian Noise dYt = 3Yt dt + dWtH

Explorative Data
Analysis > set.seed(123)
> X <- simulate(mod4, n=1000)
Estimation of Financial
Models > plot(X, main="I’m fractional!")

Likelihood approach
I’m fractional!
Two stage least squares
estimation
4

Model selection

Numerical Evidence
3

Application to real data


2
y

The change point


problem
1

Overview of the yuima


package
0

yuima object
functionalities
how to use it 0.0 0.2 0.4 0.6 0.8 1.0

Option Pricing with R t

S.M. Iacus 2011 R in Finance – 94 / 113


Jump processes

Explorative Data
Analysis
Jump processes can be specified in different ways in mathematics (and
Estimation of Financial
hence in yuima package).
Models

Likelihood approach Let Zt be a Compound Poisson Process (i.e. jumps follow some
Two stage least squares
estimation
distribution, e.g. Gaussian)
Model selection
Then is is possible to consider the following SDE which involves jumps
Numerical Evidence

Application to real data


dXt = a(Xt )dt + b(Xt )dWt + dZt
The change point
problem
Next is an example of Poisson process with intensity λ = 10 and
Overview of the yuima
package Gaussian jumps.
yuima object
functionalities
how to use it
In this case we specify measure.type as “CP” (Compound Poisson)
Option Pricing with R

S.M. Iacus 2011 R in Finance – 95 / 113


Jump process: dXt = −θXt dt + σdWt + Zt

Explorative Data
Analysis > mod5 <- setModel(drift=c("-theta*x"), diffusion="sigma",
jump.coeff="1", measure=list(intensity="10", df=list("dnorm(z, 0, 1)")
Estimation of Financial
Models measure.type="CP", solve.variable="x")
> set.seed(123)
Likelihood approach > X <- simulate(mod5, true.p=list(theta=1,sigma=3),n=1000)
Two stage least squares > plot(X, main="I’m jumping!")
estimation

Model selection
I’m jumping!
Numerical Evidence

Application to real data


0

The change point


−2

problem

Overview of the yuima


package
−4
x

yuima object
functionalities
−6

how to use it
−8

Option Pricing with R

0.0 0.2 0.4 0.6 0.8 1.0

S.M. Iacus 2011 t R in Finance – 96 / 113


Jump processes

Explorative Data
Analysis
Another way is to specify the Lévy measure. Without going into too much
details, here is an example of a simple OU process with IG Lévy measure
Estimation of Financial
Models dXt = −Xt dt + dZt
Likelihood approach

Two stage least squares > mod6 <- setModel(drift="-x", xinit=1, jump.coeff="1",
estimation measure.type="code", measure=list(df="rIG(z, 1, 0.1)"))
Model selection > set.seed(123)
> plot( simulate(mod6, Terminal=10, n=10000), main="I’m also jumping!")
Numerical Evidence

Application to real data I’m also jumping!


The change point
problem
20

Overview of the yuima


package
yuima object
15

functionalities
x

how to use it
10

Option Pricing with R


5
0

S.M. Iacus 2011 0 2 4 6 8 R10in Finance – 97 / 113


The setModel method

Explorative Data
Analysis
Models are specified via
Estimation of Financial
setModel(drift, diffusion, hurst = 0.5, jump.coeff, measure,
Models
measure.type, state.variable = x, jump.variable = z, time.variable
Likelihood approach
= t, solve.variable, xinit) in
Two stage least squares
estimation
dXt = a(Xt )dt + b(Xt )dWt + c(Xt )dZt
Model selection

Numerical Evidence
The package implements many multivariate RNG to simulate Lévy paths
Application to real data
including rIG, rNIG, rbgamma, rngamma, rstable.
The change point
problem

Overview of the yuima Other user-defined or packages-defined RNG can be used freely.
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 98 / 113


The setModel method

Explorative Data
Analysis
Models are specified via
Estimation of Financial
setModel(drift, diffusion, hurst = 0.5, jump.coeff, measure,
Models
measure.type, state.variable = x, jump.variable = z, time.variable
Likelihood approach
= t, solve.variable, xinit) in
Two stage least squares
estimation
dXt = a(Xt )dt + b(Xt )dWt + c(Xt )dZt
Model selection

Numerical Evidence
The package implements many multivariate RNG to simulate Lévy paths
Application to real data
including rIG, rNIG, rbgamma, rngamma, rstable.
The change point
problem

Overview of the yuima Other user-defined or packages-defined RNG can be used freely.
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 99 / 113


The setModel method

Explorative Data
Analysis
Models are specified via
Estimation of Financial
setModel(drift, diffusion, hurst = 0.5, jump.coeff, measure,
Models
measure.type, state.variable = x, jump.variable = z, time.variable
Likelihood approach
= t, solve.variable, xinit) in
Two stage least squares
estimation
dXt = a(Xt )dt + b(Xt )dWt + c(Xt )dZt
Model selection

Numerical Evidence
The package implements many multivariate RNG to simulate Lévy paths
Application to real data
including rIG, rNIG, rbgamma, rngamma, rstable.
The change point
problem

Overview of the yuima Other user-defined or packages-defined RNG can be used freely.
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 100 / 113


The setModel method

Explorative Data
Analysis
Models are specified via
Estimation of Financial
setModel(drift, diffusion, hurst = 0.5, jump.coeff, measure,
Models
measure.type, state.variable = x, jump.variable = z, time.variable
Likelihood approach
= t, solve.variable, xinit) in
Two stage least squares
estimation
dXt = a(Xt )dt + b(Xt )dWt + c(Xt )dZt
Model selection

Numerical Evidence
The package implements many multivariate RNG to simulate Lévy paths
Application to real data
including rIG, rNIG, rbgamma, rngamma, rstable.
The change point
problem

Overview of the yuima Other user-defined or packages-defined RNG can be used freely.
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 101 / 113


The setModel method

Explorative Data
Analysis
Models are specified via
Estimation of Financial
setModel(drift, diffusion, hurst = 0.5, jump.coeff, measure,
Models
measure.type, state.variable = x, jump.variable = z, time.variable
Likelihood approach
= t, solve.variable, xinit) in
Two stage least squares
estimation
dXt = a(Xt )dt + b(Xt )dWt + c(Xt )dZt
Model selection

Numerical Evidence
The package implements many multivariate RNG to simulate Lévy paths
Application to real data
including rIG, rNIG, rbgamma, rngamma, rstable.
The change point
problem Other user-defined or packages-defined RNG can be used freely.
Overview of the yuima
package
yuima object
functionalities
how to use it

Option Pricing with R

S.M. Iacus 2011 R in Finance – 102 / 113


Explorative Data
Analysis

Estimation of Financial
Models

Likelihood approach

Two stage least squares


estimation

Model selection Option Pricing with R


Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 103 / 113


R options for option pricing

Explorative Data
Analysis
There are several good packages on CRAN which implement the basic
Estimation of Financial
option pricing formulas. Just to mention a couple (with apologizes to the
Models
authors pf the other packages)
Likelihood approach

Two stage least squares  Rmetrics suite which includes: fOptions, fAsianOptions,
estimation
fExoticOptions
Model selection

Numerical Evidence  RQuanLib for European, American and Asian option pricing
Application to real data

The change point


But if you need to go outside the standard Black & Scholes geometric
problem
Brownian Motion model, the only way reamins Monte Carlo analysis.
Overview of the yuima
package
And for jump processes, maybe, yuima package is one of the current
Option Pricing with R
frameworks you an think to use.
Advertizing
In addition, yuima also offers asymptotic expansion formulas for general
diffusion processes which can be used in Asian option pricing.

S.M. Iacus 2011 R in Finance – 104 / 113


Asymptotic expansion

Explorative Data
Analysis
The yuima package can handle asymptotic expansion of functionals of
Estimation of Financial
d-dimensional diffusion process
Models

Likelihood approach dXtε = a(Xtε , ε)dt + b(Xtε , ε)dWt , ε ∈ (0, 1]


Two stage least squares
estimation
with Wt and r-dimensional Wiener process, i.e. Wt = (Wt1 , . . . , Wtr ).
Model selection

Numerical Evidence
The functional is expressed in the following abstract form
Application to real data
r Z T
The change point X
problem F ε (Xtε ) = fα (Xtε , d)dWtα + F (Xtε , ε), Wt0 = t
Overview of the yuima α=0 0
package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 105 / 113


Estimation of functionals. Example.

Explorative Data
Analysis
Example: B&S asian call option
Estimation of Financial
Models dXtε = µXtε dt + εXtε dWt
Likelihood approach
  T 
1
Z
Xtε dt − K, 0
Two stage least squares
estimation and the B&S price is related to E max . Thus
T 0
Model selection
the functional of interest is
Numerical Evidence
T
1
Z
ε ε
Xtε dt,
Application to real data
F (Xt ) = r=1
The change point T 0
problem

Overview of the yuima


package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 106 / 113


Estimation of functionals. Example.

Explorative Data
Analysis
Example: B&S asian call option
Estimation of Financial
Models dXtε = µXtε dt + εXtε dWt
Likelihood approach
  T 
1
Z
Xtε dt − K, 0
Two stage least squares
estimation and the B&S price is related to E max . Thus
T 0
Model selection
the functional of interest is
Numerical Evidence
T
1
Z
ε ε
Xtε dt,
Application to real data
F (Xt ) = r=1
The change point T 0
problem

Overview of the yuima with


package x
f0 (x, ε) = , f1 (x, ε) = 0, F (x, ε) = 0
Option Pricing with R T
Advertizing
in
r Z
X T
F ε (Xtε ) = fα (Xtε , d)dWtα + F (Xtε , ε)
α=0 0

S.M. Iacus 2011 R in Finance – 106 / 113


Estimation of functionals. Example.

Explorative Data
Analysis
So, the call option price requires the composition of a smooth functional
Estimation of Financial
T
1
Z
Models
ε
F (Xtε ) = Xtε dt, r=1
Likelihood approach T 0
Two stage least squares
estimation
with the irregular function
Model selection

Numerical Evidence max(x − K, 0)


Application to real data

The change point Monte Carlo methods require a HUGE number of simulations to get the
problem
desired accuracy of the calculation of the price, while asymptotic
Overview of the yuima
package expansion of F ε provides unexpectedly accurate approximations.
Option Pricing with R
Advertizing The yuima package provides functions to construct the functional F ε ,
and automatic asymptotic expansion based on Malliavin calculus starting
from a yuima object.

S.M. Iacus 2011 R in Finance – 107 / 113


setFunctional method

Explorative Data
Analysis > diff.matrix <- matrix( c("x*e"), 1,1)
> model <- setModel(drift = c("x"), diffusion = diff.matrix)
Estimation of Financial
Models > T <- 1
> xinit <- 1
Likelihood approach > f <- list( expression(x/T), expression(0))
Two stage least squares > F <- 0
estimation > e <- .3
Model selection > yuima <- setYuima(model = model, sampling = setSampling(Terminal=T, n
> yuima <- setFunctional( yuima, f=f,F=F, xinit=xinit,e=e)
Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 108 / 113


setFunctional method

Explorative Data
Analysis > diff.matrix <- matrix( c("x*e"), 1,1)
> model <- setModel(drift = c("x"), diffusion = diff.matrix)
Estimation of Financial
Models > T <- 1
> xinit <- 1
Likelihood approach > f <- list( expression(x/T), expression(0))
Two stage least squares > F <- 0
estimation > e <- .3
Model selection > yuima <- setYuima(model = model, sampling = setSampling(Terminal=T, n
> yuima <- setFunctional( yuima, f=f,F=F, xinit=xinit,e=e)
Numerical Evidence yuima
yuima-class
Application to real data
yuima-class
The change point model
problem data
sampling
Overview of the yuima characteristic
functional
package

Option Pricing with R


Advertizing model sampling data functional
yuima.model yuima.sampling yuima.data yuima.functional

yuima.model yuima.sampling yuima.data yuima.functional


drift Initial original.data F
diffusion Terminal zoo.data f
hurst n xinit
measure delta e
measure.type grid
state.variable random
parameter characteristic
regular
S.M. Iacus 2011 state.variable sdelta yuima.characteristic R in Finance – 108 / 113
setFunctional method

Explorative Data
Analysis > diff.matrix <- matrix( c("x*e"), 1,1)
> model <- setModel(drift = c("x"), diffusion = diff.matrix)
Estimation of Financial
Models > T <- 1
> xinit <- 1
Likelihood approach > f <- list( expression(x/T), expression(0))
Two stage least squares > F <- 0
estimation > e <- .3
Model selection > yuima <- setYuima(model = model, sampling = setSampling(Terminal=T, n
> yuima <- setFunctional( yuima, f=f,F=F, xinit=xinit,e=e)
Numerical Evidence

Application to real data the definition of the functional is now included in the yuima object
The change point (some output dropped)
problem > str(yuima)
Formal class ’yuima’ [package "yuima"] with 5 slots
Overview of the yuima ..@ data :Formal class ’yuima.data’ [package "yuima"] with 2 slots
package ..@ model :Formal class ’yuima.model’ [package "yuima"] with 16 slots
..@ sampling :Formal class ’yuima.sampling’ [package "yuima"] with 11 slots
Option Pricing with R ..@ functional :Formal class ’yuima.functional’ [package "yuima"] with 4 slots
Advertizing .. .. ..@ F : num 0
.. .. ..@ f :List of 2
.. .. .. ..$ : expression(x/T)
.. .. .. ..$ : expression(0)
.. .. ..@ xinit: num 1
.. .. ..@ e : num 0.3

S.M. Iacus 2011 R in Finance – 109 / 113


Estimation of functionals. Example.

Explorative Data
Analysis
Then, it is as easy as
Estimation of Financial
> F0 <- F0(yuima)
Models > F0
[1] 1.716424
Likelihood approach
> max(F0-K,0) # asian call option price
Two stage least squares [1] 0.7164237
estimation

Model selection

Numerical Evidence

Application to real data

The change point


problem

Overview of the yuima


package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 110 / 113


Estimation of functionals. Example.

Explorative Data
Analysis
Then, it is as easy as
Estimation of Financial
> F0 <- F0(yuima)
Models > F0
[1] 1.716424
Likelihood approach
> max(F0-K,0) # asian call option price
Two stage least squares [1] 0.7164237
estimation

Model selection
and back to asymptotic expansion, the following script may work
> rho <- expression(0)
Numerical Evidence
> get_ge <- function(x,epsilon,K,F0){
Application to real data + tmp <- (F0 - K) + (epsilon * x)
The change point
+ tmp[(epsilon * x) < (K-F0)] <- 0
problem + return( tmp )
+ }
Overview of the yuima
package > K <- 1 # strike
> epsilon <- e # noise level
Option Pricing with R
> g <- function(x) {
Advertizing + tmp <- (F0 - K) + (epsilon * x)
+ tmp[(epsilon * x) < (K-F0)] <- 0
+ tmp
+ }

S.M. Iacus 2011 R in Finance – 110 / 113


Add more terms to the expansion

The expansion of previous functional gives


> asymp <- asymptotic_term(yuima, block=10, rho, g)
calculating d0 ...done
calculating d1 term ...done
> asymp$d0 + e * asymp$d1 # asymp. exp. of asian call price
[1] 0.7148786

> library(fExoticOptions) # From RMetrics suite


> TurnbullWakemanAsianApproxOption("c", S = 1, SA = 1, X = 1,
+ Time = 1, time = 1, tau = 0.0, r = 0, b = 1, sigma = e)
Option Price:
[1] 0.7184944

> LevyAsianApproxOption("c", S = 1, SA = 1, X = 1,
+ Time = 1, time = 1, r = 0, b = 1, sigma = e)
Option Price:

[1] 0.7184944
> X <- sde.sim(drift=expression(x), sigma=expression(e*x), N=1000,M=1000)
> mean(colMeans((X-K)*(X-K>0))) # MC asian call price based on M=1000 repl.

[1] 0.707046

S.M. Iacus 2011 R in Finance – 111 / 113


Multivariate Asymptotic Expansion

Explorative Data
Analysis
Asymptotic expansion is now also available for multidimensional
Estimation of Financial
diffusion processes like the Heston model
Models
q
dXt1,ε = aXt1,ε dt + εXt1,ε Xt2,ε dWt1
Likelihood approach

Two stage least squares q


estimation  
dXt2,ε = c(d − Xt2,ε )dt + ε Xt2,ε ρdWt1 + 1 − ρ2 dWt2
p
Model selection

Numerical Evidence

Application to real data


i.e. functionals of the form F (X 1,ε , X 2,ε ).
The change point
problem

Overview of the yuima


package

Option Pricing with R


Advertizing

S.M. Iacus 2011 R in Finance – 112 / 113


Advertizing

 Iacus (2008) Simulation and Inference for Stochastic Differential Equations with R
Examples, Springer, New York.

 Iacus (2011) Option Pricing and Estimation of Financial Models with R, Wiley &
Sons, Chichester.

S.M. Iacus 2011 R in Finance – 113 / 113

You might also like