Debian Codegraph @

src/hmmlearn/tests/test_poisson_hmm.py

raw symbols SCIP
1 import numpy as np
2 from numpy.testing import assert_allclose
3 import pytest
4 from sklearn.utils import check_random_state
5
6 from hmmlearn import hmm
7
8 from . import assert_log_likelihood_increasing, normalized
9
10
11 class TestPoissonHMM:
12 n_components = 2
13 n_features = 3
14
15 def new_hmm(self, impl):
16 h = hmm.PoissonHMM(self.n_components, implementation=impl,
17 random_state=0)
18 h.startprob_ = np.array([0.6, 0.4])
19 h.transmat_ = np.array([[0.7, 0.3], [0.4, 0.6]])
20 h.lambdas_ = np.array([[3.1, 1.4, 4.5], [1.6, 5.3, 0.1]])
21 return h
22
23 @pytest.mark.parametrize("implementation", ["scaling", "log"])
24 def test_attributes(self, implementation):
25 with pytest.raises(ValueError):
26 h = self.new_hmm(implementation)
27 h.lambdas_ = []
28 h._check()
29 with pytest.raises(ValueError):
30 h.lambdas_ = np.zeros((self.n_components - 2, self.n_features))
31 h._check()
32
33 @pytest.mark.parametrize("implementation", ["scaling", "log"])
34 def test_score_samples(self, implementation, n_samples=1000):
35 h = self.new_hmm(implementation)
36 X, state_sequence = h.sample(n_samples)
37 assert X.ndim == 2
38 assert len(X) == len(state_sequence) == n_samples
39
40 ll, posteriors = h.score_samples(X)
41 assert posteriors.shape == (n_samples, self.n_components)
42 assert_allclose(posteriors.sum(axis=1), np.ones(n_samples))
43
44 @pytest.mark.parametrize("implementation", ["scaling", "log"])
45 def test_fit(self, implementation, params='stl', n_iter=5):
46
47 h = self.new_hmm(implementation)
48 h.params = params
49
50 lengths = np.array([10] * 10)
51 X, _state_sequence = h.sample(lengths.sum())
52
53 # Mess up the parameters and see if we can re-learn them.
54 np.random.seed(0)
55 h.startprob_ = normalized(np.random.random(self.n_components))
56 h.transmat_ = normalized(
57 np.random.random((self.n_components, self.n_components)),
58 axis=1)
59 h.lambdas_ = np.random.gamma(
60 shape=2, size=(self.n_components, self.n_features))
61
62 assert_log_likelihood_increasing(h, X, lengths, n_iter)
63
64 @pytest.mark.parametrize("implementation", ["scaling", "log"])
65 def test_fit_lambdas(self, implementation):
66 self.test_fit(implementation, 'l')
67
68 @pytest.mark.parametrize("implementation", ["scaling", "log"])
69 def test_fit_with_init(self, implementation, params='stl', n_iter=5):
70 lengths = [10] * 10
71 h = self.new_hmm(implementation)
72 X, _state_sequence = h.sample(sum(lengths))
73
74 # use init_function to initialize paramerters
75 h = hmm.PoissonHMM(self.n_components, params=params,
76 init_params=params)
77 h._init(X, lengths)
78
79 assert_log_likelihood_increasing(h, X, lengths, n_iter)
80
81 @pytest.mark.parametrize("implementation", ["scaling", "log"])
82 def test_criterion(self, implementation):
83 random_state = check_random_state(412)
84 m1 = self.new_hmm(implementation)
85 X, _ = m1.sample(2000, random_state=random_state)
86
87 aic = []
88 bic = []
89 ns = [2, 3, 4]
90 for n in ns:
91 h = hmm.PoissonHMM(n, n_iter=500,
92 random_state=random_state, implementation=implementation)
93 h.fit(X)
94 aic.append(h.aic(X))
95 bic.append(h.bic(X))
96
97 assert np.all(aic) > 0
98 assert np.all(bic) > 0
99 # AIC / BIC pick the right model occasionally
100 # assert ns[np.argmin(aic)] == 2
101 # assert ns[np.argmin(bic)] == 2