13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

104

Figure 10.2: Realisation of AR(1) Model, with α 1 = −0.6 and Associated Correlogram.

> x.ar$ar + c(-1.96, 1.96)*sqrt(x.ar$asy.var)

[1] -0.7538593 -0.4408353

Once again we recover the correct order of the model, with a very good estimate ˆα 1 = −0.597

of α 1 = −0.6. We also see that the true parameter falls within the 95% confidence interval once

again.

AR(2)

Let us add some more complexity to our autoregressive processes by simulating a model of order

2. In particular, we will set α 1 = 0.666, but also set α 2 = −0.333. Here’s the full code to simulate

and plot the realisation, as well as the correlogram for such a series, given in Figure 10.3.

> set.seed(1)

> x <- w <- rnorm(100)

> for (t in 3:100) x[t] <- 0.666*x[t-1] - 0.333*x[t-2] + w[t]

> layout(1:2)

> plot(x, type="l")

> acf(x)

As before we can see that the correlogram differs significantly from that of white noise, as we

would expect. There are statistically significant peaks at k = 1, k = 3 and k = 4.

Once again we are going to use the ar command to fit an AR(p) model to our underlying

AR(2) realisation. The procedure is similar as for the AR(1) fit:

> set.seed(1)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!