| 1 |
import numpy as np |
| 2 |
from numpy.testing import assert_allclose |
| 3 |
import pytest |
| 4 |
|
| 5 |
from hmmlearn import hmm |
| 6 |
|
| 7 |
from . import assert_log_likelihood_increasing, normalized |
| 8 |
|
| 9 |
|
| 10 |
class TestMultinomialHMM: |
| 11 |
n_components = 2 |
| 12 |
n_features = 4 |
| 13 |
n_trials = 5 |
| 14 |
|
| 15 |
def new_hmm(self, impl): |
| 16 |
h = hmm.MultinomialHMM( |
| 17 |
n_components=self.n_components, |
| 18 |
n_trials=self.n_trials, |
| 19 |
implementation=impl) |
| 20 |
h.startprob_ = np.array([.6, .4]) |
| 21 |
h.transmat_ = np.array([[.8, .2], [.2, .8]]) |
| 22 |
h.emissionprob_ = np.array([[.5, .3, .1, .1], [.1, .1, .4, .4]]) |
| 23 |
return h |
| 24 |
|
| 25 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 26 |
def test_attributes(self, implementation): |
| 27 |
with pytest.raises(ValueError): |
| 28 |
h = self.new_hmm(implementation) |
| 29 |
h.emissionprob_ = [] |
| 30 |
h._check() |
| 31 |
with pytest.raises(ValueError): |
| 32 |
h.emissionprob_ = np.zeros((self.n_components - 2, |
| 33 |
self.n_features)) |
| 34 |
h._check() |
| 35 |
|
| 36 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 37 |
def test_score_samples(self, implementation): |
| 38 |
X = np.array([ |
| 39 |
[1, 1, 3, 0], |
| 40 |
[3, 1, 1, 0], |
| 41 |
[3, 0, 2, 0], |
| 42 |
[2, 2, 0, 1], |
| 43 |
[2, 2, 0, 1], |
| 44 |
[0, 1, 1, 3], |
| 45 |
[1, 0, 3, 1], |
| 46 |
[2, 0, 1, 2], |
| 47 |
[0, 2, 1, 2], |
| 48 |
[1, 0, 1, 3], |
| 49 |
]) |
| 50 |
n_samples = X.shape[0] |
| 51 |
h = self.new_hmm(implementation) |
| 52 |
|
| 53 |
ll, posteriors = h.score_samples(X) |
| 54 |
assert posteriors.shape == (n_samples, self.n_components) |
| 55 |
assert_allclose(posteriors.sum(axis=1), np.ones(n_samples)) |
| 56 |
|
| 57 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 58 |
def test_sample(self, implementation, n_samples=1000): |
| 59 |
h = self.new_hmm(implementation) |
| 60 |
X, state_sequence = h.sample(n_samples) |
| 61 |
assert X.ndim == 2 |
| 62 |
assert len(X) == len(state_sequence) == n_samples |
| 63 |
assert len(np.unique(X)) == self.n_trials + 1 |
| 64 |
assert (X.sum(axis=1) == self.n_trials).all() |
| 65 |
h.n_trials = None |
| 66 |
with pytest.raises(ValueError): |
| 67 |
h.sample(n_samples) |
| 68 |
h.n_trials = [1, 2, 3] |
| 69 |
with pytest.raises(ValueError): |
| 70 |
h.sample(n_samples) |
| 71 |
|
| 72 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 73 |
def test_fit(self, implementation, params='ste', n_iter=5): |
| 74 |
h = self.new_hmm(implementation) |
| 75 |
h.params = params |
| 76 |
|
| 77 |
lengths = np.array([10] * 10) |
| 78 |
X, _state_sequence = h.sample(lengths.sum()) |
| 79 |
|
| 80 |
|
| 81 |
h.startprob_ = normalized(np.random.random(self.n_components)) |
| 82 |
h.transmat_ = normalized( |
| 83 |
np.random.random((self.n_components, self.n_components)), |
| 84 |
axis=1) |
| 85 |
h.emissionprob_ = normalized( |
| 86 |
np.random.random((self.n_components, self.n_features)), |
| 87 |
axis=1) |
| 88 |
|
| 89 |
h.n_trials = None |
| 90 |
X[::2] *= 2 |
| 91 |
|
| 92 |
assert_log_likelihood_increasing(h, X, lengths, n_iter) |
| 93 |
|
| 94 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 95 |
def test_fit_emissionprob(self, implementation): |
| 96 |
self.test_fit(implementation, 'e') |
| 97 |
|
| 98 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 99 |
def test_fit_with_init(self, implementation, params='ste', n_iter=5): |
| 100 |
lengths = [10] * 10 |
| 101 |
h = self.new_hmm(implementation) |
| 102 |
X, _state_sequence = h.sample(sum(lengths)) |
| 103 |
|
| 104 |
|
| 105 |
h = hmm.MultinomialHMM( |
| 106 |
n_components=self.n_components, n_trials=self.n_trials, |
| 107 |
params=params, init_params=params) |
| 108 |
h._init(X, lengths) |
| 109 |
|
| 110 |
assert_log_likelihood_increasing(h, X, lengths, n_iter) |
| 111 |
|
| 112 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 113 |
def test__check_and_set_multinomial_n_features_n_trials( |
| 114 |
self, implementation): |
| 115 |
h = hmm.MultinomialHMM( |
| 116 |
n_components=2, n_trials=None, implementation=implementation) |
| 117 |
h._check_and_set_n_features( |
| 118 |
np.array([[0, 2, 3, 0], [1, 0, 2, 2]])) |
| 119 |
assert (h.n_trials == 5).all() |
| 120 |
with pytest.raises(ValueError): |
| 121 |
h._check_and_set_n_features( |
| 122 |
np.array([[0, 0, 2, 1, 3, 1, 1]])) |
| 123 |
with pytest.raises(ValueError): |
| 124 |
h._check_and_set_n_features( |
| 125 |
np.array([[0, 0, 1, 1], [3, 1, 1, 0]])) |
| 126 |
with pytest.raises(ValueError): |
| 127 |
h._check_and_set_n_features( |
| 128 |
np.array([[0., 2., 0., 3.], [0.0, 2.5, 2.5, 0.0]])) |
| 129 |
with pytest.raises(ValueError): |
| 130 |
h._check_and_set_n_features( |
| 131 |
np.array([[0, -2, 1, 6], [5, 6, -6, 0]])) |
| 132 |
|
| 133 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 134 |
def test_compare_with_categorical_hmm(self, implementation): |
| 135 |
n_components = 2 |
| 136 |
n_features = 3 |
| 137 |
n_trials = 1 |
| 138 |
startprob = np.array([0.6, 0.4]) |
| 139 |
transmat = np.array([[0.7, 0.3], [0.4, 0.6]]) |
| 140 |
emissionprob = np.array([[0.1, 0.4, 0.5], |
| 141 |
[0.6, 0.3, 0.1]]) |
| 142 |
h1 = hmm.MultinomialHMM( |
| 143 |
n_components=n_components, n_trials=n_trials, |
| 144 |
implementation=implementation) |
| 145 |
h2 = hmm.CategoricalHMM( |
| 146 |
n_components=n_components, implementation=implementation) |
| 147 |
|
| 148 |
h1.startprob_ = startprob |
| 149 |
h2.startprob_ = startprob |
| 150 |
|
| 151 |
h1.transmat_ = transmat |
| 152 |
h2.transmat_ = transmat |
| 153 |
|
| 154 |
h1.emissionprob_ = emissionprob |
| 155 |
h2.emissionprob_ = emissionprob |
| 156 |
|
| 157 |
X1 = np.array([[1, 0, 0], |
| 158 |
[0, 1, 0], |
| 159 |
[0, 0, 1]]) |
| 160 |
X2 = [[0], [1], [2]] |
| 161 |
log_prob1, state_sequence1 = h1.decode(X1, algorithm="viterbi") |
| 162 |
log_prob2, state_sequence2 = h2.decode(X2, algorithm="viterbi") |
| 163 |
assert round(np.exp(log_prob1), 5) == 0.01344 |
| 164 |
assert round(np.exp(log_prob2), 5) == 0.01344 |
| 165 |
|
| 166 |
assert_allclose(state_sequence1, [1, 0, 0]) |
| 167 |
assert_allclose(state_sequence2, [1, 0, 0]) |
| 168 |
|
| 169 |
posteriors1 = h1.predict_proba(X1) |
| 170 |
assert_allclose(posteriors1, [ |
| 171 |
[0.23170303, 0.76829697], |
| 172 |
[0.62406281, 0.37593719], |
| 173 |
[0.86397706, 0.13602294], |
| 174 |
], rtol=0, atol=1e-6) |
| 175 |
|
| 176 |
posteriors2 = h2.predict_proba(X2) |
| 177 |
assert_allclose(posteriors2, [ |
| 178 |
[0.23170303, 0.76829697], |
| 179 |
[0.62406281, 0.37593719], |
| 180 |
[0.86397706, 0.13602294], |
| 181 |
], rtol=0, atol=1e-6) |