| 1 |
import numpy as np |
| 2 |
from numpy.testing import assert_allclose, assert_array_less |
| 3 |
import pytest |
| 4 |
from sklearn.utils import check_random_state |
| 5 |
|
| 6 |
from ..hmm import GMMHMM |
| 7 |
from .test_gmm_hmm import create_random_gmm |
| 8 |
from . import assert_log_likelihood_increasing, normalized |
| 9 |
|
| 10 |
|
| 11 |
def sample_from_parallelepiped(low, high, n_samples, random_state): |
| 12 |
(n_features,) = low.shape |
| 13 |
X = np.zeros((n_samples, n_features)) |
| 14 |
for i in range(n_features): |
| 15 |
X[:, i] = random_state.uniform(low[i], high[i], n_samples) |
| 16 |
return X |
| 17 |
|
| 18 |
|
| 19 |
def prep_params(n_comps, n_mix, n_features, covar_type, |
| 20 |
low, high, random_state): |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
dim_lims = np.zeros((n_comps + 1, n_features)) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
dim_lims[1:] = np.cumsum( |
| 29 |
random_state.uniform(low, high, (n_comps, n_features)), axis=0 |
| 30 |
) |
| 31 |
|
| 32 |
means = np.zeros((n_comps, n_mix, n_features)) |
| 33 |
for i, (left, right) in enumerate(zip(dim_lims, dim_lims[1:])): |
| 34 |
means[i] = sample_from_parallelepiped(left, right, n_mix, |
| 35 |
random_state) |
| 36 |
|
| 37 |
startprob = np.zeros(n_comps) |
| 38 |
startprob[0] = 1 |
| 39 |
|
| 40 |
transmat = normalized(random_state.uniform(size=(n_comps, n_comps)), |
| 41 |
axis=1) |
| 42 |
|
| 43 |
if covar_type == "spherical": |
| 44 |
covs = random_state.uniform(0.1, 5, size=(n_comps, n_mix)) |
| 45 |
elif covar_type == "diag": |
| 46 |
covs = random_state.uniform(0.1, 5, size=(n_comps, n_mix, n_features)) |
| 47 |
elif covar_type == "tied": |
| 48 |
covs = np.zeros((n_comps, n_features, n_features)) |
| 49 |
for i in range(n_comps): |
| 50 |
low = random_state.uniform(-2, 2, (n_features, n_features)) |
| 51 |
covs[i] = low.T @ low |
| 52 |
elif covar_type == "full": |
| 53 |
covs = np.zeros((n_comps, n_mix, n_features, n_features)) |
| 54 |
for i in range(n_comps): |
| 55 |
for j in range(n_mix): |
| 56 |
low = random_state.uniform(-2, 2, |
| 57 |
size=(n_features, n_features)) |
| 58 |
covs[i, j] = low.T @ low |
| 59 |
|
| 60 |
weights = normalized(random_state.uniform(size=(n_comps, n_mix)), axis=1) |
| 61 |
|
| 62 |
return covs, means, startprob, transmat, weights |
| 63 |
|
| 64 |
|
| 65 |
class GMMHMMTestMixin: |
| 66 |
n_components = 3 |
| 67 |
n_mix = 2 |
| 68 |
n_features = 2 |
| 69 |
low, high = 10, 15 |
| 70 |
|
| 71 |
def new_hmm(self, implementation): |
| 72 |
prng = np.random.RandomState(14) |
| 73 |
covars, means, startprob, transmat, weights = prep_params( |
| 74 |
self.n_components, self.n_mix, self.n_features, |
| 75 |
self.covariance_type, self.low, self.high, prng) |
| 76 |
h = GMMHMM(n_components=self.n_components, n_mix=self.n_mix, |
| 77 |
covariance_type=self.covariance_type, |
| 78 |
random_state=prng, |
| 79 |
implementation=implementation) |
| 80 |
h.startprob_ = startprob |
| 81 |
h.transmat_ = transmat |
| 82 |
h.weights_ = weights |
| 83 |
h.means_ = means |
| 84 |
h.covars_ = covars |
| 85 |
return h |
| 86 |
|
| 87 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 88 |
def test_check_bad_covariance_type(self, implementation): |
| 89 |
h = self.new_hmm(implementation) |
| 90 |
with pytest.raises(ValueError): |
| 91 |
h.covariance_type = "bad_covariance_type" |
| 92 |
h._check() |
| 93 |
|
| 94 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 95 |
def test_check_good_covariance_type(self, implementation): |
| 96 |
h = self.new_hmm(implementation) |
| 97 |
h._check() |
| 98 |
|
| 99 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 100 |
def test_sample(self, implementation): |
| 101 |
n_samples = 1000 |
| 102 |
h = self.new_hmm(implementation) |
| 103 |
X, states = h.sample(n_samples) |
| 104 |
assert X.shape == (n_samples, self.n_features) |
| 105 |
assert len(states) == n_samples |
| 106 |
|
| 107 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 108 |
def test_init(self, implementation): |
| 109 |
n_samples = 1000 |
| 110 |
h = self.new_hmm(implementation) |
| 111 |
X, _states = h.sample(n_samples) |
| 112 |
h._init(X, [n_samples]) |
| 113 |
h._check() |
| 114 |
|
| 115 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 116 |
def test_score_samples_and_decode(self, implementation): |
| 117 |
n_samples = 1000 |
| 118 |
h = self.new_hmm(implementation) |
| 119 |
X, states = h.sample(n_samples) |
| 120 |
|
| 121 |
_ll, posteriors = h.score_samples(X) |
| 122 |
assert_allclose(np.sum(posteriors, axis=1), np.ones(n_samples)) |
| 123 |
|
| 124 |
_viterbi_ll, decoded_states = h.decode(X) |
| 125 |
assert_allclose(states, decoded_states) |
| 126 |
|
| 127 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 128 |
def test_fit(self, implementation): |
| 129 |
n_iter = 5 |
| 130 |
n_samples = 1000 |
| 131 |
lengths = None |
| 132 |
h = self.new_hmm(implementation) |
| 133 |
X, _state_sequence = h.sample(n_samples) |
| 134 |
|
| 135 |
|
| 136 |
covs0, means0, priors0, trans0, weights0 = prep_params( |
| 137 |
self.n_components, self.n_mix, self.n_features, |
| 138 |
self.covariance_type, self.low, self.high, |
| 139 |
np.random.RandomState(15) |
| 140 |
) |
| 141 |
h.covars_ = covs0 * 100 |
| 142 |
h.means_ = means0 |
| 143 |
h.startprob_ = priors0 |
| 144 |
h.transmat_ = trans0 |
| 145 |
h.weights_ = weights0 |
| 146 |
assert_log_likelihood_increasing(h, X, lengths, n_iter) |
| 147 |
|
| 148 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 149 |
def test_fit_sparse_data(self, implementation): |
| 150 |
n_samples = 1000 |
| 151 |
h = self.new_hmm(implementation) |
| 152 |
h.means_ *= 1000 |
| 153 |
X, _states = h.sample(n_samples) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
h._init(X, [1000]) |
| 158 |
h.fit(X) |
| 159 |
|
| 160 |
@pytest.mark.xfail |
| 161 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 162 |
def test_fit_zero_variance(self, implementation): |
| 163 |
|
| 164 |
|
| 165 |
X = np.asarray([ |
| 166 |
[7.15000000e+02, 5.8500000e+02, 0.00000000e+00, 0.00000000e+00], |
| 167 |
[7.15000000e+02, 5.2000000e+02, 1.04705811e+00, -6.03696289e+01], |
| 168 |
[7.15000000e+02, 4.5500000e+02, 7.20886230e-01, -5.27055664e+01], |
| 169 |
[7.15000000e+02, 3.9000000e+02, -4.57946777e-01, -7.80605469e+01], |
| 170 |
[7.15000000e+02, 3.2500000e+02, -6.43127441e+00, -5.59954834e+01], |
| 171 |
[7.15000000e+02, 2.6000000e+02, -2.90063477e+00, -7.80220947e+01], |
| 172 |
[7.15000000e+02, 1.9500000e+02, 8.45532227e+00, -7.03294373e+01], |
| 173 |
[7.15000000e+02, 1.3000000e+02, 4.09387207e+00, -5.83621216e+01], |
| 174 |
[7.15000000e+02, 6.5000000e+01, -1.21667480e+00, -4.48131409e+01] |
| 175 |
]) |
| 176 |
|
| 177 |
h = self.new_hmm(implementation) |
| 178 |
h.fit(X) |
| 179 |
|
| 180 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 181 |
def test_criterion(self, implementation): |
| 182 |
random_state = check_random_state(2013) |
| 183 |
m1 = self.new_hmm(implementation) |
| 184 |
|
| 185 |
m1.means_ *= 10 |
| 186 |
|
| 187 |
X, _ = m1.sample(4000, random_state=random_state) |
| 188 |
aic = [] |
| 189 |
bic = [] |
| 190 |
ns = [2, 3, 4, 5] |
| 191 |
for n in ns: |
| 192 |
h = GMMHMM(n, n_mix=2, covariance_type=self.covariance_type, |
| 193 |
random_state=random_state, implementation=implementation) |
| 194 |
h.fit(X) |
| 195 |
aic.append(h.aic(X)) |
| 196 |
bic.append(h.bic(X)) |
| 197 |
|
| 198 |
assert np.all(aic) > 0 |
| 199 |
assert np.all(bic) > 0 |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
class TestGMMHMMWithSphericalCovars(GMMHMMTestMixin): |
| 206 |
covariance_type = 'spherical' |
| 207 |
|
| 208 |
|
| 209 |
class TestGMMHMMWithDiagCovars(GMMHMMTestMixin): |
| 210 |
covariance_type = 'diag' |
| 211 |
|
| 212 |
|
| 213 |
class TestGMMHMMWithTiedCovars(GMMHMMTestMixin): |
| 214 |
covariance_type = 'tied' |
| 215 |
|
| 216 |
|
| 217 |
class TestGMMHMMWithFullCovars(GMMHMMTestMixin): |
| 218 |
covariance_type = 'full' |
| 219 |
|
| 220 |
|
| 221 |
class TestGMMHMM_KmeansInit: |
| 222 |
@pytest.mark.parametrize("implementation", ["scaling", "log"]) |
| 223 |
def test_kmeans(self, implementation): |
| 224 |
|
| 225 |
|
| 226 |
np.random.seed(0) |
| 227 |
data1 = np.random.uniform(low=0, high=1, size=(100, 2)) |
| 228 |
data2 = np.random.uniform(low=5, high=6, size=(5, 2)) |
| 229 |
data = np.r_[data1, data2] |
| 230 |
model = GMMHMM(n_components=2, n_mix=10, n_iter=5, |
| 231 |
implementation=implementation) |
| 232 |
model.fit(data) |
| 233 |
|
| 234 |
assert_array_less(0, model.means_) |
| 235 |
assert_array_less(model.means_, 6) |
| 236 |
|
| 237 |
|
| 238 |
class TestGMMHMM_MultiSequence: |
| 239 |
|
| 240 |
@pytest.mark.parametrize("covtype", |
| 241 |
["diag", "spherical", "tied", "full"]) |
| 242 |
def test_chunked(sellf, covtype, init_params='mcw'): |
| 243 |
np.random.seed(0) |
| 244 |
gmm = create_random_gmm(3, 2, covariance_type=covtype, prng=0) |
| 245 |
gmm.covariances_ = gmm.covars_ |
| 246 |
data = gmm.sample(n_samples=1000)[0] |
| 247 |
|
| 248 |
model1 = GMMHMM(n_components=3, n_mix=2, covariance_type=covtype, |
| 249 |
random_state=1, init_params=init_params) |
| 250 |
model2 = GMMHMM(n_components=3, n_mix=2, covariance_type=covtype, |
| 251 |
random_state=1, init_params=init_params) |
| 252 |
|
| 253 |
init = 1. / model1.n_components |
| 254 |
for model in (model1, model2): |
| 255 |
model.startprob_ = np.full(model.n_components, init) |
| 256 |
model.transmat_ = \ |
| 257 |
np.full((model.n_components, model.n_components), init) |
| 258 |
|
| 259 |
model1.fit(data) |
| 260 |
model2.fit(data, lengths=[200] * 5) |
| 261 |
|
| 262 |
assert_allclose(model1.means_, model2.means_, rtol=0, atol=1e-2) |
| 263 |
assert_allclose(model1.covars_, model2.covars_, rtol=0, atol=1e-3) |
| 264 |
assert_allclose(model1.weights_, model2.weights_, rtol=0, atol=1e-3) |
| 265 |
assert_allclose(model1.transmat_, model2.transmat_, rtol=0, atol=1e-2) |