Accord.Tests.Statistics.GenericSequenceClassifierTest2.testThresholdModel C# (CSharp) Method

testThresholdModel() private static method

private static testThresholdModel ( int inputs, int outputs, int>.HiddenMarkovClassifier classifier, double likelihood ) : double
inputs int
outputs int
classifier int>.HiddenMarkovClassifier
likelihood double
return double
        private static double testThresholdModel(int[][] inputs, int[] outputs, HiddenMarkovClassifier<GeneralDiscreteDistribution, int> classifier, double likelihood)
        {
            var threshold = classifier.Threshold;

            Assert.AreEqual(classifier.Models[0].LogTransitions[0][0], threshold.LogTransitions[0][0], 1e-10);
            Assert.AreEqual(classifier.Models[0].LogTransitions[1][1], threshold.LogTransitions[1][1], 1e-10);
            Assert.AreEqual(classifier.Models[0].LogTransitions[2][2], threshold.LogTransitions[2][2], 1e-10);

            Assert.AreEqual(classifier.Models[1].LogTransitions[0][0], threshold.LogTransitions[3][3], 1e-10);
            Assert.AreEqual(classifier.Models[1].LogTransitions[1][1], threshold.LogTransitions[4][4], 1e-10);
            Assert.AreEqual(classifier.Models[1].LogTransitions[2][2], threshold.LogTransitions[5][5], 1e-10);

            for (int i = 0; i < 3; i++)
                for (int j = 3; j < 6; j++)
                    Assert.AreEqual(Double.NegativeInfinity, threshold.LogTransitions[i][j]);

            for (int i = 3; i < 6; i++)
                for (int j = 0; j < 3; j++)
                    Assert.AreEqual(Double.NegativeInfinity, threshold.LogTransitions[i][j]);

            Assert.IsFalse(Matrix.HasNaN(threshold.LogTransitions));


            classifier.Sensitivity = 0.5;

            // Will assert the models have learned the sequences correctly.
            for (int i = 0; i < inputs.Length; i++)
            {
                int expected = outputs[i];
                int actual = classifier.Decide(inputs[i]);
                likelihood = classifier.Probability(inputs[i]);
                Assert.AreEqual(expected, actual);
            }


            int[] r0 = new int[] { 1, 1, 0, 0, 2 };


            double logRejection;
            int c = classifier.Decide(r0);
            logRejection = classifier.Probability(r0);

            Assert.AreEqual(-1, c);
            Assert.AreEqual(0.99993993054384978, logRejection);

            logRejection = threshold.LogLikelihood(r0);
            Assert.AreEqual(-5.6367018741984483, logRejection);
            Assert.IsFalse(double.IsNaN(logRejection));

            threshold.Decode(r0, out logRejection);
            Assert.AreEqual(-8.1618027917853073, logRejection);
            Assert.IsFalse(double.IsNaN(logRejection));

            foreach (var model in classifier.Models)
            {
                double[,] A = model.LogTransitions.ToMatrix();

                for (int i = 0; i < A.GetLength(0); i++)
                {
                    double[] row = A.Exp().GetRow(i);
                    double sum = row.Sum();
                    Assert.AreEqual(1, sum, 1e-10);
                }
            }
            {
                double[,] A = classifier.Threshold.LogTransitions.ToMatrix();

                for (int i = 0; i < A.GetLength(0); i++)
                {
                    double[] row = A.GetRow(i);
                    double sum = row.Exp().Sum();
                    Assert.AreEqual(1, sum, 1e-6);
                }
            }
            return likelihood;
        }