public void CoefficientsTest1() { double[][] inputs; int[] outputs; MultinomialLogisticRegressionTest.CreateInputOutputsExample2(out inputs, out outputs); var analysis = new MultinomialLogisticRegressionAnalysis(inputs, outputs); int inputCount = 5; int coeffCount = inputCount + 1; var mlr = analysis.regression; analysis.Iterations = 100; analysis.Tolerance = 1e-6; analysis.Compute(); foreach (var coefficient in analysis.Coefficients) { Assert.IsNotNull(coefficient.Analysis); Assert.IsNotNull(coefficient.Confidence); Assert.IsNotNull(coefficient.ConfidenceLower); Assert.IsNotNull(coefficient.ConfidenceUpper); Assert.IsNotNull(coefficient.Name); Assert.IsNotNull(coefficient.Class); Assert.IsNotNull(coefficient.StandardError); Assert.IsNotNull(coefficient.Value); } Assert.AreEqual(13, analysis.Coefficients.Count); Assert.AreEqual(2, analysis.CoefficientValues.Length); var class1 = analysis.CoefficientValues[0]; Assert.AreEqual(-11.774547061739975, class1[0], 1e-10); Assert.AreEqual(0.523813075806107, class1[1], 1e-10); Assert.AreEqual(0.36820307277024716, class1[2], 1e-10); Assert.AreEqual(0, class1[3], 1e-6); Assert.AreEqual(0, class1[4], 1e-6); Assert.AreEqual(0, class1[5], 1e-6); var class2 = analysis.CoefficientValues[1]; Assert.AreEqual(-22.721272157115514, class2[0], 1e-10); Assert.AreEqual(0.46593949381162203, class2[1], 1e-10); Assert.AreEqual(0.68590438098052586, class2[2], 1e-10); Assert.AreEqual(0, class2[3], 1e-6); Assert.AreEqual(0, class2[4], 1e-6); Assert.AreEqual(0, class2[5], 1e-6); }
public void RegressTest2() { Accord.Math.Random.Generator.Seed = 0; double[][] inputs; int[] outputs; MultinomialLogisticRegressionTest.CreateInputOutputsExample1(out inputs, out outputs); // Create an algorithm to estimate the regression var msgd = new MultinomialLogisticLearning <ConjugateGradient>(); // Now, we can iteratively estimate our model MultinomialLogisticRegression mlr = msgd.Learn(inputs, outputs); int[] predicted = mlr.Decide(inputs); double acc = new ZeroOneLoss(outputs).Loss(predicted); Assert.AreEqual(0.61088435374149663, acc, 1e-8); }
public void GradientTest() { double[][] inputs; int[] outputs; MultinomialLogisticRegressionTest.CreateInputOutputsExample1(out inputs, out outputs); // Create an algorithm to estimate the regression var msgd = new MultinomialLogisticLearning <ConjugateGradient>(); msgd.Method.MaxIterations = 1; msgd.Learn(inputs, outputs); int variables = inputs.Columns() * outputs.DistinctCount(); var fd = new FiniteDifferences(variables, msgd.crossEntropy); double[] probe = { 0.1, 0.2, 0.5, 0.6, 0.2, 0.1 }; double[] expected = fd.Compute(probe); double[] actual = msgd.crossEntropyGradient(probe); Assert.IsTrue(expected.IsEqual(actual, 1e-5)); }
public void ComputeTest2() { double[][] inputs; int[] outputs; MultinomialLogisticRegressionTest.CreateInputOutputsExample2(out inputs, out outputs); var analysis = new MultinomialLogisticRegressionAnalysis(inputs, outputs); int inputCount = 5; int outputCount = 3; int coeffCount = inputCount + 1; var mlr = analysis.regression; Assert.AreEqual(inputCount, mlr.Inputs); Assert.AreEqual(outputCount, mlr.Categories); Assert.AreEqual(inputCount, analysis.Inputs.Length); Assert.AreEqual(outputCount, analysis.OutputNames.Length); analysis.Iterations = 100; analysis.Tolerance = 1e-6; analysis.Compute(); Assert.AreEqual(outputCount - 1, analysis.CoefficientValues.Length); Assert.AreEqual(outputCount - 1, analysis.StandardErrors.Length); Assert.AreEqual(outputCount - 1, analysis.WaldTests.Length); Assert.AreEqual(outputCount - 1, analysis.Confidences.Length); for (int i = 0; i < analysis.CoefficientValues.Length; i++) { Assert.AreEqual(coeffCount, analysis.CoefficientValues[i].Length); Assert.AreEqual(coeffCount, analysis.StandardErrors[i].Length); Assert.AreEqual(coeffCount, analysis.WaldTests[i].Length); Assert.AreEqual(coeffCount, analysis.Confidences[i].Length); for (int j = 0; j < analysis.CoefficientValues[i].Length; j++) { Assert.IsFalse(double.IsNaN(analysis.CoefficientValues[i][j])); Assert.IsFalse(double.IsNaN(analysis.StandardErrors[i][j])); } } var coefficients = analysis.CoefficientValues; // brand 2 Assert.AreEqual(-11.774655, coefficients[0][0], 1e-3); // intercept Assert.AreEqual(0.523814, coefficients[0][1], 1e-3); // female Assert.AreEqual(0.368206, coefficients[0][2], 1e-3); // age // brand 3 Assert.AreEqual(-22.721396, coefficients[1][0], 1e-3); // intercept Assert.AreEqual(0.465941, coefficients[1][1], 1e-3); // female Assert.AreEqual(0.685908, coefficients[1][2], 1e-3); // age var standard = analysis.StandardErrors; Assert.AreEqual(-702.97, analysis.LogLikelihood, 1e-2); Assert.AreEqual(185.85, analysis.ChiSquare.Statistic, 1e-2); Assert.AreEqual(1405.9414080469473, analysis.Deviance, 1e-5); }
public void learn_test_2() { double[][] inputs; int[] outputs; MultinomialLogisticRegressionTest.CreateInputOutputsExample1(out inputs, out outputs); var analysis = new MultinomialLogisticRegressionAnalysis(); analysis.Iterations = 100; analysis.Tolerance = 1e-6; var regression = analysis.Learn(inputs, outputs); int inputCount = 2; int outputCount = 3; int coeffCount = inputCount + 1; var mlr = analysis.regression; Assert.AreEqual(inputCount, mlr.Inputs); Assert.AreEqual(outputCount, mlr.Categories); Assert.AreEqual(inputCount, analysis.Inputs.Length); Assert.AreEqual(outputCount, analysis.OutputNames.Length); Assert.AreEqual(outputCount - 1, analysis.CoefficientValues.Length); Assert.AreEqual(outputCount - 1, analysis.StandardErrors.Length); Assert.AreEqual(outputCount - 1, analysis.WaldTests.Length); Assert.AreEqual(outputCount - 1, analysis.Confidences.Length); for (int i = 0; i < analysis.CoefficientValues.Length; i++) { Assert.AreEqual(coeffCount, analysis.CoefficientValues[i].Length); Assert.AreEqual(coeffCount, analysis.StandardErrors[i].Length); Assert.AreEqual(coeffCount, analysis.WaldTests[i].Length); Assert.AreEqual(coeffCount, analysis.Confidences[i].Length); } var coefficients = analysis.CoefficientValues; // brand 2 Assert.AreEqual(-11.774655, coefficients[0][0], 1e-3); // intercept Assert.AreEqual(0.523814, coefficients[0][1], 1e-3); // female Assert.AreEqual(0.368206, coefficients[0][2], 1e-3); // age // brand 3 Assert.AreEqual(-22.721396, coefficients[1][0], 1e-3); // intercept Assert.AreEqual(0.465941, coefficients[1][1], 1e-3); // female Assert.AreEqual(0.685908, coefficients[1][2], 1e-3); // age var standard = analysis.StandardErrors; // Using the lower-bound approximation Assert.AreEqual(1.047378039787443, standard[0][0], 1e-6); Assert.AreEqual(0.153150051082552, standard[0][1], 1e-6); Assert.AreEqual(0.031640507386863, standard[0][2], 1e-6); Assert.AreEqual(1.047378039787443, standard[1][0], 1e-6); Assert.AreEqual(0.153150051082552, standard[1][1], 1e-6); Assert.AreEqual(0.031640507386863, standard[1][2], 1e-6); Assert.AreEqual(-702.97, analysis.LogLikelihood, 1e-2); Assert.AreEqual(185.85, analysis.ChiSquare.Statistic, 1e-2); Assert.AreEqual(1405.9414080469473, analysis.Deviance, 1e-4); var wald = analysis.WaldTests; Assert.AreEqual(-11.241995503283842, wald[0][0].Statistic, 1e-3); Assert.AreEqual(3.4202662152119889, wald[0][1].Statistic, 1e-4); Assert.AreEqual(11.637150673342207, wald[0][2].Statistic, 1e-3); Assert.AreEqual(-21.693553825772664, wald[1][0].Statistic, 1e-3); Assert.AreEqual(3.0423802097069097, wald[1][1].Statistic, 1e-4); Assert.AreEqual(21.678124991086548, wald[1][2].Statistic, 1e-3); }