I have created code to output observed values of a Hidden Markov Model. I see that 'hmmviterbi' would give you the most probable state sequence given an observed sequence, transmission matrix, and emission matrix. Is there a way to use this function if you don't have an emission matrix? My emission values are pulled from a normal distribution with mean dependent on 3 different states and standard deviation 1. If there is no way around to use 'hmmviterbi', how can you code on your own to produce the most probable state sequence? I am unsure how to deal with the infinite emission values. Here is my code for the initial HMM:

N = 3; %%the three states are [-1 0 1]
%A is transition prob matrix
A = [.99,.005,.005;.005,.990,.005;.005,.005,.990];
%pi is initial state vector
pi = [1/3,1/3,1/3];
%T is # of observations per simulation
T = 1000;
%n is # of simulations
n = 5;
%Allocate space for the state matrix
State = zeros(n,T);
Observe = zeros(n,T);
%loop over # of simulations

for i=1:1:n
    x = rand(1);
    if x <= (1/3)
        State(i,1) = 1;
    elseif x > (1/3) && x <= (2/3)
        State(i,1) = 2;
        State(i,1) = 3;
    if State(i,1) == 1
        b = -1;
    elseif State(i,1) == 2
        b = 0;
        b = 1;

    Observe(i,1)= normrnd(b,1);
for k=2:1:T
    State(i,k) = randsample(3, 1, true, A(State(i,k-1),:));
    if State(i,k) == 1
        c = -1;
    elseif State(i,k) == 2
        c = 0;
        c = 1;
    Observe(i,k)= normrnd(c,1);
for i = 1:1:n
    for k = 1:1:T
        if State(i,k)==1
        elseif State(i,k)==2

Indeed, it seems you cannot use the hmmviterbi function of Matlab with continuous observations. The algorithm of Viterbi is given on page 8 of this famous tutorial on HMM. Or you might find other documents easily with a google search.

As for your precise point on how to handle the emission values : in the continuous case, you do not have a transition matrix $B$, whose elements are the $b_{S_j}(O_i)$, representing probability of the (discrete) observation $O_i$ conditioned on the discrete state $S_j$. Instead, $b_{S_j}(O_i)$ will be taken as the probability density function of the gaussian distribution, evaluated at the (continuous) value $O_i$, with mean parametrized by the hidden state $S_j$ and standard-deviation of 1. This is done by normpdf in Matlab

(The good news in your problem is that you do not have to estimate the parameters!)

  • $\begingroup$ Hey! I am just getting back to this. I need help on how to handle "the mean parametrized by the hidden state". When coding the probability using "normpdf", I have an observation value, the standard deviation, but the mean chosen for each pass will be another probability or random choice? This part is confusing me, I do not know how to code correctly. $\endgroup$ May 5 '19 at 2:29
  • $\begingroup$ In the discrete case, you have an emission matrix $B$ which works just like the transition matrix $A$ in your code sample. In the continuous case, you have $b_{S_j}(O_i) = \mathcal{N}(O_i,\mu_{S_j},\sigma_{S_j})$, i.e. the gaussian distribution whose mean and standard-deviation depends on the hidden state, evaluated at point $O_i$. In matlab this would give, using your notations: normpdf(Observe(n,t), mu(State(n,t)), 1), for all simulations n for all instants t, where mu is a vector containing different means. $\endgroup$
    – TheCG
    May 5 '19 at 9:41
  • $\begingroup$ Oh! I was under the assumption I wasn't 'allowed' to access my State matrix. $\endgroup$ May 5 '19 at 19:00
  • $\begingroup$ Well, it depends which operations/algorithm you are working with, but the parameters of the gaussians do depend on the hidden states $\endgroup$
    – TheCG
    May 6 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.