Prediction step for time series using continuous hidden Markov models
Asked Answered
R

1

11

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)
Recitation answered 25/12, 2018 at 13:10 Comment(1)
I was just wondering. Shouldn't this equal 1 -"model.startprob_ = np.array([0.3,0.4,0.6])"Dispenser
C
8

You are not so far from your goal!

I have also applied Viterbi algorithm over the sample to predict the possible hidden state sequence

With the Viterbi algorithm you actually predicted the most likely sequence of hidden states. The last state corresponds to the most probable state for the last sample of the time series you passed as an input.

In order to predict the next sample you need to estimate which state is the next emission most likely to come from.

To do this, you can use the state transition matrix that has been estimated during the training phase i.e., the updated value of model.transmat_.

Once you most likely state for the next sample is predicted, you can use the Gaussian distribution that is associated to that state. Let's say you predicted state K, then the parameters of the Gaussian distribution will be found in the updated values of model.means_[K] and model.covars_[K] (by updated, I mean updated during the training phase).

Then several choices are offered to you: you can either choose to draw a random sample from the Gaussian distribution or choose to assign the new sample to the value of the mean of the Gaussian. That depends on your goals and of the problem you are solving.

Close answered 29/12, 2018 at 15:40 Comment(4)
Thanks for the answer. So if I am predicting stock values and get that my next most likely state is state 2, do I directly take the mean corresponding to state 2 as the stock price? It can be any value among large pool of values for state 2. How will 'choosing' from such large pool give us accurate stock value? Wont it cause repetition of previous stock value as answer, if my mean is same? How can I include covariance also to predict the stock value?Recitation
If you draw a sample from the Gaussian distribution it takes into account the mean and variance. You can check numpy.random functions to draw a sample from a distributionClose
Thanks for the answer. That was really helpful!Recitation
@Recitation I see you are quite new here so let me introduce you to some good practices on Stack Overflow :) When an answer has been helpful to you, you should give it an upvote (check the upward arrow on the left side of the answer). This not only rewards the person who took the time to help you but also indicates to other people who would have the same issue as you that this answer is worth reading. Finally if this solved you issue, you can "Accept the answer" by clicking the check mark next to the answer. This means that the answer actually allowed you to solve your issue.Close

© 2022 - 2024 — McMap. All rights reserved.