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.

210

historical financial data.

14.3.3 Financial Data

In the above section it was straightforward for the Hidden Markov Model to determine regimes

because they had been simulated from a pre-specified set of Gaussians. The problem of regime

detection is actually an unsupervised learning challenge since the number of states is not known

a priori, nor is there any "ground truth" on which to train the HMM.

In this section two separate modelling tasks will be carried out. The first will involve fitting

the HMM with two regime states to S&P500 returns, while the second will utilise three states.

The results between the two models will be compared.

The process for applying the Hidden Markov Model provided by depmixS4 is similar to that

carried out for the simulated data. Instead of generating the returns stream from two Gaussian

distributions it will simply be downloaded using the quantmod library:

> # Obtain S&P500 data from 2004 onwards and

> # create the returns stream from this

> getSymbols( "^GSPC", from="2004-01-01" )

> gspcRets = diff( log( Cl( GSPC ) ) )

> returns = as.numeric(gspcRets)

The gspcRets time series object can be plotted, showing the volatile periods around 2008

and 2011. See Figure 14.5.

> plot(gspcRets)

Figure 14.5: Returns of the S&P500 over the period from 2004 onwards

As before a two-state Hidden Markov Model is fitted using the EM algorithm. The returns

and posterior probabilities of each regime are plotted. See Figure 14.6.

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

Saved successfully!

Ooh no, something went wrong!