| 1 |
import numpy as np |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from sklearn.datasets import make_spd_matrix |
| 6 |
from sklearn.utils import check_random_state |
| 7 |
|
| 8 |
from hmmlearn.utils import normalize |
| 9 |
from hmmlearn.base import DECODER_ALGORITHMS |
| 10 |
|
| 11 |
|
| 12 |
np.seterr(all="warn") |
| 13 |
|
| 14 |
|
| 15 |
def make_covar_matrix(covariance_type, n_components, n_features, |
| 16 |
random_state=None): |
| 17 |
mincv = 0.1 |
| 18 |
prng = check_random_state(random_state) |
| 19 |
if covariance_type == 'spherical': |
| 20 |
return (mincv + mincv * prng.random_sample((n_components,))) ** 2 |
| 21 |
elif covariance_type == 'tied': |
| 22 |
return (make_spd_matrix(n_features) |
| 23 |
+ mincv * np.eye(n_features)) |
| 24 |
elif covariance_type == 'diag': |
| 25 |
return (mincv + mincv * |
| 26 |
prng.random_sample((n_components, n_features))) ** 2 |
| 27 |
elif covariance_type == 'full': |
| 28 |
return np.array([ |
| 29 |
(make_spd_matrix(n_features, random_state=prng) |
| 30 |
+ mincv * np.eye(n_features)) |
| 31 |
for x in range(n_components) |
| 32 |
]) |
| 33 |
|
| 34 |
|
| 35 |
def normalized(X, axis=None): |
| 36 |
X_copy = X.copy() |
| 37 |
normalize(X_copy, axis=axis) |
| 38 |
return X_copy |
| 39 |
|
| 40 |
|
| 41 |
def assert_log_likelihood_increasing(h, X, lengths, n_iter): |
| 42 |
h.n_iter = 1 |
| 43 |
h.init_params = '' |
| 44 |
log_likelihoods = np.empty(n_iter, dtype=float) |
| 45 |
for i in range(n_iter): |
| 46 |
h.fit(X, lengths=lengths) |
| 47 |
log_likelihoods[i] = h.score(X, lengths=lengths) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
diff = np.diff(log_likelihoods) |
| 52 |
value = np.finfo(float).eps ** (1/2) |
| 53 |
assert diff.max() > value, f"Non-increasing log-likelihoods:\n" \ |
| 54 |
f"lls={log_likelihoods}\n" \ |
| 55 |
f"diff={diff}\n" \ |
| 56 |
f"diff.max() < value={diff.min() < value}\n" \ |
| 57 |
f"np.finfo(float).eps={value}\n" |
| 58 |
|
| 59 |
|
| 60 |
def compare_variational_and_em_models(variational, em, sequences, lengths): |
| 61 |
em_score = em.score(sequences, lengths) |
| 62 |
vi_score = variational.score(sequences, lengths) |
| 63 |
em_scores = em.predict(sequences, lengths) |
| 64 |
vi_scores = variational.predict(sequences, lengths) |
| 65 |
assert em_score == pytest.approx(vi_score), (em_score, vi_score) |
| 66 |
assert np.all(em_scores == vi_scores) |
| 67 |
|
| 68 |
for decode_algo in DECODER_ALGORITHMS: |
| 69 |
em_logprob, em_path = em.decode(sequences, lengths, |
| 70 |
algorithm=decode_algo) |
| 71 |
vi_logprob, vi_path = variational.decode(sequences, lengths, |
| 72 |
algorithm=decode_algo) |
| 73 |
assert em_logprob == pytest.approx(vi_logprob), decode_algo |
| 74 |
assert np.all(em_path == vi_path), decode_algo |
| 75 |
|
| 76 |
em_predict = em.predict(sequences, lengths) |
| 77 |
vi_predict = variational.predict(sequences, lengths) |
| 78 |
assert np.all(em_predict == vi_predict) |
| 79 |
em_logprob, em_posteriors = em.score_samples(sequences, lengths) |
| 80 |
vi_logprob, vi_posteriors = variational.score_samples(sequences, lengths) |
| 81 |
assert em_logprob == pytest.approx(vi_logprob) |
| 82 |
assert np.all(em_posteriors == pytest.approx(vi_posteriors)) |
| 83 |
|
| 84 |
em_obs, em_states = em.sample(100, random_state=42) |
| 85 |
vi_obs, vi_states = variational.sample(100, random_state=42) |
| 86 |
assert np.all(em_obs == vi_obs) |
| 87 |
assert np.all(em_states == vi_states) |
| 88 |
|
| 89 |
|
| 90 |
def vi_uniform_startprob_and_transmat(model, lengths): |
| 91 |
nc = model.n_components |
| 92 |
model.startprob_prior_ = np.full(nc, 1/nc) |
| 93 |
model.startprob_posterior_ = np.full(nc, 1/nc) * len(lengths) |
| 94 |
model.transmat_prior_ = np.full((nc, nc), 1/nc) |
| 95 |
model.transmat_posterior_ = np.full((nc, nc), 1/nc)*sum(lengths) |
| 96 |
return model |