I'm playing around with Hidden Markov Models for a stock market prediction problem. My data matrix contains various features for a particular security:
01-01-2001, .025, .012, .01
01-02-2001, -.005, -.023, .02
I fit a simple GaussianHMM:
from hmmlearn import GaussianHMM
mdl = GaussianHMM(n_components=3,covariance_type='diag',n_iter=1000)
mdl.fit(train[:,1:])
With the model (λ), I can decode an observation vector to find the most likely hidden state sequence corresponding to the observation vector:
print mdl.decode(test[0:5,1:])
(72.75, array([2, 1, 2, 0, 0]))
Above, I've decoded the hidden state sequence of an observation vector Ot = (O1, O2, ..., Od) which contains the first five instances in a test set. I'd like to estimate the hidden state of the sixth instance in the test set. The idea is to iterate over a discrete set of possible feature values for the sixth instance, and select the observation sequence Ot+1 with highest likelihood argmax = P(O1, O2, ..., Od+1 | λ ). Once we observe the true feature values of Od+1, we can shift the sequence (of length 5) by one and do it all over again:
l = 5
for i in xrange(len(test)-l):
values = []
for a in arange(-0.05,0.05,.01):
for b in arange(-0.05,0.05,.01):
for c in arange(-0.05,0.05,.01):
values.append(mdl.decode(vstack((test[i:i+l,1:],array([a,b,c])))))
print max(enumerate(values),key=lambda x: x[1])
The problem is that when I decode the observation vector Ot+1, the prediction with the highest likelihood is almost always the same (e.g. the estimate with highest likelihood always has feature values for Od+1 that equal [ 0.04 0.04 0.04] and is hidden state [0]):
(555, (74.71248518927949, array([2, 1, 2, 0, 0, 0]))) [ 0.04 0.04 0.04]
(555, (69.41963358191555, array([2, 2, 0, 0, 0, 0]))) [ 0.04 0.04 0.04]
(555, (77.11516871816922, array([2, 0, 0, 0, 0, 0]))) [ 0.04 0.04 0.04]
It's entirely possible that I'm misunderstanding the purpose of mdl.decode, and thus using it incorrectly. If that's the case, how best can I go about iterating over possible values of Od+1, and then maximizing P(O1, O2, ..., Od+1 | λ)?