Using language mechanisms for detecting R Hidden Markov Model HMM

Original link: http://tecdat.cn/?p=9686

 


In this context, the market will return in two independent mechanism "bull" and "bear market" simulation. Hidden Markov Model recognition probability in a particular state.

After an overview of the process simulation data, the hidden Markov model used in US stocks data, to determine the basic mechanisms.

Market system

Hidden Markov model is applied state detection is tricky, because the problem is actually unsupervised learning a form. In other words, there is no "basic facts" or mark on which data can be "trained" model. Are there two, three, four or more "real" hidden market mechanisms?

The answer to these questions depends largely on the nature of the asset class to be modeled, and the time range of the data used. 

 Analog data

In this section, the rate of return generated simulation data from independent Gaussian distributions, each distribution represents a "bullish" or "bullish" market mechanism. Gauss bullish earnings from the mean and variance of the distribution of low positive, and put the proceeds from the mean slightly negative but higher variance Gaussian distribution.

The first task is to install depmixS4 and quantmod library and then import them into R.

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

In Bull distribution N (0.1,0.1) N (0.1,0.1) and bear market distribution N (-0.05,0.2) N (-0.05,0.2). Parameters provided by the following code:

# Create the parameters for the bull and
# bear market returns distributions
Nk_lower <- 50
Nk_upper <- 150
bull_mean <- 0.1
bull_var <- 0.1
bear_mean <- -0.05
bear_var <- 0.2
 
The values ​​are randomly selected NkNk:
# Create the list of durations (in days) for each regime
days <- replicate(5, sample(Nk_lower:Nk_upper, 1))
 

Gains kk cycle is randomly selected:

# Create the various bull and bear markets returns
market_bull_1 <- rnorm( days[1], bull_mean, bull_var ) 
market_bear_2 <- rnorm( days[2], bear_mean, 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 )
 

Create a true state

# Create the list of true regime states and full returns list
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)
 

FIG draw benefits may show significant changes between the switching means and variances:

plot(returns, type="l", xlab='', ylab="Returns") 
[R

 

At this stage, you can use the Expectation Maximization algorithm specified hidden Markov model and fit:

 

After fitting the model, you can draw the posterior probability in a particular state. post_probsAfter containing posterior probability.

 

 

financial data

In this section, we will perform two separate modeling tasks. The first two mechanisms will have HMM state to fit the S & P500 yield, while the second would use three states. The comparison between the two models.

使用quantmod库下载:

绘制gspcRets时间序列显示2008和2011时期:
plot(gspcRets)
[

 

使用EM算法拟合隐马尔可夫模型。每种方案的收益率和后验概率作图:

 

请注意,在2004年和2007年期间,市场较为平静,因此在此期间,隐马尔可夫模型第二种机制的可能性较高。然而,在2007年至2009年之间,由于次贷危机

市场在2010年变得较为平静,但在2011年又出现了更多动荡,这导致HMM再次给第一类机制带来了较高的后验概率。2011年之后,市场再次趋于平静,HMM始终给第二种机制以高概率。2015年,市场再次变得更加混乱,这反映在HMM机制之间的切换增加。

 

 

数据的长度使后验概率图难以解释。由于该模型被迫考虑三个单独的机制,因此在2004-2007年的平静时期导致了机制2和机制3之间的转换。但是,在2008、2010和2011年的动荡时期,机制1主导着后验概率,表明高度波动状态。在2011年之后,模型恢复为在机制2和机制3之间切换。

 

发布了445 篇原创文章 · 获赞 246 · 访问量 97万+

Guess you like

Origin blog.csdn.net/qq_19600291/article/details/103629710