Vib*_*hav 9 python time-series hidden-markov-models hmmlearn
我正在尝试使用高斯 HMM 预测股市。我不知道模型训练后预测步骤是如何完成的。我不明白准确预测最可能的状态序列如何有助于预测未来价值。
One of the question asked suggest this method: "Use the Viterbi algorithm with the (partial) sequence to obtain the most likely hidden-state-sequence. Take the emission distribution of the last hidden state in this sequence and predict e.g. the mean of that distribution (which often is Gaussian)."
I did not get what he says after predicting most likely state sequence.
I have trained my model using functions available with hmmlearn in python. I have also applied Viterbi algorithm over the sample to predict the possible hidden state sequence. But I have no idea what to do after that. I am not good with maths of continuous HMM. Please tell me how exactly prediction is done.
Code:
import numpy as np
from hmmlearn import hmm
import pandas as pd
np.random.seed(42)
model = hmm.GaussianHMM(n_components=3, covariance_type="full",algorithm='viterbi')
model.startprob_ = np.array([0.3,0.4,0.6])
model.transmat_ = np.array([[0.7, 0.2, 0.1], [0.3, 0.5, 0.2], [0.3, 0.3, 0.4]])
model.means_ = np.array([[0.0], [3.0], [5.0]])
model.covars_ = np.tile(np.identity(1), (3, 1, 1))
df = pd.read_csv("HistoricalQuotes.csv")
Y = df['close'][2:40]
Y = Y[::-1]
X = np.array(Y)
X = np.reshape(X, (-1,1))
model.fit(X)
Y = df['close'][40:55]
Y = Y[::-1]
X = np.array(Y)
Z = model.predict(X)
Run Code Online (Sandbox Code Playgroud)
你离你的目标不远了!
我还在样本上应用了 Viterbi 算法来预测可能的隐藏状态序列
使用 Viterbi 算法,您实际上预测了最可能的隐藏状态序列。最后一个状态对应于您作为输入传递的时间序列的最后一个样本的最可能状态。
为了预测下一个样本,您需要估计下一次排放最有可能来自哪个状态。
为此,您可以使用在训练阶段估计的状态转换矩阵,即 的更新值model.transmat_。
一旦预测了下一个样本最有可能的状态,您就可以使用与该状态相关联的高斯分布。比方说,你预测的状态K,那么高斯分布的参数将在更新后的价值发现model.means_[K]和model.covars_[K](通过更新,我的意思是在训练阶段更新)。
然后为您提供了几种选择:您可以选择从高斯分布中随机抽取样本,也可以选择将新样本分配给高斯分布的均值。这取决于您的目标和您正在解决的问题。
| 归档时间: |
|
| 查看次数: |
3365 次 |
| 最近记录: |