Original link:tecdat.cn/?p=9686



In this paper, the market returns under two independent mechanisms of “bull market” and “bear market” will be simulated. A hidden Markov model identifies the probability of being in a particular state.

After an overview of the process for simulating data, the hidden Markov model is applied to stock data to determine the underlying mechanism.

Market system

Applying hidden Markov models to state detection is tricky because the problem is really a form of unsupervised learning. That is, there are no “base facts” or marker data to “train” the model. Are there two, three, four or more “true” hidden market mechanics?

The answers to these questions depend largely on the asset class to be modeled, the choice of time horizon, and the nature of the data used.

Simulated data

In this section, simulated yield data are generated from independent Gaussian distributions, each representing a “bullish” or “bullish” market mechanism. Bullish gains come from gaussian distributions with a positive mean and low variance, while bearish gains come from Gaussian distributions with a slightly negative mean but high variance.

The first task is to install the depmixS4 and Quantmod libraries and then import them into R.

install.packages('depmixS4')
install.packages('quantmod')
library('depmixS4')
library('quantmod')
set.seed(1)
Copy the code

 

 

Bull market distribution N(0.1,0.1) and bear market distribution N(−0.05,0.2). Set the parameters with the following code:

Nk_lower < -50 Nk_upper < -150 bull_mean < -0.1 bull_var < -0.1 bear_mean < -0.05 bear_var < -0.2Copy the code

 

The Nk values are randomly selected:

Days < -replicate (5, sample(Nk_lower:Nk_upper, 1))Copy the code

 

The returns of the KTH cycle are randomly selected:

Market_bear_2 < -rnorm (days[1], bull_mean, bull_var) bear_var ) market_bull_3 <- rnorm( days[3], bull_mean, bull_var ) market_bear_4 <- rnorm( days[4], bear_mean, bear_var ) market_bull_5 <- rnorm( days[5], bull_mean, bull_var )Copy the code

 

Create real State

True_regimes < -C (rep(1,days[1]), REP (2,days[2]), REP (1,days[3]), rep(2,days[4]), rep(1,days[5])) returns <- c( market_bull_1, market_bear_2, market_bull_3, market_bear_4, market_bull_5)Copy the code

 

Draw a revenue graph to show the significant changes in mean and variance between mechanism switches:

plot(returns, type="l", xlab='', ylab="Returns") 
Copy the code

[R

 

At this stage, the Expectation Maximization algorithm can be used to specify the hidden Markov model and fit it:

After model fitting, a posterior probability of being in a particular state can be plotted. Post_probs contains a posterior probability.

 

Financial data

In this section, you will perform two separate modeling tasks. The first will have the HMM have two mechanism states to fit the S&P500 returns, while the second will leverage three states. Compare the results between the two models.

Download using the Quantmod library:

Draw time series:

plot(gspcRets)
Copy the code

[

 

EM algorithm is used to fit hidden Markov model. Plot the yield rate and posterior probability of each scheme:

 

Note that the market was relatively flat during 2004 and 2007, so the probability of the second mechanism of the hidden Markov model is higher during this period. However, the subprime crisis occurred between 2007 and 2009.

The market became calmer in 2010, but more turbulence followed in 2011, causing HMM to once again bring high posteriori probabilities to the first type of mechanism. After 2011, when the market calmed down again, HMM consistently gave the second mechanism a high probability. In 2015, the market became more chaotic again, reflected in increased switching between HMM mechanisms.

 

 

Since the model considers three separate mechanisms, the quiet period from 2004 to 2007 resulted in a switch between mechanism 2 and mechanism 3. However, during the turbulent periods of 2008, 2010 and 2011, mechanism 1 dominated the posterior probability, indicating a highly volatile state. After 2011, the model reverted to switching between mechanism 2 and mechanism 3.