Esempio n. 1
0
    def test_dishonest_casino_larger_transition_p(self):
        '''Dishonest Casino Example.'''
        # Create transition probability matrix
        # Create set of all observable symbols
        V = [1, 2, 3, 4, 5, 6]
    
        # Instantiate an HMM, note Pi is uniform probability distribution by default
        m = hmmt.HMM(2, A=A2, B=B, V=V)
        
        Obs = [ 1, 2, 3, 4, 5, 2, 1, 6, 6, 6, 5, 6 ]
        log_prob_Obs, Alpha, c = hmmt.forward(m, Obs, scaling=1)
        assert_almost_equal(log_prob_Obs, -20.12266, decimal=3, err_msg='Wrong observation probability')
        
        Q_star, _, _ = hmmt.viterbi(m, Obs, scaling=1)
        assert_equal(Q_star, [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], err_msg='Wrong Viterbi path')

        Beta = hmmt.backward(m, Obs, c)
        Gamma, Q_star = hmmt.individually_optimal_states(Alpha, Beta)
        assert_almost_equal(Gamma,
                            [[0.8187172809335708, 0.8482909847967097, 0.8526704789882301, 0.8332628435868141, 0.7838346435680826, 0.6885099864086844, 0.5166714952004011, 0.21280359871744636, 0.11993462067436834, 0.10785580156479256, 0.15963081036800023, 0.1497144396249583], 
                             [0.1812827190664292, 0.1517090152032903, 0.14732952101176994, 0.16673715641318582, 0.21616535643191742, 0.31149001359131556, 0.4833285047995989, 0.7871964012825536, 0.8800653793256317, 0.8921441984352074, 0.8403691896319997, 0.8502855603750418]],
                            decimal=5, err_msg='Wrong state probabilities')
        assert_almost_equal(np.sum(Gamma[np.array([0,1]), :], axis=0), np.ones(len(Obs)),
                                decimal=3, err_msg='Gammas must sum up to 1 at each time')      
        assert_equal(Q_star, [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], 'Wrong individually-optimal states')
Esempio n. 2
0
 def __calculate_prob_gamma(self):
     '''Return the posterior IBD probability at each marker using individual-state
     posterior probabilities (Gamma). Sets the self.Gamma field.'''
     # Remove observations that are IBS=0 - debugging
     _, Alpha, c = hmmt.forward(self.m, self.Obs, scaling=1)
     Beta = hmmt.backward(self.m, self.Obs, c)
     Gamma, _ = hmmt.individually_optimal_states(Alpha, Beta)
     self.Gamma = Gamma
     self.p_ibd_gamma = np.sum(Gamma[np.array([0, 2, 4, 6, 7]), :], axis=0)
     return self.p_ibd_gamma
Esempio n. 3
0
 def __calculate_prob_gamma(self):
     '''Return the posterior IBD probability at each marker using individual-state
     posterior probabilities (Gamma). Sets the self.Gamma field.'''
     # Remove observations that are IBS=0 - debugging
     _, Alpha, c = hmmt.forward(self.m, self.Obs, scaling=1)
     Beta = hmmt.backward(self.m, self.Obs, c)
     Gamma, _ = hmmt.individually_optimal_states(Alpha, Beta)
     self.Gamma = Gamma
     self.p_ibd_gamma = np.sum(Gamma[np.array([0, 2, 4, 6, 7]), :], axis=0)
     return self.p_ibd_gamma 
Esempio n. 4
0
    def test_dishonest_casino(self):
        '''Dishonest Casino Example.'''
        # Create set of all observable symbols
        V = [1, 2, 3, 4, 5, 6]
    
        # Instantiate an HMM, note Pi is uniform probability distribution by default
        m = hmmt.HMM(2, A=A, B=B, V=V)
        
        Obs = [ 1, 2, 3, 4, 5, 2, 1, 6, 6, 6, 5, 6 ]
        log_prob_Obs, Alpha, c = hmmt.forward(m, Obs, scaling=1)
        assert_almost_equal(log_prob_Obs, -20.918331960920177, decimal=5, err_msg='Wrong observation probability')
        
        Q_star, _, _ = hmmt.viterbi(m, Obs, scaling=1)
        assert_equal(Q_star, np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 'Wrong Viterbi path')

        Beta = hmmt.backward(m, Obs, c)
        Gamma, Q_star = hmmt.individually_optimal_states(Alpha, Beta)
        assert_almost_equal(Gamma,
                            [[0.6497764611558418, 0.6476464636785156, 0.6395341323290045, 0.6232731859090421, 0.594520672835795, 0.5455970624010458, 0.4634351125974832, 0.3259525777265338, 0.28060687540229046, 0.2668719322118299, 0.26641411220848843, 0.26052432611296034], [0.35022353884415813, 0.3523535363214843, 0.3604658676709956, 0.3767268140909578, 0.405479327164205, 0.4544029375989542, 0.5365648874025167, 0.6740474222734663, 0.7193931245977097, 0.7331280677881701, 0.7335858877915117, 0.7394756738870396]],
                            decimal=5, err_msg='Wrong state probabilities')        
        assert_equal(Q_star, [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], 'Wrong individually-optimal states')