Skip to content
All library documents

Fitting a CARMA(2,1) Model to Daily Price Data with yuima

Article Quant Q&A · Author: torbonde

Summary

The document describes an R workflow for estimating a CARMA(2,1) model with the yuima package from daily market price data. The initial attempt passes a price column directly into the package and produces an error while constructing the model data. The accepted response attributes the issue to how the time series is supplied, then demonstrates organizing dated observations as an xts series, handling the reversed data order, and creating yuima data with an explicit time step before fitting by quasi-maximum likelihood.

The response also recommends using centered log prices when modeling a process driven by Brownian motion, because the modeled process can take negative values. It emphasizes that the time increment must reflect the daily sampling frequency and gives starting values for estimation. This is a practical example rather than a general guide: the chosen data source, sample period, parameter starts, and model assumptions may need adjustment for other datasets or CARMA specifications.

Key ideas

  • The yuima workflow expects time series data to be passed in a suitable structured form.
  • Dated observations can be represented as an xts series before creating yuima data.
  • The sample ordering should be checked because the downloaded observations may be reversed.
  • For daily observations, the model time increment should reflect the sampling interval.
  • Centered log prices are suggested for a Brownian-driven model because the process may take negative values.

Tags

Full text
# Estimating Carma(2,1) parameters (using yuima package)


# Estimating Carma(2,1) parameters (using yuima package)












I am very new to R, and particularly to the `yuima` package, so I was hoping someone would be able to help me.

I have some data (daily prices) that I wish to fit to a Carma(2,1) model by estimating the parameters.

Suppose I have

```
d <- read.csv("http://chart.yahoo.com/table.csv?s=IBM&g=d&x=.csv")
```

What I then thought I should do is

```
y <- setYuima(data = setData(d$Close), model = setCarma(2,1))
x <- qmle(y, start = list(a1 = 1, a2 = 1, b0 = 1))
```

(but with some other parameters).

However when I do this, I get the following error in the first (`y <- ...`) line:

```
Error in if (dim(data@original.data)[2] == 1) { : 
  argument is of length zero
```

I have no idea why this is, and what the `setYuima` function expects. Can anyone tell me how to do this?

## Answer by lorenzo (score 3, accepted)

https://quant.stackexchange.com/a/22201

It is just a problem of how you pass times series to yuima.

Just one more thing, if you want to estimate a CARMA driven by a Brownian motion, it is better to work with log-prices instead of prices. Indeed, in the considered model, we have a non zero probability assigned to negative values of the process.

Try the following code

```
require(yuima)
library(xts)
d <- read.csv("http://chart.yahoo.com/table.csv?s=IBM&g=d&x=.csv")
head(d)
tail(d) # data are reversed !!! take care, you need to handle data
ibm <- xts(d$Close, order.by=as.Date(d$Date, "%Y-%m-%d")) # this is too long
ibm <- ibm[time(ibm)>=as.Date("2007-01-03","%Y-%m-%d"),] # cut as above
# delta is very important in estimation set always to 1/252 for daily data
mydata3 <- setData(log(ibm)-rep(log(ibm[1]), dim(ibm)[1]),delta=1/252)
y3 <- setYuima(data = mydata3, model = setCarma(2,1))
x3 <- qmle(y3, start = list(a1 = 0.1, a2 = 0.1, b0 = 0.1,b1=0.1))
coef(x3)
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.