I am trying to predict stock market using a Gaussian HMM. I am not getting how the prediction step is done after the model has been trained. I did not understand how exactly predicting the most likely state sequence can help to predict future value.
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)