# 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
|
|
| 81 | 81 | Gaussian emissions Hidden Markov Model. |
| 82 | 82 | |
| 83 | 83 | 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) |
| 87 | 89 | |
| 88 | 90 | EXAMPLES:: |
| 89 | 91 | |
| … |
… |
|
| 953 | 955 | INPUT: |
| 954 | 956 | |
| 955 | 957 | - ``A`` -- matrix; the N x N transition matrix |
| | 958 | |
| 956 | 959 | - ``B`` -- list of pairs (mu,sigma) that define the distributions |
| | 960 | |
| 957 | 961 | - ``pi`` -- initial state probabilities |
| | 962 | |
| 958 | 963 | - ``normalize`` --bool (default: True); if given, input is |
| 959 | 964 | normalized to define valid probability distributions, |
| 960 | 965 | e.g., the entries of A are made nonnegative and the rows |
diff -r 44042eb22a89 -r d415a8954cbe sage/stats/hmm/hmm.pyx
|
a
|
b
|
|
| 228 | 228 | A discrete Hidden Markov model implemented using double precision |
| 229 | 229 | floating point arithmetic. |
| 230 | 230 | |
| | 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 | |
| 231 | 255 | EXAMPLES:: |
| 232 | 256 | |
| 233 | 257 | sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.1,0.9],[0.5,0.5]], [.5,.5]); m |
| … |
… |
|
| 257 | 281 | sage: m.sample(10) |
| 258 | 282 | [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] |
| 259 | 283 | 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'] |
| 260 | 290 | """ |
| 261 | 291 | cdef TimeSeries B |
| 262 | 292 | cdef int n_out |
| … |
… |
|
| 269 | 299 | probabilities pi, and given emission symbols (which default |
| 270 | 300 | to the first few nonnegative integers). |
| 271 | 301 | |
| 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 | | |
| 296 | 302 | EXAMPLES:: |
| 297 | 303 | |
| 298 | 304 | sage: hmm.DiscreteHiddenMarkovModel([.5,0,-1,.5], [[1],[1]],[.5,.5]).transition_matrix() |
| … |
… |
|
| 773 | 779 | # that this should get factored out into a call to something |
| 774 | 780 | # defined in distributions.pyx. |
| 775 | 781 | 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] |
| 777 | 783 | if r < a + accum: |
| 778 | 784 | return j |
| 779 | 785 | else: |