简体   繁体   English

是否可以在hmmlearn中拟合多元GMHMM?

[英]Is it possible to fit a multivariate GMHMM in hmmlearn?

I know it is possible to fit several sequences into hmmlearn but it seems to me that these sequences need to be drawn from the same distributions. 我知道可以将多个序列拟合到hmmlearn中,但是在我看来,这些序列需要从相同的分布中得出。

Is it possible to fit a GMHMM with several observations sequences drawn from different distributions in hmmlearn? 是否可以用从hmmlearn中的不同分布得出的几个观测序列拟合GMHMM?

My use case : I would like to fit a GMHMM with K financial time series from different stocks and predict the market regime that generated the K stock prices at a specified time. 我的用例:我想用来自不同股票的K金融时间序列拟合GMHMM,并预测在指定时间产生K股价的市场制度。 So the matrix input has dimension N (number of dates) × K (number of stocks). 因此,矩阵输入的维数为N(日期数)×K(股票数)。 If hmmlearn can't do that, please tell me if it is possible with another package in python or R? 如果hmmlearn无法做到这一点,请告诉我是否可以在python或R中使用另一个包? Thanks for you help! 感谢您的帮助!

My approach to your problem will be to use a multi-variate Gaussian for emission probabilities. 我对您的问题的处理方法是对排放概率使用多元高斯模型。

For example: let's assume that K is 2, ie, the number of locations is 2. 例如:假设K为2,即位置数为2。

In hmmlearn, the K will be encoded in the dimensions of the mean matrix. 在hmmlearn中,K将以均值矩阵的维数进行编码。

See, this example Sampling from HMM has a 2-dimensional output. 请参见本示例, 从HMM进行采样具有二维输出。 In other words the X.shape = (N, K) where N is the length of the sample 500 in this case, and K is the dimension of the output which is 2. 换句话说,X.shape =(N,K),其中N是这种情况下样本500的长度,K是输出的尺寸,即2。

Notice that the authors plotted each dimension on an axis, ie, x-axis plots the first dimension X[:, 0], and the y-axis the second dimension X[:, 1]. 请注意,作者在轴上绘制了每个尺寸,即x轴绘制了第一尺寸X [:, 0],y轴绘制了第二尺寸X [:, 1]。

To train your model, make sure that X1 and X2 are of the same shape as the sampled X in the example, and form the training dataset as described here . 要训​​练您的模型,请确保X1和X2与示例中的采样X形状相同,并按此处所述形成训练数据集。

In summary, adapt the example to your case by adjusting the K instead of K=2 and convert it to the GMHMM instead of GaussianHMM. 总之,通过调整K而不是K = 2来将示例适应您的情况,并将其转换为GMHMM而不是GaussianHMM。

# Another example
model = hmm.GaussianHMM(n_components=5, covariance_type="diag", n_iter=100)

K = 3 # Number of sites
model.n_features = K # initialise that the model has size of observations =  K 

# Create a random training sequence (only 1 sequence) with length = 100.
X1 = np.random.randn(100, K) # 100 observation for K sites
model.fit(X1)

# Sample the fitted model
X, Z = model.sample(200)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM