Accord.Statistics.Models.Markov.HiddenMarkovModel.Decode C# (CSharp) Метод

Decode() публичный Метод

Calculates the most likely sequence of hidden states that produced the given observation sequence.
Decoding problem. Given the HMM M = (A, B, pi) and the observation sequence O = {o1,o2, ..., oK}, calculate the most likely sequence of hidden states Si that produced this observation sequence O. This can be computed efficiently using the Viterbi algorithm.
public Decode ( int observations, bool logarithm, double &probability ) : int[]
observations int A sequence of observations.
logarithm bool True to return the log-likelihood, false to return /// the likelihood. Default is false (default is to return the likelihood).
probability double The state optimized probability.
Результат int[]
        public int[] Decode(int[] observations, bool logarithm, out double probability)
        {
            if (observations == null)
                throw new ArgumentNullException("observations");

            if (observations.Length == 0)
            {
                probability = 0.0;
                return new int[0];
            }


            // Viterbi-forward algorithm.
            int T = observations.Length;
            int states = States;
            int minState;
            double minWeight;
            double weight;

            double[] pi = Probabilities;
            double[,] A = Transitions;

            var s = new int[states,T];
            var a = new double[states,T];


            // Base
            for (int i = 0; i < states; i++)
                a[i, 0] = -System.Math.Log(pi[i]) - System.Math.Log(B[i, observations[0]]);

            // Induction
            for (int t = 1; t < T; t++)
            {
                int observation = observations[t];

                for (int j = 0; j < states; j++)
                {
                    minState = 0;
                    minWeight = a[0, t - 1] - System.Math.Log(A[0, j]);

                    for (int i = 1; i < states; i++)
                    {
                        weight = a[i, t - 1] - System.Math.Log(A[i, j]);

                        if (weight < minWeight)
                        {
                            minState = i;
                            minWeight = weight;
                        }
                    }

                    a[j, t] = minWeight - System.Math.Log(B[j, observation]);
                    s[j, t] = minState;
                }
            }


            // Find minimum value for time T-1
            minState = 0;
            minWeight = a[0, T - 1];

            for (int i = 1; i < states; i++)
            {
                if (a[i, T - 1] < minWeight)
                {
                    minState = i;
                    minWeight = a[i, T - 1];
                }
            }


            // Trackback
            var path = new int[T];
            path[T - 1] = minState;

            for (int t = T - 2; t >= 0; t--)
                path[t] = s[path[t + 1], t + 1];


            // Returns the sequence probability as an out parameter
            probability = logarithm ? -minWeight : System.Math.Exp(-minWeight);

            // Returns the most likely (Viterbi path) for the given sequence
            return path;
        }

Same methods

HiddenMarkovModel::Decode ( int observations, double &probability ) : int[]

Usage Example

Пример #1
0
    private void btnModel_Click(object sender, EventArgs e)
    {
      var transition = new double[,]
                                {
                           {2.0/8, 1.0/8, 2.0/8, 3.0/8},
                           {0, 0, 0, 1.0/8},
                           {0, 0, 0, 0},
                           {1, 0, 0, 0},
                         };

      var emission = new[,]
                       {
                         {2.0/8, 0, 0, 0, 0},
                         {0, 0, 0, 0, 0},
                         {0, 0, 1, 0, 0},
                         {0, 1, 0, 1, 0},
                       };

      var start = new double[] {1, 0, 0, 0};
      
      var hmm = new HiddenMarkovModel(transition, emission, start, false);

      var liklyhood = 0d;
      var x = hmm.Decode(new[] {1}, out liklyhood);
    }
All Usage Examples Of Accord.Statistics.Models.Markov.HiddenMarkovModel::Decode