public void LearnTest6()
{
Accord.Math.Tools.SetupGenerator(0);
// Continuous Markov Models can operate using any
// probability distribution, including discrete ones.
// In the following example, we will try to create a
// Continuous Hidden Markov Model using a discrete
// distribution to detect if a given sequence starts
// with a zero and has any number of ones after that.
double[][] sequences = new double[][]
{
new double[] { 0,1,1,1,1,0,1,1,1,1 },
new double[] { 0,1,1,1,0,1,1,1,1,1 },
new double[] { 0,1,1,1,1,1,1,1,1,1 },
new double[] { 0,1,1,1,1,1 },
new double[] { 0,1,1,1,1,1,1 },
new double[] { 0,1,1,1,1,1,1,1,1,1 },
new double[] { 0,1,1,1,1,1,1,1,1,1 },
};
// Create a new Hidden Markov Model with 3 states and
// a generic discrete distribution with two symbols
var hmm = HiddenMarkovModel.CreateGeneric(new Forward(3), 2);
// Try to fit the model to the data until the difference in
// the average log-likelihood changes only by as little as 0.0001
var teacher = new ViterbiLearning<GeneralDiscreteDistribution>(hmm)
{
Tolerance = 0.0001,
Iterations = 0,
FittingOptions = new GeneralDiscreteOptions()
{
UseLaplaceRule = true
}
};
double ll = teacher.Run(sequences);
// Calculate the probability that the given
// sequences originated from the model
double l1 = hmm.Evaluate(new double[] { 0, 1 }); // 0.613
double l2 = hmm.Evaluate(new double[] { 0, 1, 1, 1 }); // 0.500
// Sequences which do not start with zero have much lesser probability.
double l3 = hmm.Evaluate(new double[] { 1, 1 }); // 0.186
double l4 = hmm.Evaluate(new double[] { 1, 0, 0, 0 }); // 0.003
// Sequences which contains few errors have higher probability
// than the ones which do not start with zero. This shows some
// of the temporal elasticity and error tolerance of the HMMs.
double l5 = hmm.Evaluate(new double[] { 0, 1, 0, 1, 1, 1, 1, 1, 1 }); // 0.033
double l6 = hmm.Evaluate(new double[] { 0, 1, 1, 1, 1, 1, 1, 0, 1 }); // 0.026
double pl = System.Math.Exp(ll);
double p1 = System.Math.Exp(l1);
double p2 = System.Math.Exp(l2);
double p3 = System.Math.Exp(l3);
double p4 = System.Math.Exp(l4);
double p5 = System.Math.Exp(l5);
double p6 = System.Math.Exp(l6);
Assert.AreEqual(1.754393540912413, pl, 1e-6);
Assert.AreEqual(0.61368718756104801, p1, 1e-6);
Assert.AreEqual(0.50049466955818356, p2, 1e-6);
Assert.AreEqual(0.18643340385264684, p3, 1e-6);
Assert.AreEqual(0.00300262431355424, p4, 1e-6);
Assert.AreEqual(0.03338686211012481, p5, 1e-6);
Assert.AreEqual(0.02659161933179825, p6, 1e-6);
Assert.IsFalse(Double.IsNaN(ll));
Assert.IsFalse(Double.IsNaN(l1));
Assert.IsFalse(Double.IsNaN(l2));
Assert.IsFalse(Double.IsNaN(l3));
Assert.IsFalse(Double.IsNaN(l4));
Assert.IsFalse(Double.IsNaN(l5));
Assert.IsFalse(Double.IsNaN(l6));
Assert.IsTrue(l1 > l3 && l1 > l4);
Assert.IsTrue(l2 > l3 && l2 > l4);
}