In [ ]:
% matplotlib inline
import numpy as np
from hmmlearn.hmm import GaussianHMM
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
In [ ]:
toSick = 0.001
toWell = 0.02
model = GaussianHMM(n_components=2, covariance_type='tied')
model.startprob_ = np.array([1, 0]) # Healthy is 0, sick is 1
model.transmat_ = np.array([[1 - toSick, toSick], [toWell, 1 - toWell]])
print(model.transmat_)
model.means_ = np.reshape(np.array([98.6, 100.2]), (2, 1))
model._covars_ = np.array([[3]])
model._check()
In [ ]:
data, samm = model.sample(n_samples = 1000, random_state=4)
In [ ]:
plt.plot(data);
In [ ]:
def movingaverage (values, window):
weights = np.repeat(1.0, window)/window
return np.convolve(np.squeeze(values), weights, 'valid')
plt.plot(movingaverage(data, 20));
In [ ]:
# Make an HMM instance and execute fit
modelPred = GaussianHMM(n_components=2, n_iter=10000, tol=0.00001)
# Use EM to fit the parameters of a model
modelPred.fit(data);
# startprob_, transmat_, means_, _covars_
print(modelPred.means_)
In [ ]:
# Predict the optimal sequence of internal hidden state
hidden_states = modelPred.predict(data)
plt.plot(hidden_states);
In [ ]:
stateP = modelPred.predict_proba(data)
plt.plot(np.log10(stateP));
plt.ylim(-5, 0.1);
In [ ]:
# How sure can we be a sickness event occured?
maxL = modelPred.score(data)
modelPredOne = GaussianHMM(n_components=1, n_iter=10000, tol=0.00001)
modelPredOne.fit(data)
wellL = modelPredOne.score(data)
print(maxL)
print(wellL)
In [ ]:
plt.plot(samm);
In [ ]: