13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

207

14.3.1 Market Regimes

Applying Hidden Markov Models to regime detection is tricky since the problem is actually a

form of unsupervised learning. That is, there is no "ground truth" or labelled data on which

to "train" the model. It is also unclear how many regime states exist a priori. Are there two,

three, four or more "true" hidden market regimes?

Answers to these questions depend heavily on the asset class being modelled, the choice of

time frame and the nature of data utilised. For instance, daily returns data in equities markets

often exhibits periods of lower volatility, even over a number of years, with exceptional periods

of high volatility in moments of "panic" or "correction". Is it natural then to consider modelling

equity indices with two states? Might there be a third intermediate state representing more vol

than usual but not outright panic?

Utilising Hidden Markov Models as overlays to a risk manager that can interfere with strategygenerated

orders requires careful research analysis and a solid understanding of the asset class(es)

being modelled. In a later chapter the performance of a systematic trading strategy will be

studied under a Hidden Markov Model-based risk manager.

14.3.2 Simulated Data

In this section simulated returns data will be generated from two separate Gaussian distributions,

each of which represents a "bullish" or "bearish" market regime. The bullish returns draw from

a Gaussian distribution with positive mean and low variance, while the bearish returns draw

from a Gaussian distribution with slight negative mean but higher variance.

Five separate market regime periods will be simulated and "stitched" together in R. The

subsequent stream of returns will then be utilised by a Hidden Markov Model in order to infer

posterior probabilities of the regime states, given the sequence of observations.

The first task is to install the depmixS4 and quantmod libraries and then import them into

R. The random seed will also be fixed in order to allow consistent replication of results:

> install.packages(’depmixS4’)

> install.packages(’quantmod’)

> library(’depmixS4’)

> library(’quantmod’)

> set.seed(1)

At this stage a two-regime market will be simulated. This is achieved by assuming market

returns are normally distributed. Separate regimes will be simulated with each containing N k

days of returns. Each of the k regimes will be bullish or bearish. The goal of the Hidden Markov

Model will be to identify when the regime has switched from bullish to bearish and vice versa.

In this example k = 5 and each N k is drawn from the interval [50, 150] uniformly. The bull

market is distributed as N (0.1, 0.1) while the bear market is distributed as N (−0.05, 0.2). The

parameters are set via the following code:

> # Create the parameters for the bull and

> # bear market returns distributions

> Nk_lower <- 50

> Nk_upper <- 150

> bull_mean <- 0.1

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

Saved successfully!

Ooh no, something went wrong!