public void LearnTest3()
{
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 },
};
// Creates a new Hidden Markov Model with 3 states
var hmm = CreateDiscrete(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 BaumWelchLearning<GeneralDiscreteDistribution, int>(hmm)
{
Topology = new Ergodic(3),
Tolerance = 0.0001
};
var hmm2 = teacher.Learn(sequences.ToInt32());
double ll = teacher.LogLikelihood;
// Calculate the probability that the given
// sequences originated from the model
double l1; hmm.Decode(new int[] { 0, 1 }, out l1); // 0.4999
double l2; hmm.Decode(new int[] { 0, 1, 1, 1 }, out l2); // 0.1145
double l3; hmm.Decode(new int[] { 1, 1 }, out l3); // 0.0000
double l4; hmm.Decode(new int[] { 1, 0, 0, 0 }, out l4); // 0.0000
double l5; hmm.Decode(new int[] { 0, 1, 0, 1, 1, 1, 1, 1, 1 }, out l5); // 0.0002
double l6; hmm.Decode(new int[] { 0, 1, 1, 1, 1, 1, 1, 0, 1 }, out l6); // 0.0002
ll = System.Math.Exp(ll);
l1 = System.Math.Exp(l1);
l2 = System.Math.Exp(l2);
l3 = System.Math.Exp(l3);
l4 = System.Math.Exp(l4);
l5 = System.Math.Exp(l5);
l6 = System.Math.Exp(l6);
Assert.AreEqual(1.2114235662225716, ll, 1e-4);
Assert.AreEqual(0.4999419764097881, l1, 1e-4);
Assert.AreEqual(0.1145702973735144, l2, 1e-4);
Assert.AreEqual(0.0000529972606821, l3, 1e-4);
Assert.AreEqual(0.0000000000000001, l4, 1e-4);
Assert.AreEqual(0.0002674509390361, l5, 1e-4);
Assert.AreEqual(0.0002674509390361, l6, 1e-4);
Assert.IsTrue(l1 > l3 && l1 > l4);
Assert.IsTrue(l2 > l3 && l2 > l4);
Assert.AreEqual(1, hmm.NumberOfInputs);
}