public void LinearLogisticRegression() { // do a set of logistic regression fits // make sure not only that the fit parameters are what they should be, but that their variances/covariances are as returned Random rng = new Random(314159); // define logistic parameters double a0 = 1.0; double b0 = -1.0 / 2.0; //double a0 = -0.5; double b0 = 2.0; // keep track of sample of returned a and b fit parameters BivariateSample ps = new BivariateSample(); // also keep track of returned covariance estimates // since these vary slightly from fit to fit, we will average them double caa = 0.0; double cbb = 0.0; double cab = 0.0; // do 50 fits for (int k = 0; k < 50; k++) { Console.WriteLine("k={0}", k); // generate a synthetic data set BivariateSample s = new BivariateSample(); for (int i = 0; i < 50; i++) { double x = 2.0 * rng.NextDouble() - 1.0; double ez = Math.Exp(a0 + b0 * x); double P = ez / (1.0 + ez); if (rng.NextDouble() < P) { s.Add(x, 1.0); } else { s.Add(x, 0.0); } } // do the regression LinearLogisticRegressionResult r = s.LinearLogisticRegression(); // record best fit parameters double a = r.Intercept.Value; double b = r.Slope.Value; ps.Add(a, b); Console.WriteLine("{0}, {1}", a, b); // record estimated covariances caa += r.Parameters.CovarianceMatrix[0, 0]; cbb += r.Parameters.CovarianceMatrix[1, 1]; cab += r.Parameters.CovarianceMatrix[0, 1]; } caa /= ps.Count; cbb /= ps.Count; cab /= ps.Count; // check that mean parameter estimates are what they should be: the underlying population parameters Assert.IsTrue(ps.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(a0)); Assert.IsTrue(ps.Y.PopulationMean.ConfidenceInterval(0.95).ClosedContains(b0)); // check that parameter covarainces are what they should be: the reported covariance estimates Assert.IsTrue(ps.X.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(caa)); Assert.IsTrue(ps.Y.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(cbb)); Assert.IsTrue(ps.PopulationCovariance.ConfidenceInterval(0.95).ClosedContains(cab)); }