HMM definitions

Floor HMM values (NormalizeHMM, FloorHMM)

NormalizeRows[mat_] :=  (#/(Plus @@ #)) & /@ mat ;

NormalizeColumns[mat_] := Transpose[NormalizeRows[mat // Transpose]] ;

NormalizeHMM[λ_] := Module[{a, b, π},  {a, b, π} = λ ;  {NormalizeRows[a], NormalizeColumns[b], NormalizeRows[{π}]//First} // N] ; 

FloorHMM[λ_, which_: {1, 1, 1}, floor_:10^-9] := Module[{a, b, π, af, bf, &# ... 60;[[i]], individualFloors[[3]]], {i, 1, n}] ; NormalizeHMM[{af, bf, πf}] ] ;

Random HMM model (RandomHMM)

(*structure = anything except 1 -- > fully connected ; structure = 1 & ...            --> left - to - right ; *)

RandomHMM[n_, L_, structure_] := Module[{a, b, π}, a = If[structureᡫ ... 62371;π = Table[Random[Real, {0, 1}], {n}] ; NormalizeHMM[{a, b, π}] ] ;

Generate Markov (and HMM) sequences (GenerateObservationSequence)

RowBox[{RowBox[{rr, :=, RowBox[{Random, [, RowBox[{Real, ,,  , RowBox[{{, RowBox[{0., ,,  , 1.}], }}]}], ]}]}], ;}]

TransformProbabilityVector[p_] := Table[Plus @@ Take[p, {1, i}], {i, 1, Length[p]}] // N ;

PickOne[ p_] := Module[{r}, r = rr ; (Position[(r <= #) & /@ TransformProbabilityVector[p], True] // Flatten) // First] ;

GenerateNextState[q_, A_] := PickOne[A[[q]]] ;

GenerateStateSequence[λ_, n_] := Module[{A, B, π},  {A, B, π} = λ ; NestList[GenerateNextState[#, A] &, PickOne[π], n - 1] ] ;

GenerateOneObservation[q_, B_] := PickOne[(B // Transpose)[[q]]] ;

GenerateObservationSequence[λ_, n_] := Module[{A, B, π, q},  {A, B,  ...  q = GenerateStateSequence[λ, n] ; GenerateOneObservation[#, B] & /@ q] ;

GenerateBothSequences[λ_, n_] := Module[{A, B, π, q},  {A, B, π ... tateSequence[λ, n] ; o = GenerateOneObservation[#, B] & /@ q ;  {q, o}] ;

Forward-backward algorithm (ForwardBackward)

(* defined for multiple observation sequences *)

RowBox[{RowBox[{ForwardBackward[o_, λ_], :=, , RowBox[{Module, [, RowBox[{{a, b,  ... T}], Table[βs[t, i], {i, 1, n}, {t, 1, T}], Table[c[t], {t, 1, T}]}}]}], , ]}]}], ;}]

Evaluation problem (LogProb, NormProb)

(* defined for single observation sequences *)

LogProb[o_, λ_] := Module[{αs, βs, c, T}, T = Length[o] ; ɯ ... , βs, c} = ForwardBackward[o, λ] ;  -Sum[Log[10, c[[t]]], {t, 1, T}] ] ;

NormProb[o_, λ_] := 10^(LogProb[o, λ]/Length[o]) ;

Decoding problem (DecodeHMM)

ArgMax[v_] := Position[v, Max[v]] // Flatten // First ;

(* defined for single observation sequence *)

DecodeHMM[o_, λ_] := Module[{a, b, π, ϕ, ψ, q, n, T},  {a, ... Do[q[t] = ψ[t + 1, q[t + 1]], {t, T - 1, 1, -1}] ; Table[q[t], {t, 1, T}] ] ;

Training problem (TrainHMM)

convHMM[param1_, param2_] := Module[{df}, df = Flatten[param1] - Flatten[param2] ; Sqrt[(Plus @@ (df . df))/Length[df]] ] ;

(* defined for multiple observation sequences *)

RowBox[{RowBox[{TrainHMMOneStep[o_, λ_], :=, , RowBox[{Module, [, RowBox[{{fb, &# ... ], {i, 1, n}]}], ]}], ;, , NormalizeHMM[{anew, bnew, πnew}]}]}], , ]}]}], ;}]

(* defined for multiple observation sequences *)

TrainHMM[o_, λinitial_, max_, tst_:.001] := NestWhileList[TrainHMMOneStep[o, #] &, λinitial, (convHMM[#1, #2] >tst) &, 2, max] ;


Created by Mathematica  (October 20, 2003)