def test_exact_fhmm(self): n_chains = 3 n_states = 4 n_features = 2 obs = [np.array([[-600, 100], [-300, 200], [0, 300]])] means = np.array([[162.2636, 154.0879], [-299.5184, 0.1605], [-313.1509, -4.3836], [ -15.0993, 94.9669], [-102.5866, 65.8045], [ -88.9765, 70.3412], [-135.6746, 54.7751], [-138.2674, 53.9109], [-147.2465, 50.9178], [-135.9769, 54.6744], [-33.4946, 88.8351], [-148.7870, 50.4043]]) means = means.reshape((n_chains, n_states, n_features)) covar = 1.0e+03 * np.array([[9.1968, 3.0656], [3.0656, 1.0219]]) transmat = np.array([[0.3660, 0.0045, 0.0072, 0.6223], [0.2442, 0.0546, 0.0053, 0.6959], [0.0037, 0.0887, 0.0503, 0.8573], [0.9465, 0.0006, 0.0004, 0.0525], [0.1922, 0.5774, 0.0381, 0.1923], [0.3122, 0.4747, 0.0784, 0.1347], [0.0432, 0.6053, 0.2195, 0.1320], [0.3434, 0.2505, 0.0443, 0.3618], [0.0864, 0.3736, 0.5159, 0.0241], [0.2319, 0.2389, 0.5001, 0.0291], [0.0741, 0.1285, 0.6239, 0.1735], [0.3229, 0.2213, 0.2160, 0.2398]]) transmat = transmat.reshape((n_chains, n_states, n_states)) self.assertTrue(np.allclose(np.sum(transmat, axis=2), 1)) startprob = np.array([[0.0012, 0.1196, 0.3150], [0.5932, 0.1659, 0.3572], [0.4049, 0.1440, 0.0027], [0.0007, 0.5705, 0.3251]]) startprob = startprob.T.reshape((n_chains, n_states)) self.assertTrue(np.allclose(np.sum(startprob, axis=1), 1.0)) fhmm = ExactGaussianFHMM(n_states=n_states, n_chains=n_chains, topology='full', n_training_iterations=1) fhmm.fit([np.random.random((20, 2))]) fhmm.log_startprob = np.log(startprob) fhmm.log_transmat = np.log(transmat) fhmm.means = means fhmm.covar = covar actual_loglikelihood = fhmm.loglikelihood(obs[0]) # The loglikelihood calculated by the reference impl is -15.0552, which is probably due to to the fact that we # calculate in logs and the ref implementation uses scaling loglikelihood = -11.425412024522673 self.assertAlmostEqual(actual_loglikelihood, loglikelihood)
def test_exact_fhmm_train(self): path = os.path.join(TESTDATA_PATH, 'X.mat') self.assertTrue(os.path.exists(path)) X = loadmat(path)['X'] obs = [X] fhmm_exact = ExactGaussianFHMM(n_chains=2, n_states=3, n_training_iterations=20, training_threshold=0.01, topology='full') fhmm_exact.fit(obs) print 'exact:', fhmm_exact.loglikelihood(obs[0]) fhmm_seq = SequentialGaussianFHMM(n_chains=2, n_states=3, n_training_iterations=20, training_threshold=0.01, topology='full') fhmm_seq.fit(obs) print 'seq:', fhmm_seq.loglikelihood(obs[0])
def test_foo(self): obs = [np.array([[-600, 100], [-300, 200], [0, 300]]) for _ in xrange(10)] fhmm = ExactGaussianFHMM(n_chains=2, n_states=3) fhmm.fit(obs) print fhmm.loglikelihood(obs[0])