def test_8(): #"Sample from a VMHMM and then fit to it" n_states = 2 vm = VonMisesHMM(n_states=n_states) means = np.array([[0, 0, 0], [np.pi, np.pi, np.pi]]) kappas = np.array([[1, 2, 3], [2, 3, 4]]) transmat = np.array([[0.9, 0.1], [0.1, 0.9]]) vm.means_ = means vm.kappas_ = kappas vm.transmat_ = transmat x, s = vm.sample(1000) vm = VonMisesHMM(n_states=2) vm.fit([x]) mappingcost = np.zeros((n_states, n_states)) for i in range(n_states): for j in range(n_states): mappingcost[i, j] = np.sum(circwrap(vm.means_[i, :] - means[j, :])**2) mapping = Munkres().compute(mappingcost) means_ = np.array([vm.means_[j, :] for i, j in mapping]) kappas_ = np.array([vm.kappas_[j, :] for i, j in mapping]) transmat_ = vm.transmat_ print('means\n', means, '\nmeans_\n', means_) print('kappas\n', kappas, '\nkappas_\n', kappas_) print('transmat\n', transmat, '\ntransmat_\n', transmat_) #vm.score(x) assert np.all(np.abs(kappas - kappas_) < 0.5) assert np.all(circwrap(means - means_) < 0.25)
def test_8(): #"Sample from a VMHMM and then fit to it" n_states = 2 vm = VonMisesHMM(n_states=n_states) means = np.array([[0, 0, 0], [np.pi, np.pi, np.pi]]) kappas = np.array([[1, 2, 3], [2, 3, 4]]) transmat = np.array([[0.9, 0.1], [0.1, 0.9]]) vm.means_ = means vm.kappas_ = kappas vm.transmat_ = transmat x, s = vm.sample(1000) vm = VonMisesHMM(n_states=2) vm.fit([x]) mappingcost = np.zeros((n_states, n_states)) for i in range(n_states): for j in range(n_states): mappingcost[i, j] = np.sum( circwrap(vm.means_[i, :] - means[j, :])**2) mapping = Munkres().compute(mappingcost) means_ = np.array([vm.means_[j, :] for i, j in mapping]) kappas_ = np.array([vm.kappas_[j, :] for i, j in mapping]) transmat_ = vm.transmat_ print('means\n', means, '\nmeans_\n', means_) print('kappas\n', kappas, '\nkappas_\n', kappas_) print('transmat\n', transmat, '\ntransmat_\n', transmat_) #vm.score(x) assert np.all(np.abs(kappas - kappas_) < 0.5) assert np.all(circwrap(means - means_) < 0.25)
def test_6(): """"Test that _c_fitkappa is consistent with the two-step python implementation""" np.random.seed(42) vm = VonMisesHMM(n_states=13) kappas = np.random.randn(13, 7) posteriors = np.random.randn(100, 13) obs = np.random.randn(100, 7) means = np.random.randn(13, 7) vm.kappas_ = kappas vm._c_fitkappas(posteriors, obs, means) c_kappas = np.copy(vm._kappas_) vm._py_fitkappas(posteriors, obs, means) py_kappas = np.copy(vm._kappas_) np.testing.assert_array_almost_equal(py_kappas, c_kappas)