## 2692 days ago by macieksk

# Hidden Markov Models # http://cran.r-project.org/web/packages/HMM/index.html library(HMM)
# Initialise HMM hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.8,.2,.2,.8),2), emissionProbs=matrix(c(.6,.4,.4,.6),2)) print(hmm)
 ```\$States [1] "A" "B" \$Symbols [1] "L" "R" \$startProbs A B 0.5 0.5 \$transProbs to from A B A 0.8 0.2 B 0.2 0.8 \$emissionProbs symbols states L R A 0.6 0.4 B 0.4 0.6```
# Sequence of observations observations = c("L","L","R","R") # Calculate forward probablities logForwardProbabilities = forward(hmm,observations) print(exp(logForwardProbabilities)) ?forward
 ``` index states 1 2 3 4 A 0.3 0.168 0.0608 0.024448 B 0.2 0.088 0.0624 0.037248 forward package:HMM R Documentation _C_o_m_p_u_t_e_s _t_h_e _f_o_r_w_a_r_d _p_r_o_b_a_b_i_l_i_t_i_e_s _D_e_s_c_r_i_p_t_i_o_n: The ‘forward’-function computes the forward probabilities. The forward probability for state X up to observation at time k is defined as the probability of observing the sequence of observations e_1, ... ,e_k and that the state at time k is X. That is: ‘f[X,k] := Prob(E_1 = e_1, ... , E_k = e_k , X_k = X)’. Where ‘E_1...E_n = e_1...e_n’ is the sequence of observed emissions and ‘X_k’ is a random variable that represents the state at time ‘k’. _U_s_a_g_e: forward(hmm, observation) _A_r_g_u_m_e_n_t_s: hmm : A Hidden Markov Model. observation : A sequence of observations. _F_o_r_m_a_t: Dimension and Format of the Arguments. hmm A valid Hidden Markov Model, for example instantiated by ‘initHMM’. observation A vector of strings with the observations. _V_a_l_u_e: Return Value: forward : A matrix containing the forward probabilities. The probabilities are given on a logarithmic scale (natural logarithm). The first dimension refers to the state and the second dimension to time. _A_u_t_h_o_r(_s): Lin Himmelmann , Scientific Software Development _R_e_f_e_r_e_n_c_e_s: Lawrence R. Rabiner: A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE 77(2) p.257-286, 1989. _S_e_e _A_l_s_o: See ‘backward’ for computing the backward probabilities. _E_x_a_m_p_l_e_s: # Initialise HMM hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.8,.2,.2,.8),2), emissionProbs=matrix(c(.6,.4,.4,.6),2)) print(hmm) # Sequence of observations observations = c("L","L","R","R") # Calculate forward probablities logForwardProbabilities = forward(hmm,observations) print(exp(logForwardProbabilities)) ```

 `Error: could not find function "posterior"`
posterior(hmm, observations) ?posterior # This function computes the posterior probabilities of being in # state X at time k for a given sequence of observations and a given # Hidden Markov Model.
 ``` index states 1 2 3 4 A 0.6037344 0.56639 0.43361 0.3962656 B 0.3962656 0.43361 0.56639 0.6037344 posterior package:HMM R Documentation _C_o_m_p_u_t_e_s _t_h_e _p_o_s_t_e_r_i_o_r _p_r_o_b_a_b_i_l_i_t_i_e_s _f_o_r _t_h_e _s_t_a_t_e_s _D_e_s_c_r_i_p_t_i_o_n: This function computes the posterior probabilities of being in state X at time k for a given sequence of observations and a given Hidden Markov Model. _U_s_a_g_e: posterior(hmm, observation) _A_r_g_u_m_e_n_t_s: hmm : A Hidden Markov Model. observation : A sequence of observations. _F_o_r_m_a_t: Dimension and Format of the Arguments. hmm A valid Hidden Markov Model, for example instantiated by ‘initHMM’. observation A vector of observations. _D_e_t_a_i_l_s: The posterior probability of being in a state X at time k can be computed from the ‘forward’ and ‘backward’ probabilities: ‘ Ws(X_k = X | E_1 = e_1, ... , E_n = e_n) = f[X,k] * b[X,k] / Prob(E_1 = e_1, ... , E_n = e_n)’ Where ‘E_1...E_n = e_1...e_n’ is the sequence of observed emissions and ‘X_k’ is a random variable that represents the state at time ‘k’. _V_a_l_u_e: Return Values: posterior : A matrix containing the posterior probabilities. The first dimension refers to the state and the second dimension to time. _A_u_t_h_o_r(_s): Lin Himmelmann , Scientific Software Development _R_e_f_e_r_e_n_c_e_s: Lawrence R. Rabiner: A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE 77(2) p.257-286, 1989. _S_e_e _A_l_s_o: See ‘forward’ for computing the forward probabilities and ‘backward’ for computing the backward probabilities. _E_x_a_m_p_l_e_s: # Initialise HMM hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.8,.2,.2,.8),2), emissionProbs=matrix(c(.6,.4,.4,.6),2)) print(hmm) # Sequence of observations observations = c("L","L","R","R") # Calculate posterior probablities of the states posterior = posterior(hmm,observations) print(posterior) ```
# Initialise HMM hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.6,.4,.4,.6),2), emissionProbs=matrix(c(.6,.4,.4,.6),2)) print(hmm) # Sequence of observations observations = c("L","L","R","R") # Calculate Viterbi path viterbi = viterbi(hmm,observations)
 ```\$States [1] "A" "B" \$Symbols [1] "L" "R" \$startProbs A B 0.5 0.5 \$transProbs to from A B A 0.6 0.4 B 0.4 0.6 \$emissionProbs symbols states L R A 0.6 0.4 B 0.4 0.6```
