Ejemplo n.º 1
0
    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])
Ejemplo n.º 2
0
    def test_forward(self):
        chain0_means = np.array([[-5.99882413e+02, 9.99894546e+01],
                                 [-3.00048799e+02, 1.99167070e+02],
                                 [-4.28859220e-02, 2.99416348e+02]])
        chain0_covars = np.array([[0.93523132, 0.75423995],
                                  [0.58861593, 0.4016048],
                                  [0.25641893, 1.60482337]])
        chain0_logstart = np.array([-4.44089210e-16, -3.60436534e+01, -3.60436534e+01])
        chain0_logtransmat = np.array([[-38.34619345,  0.0, -38.34619345],
                                       [-38.34619345, -38.34619345, 0.0],
                                       [-1.09861229, -1.09861229, -1.09861229]])

        chain1_means = np.array([[-5.99882413e+02, 9.99894546e+01],
                                 [-3.00048799e+02, 1.99167070e+02],
                                 [-4.39995282e-02, 2.99415977e+02]])
        chain1_covars = np.array([[3.73792529, 3.01395982],
                                  [2.35146944, 1.60341962],
                                  [1.3566332, 6.45346843]])
        chain1_logstart = np.array([-4.44089210e-16, -3.60436534e+01, -3.60436534e+01])
        chain1_logtransmat = np.array([[-3.83461934e+01, -3.71345941e-06, -1.25035485e+01],
                                       [-3.83461897e+01, -2.35611266e+01, -5.85509419e-11],
                                       [-2.58426449e+01, -2.58426449e+01, -1.19594334e-11]])

        loglikelihood = -55.089351487070289
        obs = [np.array([[-600, 100], [-300, 200], [0, 300]]) for _ in xrange(1)]

        # Fit the FHMM on random noise b/c we need it to be fitted in order to manipulate the chains
        fhmm = SequentialGaussianFHMM(n_chains=2, n_states=3)
        fhmm.fit([np.random.random((3, 2))])

        # Update chain0
        fhmm.chains_[0]._means_ = chain0_means
        fhmm.chains_[0]._covars_ = chain0_covars
        fhmm.chains_[0]._log_startprob = chain0_logstart
        fhmm.chains_[0]._log_transmat = chain0_logtransmat

        # Update chain1
        fhmm.chains_[1]._means_ = chain1_means
        fhmm.chains_[1]._covars_ = chain1_covars
        fhmm.chains_[1]._log_startprob = chain1_logstart
        fhmm.chains_[1]._log_transmat = chain1_logtransmat

        self.assertEqual(loglikelihood, fhmm.loglikelihood(obs[0]))