Accord.Tests.Statistics.Models.Fields.ForwardBackwardAlgorithmTest.ForwardTest C# (CSharp) Méthode

ForwardTest() private méthode

private ForwardTest ( ) : void
Résultat void
        public void ForwardTest()
        {
            HiddenMarkovModel hmm = Accord.Tests.Statistics.Models.Markov.
                ForwardBackwardAlgorithmTest.CreateModel1();

            MarkovDiscreteFunction function = new MarkovDiscreteFunction(hmm);


            //                     G  G  C  A
            int[] observations = { 2, 2, 1, 0 };

            double[,] actual = Accord.Statistics.Models.Fields.
                ForwardBackwardAlgorithm.Forward(function.Factors[0], observations);

            var A = Matrix.Exp(hmm.Transitions);
            var B = Matrix.Exp(hmm.Emissions);
            var P = Matrix.Exp(hmm.Probabilities);

            double a00 = P[0] * B[0, 2];
            double a01 = P[1] * B[1, 2];

            double a10 = (a00 * A[0, 0] + a01 * A[1, 0]) * B[0, 2];
            double a11 = (a01 * A[1, 1] + a00 * A[0, 1]) * B[1, 2];

            double a20 = (a10 * A[0, 0] + a11 * A[1, 0]) * B[0, 1];
            double a21 = (a11 * A[1, 1] + a10 * A[0, 1]) * B[1, 1];

            double a30 = (a20 * A[0, 0] + a21 * A[1, 0]) * B[0, 0];
            double a31 = (a21 * A[1, 1] + a20 * A[0, 1]) * B[1, 0];

            Assert.AreEqual(a00, actual[0, 0], 1e-10);
            Assert.AreEqual(a01, actual[0, 1], 1e-10);

            Assert.AreEqual(a10, actual[1, 0], 1e-10);
            Assert.AreEqual(a11, actual[1, 1], 1e-10);

            Assert.AreEqual(a20, actual[2, 0], 1e-10);
            Assert.AreEqual(a21, actual[2, 1], 1e-10);

            Assert.AreEqual(a30, actual[3, 0], 1e-10);
            Assert.AreEqual(a31, actual[3, 1], 1e-10);

            foreach (double e in actual)
                Assert.IsFalse(double.IsNaN(e));

            double p = 0;
            for (int i = 0; i < hmm.States; i++)
                p += actual[observations.Length - 1, i];

            Assert.AreEqual(0.00384315, p, 1e-8);
            Assert.IsFalse(double.IsNaN(p));
        }