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2,0,0,1,2,0,0,0,2,1,1,1,1,1,2,2,0,0,1,1,1,0,0,
2,0,1,1,0,2,2,0,0,0,1,1,1,1,1,1,2,1,1,0,2,0,0,0,1,1,
1,2,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,2,2,2,0,2,0,2,1,
2,1,0,2,1,2,1,0,0,2,1,1,1,1,0,0,0,1,2,0,2,2,1,2,1,1,
1,0,1,0,0,0,0,2,0,1,1,1,0,2,0,1,0,2,1,2,2,0,2,1,0,0,
2,1,2,2,0,2,1,2,1,2,0,0,0,1,2,1,2,2,1,0,0,0,1,1,2,0,
2,1,0,0,0,1,0,0,1,2,0,0,1,2,2,2,0,1,2,0,1,0,1,0,2,2,
0,2,0,1,1,0,1,1,1,2,2,0,0,0,0,0,1,1,0,0,2,0,0,1,0,0,
1,0,2,1,1,1,1,1,2,0,0,2,0,1,2,0,1,1,1,2,0,0,0,1,2,0,
0,0,2,2,1,1,1,0,1,1,0,2,2,0,1,2,2,1,1,1,2,1,0,2,0,0,
1,1,1,1,1,1,2,1,2,1,0,1,0,2,2,0,1,2,1,1,2,1,0,1,2,1
};
var teacher = new BaumWelchLearning(hmm)
{
Iterations = 650,
Tolerance = 0
};
double ll = teacher.Run(observation);
double[] pi = { 1.0, 0.0 };
double[,] A =
{
{ 0.7, 0.3 },
{ 0.5, 0.5 }
};
double[,] B =
{
{ 0.6, 0.1, 0.3 },
{ 0.1, 0.7, 0.2 }
};
var hmmA = Matrix.Exp(hmm.Transitions);
var hmmB = Matrix.Exp(hmm.Emissions);
var hmmP = Matrix.Exp(hmm.Probabilities);
Assert.IsTrue(Matrix.IsEqual(A, hmmA, atol: 0.1));
Assert.IsTrue(Matrix.IsEqual(B, hmmB, atol: 0.1));
Assert.IsTrue(Matrix.IsEqual(pi, hmmP));
}