print(viterbi)
 `[1] "A" "A" "B" "B"`
?viterbi # The Viterbi-algorithm computes the most probable path of states # for a sequence of observations for a given Hidden Markov Model.
 ```viterbi package:HMM R Documentation _C_o_m_p_u_t_e_s _t_h_e _m_o_s_t _p_r_o_b_a_b_l_e _p_a_t_h _o_f _s_t_a_t_e_s _D_e_s_c_r_i_p_t_i_o_n: The Viterbi-algorithm computes the most probable path of states for a sequence of observations for a given Hidden Markov Model. _U_s_a_g_e: viterbi(hmm, observation) _A_r_g_u_m_e_n_t_s: hmm : A Hidden Markov Model. observation : A sequence of observations. _F_o_r_m_a_t: Dimension and Format of the Arguments. hmm A valid Hidden Markov Model, for example instantiated by ‘initHMM’. observation A vector of observations. _V_a_l_u_e: Return Value: viterbiPath : A vector of strings, containing the most probable path of states. _A_u_t_h_o_r(_s): Lin Himmelmann , Scientific Software Development _R_e_f_e_r_e_n_c_e_s: Lawrence R. Rabiner: A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE 77(2) p.257-286, 1989. _E_x_a_m_p_l_e_s: # Initialise HMM hmm = initHMM(c("A","B"), c("L","R"), transProbs=matrix(c(.6,.4,.4,.6),2), emissionProbs=matrix(c(.6,.4,.4,.6),2)) print(hmm) # Sequence of observations observations = c("L","L","R","R") # Calculate Viterbi path viterbi = viterbi(hmm,observations) print(viterbi) ```
 ```--2012-04-24 12:42:46-- http://sage.mimuw.edu.pl/home/pub/64/cells/88/jap.nucl.Rdata Translacja sage.mimuw.edu.pl... 193.0.109.27 Łączenie się z sage.mimuw.edu.pl|193.0.109.27|:80... połączono. Żądanie HTTP wysłano, oczekiwanie na odpowiedź... 200 OK Długość: 1770659 (1,7M) [text/plain] Zapis do: `data/jap.nucl.Rdata' 0% [ ] 0 --.-K/s 100%[======================================>] 1.770.659 --.-K/s w 0,009s 2012-04-24 12:42:46 (185 MB/s) - zapisano `data/jap.nucl.Rdata' [1770659/1770659]```
 ``` [1] "fit1" "fit2" "fit3" [4] "fit4" "fit5" "fit6" [7] "fit7" "hmm" "logForwardProbabilities" [10] "nucl.df" "observations" "viterbi" ```
 ``` akt nast nnast akt.pir nast.pir nnast.pir nnnast nnnast.pir 1 G A A FALSE FALSE FALSE C TRUE 2 A A C FALSE FALSE TRUE C TRUE 3 A C C FALSE TRUE TRUE C TRUE 4 C C C TRUE TRUE TRUE T TRUE 5 C C T TRUE TRUE TRUE A FALSE 6 C T A TRUE TRUE FALSE A FALSE```
 ```baumWelch package:HMM R Documentation _I_n_f_e_r_r_i_n_g _t_h_e _p_a_r_a_m_e_t_e_r_s _o_f _a _H_i_d_d_e_n _M_a_r_k_o_v _M_o_d_e_l _v_i_a _t_h_e _B_a_u_m-_W_e_l_c_h _a_l_g_o_r_i_t_h_m _D_e_s_c_r_i_p_t_i_o_n: For an initial Hidden Markov Model (HMM) and a given sequence of observations, the Baum-Welch algorithm infers optimal parameters to the HMM. Since the Baum-Welch algorithm is a variant of the Expectation-Maximisation algorithm, the algorithm converges to a local solution which might not be the global optimum. _U_s_a_g_e: baumWelch(hmm, observation, maxIterations=100, delta=1E-9, pseudoCount=0) _A_r_g_u_m_e_n_t_s: hmm : A Hidden Markov Model. observation : A sequence of observations. maxIterations : The maximum number of iterations in the Baum-Welch algorithm. delta : Additional termination condition, if the transition and emission matrices converge, before reaching the maximum number of iterations (‘maxIterations’). The difference of transition and emission parameters in consecutive iterations must be smaller than ‘delta’ to terminate the algorithm. pseudoCount : Adding this amount of pseudo counts in the estimation-step of the Baum-Welch algorithm. _F_o_r_m_a_t: Dimension and Format of the Arguments. hmm A valid Hidden Markov Model, for example instantiated by ‘initHMM’. observation A vector of observations. _V_a_l_u_e: Return Values: hmm : The inferred HMM. The representation is equivalent to the representation in ‘initHMM’. difference : Vector of differences calculated from consecutive transition and emission matrices in each iteration of the Baum-Welch procedure. The difference is the sum of the distances between consecutive transition and emission matrices in the L2-Norm. _A_u_t_h_o_r(_s): Lin Himmelmann , Scientific Software Development _R_e_f_e_r_e_n_c_e_s: For details see: Lawrence R. Rabiner: A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE 77(2) p.257-286, 1989. _S_e_e _A_l_s_o: See ‘viterbiTraining’. _E_x_a_m_p_l_e_s: # Initial HMM hmm = initHMM(c("A","B"),c("L","R"), transProbs=matrix(c(.9,.1,.1,.9),2), emissionProbs=matrix(c(.5,.51,.5,.49),2)) print(hmm) # Sequence of observation a = sample(c(rep("L",100),rep("R",300))) b = sample(c(rep("L",300),rep("R",100))) observation = c(a,b) # Baum-Welch bw = baumWelch(hmm,observation,10) print(bw\$hmm) ```