This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-167: Posterior method has wrong signature in continuous HMMs.
- Loading branch information
1 parent
6923698
commit 5b84858
Showing
3 changed files
with
152 additions
and
97 deletions.
There are no files selected for viewing
247 changes: 151 additions & 96 deletions
247
Sources/Accord.Statistics/Models/Markov/Base/IHiddenMarkovModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
BIN
+6.62 KB
(110%)
Sources/Accord.Statistics/Models/Markov/HiddenMarkovModel.cs
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters