blib/lib/PDL/HMM.pm

filter: Statementstmt 86.6% 26 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 87.8% 29 / 33 CC 1 SCAR 0.0
CC
1
Cov
87%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« pp-logdiff.c ↑ Summary
1 #
2 # GENERATED WITH PDL::PP from HMM.pd! Don't modify!
3 #
4 package PDL::HMM;
5  
6 5 our @EXPORT_OK = qw(logzero logadd logdiff logsumover hmmfw hmmalpha hmmfwq hmmalphaq hmmbw hmmbeta hmmbwq hmmbetaq hmmexpect0 hmmexpect hmmexpectq hmmmaximize hmmviterbi hmmviterbiq hmmpath hmmpathq );
7 5 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8  
9 5 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 5 our $VERSION = '0.06010';
15 5 our @ISA = ( 'PDL::Exporter','DynaLoader' );
16 5 push @PDL::Core::PP, __PACKAGE__;
17 5 bootstrap PDL::HMM $VERSION;
18  
19  
20  
21  
22  
23  
24  
25  
26 #line 23 "HMM.pd"
27  
28 =pod
29  
30 =head1 NAME
31  
32 PDL::HMM - Hidden Markov Model utilities in PDL
33  
34 =head1 SYNOPSIS
35  
36 use PDL::HMM;
37  
38 ##-----------------------------------------------------
39 ## Dimensions
40  
41 $N = $number_of_states;
42 $M = $number_of_symbols;
43 $T = $length_of_input;
44  
45 $A = $maximum_ambiguity;
46  
47 ##-----------------------------------------------------
48 ## Parameters
49  
50 $af = log(random($N,$N));
51 $bf = log(random($N,$M));
52 $pif = log(random($N));
53 $omegaf = log(random($N));
54  
55 @theta = ($a,$b,$pi,$omega) = hmmmaximize($af,$bf,$pif,$omegaf);
56  
57 $o = long(rint($M*random($T)));
58  
59 maximum_n_ind(dice_axis($a->logsumover+$pi+$b, 1,$o),
60 ($oq=zeroes(long,$A,$T))); ##-- for constrained variants
61  
62 ##-----------------------------------------------------
63 ## Log arithmetic
64  
65 $log0 = logzero;
66 $logz = logadd(log($x),log($y));
67 $logz = logdiff(log($x),log($y));
68 $logz = logsumover(log($x));
69  
70 ##-----------------------------------------------------
71 ## Sequence Probability
72  
73 $alpha = hmmfw ($a,$b,$pi, $o ); ##-- forward (full)
74 $alphaq = hmmfwq($a,$b,$pi, $o, $oq); ##-- forward (constrained)
75  
76 $beta = hmmbw ($a,$b,$omega, $o ); ##-- backward (full)
77 $betaq = hmmbwq($a,$b,$omega, $o,$oq); ##-- backward (constrained)
78  
79 ##-----------------------------------------------------
80 ## Parameter Estimation
81  
82 @expect = ($ea,$eb,$epi,$eomega) = hmmexpect0(@theta); ##-- initialize
83  
84 hmmexpect (@theta, $o, $alpha, $beta, $ea,$eb,$epi); ##-- expect (full)
85 hmmexpectq(@theta, $o,$oq, $alphaq,$betaq, $ea,$eb,$epi); ##-- expect (constrained)
86  
87 ($a,$b,$pi,$omega) = hmmmaximize($ea,$eb,$epi,$eomega); ##-- maximize
88  
89 ##-----------------------------------------------------
90 ## Sequence Analysis
91  
92 ($delta,$psi) = hmmviterbi ($a,$b,$pi, $o); ##-- trellis (full)
93 ($deltaq,$psiq) = hmmviterbiq($a,$b,$pi, $o,$oq); ##-- trellis (constrained)
94  
95 $paths = hmmpath ( $psi, sequence($N)); ##-- backtrace (full)
96 $pathsq = hmmpathq($oq, $psiq, sequence($A)); ##-- backtrace (constrained)
97  
98 =cut
99 #line 100 "HMM.pm"
100  
101  
102 =head1 FUNCTIONS
103  
104 =cut
105  
106  
107  
108  
109  
110 #line 178 "HMM.pd"
111  
112 =pod
113  
114 =head1 Log Arithmetic
115  
116 =cut
117 #line 118 "HMM.pm"
118  
119  
120 =head2 logzero
121  
122 =for sig
123  
124 Signature: ([o]a())
125 Types: (float double ldouble)
126  
127 =for usage
128  
129 $a = logzero();
130 logzero($a); # all arguments given
131  
132 =for ref
133  
134 Approximates $a() = log(0), avoids nan.
135  
136 =pod
137  
138 Broadcasts over its inputs.
139  
140 =for bad
141  
142 logzero() handles bad values. The state of the output PDL is always good.
143  
144 =cut
145  
146  
147  
148  
149 5 *logzero = \&PDL::logzero;
150  
151  
152  
153  
154  
155  
156 =head2 logadd
157  
158 =for sig
159  
160 Signature: (a(); b(); [o]c())
161 Types: (float double ldouble)
162  
163 =for usage
164  
165 $c = logadd($a, $b);
166 logadd($a, $b, $c); # all arguments given
167 $c = $a->logadd($b); # method call
168 $a->logadd($b, $c);
169 $a->inplace->logadd($b); # can be used inplace
170 logadd($a->inplace, $b);
171  
172 =for ref
173  
174 Computes $c() = log(exp($a()) + exp($b())), should be more stable.
175  
176 =pod
177  
178 Can operate inplace with C<a> as output C<c>.
179 Broadcasts over its inputs.
180  
181 =for bad
182  
183 C<logadd> does not process bad values.
184 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
185  
186 =cut
187  
188  
189  
190  
191 5 *logadd = \&PDL::logadd;
192  
193  
194  
195  
196  
197  
198 =head2 logdiff
199  
200 =for sig
201  
202 Signature: (a(); b(); [o]c())
203 Types: (float double ldouble)
204  
205 =for usage
206  
207 $c = logdiff($a, $b);
208 logdiff($a, $b, $c); # all arguments given
209 $c = $a->logdiff($b); # method call
210 $a->logdiff($b, $c);
211 $a->inplace->logdiff($b); # can be used inplace
212 logdiff($a->inplace, $b);
213  
214 =for ref
215  
216 Computes log symmetric difference c = log(exp(max(a,b)) - exp(min(a,b))), may be more stable.
217  
218 =pod
219  
220 Can operate inplace with C<a> as output C<c>.
221 Broadcasts over its inputs.
222  
223 =for bad
224  
225 C<logdiff> does not process bad values.
226 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
227  
228 =cut
229  
230  
231  
232  
233 5 *logdiff = \&PDL::logdiff;
234  
235  
236  
237  
238  
239  
240 =head2 logsumover
241  
242 =for sig
243  
244 Signature: (a(n); [o]b())
245 Types: (float double ldouble)
246  
247 =for usage
248  
249 $b = logsumover($a);
250 logsumover($a, $b); # all arguments given
251 $b = $a->logsumover; # method call
252 $a->logsumover($b);
253  
254 =for ref
255  
256 Computes $b() = log(sumover(exp($a()))), should be more stable.
257  
258 =pod
259  
260 Broadcasts over its inputs.
261  
262 =for bad
263  
264 C<logsumover> does not process bad values.
265 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
266  
267 =cut
268  
269  
270  
271  
272 5 *logsumover = \&PDL::logsumover;
273  
274  
275  
276  
277  
278 #line 234 "HMM.pd"
279  
280 =pod
281  
282 =head1 Sequence Probability
283  
284 =cut
285 #line 286 "HMM.pm"
286  
287  
288 =head2 hmmfw
289  
290 =for sig
291  
292 Signature: (a(N,N); b(N,M); pi(N); o(T); [o]alpha(N,T))
293 Types: (float double ldouble)
294  
295 =for usage
296  
297 $alpha = hmmfw($a, $b, $pi, $o);
298 hmmfw($a, $b, $pi, $o, $alpha); # all arguments given
299 $alpha = $a->hmmfw($b, $pi, $o); # method call
300 $a->hmmfw($b, $pi, $o, $alpha);
301  
302 Compute forward probability (alpha) matrix
303 for input $o given model parameters
304 @theta = ($a, $b, $pi, $omega).
305  
306 Output (pseudocode) for all 0<=i<N, 0<=t<T:
307  
308 $alpha(i,t) = log P( $o(0:t), q(t)==i | @theta )
309  
310 Note that the final-state probability vector $omega() is neither
311 passed to this function nor used in the computation, but
312 can be used to compute the final sequence probability for $o as:
313  
314 log P( $o | @theta ) = logsumover( $omega() + $alpha(:,t-1) )
315  
316 =pod
317  
318 Broadcasts over its inputs.
319  
320 =for bad
321  
322 C<hmmfw> does not process bad values.
323 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
324  
325 =cut
326  
327  
328  
329  
330 5 *hmmfw = \&PDL::hmmfw;
331  
332  
333  
334  
335  
336 #line 325 "HMM.pd"
337  
338 *hmmalpha = \&hmmfw;
339 #line 340 "HMM.pm"
340  
341  
342 =head2 hmmfwq
343  
344 =for sig
345  
346 Signature: (a(N,N); b(N,M); pi(N); o(T); oq(Q,T); [o]alphaq(Q,T))
347 Types: (float double ldouble)
348  
349 =for usage
350  
351 $alphaq = hmmfwq($a, $b, $pi, $o, $oq);
352 hmmfwq($a, $b, $pi, $o, $oq, $alphaq); # all arguments given
353 $alphaq = $a->hmmfwq($b, $pi, $o, $oq); # method call
354 $a->hmmfwq($b, $pi, $o, $oq, $alphaq);
355  
356 Compute constrained forward probability (alphaq) matrix
357 for input $o given model parameters
358 @theta = ($a, $b, $pi, $omega),
359 considering only the initial
360 non-negative state indices in $oq(:,t) for each observation $o(t).
361  
362 Output (pseudocode) for all 0<=qi<Q, 0<=t<T:
363  
364 $alphaq(qi,t) = log P( $o(0:t), q(t)==$oq(qi,t) | @theta )
365  
366 Note that the final-state probability vector $omega() is neither
367 passed to this function nor used in the computation, but
368 can be used to compute the final sequence probability for $o as:
369  
370 log P( $o | @theta ) = logsumover( $alphaq(:,t-1) + $omega($oqTi) )
371  
372 where:
373  
374 $oqTi = $oq(:,t-1)->where($oq(:,t-1)>=0)
375  
376 =pod
377  
378 Broadcasts over its inputs.
379  
380 =for bad
381  
382 C<hmmfwq> does not process bad values.
383 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
384  
385 =cut
386  
387  
388  
389  
390 0 *hmmfwq = \&PDL::hmmfwq;
391  
392  
393  
394  
395  
396 #line 423 "HMM.pd"
397  
398 *hmmalphaq = \&hmmfwq;
399 #line 400 "HMM.pm"
400  
401  
402 =head2 hmmbw
403  
404 =for sig
405  
406 Signature: (a(N,N); b(N,M); omega(N); o(T); [o]beta(N,T))
407 Types: (float double ldouble)
408  
409 =for usage
410  
411 $beta = hmmbw($a, $b, $omega, $o);
412 hmmbw($a, $b, $omega, $o, $beta); # all arguments given
413 $beta = $a->hmmbw($b, $omega, $o); # method call
414 $a->hmmbw($b, $omega, $o, $beta);
415  
416 Compute backward probability (beta) matrix
417 for input $o given model parameters
418 @theta = ($a, $b, $pi, $omega).
419  
420 Output (pseudocode) for all 0<=i<N, 0<=t<T:
421  
422 $beta(i,t) = log P( $o(t+1:T-1) | q(t)==i, @theta )
423  
424 Note that the initial-state probability vector $pi() is neither
425 passed to this function nor used in the computation, but
426 can be used to compute the final sequence probability for $o as:
427  
428 log P( $o | @theta ) = logsumover( $pi() + $b(:,$o(0)) + $beta(:,0) )
429  
430 =pod
431  
432 Broadcasts over its inputs.
433  
434 =for bad
435  
436 C<hmmbw> does not process bad values.
437 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
438  
439 =cut
440  
441  
442  
443  
444 5 *hmmbw = \&PDL::hmmbw;
445  
446  
447  
448  
449  
450 #line 497 "HMM.pd"
451  
452 *hmmbeta = \&hmmbw;
453 #line 454 "HMM.pm"
454  
455  
456 =head2 hmmbwq
457  
458 =for sig
459  
460 Signature: (a(N,N); b(N,M); omega(N); o(T); oq(Q,T); [o]betaq(Q,T))
461 Types: (float double ldouble)
462  
463 =for usage
464  
465 $betaq = hmmbwq($a, $b, $omega, $o, $oq);
466 hmmbwq($a, $b, $omega, $o, $oq, $betaq); # all arguments given
467 $betaq = $a->hmmbwq($b, $omega, $o, $oq); # method call
468 $a->hmmbwq($b, $omega, $o, $oq, $betaq);
469  
470 Compute constrained backward probability (betaq) matrix
471 for input $o given model parameters
472 @theta = ($a, $b, $pi, $omega),
473 considering only the initial non-negative state indices in $oq(:,t) for
474 each observation $o(t).
475  
476 Output (pseudocode) for all 0<=qi<Q, 0<=t<T:
477  
478 $betaq(qi,t) = log P( $o(t+1:T-1) | q(t)==$oq(qi,t), @theta )
479  
480 Note that the initial-state probability vector $pi() is neither
481 passed to this function nor used in the computation, but
482 can be used to compute the final sequence probability for $o as:
483  
484 log P( $o | @theta ) = logsumover( $betaq(:,0) + $pi($oq0i) + $b($oq0i,$o(0)) )
485  
486 where:
487  
488 $oq0i = $oq(:,0)->where( $oq(:,0) >= 0 );
489  
490 =pod
491  
492 Broadcasts over its inputs.
493  
494 =for bad
495  
496 C<hmmbwq> does not process bad values.
497 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
498  
499 =cut
500  
501  
502  
503  
504 0 *hmmbwq = \&PDL::hmmbwq;
505  
506  
507  
508  
509  
510 #line 571 "HMM.pd"
511  
512 *hmmbetaq = \&hmmbwq;
513  
514 #line 580 "HMM.pd"
515  
516 =pod
517  
518 =head1 Parameter Estimation
519  
520 =head2 hmmexpect0
521  
522 =for sig
523  
524 Signature: (a(N,N); b(N,M); pi(N); omega(N); [o]ea(N,N); [o]eb(N,M); [o]epi(N); [o]eomega(N))
525  
526 Initializes expectation matrices $ea(), $eb() and $epi() to logzero().
527 For use with hmmexpect().
528  
529 =cut
530  
531 sub hmmexpect0 {
532 my ($a,$b,$pi,$omega, $ea,$eb,$epi,$eomega) = @_;
533  
534 $ea = zeroes($a->type, $a->dims) if (!defined($ea));
535 $eb = zeroes($b->type, $b->dims) if (!defined($eb));
536 $epi = zeroes($pi->type, $pi->dims) if (!defined($epi));
537 $eomega = zeroes($omega->type, $omega->dims) if (!defined($eomega));
538  
539 $ea .= PDL::logzero();
540 $eb .= PDL::logzero();
541 $epi .= PDL::logzero();
542 $eomega .= PDL::logzero();
543  
544 return ($ea,$eb,$epi,$eomega);
545 }
546 #line 547 "HMM.pm"
547  
548  
549 =head2 hmmexpect
550  
551 =for sig
552  
553 Signature: (a(N,N); b(N,M); pi(N); omega(N); o(T); alpha(N,T); beta(N,T); [o]ea(N,N); [o]eb(N,M); [o]epi(N); [o]eomega(N))
554 Types: (float double ldouble)
555  
556 =for usage
557  
558 ($ea, $eb, $epi, $eomega) = hmmexpect($a, $b, $pi, $omega, $o, $alpha, $beta);
559 hmmexpect($a, $b, $pi, $omega, $o, $alpha, $beta, $ea, $eb, $epi, $eomega); # all arguments given
560 ($ea, $eb, $epi, $eomega) = $a->hmmexpect($b, $pi, $omega, $o, $alpha, $beta); # method call
561 $a->hmmexpect($b, $pi, $omega, $o, $alpha, $beta, $ea, $eb, $epi, $eomega);
562  
563 Compute partial Baum-Welch re-estimation of the model @theta = ($a, $b, $pi, $omega)
564 for the observation sequence $o() with forward- and backward-probability
565 matrices $alpha(), $beta(). Result is recorded as log pseudo-frequencies
566 in the expectation matrices $ea(), $eb(), $epi(), and $eomega(), which are required parameters,
567 and should have been initialized (e.g. by L</hmmexpect0>()) before calling this function.
568  
569 Can safely be called sequentially for incremental reestimation.
570  
571 =pod
572  
573 Broadcasts over its inputs.
574  
575 =for bad
576  
577 C<hmmexpect> does not process bad values.
578 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
579  
580 =cut
581  
582  
583  
584  
585 5 *hmmexpect = \&PDL::hmmexpect;
586  
587  
588  
589  
590  
591  
592 =head2 hmmexpectq
593  
594 =for sig
595  
596 Signature: (a(N,N); b(N,M); pi(N); omega(N); o(T); oq(Q,T); alphaq(Q,T); betaq(Q,T); [o]ea(N,N); [o]eb(N,M); [o]epi(N); [o]eomega(N))
597 Types: (float double ldouble)
598  
599 =for usage
600  
601 ($ea, $eb, $epi, $eomega) = hmmexpectq($a, $b, $pi, $omega, $o, $oq, $alphaq, $betaq);
602 hmmexpectq($a, $b, $pi, $omega, $o, $oq, $alphaq, $betaq, $ea, $eb, $epi, $eomega); # all arguments given
603 ($ea, $eb, $epi, $eomega) = $a->hmmexpectq($b, $pi, $omega, $o, $oq, $alphaq, $betaq); # method call
604 $a->hmmexpectq($b, $pi, $omega, $o, $oq, $alphaq, $betaq, $ea, $eb, $epi, $eomega);
605  
606 Compute constrained partial Baum-Welch re-estimation of the model @theta = ($a, $b, $pi, $omega)
607 for the observation sequence $o(),
608 with constrained forward- and backward-probability
609 matrices $alphaq(), $betaq(),
610 considering only the initial non-negative state
611 indices in $oq(:,t) for observation $o(t).
612 Result is recorded as log pseudo-frequencies
613 in the expectation matrices $ea(), $eb(), $epi(), and $eomega(), which are required parameters,
614 and should have been initialized (e.g. by L</hmmexpect0>()) before calling this function.
615  
616 Can safely be called sequentially for incremental reestimation.
617  
618 =pod
619  
620 Broadcasts over its inputs.
621  
622 =for bad
623  
624 C<hmmexpectq> does not process bad values.
625 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
626  
627 =cut
628  
629  
630  
631  
632 0 *hmmexpectq = \&PDL::hmmexpectq;
633  
634  
635  
636  
637  
638 #line 789 "HMM.pd"
639  
640 =pod
641  
642 =head2 hmmmaximize
643  
644 =for sig
645  
646 Signature: (Ea(N,N); Eb(N,M); Epi(N); Eomega(N); [o]ahat(N,N); [o]bhat(N,M); [o]pihat(N); [o]omegahat(N));
647  
648 Maximizes expectation values from $Ea(), $Eb(), $Epi(), and $Eomega()
649 to log-probability matrices $ahat(), $bhat(), $pihat(), and $omegahat().
650 Can also be used to compile a maximum-likelihood model
651 from log-frequency matrices.
652  
653 =cut
654  
655 sub hmmmaximize {
656 my ($ea,$eb,$epi,$eomega, $ahat,$bhat,$pihat,$omegahat) = @_;
657  
658 $ahat = zeroes($ea->type, $ea->dims) if (!defined($ahat));
659 $bhat = zeroes($eb->type, $eb->dims) if (!defined($bhat));
660 $pihat = zeroes($epi->type, $epi->dims) if (!defined($pihat));
661 $omegahat = zeroes($eomega->type, $eomega->dims) if (!defined($omegahat));
662  
663 my $easumover = $ea->xchg(0,1)->logsumover->inplace->logadd($eomega);
664  
665 $ahat .= $ea - $easumover;
666 $bhat .= $eb - $eb->xchg(0,1)->logsumover;
667 $pihat .= $epi - $epi->logsumover;
668 $omegahat .= $eomega - $easumover;
669  
670 return ($ahat,$bhat,$pihat,$omegahat);
671 }
672  
673 #line 830 "HMM.pd"
674  
675 =pod
676  
677 =head1 Sequence Analysis
678  
679 =cut
680 #line 681 "HMM.pm"
681  
682  
683 =head2 hmmviterbi
684  
685 =for sig
686  
687 Signature: (a(N,N); b(N,M); pi(N); o(T); [o]delta(N,T); int [o]psi(N,T))
688 Types: (float double ldouble)
689  
690 =for usage
691  
692 ($delta, $psi) = hmmviterbi($a, $b, $pi, $o);
693 hmmviterbi($a, $b, $pi, $o, $delta, $psi); # all arguments given
694 ($delta, $psi) = $a->hmmviterbi($b, $pi, $o); # method call
695 $a->hmmviterbi($b, $pi, $o, $delta, $psi);
696  
697 Computes Viterbi algorithm trellises $delta() and $psi() for the
698 observation sequence $o() given the model parameters @theta = ($a,$b,$pi,$omega).
699  
700 Outputs:
701  
702 Probability matrix $delta(): log probability of best path to state $j at time $t:
703  
704 $delta(j,t) = max_{q(0:t)} log P( $o(0:t), q(0:t-1), $q(t)==j | @theta )
705  
706 Path backtrace matrix $psi(): best predecessor for state $j at time $t:
707  
708 $psi(j,t) = arg_{q(t-1)} max_{q(0:t)} P( $o(0:t), q(0:t-1), $q(t)==j | @theta )
709  
710 Note that if you are using termination probabilities $omega(),
711 then in order to find the most likely final state, you need to
712 compute the contribution of $omega() yourself, which is easy
713 to do:
714  
715 $best_final_q = maximum_ind($delta->slice(",-1") + $omega);
716  
717 =pod
718  
719 Broadcasts over its inputs.
720  
721 =for bad
722  
723 C<hmmviterbi> does not process bad values.
724 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
725  
726 =cut
727  
728  
729  
730  
731 5 *hmmviterbi = \&PDL::hmmviterbi;
732  
733  
734  
735  
736  
737  
738 =head2 hmmviterbiq
739  
740 =for sig
741  
742 Signature: (a(N,N); b(N,M); pi(N); o(T); oq(Q,T); [o]deltaq(Q,T); indx [o]psiq(Q,T))
743 Types: (float double ldouble)
744  
745 =for usage
746  
747 ($deltaq, $psiq) = hmmviterbiq($a, $b, $pi, $o, $oq);
748 hmmviterbiq($a, $b, $pi, $o, $oq, $deltaq, $psiq); # all arguments given
749 ($deltaq, $psiq) = $a->hmmviterbiq($b, $pi, $o, $oq); # method call
750 $a->hmmviterbiq($b, $pi, $o, $oq, $deltaq, $psiq);
751  
752 Computes constrained Viterbi algorithm trellises $deltaq() and $psiq() for the
753 observation sequence $o() given the model parameters @theta = ($a,$b,$pi,$omega),
754 considering only the initial non-negative state indices $oq(:,t) for each
755 observarion $o(t).
756  
757 Outputs:
758  
759 Constrained probability matrix $deltaq(): log probability of best path to state $oq(j,t) at time $t:
760  
761 $deltaq(j,t) = max_{j(0:t)} log P( $o(0:t), q(0:t-1)==$oq(:,j(0:t-1)), q(t)==$oq(j,t) | @theta )
762  
763 Constrained path backtrace matrix $psiq(): best predecessor index for state $oq(j,t) at time $t:
764  
765 $psiq(j,t) = arg_{j(t-1)} max_{j(0:t)} P( $o(0:t), q(0:t-1)=$oq(:,j(0:t-1)), q(t)==$oq(j,t) | @theta )
766  
767 Note that if you are using termination probabilities $omega(),
768 then in order to find the most likely final state, you need to
769 compute the contribution of $omega() yourself, which is quite easy
770 to do:
771  
772 $best_final_j = maximum_ind($deltaq->slice(",-1") + $omega->index($oq->slice(",(-1)")))
773  
774 =pod
775  
776 Broadcasts over its inputs.
777  
778 =for bad
779  
780 C<hmmviterbiq> does not process bad values.
781 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
782  
783 =cut
784  
785  
786  
787  
788 0 *hmmviterbiq = \&PDL::hmmviterbiq;
789  
790  
791  
792  
793  
794  
795 =head2 hmmpath
796  
797 =for sig
798  
799 Signature: (psi(N,T); indx qfinal(); indx [o]path(T))
800 Types: (float double ldouble)
801  
802 =for usage
803  
804 $path = hmmpath($psi, $qfinal);
805 hmmpath($psi, $qfinal, $path); # all arguments given
806 $path = $psi->hmmpath($qfinal); # method call
807 $psi->hmmpath($qfinal, $path);
808  
809 Computes best-path backtrace $path() for the final state $qfinal()
810 from completed Viterbi trellis $psi().
811  
812 Outputs:
813  
814 Path backtrace $path(): state (in best sequence) at time $t:
815  
816 $path(t) = arg_{q(t)} max_{q(0:T-1)} log P( $o(), q(0:T-2), $q(T-1)==$qfinal() | @theta )
817  
818 This even threads over multiple final states, if specified,
819 so you can align paths to their final states just by calling:
820  
821 $bestpaths = hmmpath($psi, sequence($N));
822  
823 Note that $path(T-1) == $qfinal(): yes, this is redundant,
824 but also tends to be quite convenient.
825  
826 =pod
827  
828 Broadcasts over its inputs.
829  
830 =for bad
831  
832 C<hmmpath> does not process bad values.
833 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
834  
835 =cut
836  
837  
838  
839  
840 5 *hmmpath = \&PDL::hmmpath;
841  
842  
843  
844  
845  
846  
847 =head2 hmmpathq
848  
849 =for sig
850  
851 Signature: (indx oq(Q,T); psiq(Q,T); indx qfinalq(); indx [o]path(T))
852 Types: (float double ldouble)
853  
854 =for usage
855  
856 $path = hmmpathq($oq, $psiq, $qfinalq);
857 hmmpathq($oq, $psiq, $qfinalq, $path); # all arguments given
858 $path = $oq->hmmpathq($psiq, $qfinalq); # method call
859 $oq->hmmpathq($psiq, $qfinalq, $path);
860  
861 Computes constrained best-path backtrace $path() for the final state index $qfinalq()
862 from completed constrained Viterbi trellis $psiq().
863  
864 Outputs:
865  
866 Path backtrace $path(): state (in best sequence) at time $t:
867  
868 $path(t) = arg_{q(t)} max_{q(0:T-1)} log P( $o(), q(0:T-2), $q(T-1)==$oq($qfinalq(),T-1) | @theta )
869  
870 This is really just a convenience method for dealing with constrained
871 lookup -- the same thing can be accomplished using hmmpath() and
872 some PDL index magic.
873  
874 =pod
875  
876 Broadcasts over its inputs.
877  
878 =for bad
879  
880 C<hmmpathq> does not process bad values.
881 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
882  
883 =cut
884  
885  
886  
887  
888 5 *hmmpathq = \&PDL::hmmpathq;
889  
890  
891  
892  
893  
894 #line 1132 "HMM.pd"
895  
896 ##---------------------------------------------------------------------
897 =pod
898  
899 =head1 COMMON PARAMETERS
900  
901 HMMs are specified by parameters $a(N,N), $b(N,M), $pi(N), and $omega(N);
902 input sequences are represented as vectors $o(T) of integer values in the range [0..M-1],
903 where the following notational conventions are used:
904  
905 =over 4
906  
907 =item States:
908  
909 The model has $N states, denoted $q,
910 0 <= $q < $N.
911  
912 =item Alphabet:
913  
914 The input- (aka "observation-") alphabet of the model has $M elements,
915 denoted $o(t), 0 <= $o(t) < $M.
916  
917 =item Time indices:
918  
919 Time indices are denoted $t,
920 1 <= $t < $T.
921  
922 =item Input Sequences:
923  
924 Input- (aka "observation-") sequences are represented as vectors of
925 of length $T whose component values are in the range [0..M-1],
926 i.e. alphabet indices.
927  
928 =item Initial Probabilities:
929  
930 The vector $pi(N) gives the (log) initial state probability distribution:
931  
932 $pi(i) = log P( $q(0)==i )
933  
934 =item Final Probabilities:
935  
936 The vector $omega(N) gives the (log) final state probability distribution:
937  
938 $omega(i) = log P( $q($T)==i )
939  
940 This parameter is a nonstandard extension.
941 You can simulate the behavior of more traditional definitions
942 (such as that given in Rabiner (1989)) by setting:
943  
944 $omega = zeroes($N);
945  
946 wherever it is required.
947  
948 =item Arc Probabilities:
949  
950 The matrix $a(N,N) gives the (log) conditional state-transition probability distribution:
951  
952 $a(i,j) = log P( $q(t+1)==j | $q(t)==i )
953  
954 =item Emission Probabilities:
955  
956 The matrix $b(N,M) gives the (log) conditional symbol emission probability:
957  
958 $b(j,o) = log P( $o(t)==o | $q(t)==j )
959  
960 =back
961  
962 =cut
963  
964 ##---------------------------------------------------------------------
965 =pod
966  
967 =head1 ACKNOWLEDGEMENTS
968  
969 Perl by Larry Wall.
970  
971 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
972  
973 Implementation based largely on the formulae in:
974 L. E. Rabiner, "A tutorial on Hidden Markov Models and selected
975 applications in speech recognition," Proceedings of the IEEE 77:2,
976 Februrary, 1989, pages 257--286.
977  
978 =cut
979  
980 ##----------------------------------------------------------------------
981 =pod
982  
983 =head1 KNOWN BUGS
984  
985 Probably many.
986  
987 =cut
988  
989 ##---------------------------------------------------------------------
990 =pod
991  
992 =head1 AUTHOR
993  
994 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
995  
996 =head2 Copyright Policy
997  
998 Copyright (C) 2005-2023 by Bryan Jurish. All rights reserved.
999  
1000 This package is free software, and entirely without warranty.
1001 You may redistribute it and/or modify it under the same terms
1002 as Perl itself.
1003  
1004 =head1 SEE ALSO
1005  
1006 perl(1), PDL(3perl).
1007  
1008 =cut
1009 #line 1010 "HMM.pm"
1010  
1011 # Exit with OK status
1012  
1013 5 1;
« pp-logdiff.c ↑ Summary