Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
GH-167: Posterior method has wrong signature in continuous HMMs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Jun 3, 2016
1 parent 6923698 commit 5b84858
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 97 deletions.
247 changes: 151 additions & 96 deletions Sources/Accord.Statistics/Models/Markov/Base/IHiddenMarkovModel.cs
@@ -1,96 +1,151 @@
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2016
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Models.Markov
{
using System;

/// <summary>
/// Common interface for Hidden Markov Models.
/// </summary>
///
public interface IHiddenMarkovModel
{

/// <summary>
/// Calculates the most likely sequence of hidden states
/// that produced the given observation sequence.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="observations">
/// A sequence of observations.</param>
/// <param name="logLikelihood">
/// The state optimized probability.</param>
/// <returns>
/// The sequence of states that most likely produced the sequence.
/// </returns>
///
int[] Decode(Array observations, out double logLikelihood);

/// <summary>
/// Calculates the probability that this model has generated the given sequence.
/// </summary>
/// <remarks>
/// Evaluation problem. Given the HMM M = (A, B, pi) and the observation
/// sequence O = {o1, o2, ..., oK}, calculate the probability that model
/// M has generated sequence O. This can be computed efficiently using the
/// Forward algorithm. </remarks>
/// <param name="observations">
/// A sequence of observations. </param>
/// <returns>
/// The probability that the given sequence has been generated by this model.
/// </returns>
///
double Evaluate(Array observations);

/// <summary>
/// Gets the number of states of this model.
/// </summary>
///
int States { get; }

/// <summary>
/// Gets the initial probabilities for this model.
/// </summary>
///
double[] Probabilities { get; }

/// <summary>
/// Gets the Transition matrix (A) for this model.
/// </summary>
///
double[,] Transitions { get; }

/// <summary>
/// Gets or sets a user-defined tag.
/// </summary>
///
object Tag { get; set; }

}

}
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2016
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Models.Markov
{
using System;

/// <summary>
/// Common interface for Hidden Markov Models.
/// </summary>
///
public interface IHiddenMarkovModel
{

/// <summary>
/// Calculates the most likely sequence of hidden states
/// that produced the given observation sequence.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="observations">
/// A sequence of observations.</param>
/// <param name="logLikelihood">
/// The state optimized probability.</param>
/// <returns>
/// The sequence of states that most likely produced the sequence.
/// </returns>
///
int[] Decode(Array observations, out double logLikelihood);

/// <summary>
/// Calculates the probability that this model has generated the given sequence.
/// </summary>
/// <remarks>
/// Evaluation problem. Given the HMM M = (A, B, pi) and the observation
/// sequence O = {o1, o2, ..., oK}, calculate the probability that model
/// M has generated sequence O. This can be computed efficiently using the
/// Forward algorithm. </remarks>
/// <param name="observations">
/// A sequence of observations. </param>
/// <returns>
/// The probability that the given sequence has been generated by this model.
/// </returns>
///
double Evaluate(Array observations);

/// <summary>
/// Gets the number of states of this model.
/// </summary>
///
int States { get; }

/// <summary>
/// Gets the initial probabilities for this model.
/// </summary>
///
double[] Probabilities { get; }

/// <summary>
/// Gets the Transition matrix (A) for this model.
/// </summary>
///
double[,] Transitions { get; }

/// <summary>
/// Gets or sets a user-defined tag.
/// </summary>
///
object Tag { get; set; }



/// <summary>
/// Calculates the probability of each hidden state for each
/// observation in the observation vector.
/// </summary>
///
/// <remarks>
/// If there are 3 states in the model, and the <paramref name="observations"/>
/// array contains 5 elements, the resulting vector will contain 5 vectors of
/// size 3 each. Each vector of size 3 will contain probability values that sum
/// up to one. By following those probabilities in order, we may decode those
/// probabilities into a sequence of most likely states. However, the sequence
/// of obtained states may not be valid in the model.
/// </remarks>
///
/// <param name="observations">A sequence of observations.</param>
///
/// <returns>A vector of the same size as the observation vectors, containing
/// the probabilities for each state in the model for the current observation.
/// If there are 3 states in the model, and the <paramref name="observations"/>
/// array contains 5 elements, the resulting vector will contain 5 vectors of
/// size 3 each. Each vector of size 3 will contain probability values that sum
/// up to one.</returns>
///
double[][] Posterior(Array observations);

/// <summary>
/// Calculates the probability of each hidden state for each observation
/// in the observation vector, and uses those probabilities to decode the
/// most likely sequence of states for each observation in the sequence
/// using the posterior decoding method. See remarks for details.
/// </summary>
///
/// <remarks>
/// If there are 3 states in the model, and the <paramref name="observations"/>
/// array contains 5 elements, the resulting vector will contain 5 vectors of
/// size 3 each. Each vector of size 3 will contain probability values that sum
/// up to one. By following those probabilities in order, we may decode those
/// probabilities into a sequence of most likely states. However, the sequence
/// of obtained states may not be valid in the model.
/// </remarks>
///
/// <param name="observations">A sequence of observations.</param>
/// <param name="path">The sequence of states most likely associated with each
/// observation, estimated using the posterior decoding method.</param>
///
/// <returns>A vector of the same size as the observation vectors, containing
/// the probabilities for each state in the model for the current observation.
/// If there are 3 states in the model, and the <paramref name="observations"/>
/// array contains 5 elements, the resulting vector will contain 5 vectors of
/// size 3 each. Each vector of size 3 will contain probability values that sum
/// up to one.</returns>
///
double[][] Posterior(Array observations, out int[] path);
}

}
Binary file modified Sources/Accord.Statistics/Models/Markov/HiddenMarkovModel.cs
Binary file not shown.
Expand Up @@ -525,7 +525,7 @@ public double[][] Posterior(Array observations)
/// size 3 each. Each vector of size 3 will contain probability values that sum
/// up to one.</returns>
///
public double[][] Posterior(int[] observations, out int[] path)
public double[][] Posterior(Array observations, out int[] path)
{
double[][] probabilities = Posterior(observations);

Expand Down

0 comments on commit 5b84858

Please sign in to comment.