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.

131

residuals to determine if we have achieved a good fit.

Let us begin by simulating an ARMA(3,2) series:

> set.seed(3)

> x <- arima.sim(n=1000, model=list(ar=c(0.5, -0.25, 0.4), ma=c(0.5, -0.3)))

We will now create an object final to store the best model fit and lowest AIC value. We loop

over the various p, q combinations and use the current object to store the fit of an ARMA(i,j)

model, for the looping variables i and j.

If the current AIC is less than any previously calculated AIC we set the final AIC to this

current value and select that order. Upon termination of the loop we have the order of the

ARMA model stored in final.order and the ARIMA(p,d,q) fit itself (with the "Integrated" d

component set to 0) stored as final.arma:

> final.aic <- Inf

> final.order <- c(0,0,0)

> for (i in 0:4) for (j in 0:4) {

> current.aic <- AIC(arima(x, order=c(i, 0, j)))

> if (current.aic < final.aic) {

> final.aic <- current.aic

> final.order <- c(i, 0, j)

> final.arma <- arima(x, order=final.order)

> }

> }

Let us output the AIC, order and ARIMA coefficients:

> final.aic

[1] 2863.365

> final.order

[1] 3 0 2

> final.arma

Call:

arima(x = x, order = final.order)

Coefficients:

ar1 ar2 ar3 ma1 ma2 intercept

0.4470 -0.2822 0.4079 0.5519 -0.2367 0.0274

s.e. 0.0867 0.0345 0.0309 0.0954 0.0905 0.0975

sigma^2 estimated as 1.009: log likelihood = -1424.68, aic = 2863.36

We can see that the original order of the simulated ARMA model was recovered, namely with

p = 3 and q = 2. We can plot the corelogram of the residuals of the model to see if they look

like a realisation of discrete white noise (DWN), as given in Figure 10.23.

> acf(resid(final.arma))

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

Saved successfully!

Ooh no, something went wrong!