Debian Codegraph @

src/hmmlearn/tests/test_variational_gaussian.py

raw symbols SCIP
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 make_covar_matrix, normalized, vi_uniform_startprob_and_transmat)
9
10
11 def get_mcgrory_titterington():
12 m1 = hmm.GaussianHMM(4, init_params="")
13 m1.n_features = 4
14 m1.startprob_ = np.array([1/4., 1/4., 1/4., 1/4.])
15 m1.transmat_ = np.array([[0.2, 0.2, 0.3, 0.3],
16 [0.3, 0.2, 0.2, 0.3],
17 [0.2, 0.3, 0.3, 0.2],
18 [0.3, 0.3, 0.2, 0.2]])
19 m1.means_ = np.array([[-1.5], [0], [1.5], [3.]])
20 m1.covars_ = np.sqrt([[0.25], [0.25], [0.25], [0.25]])
21 return m1
22
23
24 def get_sequences(length, N, model, rs=None):
25 sequences = []
26 lengths = []
27 rs = check_random_state(rs)
28 for i in range(N):
29 sequences.append(
30 model.sample(length, random_state=rs)[0])
31 lengths.append(len(sequences[-1]))
32
33 sequences = np.concatenate(sequences)
34 return sequences, lengths
35
36
37 class _TestGaussian:
38
39 @pytest.mark.parametrize("implementation", ["scaling", "log"])
40 def test_random_fit(self, implementation, params='stmc', n_features=3,
41 n_components=3, **kwargs):
42 h = hmm.GaussianHMM(n_components, self.covariance_type,
43 implementation=implementation, init_params="")
44 rs = check_random_state(1)
45 h.startprob_ = normalized(rs.rand(n_components))
46 h.transmat_ = normalized(
47 rs.rand(n_components, n_components), axis=1)
48 h.means_ = rs.randint(-20, 20, (n_components, n_features))
49 h.covars_ = make_covar_matrix(
50 self.covariance_type, n_components, n_features, random_state=rs)
51 lengths = [200] * 5
52 X, _state_sequence = h.sample(sum(lengths), random_state=rs)
53 # Now learn a model
54 model = vhmm.VariationalGaussianHMM(
55 n_components, n_iter=50, tol=1e-9, random_state=rs,
56 covariance_type=self.covariance_type,
57 implementation=implementation)
58
59 assert_log_likelihood_increasing(model, X, lengths, n_iter=10)
60
61 @pytest.mark.parametrize("implementation", ["scaling", "log"])
62 def test_fit_mcgrory_titterington1d(self, implementation):
63 random_state = check_random_state(234234)
64 # Setup to assure convergence
65
66 sequences, lengths = get_sequences(500, 1,
67 model=get_mcgrory_titterington(),
68 rs=random_state)
69 model = vhmm.VariationalGaussianHMM(
70 5, n_iter=1000, tol=1e-9, random_state=random_state,
71 init_params="mc",
72 covariance_type=self.covariance_type,
73 implementation=implementation)
74 vi_uniform_startprob_and_transmat(model, lengths)
75 model.fit(sequences, lengths)
76 # Perform one check that we are converging to the right answer
77 assert (list(sorted(model.means_posterior_.ravel()))[3]
78 == pytest.approx(self.test_fit_mcgrory_titterington1d_mean)), \
79 model.means_posterior_
80
81 em_hmm = hmm.GaussianHMM(
82 n_components=model.n_components,
83 implementation=implementation,
84 covariance_type=self.covariance_type,
85 )
86 em_hmm.startprob_ = model.startprob_
87 em_hmm.transmat_ = model.transmat_
88 em_hmm.means_ = model.means_posterior_
89 em_hmm.covars_ = model._covars_
90
91 compare_variational_and_em_models(model, em_hmm, sequences, lengths)
92
93 @pytest.mark.parametrize("implementation", ["scaling", "log"])
94 def test_common_initialization(self, implementation):
95 sequences, lengths = get_sequences(50, 10,
96 model=get_mcgrory_titterington())
97
98 with pytest.raises(ValueError):
99 model = vhmm.VariationalGaussianHMM(
100 4, n_iter=500, tol=1e-9,
101 covariance_type="incorrect",
102 implementation=implementation)
103 assert_log_likelihood_increasing(model, sequences, lengths, 10)
104
105 with pytest.raises(ValueError):
106 model = vhmm.VariationalGaussianHMM(
107 4, n_iter=500, tol=1e-9,
108 covariance_type="incorrect",
109 init_params="",
110 implementation=implementation)
111 model.startprob_= np.asarray([.25, .25, .25, .25])
112 model.score(sequences, lengths)
113
114 # Manually setup means - should converge
115 model = vhmm.VariationalGaussianHMM(
116 4, n_iter=500, tol=1e-9, init_params="stc",
117 covariance_type=self.covariance_type,
118 implementation=implementation)
119 model.means_prior_ = [[1], [1], [1], [1]]
120 model.means_posterior_ = [[2], [1], [3], [4]]
121 model.beta_prior_ = [1, 1, 1, 1]
122 model.beta_posterior_ = [1, 1, 1, 1]
123 assert_log_likelihood_increasing(model, sequences, lengths, 10)
124
125 # Means have wrong shape
126 with pytest.raises(ValueError):
127 model = vhmm.VariationalGaussianHMM(
128 4, n_iter=500, tol=1e-9, init_params="stc",
129 covariance_type=self.covariance_type,
130 implementation=implementation)
131 model.means_prior_ = [[1], [1], [1]]
132 model.means_posterior_ = [[1], [1], [1], [1]]
133 model.beta_prior_ = [1, 1, 1, 1]
134 model.beta_posterior_ = [1, 1, 1, 1]
135 assert_log_likelihood_increasing(model, sequences, lengths, 10)
136
137 with pytest.raises(ValueError):
138 model = vhmm.VariationalGaussianHMM(
139 4, n_iter=500, tol=1e-9, init_params="stc",
140 covariance_type=self.covariance_type,
141 implementation=implementation)
142 model.means_prior_ = [[1], [1], [1], [1]]
143 model.means_posterior_ = [[1], [1], [1]]
144 model.beta_prior_ = [1, 1, 1, 1]
145 model.beta_posterior_ = [1, 1, 1, 1]
146 assert_log_likelihood_increasing(model, sequences, lengths, 10)
147
148 # beta's have wrong shape
149 with pytest.raises(ValueError):
150 model = vhmm.VariationalGaussianHMM(
151 4, n_iter=500, tol=1e-9, init_params="stc",
152 covariance_type=self.covariance_type,
153 implementation=implementation)
154 model.means_prior_ = [[1], [1], [1], [1]]
155 model.means_posterior_ = [[2], [1], [3], [4]]
156 model.beta_prior_ = [1, 1, 1]
157 model.beta_posterior_ = [1, 1, 1, 1]
158 assert_log_likelihood_increasing(model, sequences, lengths, 10)
159
160 with pytest.raises(ValueError):
161 model = vhmm.VariationalGaussianHMM(
162 4, n_iter=500, tol=1e-9, init_params="stc",
163 covariance_type=self.covariance_type,
164 implementation=implementation)
165 model.means_prior_ = [[1], [1], [1], [1]]
166 model.means_posterior_ = [[2], [1], [3], [4]]
167 model.beta_prior_ = [1, 1, 1, 1]
168 model.beta_posterior_ = [1, 1, 1]
169 assert_log_likelihood_increasing(model, sequences, lengths, 10)
170
171
172 class TestFull(_TestGaussian):
173
174 covariance_type = "full"
175 test_fit_mcgrory_titterington1d_mean = 1.41058519
176
177 def new_for_init(self, implementation):
178 model = vhmm.VariationalGaussianHMM(
179 4, n_iter=500, tol=1e-9, init_params="stm",
180 covariance_type=self.covariance_type,
181 implementation=implementation)
182 model.dof_prior_ = [1, 1, 1, 1]
183 model.dof_posterior_ = [1, 1, 1, 1]
184 model.scale_prior_ = [[[2.]], [[2.]], [[2.]], [[2]]]
185 model.scale_posterior_ = [[[2.]], [[2.]], [[2.]], [[2.]]]
186 return model
187
188 @pytest.mark.parametrize("implementation", ["scaling", "log"])
189 def test_initialization(self, implementation):
190 random_state = check_random_state(234234)
191 sequences, lengths = get_sequences(
192 50, 10, model=get_mcgrory_titterington())
193
194 # dof's have wrong shape
195 with pytest.raises(ValueError):
196 model = self.new_for_init(implementation)
197 model.dof_prior_ = [1, 1, 1]
198 assert_log_likelihood_increasing(model, sequences, lengths, 10)
199
200 with pytest.raises(ValueError):
201 model = self.new_for_init(implementation)
202 model.dof_posterior_ = [1, 1, 1]
203 assert_log_likelihood_increasing(model, sequences, lengths, 10)
204
205 # scales's have wrong shape
206 with pytest.raises(ValueError):
207 model = self.new_for_init(implementation)
208 model.scale_prior_ = [[[2.]], [[2.]], [[2.]]]
209 assert_log_likelihood_increasing(model, sequences, lengths, 10)
210
211 with pytest.raises(ValueError):
212 model = self.new_for_init(implementation)
213 model.scale_posterior_ = [[2.]], [[2.]], [[2.]] # this is wrong
214 assert_log_likelihood_increasing(model, sequences, lengths, 10)
215
216 # Manually setup covariance
217 with pytest.raises(ValueError):
218 model = vhmm.VariationalGaussianHMM(
219 4, n_iter=500, tol=1e-9, init_params="stm",
220 covariance_type="incorrect",
221 implementation=implementation)
222 assert_log_likelihood_increasing(model, sequences, lengths, 10)
223
224 # Set priors correctly via params
225 model = vhmm.VariationalGaussianHMM(
226 4, n_iter=500, tol=1e-9, random_state=random_state,
227 covariance_type=self.covariance_type,
228 implementation=implementation,
229 means_prior=[[0.], [0.], [0.], [0.]],
230 beta_prior=[2., 2., 2., 2.],
231 dof_prior=[2., 2., 2., 2.],
232 scale_prior=[[[2.]], [[2.]], [[2.]], [[2.]]])
233 assert_log_likelihood_increasing(model, sequences, lengths, 10)
234 assert np.all(model.means_prior_ == 0)
235 assert np.all(model.beta_prior_ == 2.)
236 assert np.all(model.dof_prior_ == 2.)
237 assert np.all(model.scale_prior_ == 2.)
238
239 # Manually set everything
240 model = vhmm.VariationalGaussianHMM(
241 4, n_iter=500, tol=1e-9, random_state=random_state,
242 covariance_type=self.covariance_type,
243 implementation=implementation,
244 init_params="",
245 )
246 model.means_prior_ = [[0.], [0.], [0.], [0.]]
247 model.means_posterior_ = [[2], [1], [3], [4]]
248 model.beta_prior_ = [2., 2., 2., 2.]
249 model.beta_posterior_ = [1, 1, 1, 1]
250 model.dof_prior_ = [2., 2., 2., 2.]
251 model.dof_posterior_ = [1, 1, 1, 1]
252 modelscale_prior_ = [[[2.]], [[2.]], [[2.]], [[2.]]]
253 model.scale_posterior_ = [[[2.]], [[2.]], [[2.]], [[2.]]]
254 assert_log_likelihood_increasing(model, sequences, lengths, 10)
255
256
257 class TestTied(_TestGaussian):
258 test_fit_mcgrory_titterington1d_mean = 1.4774254
259 covariance_type = "tied"
260
261 def new_for_init(self, implementation):
262 model = vhmm.VariationalGaussianHMM(
263 4, n_iter=500, tol=1e-9, init_params="stm",
264 covariance_type=self.covariance_type,
265 implementation=implementation)
266 model.dof_prior_ = 1
267 model.dof_posterior_ = 1
268 model.scale_prior_ = [[2]]
269 model.scale_posterior_ = [[2]]
270 return model
271
272 @pytest.mark.parametrize("implementation", ["scaling", "log"])
273 def test_initialization(self, implementation):
274 random_state = check_random_state(234234)
275 sequences, lengths = get_sequences(
276 50, 10, model=get_mcgrory_titterington())
277
278 # dof's have wrong shape
279 with pytest.raises(ValueError):
280 model = self.new_for_init(implementation)
281 model.dof_prior_ = [1]
282 assert_log_likelihood_increasing(model, sequences, lengths, 10)
283
284 with pytest.raises(ValueError):
285 model = self.new_for_init(implementation)
286 model.dof_posterior_ = [1]
287 assert_log_likelihood_increasing(model, sequences, lengths, 10)
288
289 # scales's have wrong shape
290 with pytest.raises(ValueError):
291 model = self.new_for_init(implementation)
292 model.scale_prior_ = [[[2]]]
293 assert_log_likelihood_increasing(model, sequences, lengths, 10)
294
295 with pytest.raises(ValueError):
296 model = self.new_for_init(implementation)
297 model.scale_posterior_ = [[[2.]], [[2.]], [[2.]]] # this is wrong
298 assert_log_likelihood_increasing(model, sequences, lengths, 10)
299
300 # Manually setup covariance
301 with pytest.raises(ValueError):
302 model = vhmm.VariationalGaussianHMM(
303 4, n_iter=500, tol=1e-9, init_params="stm",
304 covariance_type="incorrect",
305 implementation=implementation)
306 assert_log_likelihood_increasing(model, sequences, lengths, 10)
307
308 # Set priors correctly via params
309 model = vhmm.VariationalGaussianHMM(
310 4, n_iter=500, tol=1e-9, random_state=random_state,
311 covariance_type=self.covariance_type,
312 implementation=implementation,
313 means_prior=[[0.], [0.], [0.], [0.]],
314 beta_prior=[2., 2., 2., 2.],
315 dof_prior=2,
316 scale_prior=[[2]],
317 )
318 assert_log_likelihood_increasing(model, sequences, lengths, 10)
319 assert np.all(model.means_prior_ == 0)
320 assert np.all(model.beta_prior_ == 2.)
321 assert np.all(model.dof_prior_ == 2.)
322 assert np.all(model.scale_prior_ == 2.)
323
324 # Manually set everything
325 model = vhmm.VariationalGaussianHMM(
326 4, n_iter=500, tol=1e-9, random_state=random_state,
327 covariance_type=self.covariance_type,
328 implementation=implementation,
329 init_params="",
330 )
331 model.means_prior_ = [[0.], [0.], [0.], [0.]]
332 model.means_posterior_ = [[2], [1], [3], [4]]
333 model.beta_prior_ = [2., 2., 2., 2.]
334 model.beta_posterior_ = [1, 1, 1, 1]
335 model.dof_prior_ = 2
336 model.dof_posterior_ = 1
337 model.scale_prior_ = [[2]]
338 model.scale_posterior_ = [[2]]
339 assert_log_likelihood_increasing(model, sequences, lengths, 10)
340
341
342 class TestSpherical(_TestGaussian):
343 test_fit_mcgrory_titterington1d_mean = 1.4105851867634462
344 covariance_type = "spherical"
345
346 def new_for_init(self, implementation):
347 model = vhmm.VariationalGaussianHMM(
348 4, n_iter=500, tol=1e-9, init_params="stm",
349 covariance_type=self.covariance_type,
350 implementation=implementation)
351 model.dof_prior_ = [1, 1, 1, 1]
352 model.dof_posterior_ = [1, 1, 1, 1]
353 model.scale_prior_ = [2, 2, 2, 2]
354 model.scale_posterior_ = [2, 2, 2, 2]
355 return model
356
357 @pytest.mark.parametrize("implementation", ["scaling", "log"])
358 def test_initialization(self, implementation):
359 random_state = check_random_state(234234)
360 sequences, lengths = get_sequences(
361 50, 10, model=get_mcgrory_titterington())
362
363 # dof's have wrong shape
364 with pytest.raises(ValueError):
365 model = self.new_for_init(implementation)
366 model.dof_prior_ = [1, 1, 1]
367 assert_log_likelihood_increasing(model, sequences, lengths, 10)
368
369 with pytest.raises(ValueError):
370 model = self.new_for_init(implementation)
371 model.dof_posterior_ = [1, 1, 1]
372 assert_log_likelihood_increasing(model, sequences, lengths, 10)
373
374 # scales's have wrong shape
375 with pytest.raises(ValueError):
376 model = self.new_for_init(implementation)
377 model.scale_prior_ = [2, 2, 2]
378 assert_log_likelihood_increasing(model, sequences, lengths, 10)
379
380 with pytest.raises(ValueError):
381 model = self.new_for_init(implementation)
382 model.scale_posterior_ = [2, 2, 2] # this is wrong
383 assert_log_likelihood_increasing(model, sequences, lengths, 10)
384
385 # Manually setup covariance
386 with pytest.raises(ValueError):
387 model = vhmm.VariationalGaussianHMM(
388 4, n_iter=500, tol=1e-9, init_params="stm",
389 covariance_type="incorrect",
390 implementation=implementation)
391 assert_log_likelihood_increasing(model, sequences, lengths, 10)
392
393 # Set priors correctly via params
394 model = vhmm.VariationalGaussianHMM(
395 4, n_iter=500, tol=1e-9, random_state=random_state,
396 covariance_type=self.covariance_type,
397 implementation=implementation,
398 means_prior=[[0.], [0.], [0.], [0.]],
399 beta_prior=[2., 2., 2., 2.],
400 dof_prior=[2., 2., 2., 2.],
401 scale_prior=[2, 2, 2, 2],
402 )
403 assert_log_likelihood_increasing(model, sequences, lengths, 10)
404 assert np.all(model.means_prior_ == 0)
405 assert np.all(model.beta_prior_ == 2.)
406 assert np.all(model.dof_prior_ == 2.)
407 assert np.all(model.scale_prior_ == 2.)
408
409 # Manually set everything
410 model = vhmm.VariationalGaussianHMM(
411 4, n_iter=500, tol=1e-9, random_state=random_state,
412 covariance_type=self.covariance_type,
413 implementation=implementation,
414 init_params="",
415 )
416 model.means_prior_ = [[0.], [0.], [0.], [0.]]
417 model.means_posterior_ = [[2], [1], [3], [4]]
418 model.beta_prior_ = [2., 2., 2., 2.]
419 model.beta_posterior_ = [1, 1, 1, 1]
420 model.dof_prior_ = [2., 2., 2., 2.]
421 model.dof_posterior_ = [1, 1, 1, 1]
422 model.scale_prior_ = [2, 2, 2, 2]
423 model.scale_posterior_ = [2, 2, 2, 2]
424 assert_log_likelihood_increasing(model, sequences, lengths, 10)
425
426
427 class TestDiagonal(_TestGaussian):
428 test_fit_mcgrory_titterington1d_mean = 1.410585186763446
429 covariance_type = "diag"
430
431 def new_for_init(self, implementation):
432 model = vhmm.VariationalGaussianHMM(
433 4, n_iter=500, tol=1e-9, init_params="stm",
434 covariance_type=self.covariance_type,
435 implementation=implementation)
436 model.dof_prior_ = [1, 1, 1, 1]
437 model.dof_posterior_ = [1, 1, 1, 1]
438 model.scale_prior_ = [[2], [2], [2], [2]]
439 model.scale_posterior_ = [[2], [2], [2], [2]]
440 return model
441
442 @pytest.mark.parametrize("implementation", ["scaling", "log"])
443 def test_initialization(self, implementation):
444 random_state = check_random_state(234234)
445 sequences, lengths = get_sequences(
446 50, 10, model=get_mcgrory_titterington())
447
448 # dof's have wrong shape
449 with pytest.raises(ValueError):
450 model = self.new_for_init(implementation)
451 model.dof_prior_ = [1, 1, 1]
452 assert_log_likelihood_increasing(model, sequences, lengths, 10)
453
454 with pytest.raises(ValueError):
455 model = self.new_for_init(implementation)
456 model.dof_posterior_ = [1, 1, 1]
457 assert_log_likelihood_increasing(model, sequences, lengths, 10)
458
459 # scales's have wrong shape
460 with pytest.raises(ValueError):
461 model = self.new_for_init(implementation)
462 model.scale_prior_ = [[2], [2], [2]]
463 assert_log_likelihood_increasing(model, sequences, lengths, 10)
464
465 with pytest.raises(ValueError):
466 model = vhmm.VariationalGaussianHMM(
467 4, n_iter=500, tol=1e-9, init_params="stm",
468 covariance_type=self.covariance_type,
469 implementation=implementation)
470 model.dof_prior_ = [1, 1, 1, 1]
471 model.dof_posterior_ = [1, 1, 1, 1]
472 model.scale_prior_ = [[2], [2], [2], [2]]
473 model.scale_posterior_ = [[2, 2, 2]] # this is wrong
474 assert_log_likelihood_increasing(model, sequences, lengths, 10)
475
476 # Set priors correctly via params
477 model = vhmm.VariationalGaussianHMM(
478 4, n_iter=500, tol=1e-9, random_state=random_state,
479 covariance_type=self.covariance_type,
480 implementation=implementation,
481 means_prior=[[0.], [0.], [0.], [0.]],
482 beta_prior=[2., 2., 2., 2.],
483 dof_prior=[2., 2., 2., 2.],
484 scale_prior=[[2], [2], [2], [2]]
485 )
486 assert_log_likelihood_increasing(model, sequences, lengths, 10)
487 assert np.all(model.means_prior_ == 0)
488 assert np.all(model.beta_prior_ == 2.)
489 assert np.all(model.dof_prior_ == 2.)
490 assert np.all(model.scale_prior_ == 2.)
491
492 # Manually set everything
493 model = vhmm.VariationalGaussianHMM(
494 4, n_iter=500, tol=1e-9, random_state=random_state,
495 covariance_type=self.covariance_type,
496 implementation=implementation,
497 init_params="",
498 )
499 model.means_prior_ = [[0.], [0.], [0.], [0.]]
500 model.means_posterior_ = [[2], [1], [3], [4]]
501 model.beta_prior_ = [2., 2., 2., 2.]
502 model.beta_posterior_ = [1, 1, 1, 1]
503 model.dof_prior_ = [2., 2., 2., 2.]
504 model.dof_posterior_ = [1, 1, 1, 1]
505 model.scale_prior_ = [[2], [2], [2], [2]]
506 model.scale_posterior_ =[[2], [2], [2], [2]]
507 assert_log_likelihood_increasing(model, sequences, lengths, 10)