| 1 |
import numpy as np |
| 2 |
import pytest |
| 3 |
from sklearn.utils import check_random_state |
| 4 |
|
| 5 |
from hmmlearn import hmm, vhmm |
| 6 |
from . import ( |
| 7 |
assert_log_likelihood_increasing, compare_variational_and_em_models, |
| 8 |
vi_uniform_startprob_and_transmat) |
| 9 |
|
| 10 |
|
| 11 |
class TestVariationalCategorical: |
| 12 |
|
| 13 |
@pytest.fixture(autouse=True) |
| 14 |
def setup(self): |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
self.n_components = 3 |
| 20 |
self.implementations = ["scaling", "log"] |
| 21 |
|
| 22 |
@staticmethod |
| 23 |
def get_beal_models(): |
| 24 |
m1 = hmm.CategoricalHMM(3, init_params="") |
| 25 |
m1.n_features = 3 |
| 26 |
m1.startprob_ = np.array([1/3., 1/3., 1/3.]) |
| 27 |
m1.transmat_ = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]) |
| 28 |
m1.emissionprob_ = np.array([[1, 0, 0], |
| 29 |
[0, 1, 0], |
| 30 |
[0, 0, 1]]) |
| 31 |
m2 = hmm.CategoricalHMM(3) |
| 32 |
m2.n_features = 3 |
| 33 |
m2.startprob_ = np.array([1/3., 1/3., 1/3.]) |
| 34 |
m2.transmat_ = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) |
| 35 |
m2.emissionprob_ = np.array([[1, 0, 0], |
| 36 |
[0, 1, 0], |
| 37 |
[0, 0, 1]]) |
| 38 |
m3 = hmm.CategoricalHMM(1) |
| 39 |
m3.n_features = 3 |
| 40 |
m3.startprob_ = np.array([1]) |
| 41 |
m3.transmat_ = np.array([[1]]) |
| 42 |
m3.emissionprob_ = np.array([[0.5, 0.5, 0]]) |
| 43 |
return m1, m2, m3 |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def get_from_one_beal(cls, N, length, rs=None): |
| 47 |
|
| 48 |
model = cls.get_beal_models()[0] |
| 49 |
sequences = [] |
| 50 |
lengths = [] |
| 51 |
for i in range(N): |
| 52 |
sequences.append( |
| 53 |
model.sample(length, random_state=check_random_state(rs))[0]) |
| 54 |
lengths.append(len(sequences[-1])) |
| 55 |
sequences = np.concatenate(sequences) |
| 56 |
return sequences, lengths |
| 57 |
|
| 58 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 59 |
def test_init_priors(self, implementation): |
| 60 |
sequences, lengths = self.get_from_one_beal(7, 100, None) |
| 61 |
model = vhmm.VariationalCategoricalHMM( |
| 62 |
4, n_iter=500, random_state=1984, init_params="", |
| 63 |
implementation=implementation) |
| 64 |
model.pi_prior_ = np.full((4,), .25) |
| 65 |
model.pi_posterior_ = np.full((4,), 7/4) |
| 66 |
model.transmat_prior_ = np.full((4, 4), .25) |
| 67 |
model.transmat_posterior_ = np.full((4, 4), 7/4) |
| 68 |
model.emissionprob_prior_ = np.full((4, 3), 1/3) |
| 69 |
model.emissionprob_posterior_ = np.asarray([[.3, .4, .3], |
| 70 |
[.8, .1, .1], |
| 71 |
[.2, .2, .6], |
| 72 |
[.2, .6, .2]]) |
| 73 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 74 |
|
| 75 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 76 |
def test_n_features(self, implementation): |
| 77 |
|
| 78 |
sequences, lengths = self.get_from_one_beal(7, 100, None) |
| 79 |
|
| 80 |
model = vhmm.VariationalCategoricalHMM( |
| 81 |
4, implementation=implementation) |
| 82 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 83 |
assert model.n_features == 3 |
| 84 |
|
| 85 |
model = vhmm.VariationalCategoricalHMM( |
| 86 |
4, implementation=implementation, n_features=5) |
| 87 |
|
| 88 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 89 |
assert model.n_features == 5 |
| 90 |
|
| 91 |
|
| 92 |
with pytest.raises(ValueError): |
| 93 |
model = vhmm.VariationalCategoricalHMM( |
| 94 |
4, n_iter=500, random_state=1984, |
| 95 |
implementation=implementation) |
| 96 |
model.n_features = 2 |
| 97 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 98 |
|
| 99 |
|
| 100 |
with pytest.raises(ValueError): |
| 101 |
model = vhmm.VariationalCategoricalHMM( |
| 102 |
4, n_iter=500, random_state=1984, |
| 103 |
implementation=implementation) |
| 104 |
sequences[0] = -1 |
| 105 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 106 |
|
| 107 |
|
| 108 |
with pytest.raises(ValueError): |
| 109 |
model = vhmm.VariationalCategoricalHMM( |
| 110 |
4, n_iter=500, random_state=1984, |
| 111 |
implementation=implementation) |
| 112 |
sequences = sequences.astype(float) |
| 113 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 114 |
|
| 115 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 116 |
def test_init_incorrect_priors(self, implementation): |
| 117 |
sequences, lengths = self.get_from_one_beal(7, 100, None) |
| 118 |
|
| 119 |
|
| 120 |
with pytest.raises(ValueError): |
| 121 |
model = vhmm.VariationalCategoricalHMM( |
| 122 |
4, n_iter=500, random_state=1984, init_params="te", |
| 123 |
implementation=implementation) |
| 124 |
model.startprob_prior_ = np.full((3,), .25) |
| 125 |
model.startprob_posterior_ = np.full((4,), 7/4) |
| 126 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 127 |
|
| 128 |
with pytest.raises(ValueError): |
| 129 |
model = vhmm.VariationalCategoricalHMM( |
| 130 |
4, n_iter=500, random_state=1984, init_params="te", |
| 131 |
implementation=implementation) |
| 132 |
model.startprob_prior_ = np.full((4,), .25) |
| 133 |
model.startprob_posterior_ = np.full((3,), 7/4) |
| 134 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 135 |
|
| 136 |
|
| 137 |
with pytest.raises(ValueError): |
| 138 |
model = vhmm.VariationalCategoricalHMM( |
| 139 |
4, n_iter=500, random_state=1984, init_params="se", |
| 140 |
implementation=implementation) |
| 141 |
model.transmat_prior_ = np.full((3, 3), .25) |
| 142 |
model.transmat_posterior_ = np.full((4, 4), .25) |
| 143 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 144 |
|
| 145 |
with pytest.raises(ValueError): |
| 146 |
model = vhmm.VariationalCategoricalHMM( |
| 147 |
4, n_iter=500, random_state=1984, init_params="se", |
| 148 |
implementation=implementation) |
| 149 |
model.transmat_prior_ = np.full((4, 4), .25) |
| 150 |
model.transmat_posterior_ = np.full((3, 3), 7/4) |
| 151 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 152 |
|
| 153 |
|
| 154 |
with pytest.raises(ValueError): |
| 155 |
model = vhmm.VariationalCategoricalHMM( |
| 156 |
4, n_iter=500, random_state=1984, init_params="st", |
| 157 |
implementation=implementation) |
| 158 |
model.emissionprob_prior_ = np.full((3, 3), 1/3) |
| 159 |
model.emissionprob_posterior_ = np.asarray([[.3, .4, .3], |
| 160 |
[.8, .1, .1], |
| 161 |
[.2, .2, .6], |
| 162 |
[.2, .6, .2]]) |
| 163 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 164 |
|
| 165 |
|
| 166 |
with pytest.raises(ValueError): |
| 167 |
model = vhmm.VariationalCategoricalHMM( |
| 168 |
4, n_iter=500, random_state=1984, init_params="se", |
| 169 |
implementation=implementation) |
| 170 |
model.emissionprob_prior_ = np.full((4, 4), 7/4) |
| 171 |
model.emissionprob_posterior_ = np.full((4, 4), .25) |
| 172 |
model.n_features_ = 10 |
| 173 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 174 |
|
| 175 |
|
| 176 |
with pytest.raises(ValueError): |
| 177 |
model = vhmm.VariationalCategoricalHMM( |
| 178 |
4, n_iter=500, random_state=1984, init_params="se", |
| 179 |
implementation=implementation) |
| 180 |
model.emissionprob_prior_ = np.full((4, 4), 7/4) |
| 181 |
model.emissionprob_posterior_ = np.full((4, 4), .25) |
| 182 |
|
| 183 |
model.n_features_ = 1 |
| 184 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 185 |
|
| 186 |
|
| 187 |
model = vhmm.VariationalCategoricalHMM( |
| 188 |
4, n_iter=500, random_state=1984, init_params="ste", |
| 189 |
implementation=implementation, |
| 190 |
startprob_prior=1, transmat_prior=2, emissionprob_prior=3) |
| 191 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |
| 192 |
assert np.all(model.startprob_prior_ == 1) |
| 193 |
assert np.all(model.transmat_prior_ == 2) |
| 194 |
assert np.all(model.emissionprob_prior_ == 3) |
| 195 |
|
| 196 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 197 |
def test_fit_beal(self, implementation): |
| 198 |
rs = check_random_state(1984) |
| 199 |
m1, m2, m3 = self.get_beal_models() |
| 200 |
sequences = [] |
| 201 |
lengths = [] |
| 202 |
for i in range(7): |
| 203 |
for m in [m1, m2, m3]: |
| 204 |
sequences.append(m.sample(39, random_state=rs)[0]) |
| 205 |
lengths.append(len(sequences[-1])) |
| 206 |
sequences = np.concatenate(sequences) |
| 207 |
model = vhmm.VariationalCategoricalHMM(12, n_iter=500, |
| 208 |
implementation=implementation, |
| 209 |
tol=1e-6, |
| 210 |
random_state=rs, |
| 211 |
verbose=False) |
| 212 |
|
| 213 |
assert_log_likelihood_increasing(model, sequences, lengths, 100) |
| 214 |
|
| 215 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 216 |
def test_fit_and_compare_with_em(self, implementation): |
| 217 |
|
| 218 |
|
| 219 |
sequences, lengths = self.get_from_one_beal(7, 100, 1984) |
| 220 |
model = vhmm.VariationalCategoricalHMM( |
| 221 |
4, n_iter=500, random_state=1984, |
| 222 |
init_params="e", |
| 223 |
implementation=implementation) |
| 224 |
vi_uniform_startprob_and_transmat(model, lengths) |
| 225 |
model.fit(sequences, lengths) |
| 226 |
|
| 227 |
|
| 228 |
assert (model.transmat_posterior_[1, :] |
| 229 |
== pytest.approx(.25, rel=1e-3)) |
| 230 |
assert (model.emissionprob_posterior_[1, :] |
| 231 |
== pytest.approx(.3333, rel=1e-3)) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
em_hmm = hmm.CategoricalHMM(n_components=4, init_params="") |
| 237 |
em_hmm.startprob_ = model.startprob_ |
| 238 |
em_hmm.transmat_ = model.transmat_ |
| 239 |
em_hmm.emissionprob_ = model.emissionprob_ |
| 240 |
|
| 241 |
compare_variational_and_em_models(model, em_hmm, sequences, lengths) |
| 242 |
|
| 243 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 244 |
def test_fit_length_1_sequences(self, implementation): |
| 245 |
sequences1, lengths1 = self.get_from_one_beal(7, 100, 1984) |
| 246 |
|
| 247 |
|
| 248 |
sequences2, lengths2 = self.get_from_one_beal(1, 1, 1984) |
| 249 |
sequences = np.concatenate([sequences1, sequences2]) |
| 250 |
lengths = np.concatenate([lengths1, lengths2]) |
| 251 |
|
| 252 |
model = vhmm.VariationalCategoricalHMM( |
| 253 |
4, n_iter=500, random_state=1984, |
| 254 |
implementation=implementation) |
| 255 |
assert_log_likelihood_increasing(model, sequences, lengths, 10) |