Ticket #8732: trac_8732.patch

File trac_8732.patch, 4.8 KB (added by was, 3 years ago)
  • sage/stats/hmm/chmm.pyx

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1271799943 25200
    # Node ID d415a8954cbed11e4f229b4009562e42c336db5c
    # Parent  44042eb22a89f08124c2f7c8f99ebf113905734a
    trac 8732 -- bug in new HMM code
    
    diff -r 44042eb22a89 -r d415a8954cbe sage/stats/hmm/chmm.pyx
    a b  
    8181    Gaussian emissions Hidden Markov Model. 
    8282 
    8383    INPUT: 
    84         - A  -- matrix; the N x N transition matrix 
    85         - B -- list of pairs (mu,sigma) that define the distributions 
    86         - pi -- initial state probabilities 
     84 
     85        - ``A`` -- matrix; the N x N transition matrix 
     86        - ``B`` -- list of pairs (mu,sigma) that define the distributions 
     87        - ``pi`` -- initial state probabilities 
     88        - ``normalize`` --bool (default: True) 
    8789 
    8890    EXAMPLES:: 
    8991 
     
    953955    INPUT: 
    954956     
    955957        - ``A``  -- matrix; the N x N transition matrix 
     958 
    956959        - ``B`` -- list of pairs (mu,sigma) that define the distributions 
     960 
    957961        - ``pi`` -- initial state probabilities 
     962 
    958963        - ``normalize`` --bool (default: True); if given, input is 
    959964          normalized to define valid probability distributions, 
    960965          e.g., the entries of A are made nonnegative and the rows 
  • sage/stats/hmm/hmm.pyx

    diff -r 44042eb22a89 -r d415a8954cbe sage/stats/hmm/hmm.pyx
    a b  
    228228    A discrete Hidden Markov model implemented using double precision 
    229229    floating point arithmetic. 
    230230 
     231    INPUT:: 
     232 
     233       - ``A`` -- a list of lists or a square N x N matrix, whose 
     234         (i,j) entry gives the probability of transitioning from 
     235         state i to state j. 
     236 
     237       - ``B`` -- a list of N lists or a matrix with N rows, such that 
     238         B[i,k] gives the probability of emitting symbol k while 
     239         in state i. 
     240 
     241       - ``pi`` -- the probabilities of starting in each initial 
     242         state, i.e,. pi[i] is the probability of starting in 
     243         state i. 
     244 
     245       - ``emission_symbols`` -- None or list (default: None); if 
     246         None, the emission_symbols are the ints [0..N-1], where N 
     247         is the number of states.  Otherwise, they are the entries 
     248         of the list emissions_symbols, which must all be hashable. 
     249 
     250       - ``normalize`` --bool (default: True); if given, input is 
     251         normalized to define valid probability distributions, 
     252         e.g., the entries of A are made nonnegative and the rows 
     253         sum to 1, and the probabilities in pi are normalized. 
     254 
    231255    EXAMPLES:: 
    232256 
    233257        sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.1,0.9],[0.5,0.5]], [.5,.5]); m 
     
    257281        sage: m.sample(10) 
    258282        [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] 
    259283        sage: m.graph().plot() 
     284 
     285    A 3-state model that happens to always outputs 'b':: 
     286 
     287        sage: m = hmm.DiscreteHiddenMarkovModel([[1/3]*3]*3, [[0,1,0]]*3, [1/3]*3, ['a','b','c']) 
     288        sage: m.sample(10) 
     289        ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']     
    260290    """ 
    261291    cdef TimeSeries B 
    262292    cdef int n_out 
     
    269299        probabilities pi, and given emission symbols (which default 
    270300        to the first few nonnegative integers). 
    271301 
    272         INPUT:: 
    273  
    274            - ``A`` -- a list of lists or a square N x N matrix, whose 
    275              (i,j) entry gives the probability of transitioning from 
    276              state i to state j. 
    277  
    278            - ``B`` -- a list of N lists or a matrix with N rows, such that 
    279              B[i,k] gives the probability of emitting symbol k while 
    280              in state i. 
    281  
    282            - ``pi`` -- the probabilities of starting in each initial 
    283              state, i.e,. pi[i] is the probability of starting in 
    284              state i. 
    285  
    286            - ``emission_symbols`` -- None or list (default: None); if 
    287              None, the emission_symbols are the ints [0..N-1], where N 
    288              is the number of states.  Otherwise, they are the entries 
    289              of the list emissions_symbols, which must all be hashable. 
    290  
    291            - ``normalize`` --bool (default: True); if given, input is 
    292              normalized to define valid probability distributions, 
    293              e.g., the entries of A are made nonnegative and the rows 
    294              sum to 1, and the probabilities in pi are normalized. 
    295  
    296302        EXAMPLES:: 
    297303 
    298304            sage: hmm.DiscreteHiddenMarkovModel([.5,0,-1,.5], [[1],[1]],[.5,.5]).transition_matrix() 
     
    773779        # that this should get factored out into a call to something 
    774780        # defined in distributions.pyx. 
    775781        for j in range(self.n_out): 
    776             a = self.B._values[q*self.N+j] 
     782            a = self.B._values[q*self.n_out + j] 
    777783            if r < a + accum: 
    778784                return j 
    779785            else: