How to calculate the P(Zk|X1:k) ?

128 views
Skip to first unread message

Ali Asghar Torabi

unread,
Dec 30, 2012, 4:07:11 PM12/30/12
to jahmm-...@googlegroups.com
Hi
I need to know if it's possible to calculate the probability of being in state Z at time k after observing vectors X1 to Xk.
As far as I'm concerned this is possible using Forward Algorithm. But the only function that I found in jahmm that doesn't need states of sequences is the function mostLikelyStateSequence(). Is this Ok if I use this function for calculating the P(Zk|X1:k)??
This is the program that I've written but it doesn't work correctly. It predict the state at time k wrong most of the time!
        Hmm<ObservationVector> hmm = new Hmm<>(2, new OpdfMultiGaussianFactory(
                96));
        hmm.setPi(0, 0.8737);
        hmm.setPi(1, 0.1262);
        double[] mean0 = new double[] { 3.6571e+02, ........
        double[] mean1 = new double[] { 6.9126e+02, 5.9727e+00, 5.2631e+..........
        double[][] covariance0 = ........
        double[][] covariance1 = ........
        hmm.setOpdf(0, new OpdfMultiGaussian(mean0, covariance0));
        hmm.setOpdf(1, new OpdfMultiGaussian(mean1, covariance1));
        hmm.setAij(0, 1, 0.01);
        hmm.setAij(0, 0, 0.99);
        hmm.setAij(1, 0, 0.2);
        hmm.setAij(1, 1, 0.8);
        .
        .
        . //Start loop
         ObservationVector v = new ObservationVector(features);
                        seq.add(v);
         .// end loop
         .
          int[] states = hmm.mostLikelyStateSequence(seq);


  1. Have I done all steps of creating and using the jahmm perfectly?
  2. Is the states[0] the number of a state with the biggest P(Zk|X1:k) ?
Thanks for any help.

Jean-Marc François

unread,
Dec 30, 2012, 4:46:53 PM12/30/12
to jahmm-...@googlegroups.com
Hi Ali Asghar,

Using Hmm#mostLikelyStateSequence introduces a bias as you only care about one sequence.

I think that what you are looking for is
ForwardBackwardCalculator#alphaElement(int t, int i);
the comment of the 'alpha' field should help you figuring out the exact calculation:
/* alpha[t][i] = P(O(1), O(2),..., O(t+1), i(t+1) = i+1 | hmm), that is the
probability of the beginning of the state sequence (up to time t+1)
with the (t+1)th state being i+1. */

Best,
JM



--
You received this message because you are subscribed to the Google Groups "Jahmm HMM library" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jahmm-discuss/-/8NlPrxFqPCcJ.
To post to this group, send email to jahmm-...@googlegroups.com.
To unsubscribe from this group, send email to jahmm-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jahmm-discuss?hl=en.

Ali Asghar Torabi

unread,
Dec 31, 2012, 8:10:33 AM12/31/12
to jahmm-...@googlegroups.com
Hi Jean,
Thanks for your help.
I've just tried ForwardBackwardCalculator#alphaElement. But all alpha values are Infinity. When I use ForwardBackwardScaledCalculator all values of alpha array are Nan!
This is an example of my feature vectors:

[0.006 0.02 0.251 0.001 0.073 0.03 0.112 0.152 0.225 0.298 0.005 0.005 0.005 0.005 0.059 0.118 0.157 0.225 0.003 0.004 0.005 0.007 0.562 0.571 0.002 0.006 0.193 0 0.337 0.051 0.054 0.072 0.117 0.189 0 0 0.001 0.001 0.009 0.027 0.072 0.135 0 0 0.001 0.001 0.448 0.491 0.004 0 0.02 0 0.199 0 0.001 0 0.358 0.223 0.071 0 0.091 0.009 0.071 0.017 0.154 0.011 0.224 0.015 0.001 0 0.002 0 0.003 0 0.003 0 0.056 0.001 0.093 0.002 0.115 0.005 0.183 0.008 0.002 0 0.004 0 0.003 0 0.003 0 0.501 0.028 0.534 0.03]

Each vector has 96 value but length of sequences are short, Every observation sequence has 4 or 5 vectors.
Why I get infinity values in alpha array!?
I wrote this code:

ForwardBackwardCalculator fbc = new ForwardBackwardCalculator(seq, hmm);
if (fbc.alphaElement(endOfSequence, 1) > fbc.alphaElement(endOfSequence, 0)) {
      // We are in state 1
} else {
      // We are in state 0
}

Is that right?
 

Jean-Marc François

unread,
Dec 31, 2012, 10:54:07 AM12/31/12
to jahmm-...@googlegroups.com
Hi,

This is most probably because the provided sequence could not be generated by the provided HMM.
Note that it might be tricky to learn the OPDF associated with high-dimension feature vectors such as those you use; looking if there's nothing wrong with the state's OPDF might be a good starting point to find what could be wrong.

Regards,
Jean-Marc

On Mon, Dec 31, 2012 at 2:10 PM, Ali Asghar Torabi <aliasgha...@gmail.com> wrote:
ForwardBackwardCalculator

Ali Asghar Torabi

unread,
Dec 31, 2012, 12:39:24 PM12/31/12
to jahmm-...@googlegroups.com
Hi
I haven't use any learning algorithm. I thought it is possible to use an hmm by only setting three main parameters Pi, Opdf and Aij.
As a result, I've already calculated mean and covariances of data vectors in MATLAB program and I only import them in Java.
Hmm<ObservationVector> hmm = new Hmm<>(2, new OpdfMultiGaussianFactory(24));
hmm.setPi(0, 0.8737);
hmm.setPi(1, 0.1262);
double[] mean0 = meanReader("mean0", 24);  // Reading mean of Gaussian model for state 0 from text file
double[] mean1 = meanReader("mean1", 24); 
// Reading mean of Gaussian model for state 1 ...
double[][] covariance0 = covReader("cov0", 24); 
// Reading covariance of Gaussian model for state 1 ...
double[][] covariance1 = covReader("cov1", 24);  // ...

hmm.setOpdf(0, new OpdfMultiGaussian(mean0, covariance0));
hmm.setOpdf(1, new OpdfMultiGaussian(mean1, covariance1));
hmm.setAij(0, 1, 0.01);
hmm.setAij(0, 0, 0.99);
hmm.setAij(1, 0, 0.2);
hmm.setAij(1, 1, 0.8);


Sorry if my question is not specifically related to jahmm library. I don't know if here is the place to ask this kind of general questions or not.
But I'd be very delighted if you help me in this regard. Can I use the hmm before I run any learning algorithm? If I have to run BaumWelch Learning, Should I generate necessary sequences using MarkovGenerator() or import those sequences that I've built initial hmm before?

Thanks for any help.

Ali Asghar Torabi

unread,
Dec 31, 2012, 1:51:02 PM12/31/12
to jahmm-...@googlegroups.com
As you might have seen I reduced the number of features of observation vectors to 24 but still get infinite values in alpha array .Although when the data is not normalized to the range 0..1 the infinity problem gets resolved, the predicted states are mostly wrong :(

Jean-Marc François

unread,
Jan 1, 2013, 8:25:06 AM1/1/13
to jahmm-...@googlegroups.com

Hi,

What you did seem perfectly fine.
If I were you I'd try to see if all the observations of the sequence have a probability higher than zero to match a state: for each state s, for each observation o, display hmm.getOpdf(s).probability(o).
This should give you a clue about what's going on.

Jean-Marc

--
You received this message because you are subscribed to the Google Groups "Jahmm HMM library" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jahmm-discuss/-/kUJNJGKjErIJ.

Benny

unread,
May 23, 2014, 10:12:32 PM5/23/14
to jahmm-...@googlegroups.com
Hi Jean

I keep getting a fileformatexception when I train the model using Baum Welch with sequences from a data file.
May you please assist me with the correct format for the data file

thank you
Reply all
Reply to author
Forward
0 new messages