/// <summary> /// Creates a new GeneralizedLinearRegression that is a copy of the current instance. /// </summary> /// public object Clone() { ILinkFunction function = (ILinkFunction)linkFunction.Clone(); var regression = new GeneralizedLinearRegression(function, coefficients.Length); regression.coefficients = (double[])this.coefficients.Clone(); regression.standardErrors = (double[])this.standardErrors.Clone(); return(regression); }
/// <summary> /// The likelihood ratio test of the overall model, also called the model chi-square test. /// </summary> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// /// <remarks> /// <para> /// The Chi-square test, also called the likelihood ratio test or the log-likelihood test /// is based on the deviance of the model (-2*log-likelihood). The log-likelihood ratio test /// indicates whether there is evidence of the need to move from a simpler model to a more /// complicated one (where the simpler model is nested within the complicated one).</para> /// <para> /// The difference between the log-likelihood ratios for the researcher's model and a /// simpler model is often called the "model chi-square".</para> /// </remarks> /// public ChiSquareTest ChiSquare(double[][] input, double[] output) { double y0 = output.Count(y => y == 0.0); double y1 = output.Length - y0; GeneralizedLinearRegression regression = new GeneralizedLinearRegression(linkFunction, Inputs, Math.Log(y1 / y0)); double ratio = GetLogLikelihoodRatio(input, output, regression); return(new ChiSquareTest(ratio, coefficients.Length - 1)); }
/// <summary> /// The likelihood ratio test of the overall model, also called the model chi-square test. /// </summary> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// /// <remarks> /// <para> /// The Chi-square test, also called the likelihood ratio test or the log-likelihood test /// is based on the deviance of the model (-2*log-likelihood). The log-likelihood ratio test /// indicates whether there is evidence of the need to move from a simpler model to a more /// complicated one (where the simpler model is nested within the complicated one).</para> /// <para> /// The difference between the log-likelihood ratios for the researcher's model and a /// simpler model is often called the "model chi-square".</para> /// </remarks> /// public ChiSquareTest ChiSquare(double[][] input, double[] output) { double y0 = 0; double y1 = 0; for (int i = 0; i < output.Length; i++) { y0 += 1.0 - output[i]; y1 += output[i]; } var intercept = Math.Log(y1 / y0); var regression = new GeneralizedLinearRegression(linkFunction, Inputs, intercept); double ratio = GetLogLikelihoodRatio(input, output, regression); return(new ChiSquareTest(ratio, coefficients.Length - 1)); }
/// <summary> /// The likelihood ratio test of the overall model, also called the model chi-square test. /// </summary> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// <param name="weights">The weights associated with each input vector.</param> /// /// <remarks> /// <para> /// The Chi-square test, also called the likelihood ratio test or the log-likelihood test /// is based on the deviance of the model (-2*log-likelihood). The log-likelihood ratio test /// indicates whether there is evidence of the need to move from a simpler model to a more /// complicated one (where the simpler model is nested within the complicated one).</para> /// <para> /// The difference between the log-likelihood ratios for the researcher's model and a /// simpler model is often called the "model chi-square".</para> /// </remarks> /// public ChiSquareTest ChiSquare(double[][] input, double[] output, double[] weights) { double y0 = 0; double y1 = 0; for (int i = 0; i < output.Length; i++) { y0 += (1.0 - output[i]) * weights[i]; y1 += output[i] * weights[i]; } var regression = new GeneralizedLinearRegression(linkFunction) { NumberOfInputs = NumberOfInputs, Intercept = Math.Log(y1 / y0) }; double ratio = GetLogLikelihoodRatio(input, output, weights, regression); return(new ChiSquareTest(ratio, NumberOfInputs)); }
/// <summary> /// Gets the Log-Likelihood Ratio between two models. /// </summary> /// /// <remarks> /// The Log-Likelihood ratio is defined as 2*(LL - LL0). /// </remarks> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// <param name="regression">Another Logistic Regression model.</param> /// /// <returns>The Log-Likelihood ratio (a measure of performance /// between two models) calculated over the given data sets.</returns> /// public double GetLogLikelihoodRatio(double[][] input, double[] output, GeneralizedLinearRegression regression) { return(2.0 * (this.GetLogLikelihood(input, output) - regression.GetLogLikelihood(input, output))); }
/// <summary> /// Creates a new GeneralizedLinearRegression that is a copy of the current instance. /// </summary> /// public object Clone() { ILinkFunction function = (ILinkFunction)linkFunction.Clone(); var regression = new GeneralizedLinearRegression(function, coefficients.Length); regression.coefficients = (double[])this.coefficients.Clone(); regression.standardErrors = (double[])this.standardErrors.Clone(); return regression; }
/// <summary> /// The likelihood ratio test of the overall model, also called the model chi-square test. /// </summary> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// <param name="weights">The weights associated with each input vector.</param> /// /// <remarks> /// <para> /// The Chi-square test, also called the likelihood ratio test or the log-likelihood test /// is based on the deviance of the model (-2*log-likelihood). The log-likelihood ratio test /// indicates whether there is evidence of the need to move from a simpler model to a more /// complicated one (where the simpler model is nested within the complicated one).</para> /// <para> /// The difference between the log-likelihood ratios for the researcher's model and a /// simpler model is often called the "model chi-square".</para> /// </remarks> /// public ChiSquareTest ChiSquare(double[][] input, double[] output, double[] weights) { double y0 = 0; double y1 = 0; for (int i = 0; i < output.Length; i++) { y0 += (1.0 - output[i]) * weights[i]; y1 += output[i] * weights[i]; } var intercept = Math.Log(y1 / y0); var regression = new GeneralizedLinearRegression(linkFunction, Inputs, intercept); double ratio = GetLogLikelihoodRatio(input, output, weights, regression); return new ChiSquareTest(ratio, coefficients.Length - 1); }
/// <summary> /// Gets the Log-Likelihood Ratio between two models. /// </summary> /// /// <remarks> /// The Log-Likelihood ratio is defined as 2*(LL - LL0). /// </remarks> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// <param name="weights">The weights associated with each input vector.</param> /// <param name="regression">Another Logistic Regression model.</param> /// /// <returns>The Log-Likelihood ratio (a measure of performance /// between two models) calculated over the given data sets.</returns> /// public double GetLogLikelihoodRatio(double[][] input, double[] output, double[] weights, GeneralizedLinearRegression regression) { return 2.0 * (this.GetLogLikelihood(input, output, weights) - regression.GetLogLikelihood(input, output, weights)); }
/// <summary> /// The likelihood ratio test of the overall model, also called the model chi-square test. /// </summary> /// /// <param name="input">A set of input data.</param> /// <param name="output">A set of output data.</param> /// /// <remarks> /// <para> /// The Chi-square test, also called the likelihood ratio test or the log-likelihood test /// is based on the deviance of the model (-2*log-likelihood). The log-likelihood ratio test /// indicates whether there is evidence of the need to move from a simpler model to a more /// complicated one (where the simpler model is nested within the complicated one).</para> /// <para> /// The difference between the log-likelihood ratios for the researcher's model and a /// simpler model is often called the "model chi-square".</para> /// </remarks> /// public ChiSquareTest ChiSquare(double[][] input, double[] output) { double y0 = output.Count(y => y == 0.0); double y1 = output.Length - y0; GeneralizedLinearRegression regression = new GeneralizedLinearRegression(linkFunction, Inputs, Math.Log(y1 / y0)); double ratio = GetLogLikelihoodRatio(input, output, regression); return new ChiSquareTest(ratio, coefficients.Length - 1); }
public void ComputeTest() { // Example from http://bayes.bgsu.edu/bcwr/vignettes/probit_regression.pdf double[][] input = { new double[] { 525 }, new double[] { 533 }, new double[] { 545 }, new double[] { 582 }, new double[] { 581 }, new double[] { 576 }, new double[] { 572 }, new double[] { 609 }, new double[] { 559 }, new double[] { 543 }, new double[] { 576 }, new double[] { 525 }, new double[] { 574 }, new double[] { 582 }, new double[] { 574 }, new double[] { 471 }, new double[] { 595 }, new double[] { 557 }, new double[] { 557 }, new double[] { 584 }, new double[] { 599 }, new double[] { 517 }, new double[] { 649 }, new double[] { 584 }, new double[] { 463 }, new double[] { 591 }, new double[] { 488 }, new double[] { 563 }, new double[] { 553 }, new double[] { 549 } }; double[] output = { 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1 }; var regression = new GeneralizedLinearRegression(new ProbitLinkFunction(), inputs: 1); var teacher = new IterativeReweightedLeastSquares(regression); double delta = 0; do { // Perform an iteration delta = teacher.Run(input, output); } while (delta > 1e-6); Assert.AreEqual(2, regression.Coefficients.Length); Assert.AreEqual(-17.6984, regression.Coefficients[0], 1e-4); Assert.AreEqual(0.03293, regression.Coefficients[1], 1e-4); Assert.AreEqual(2, regression.StandardErrors.Length); Assert.AreEqual(9.2731983954911374, regression.StandardErrors[0], 1e-5); Assert.AreEqual(0.016768779446085, regression.StandardErrors[1], 1e-6); }
public void ComputeTest2() { double[][] input = { new double[] { 55, 0 }, // 0 - no cancer new double[] { 28, 0 }, // 0 new double[] { 65, 1 }, // 0 new double[] { 46, 0 }, // 1 - have cancer new double[] { 86, 1 }, // 1 new double[] { 56, 1 }, // 1 new double[] { 85, 0 }, // 0 new double[] { 33, 0 }, // 0 new double[] { 21, 1 }, // 0 new double[] { 42, 1 }, // 1 }; double[] output = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 }; var regression = new GeneralizedLinearRegression(new ProbitLinkFunction(), inputs: 2); var teacher = new IterativeReweightedLeastSquares(regression); double delta = 0; do { // Perform an iteration delta = teacher.Run(input, output); } while (delta > 0.001); Assert.AreEqual(3, regression.Coefficients.Length); Assert.AreEqual(-1.4807594445304693, regression.Coefficients[0],1e-8); Assert.AreEqual(0.012417175632016827, regression.Coefficients[1], 1e-8); Assert.AreEqual(1.072665379969842, regression.Coefficients[2], 1e-8); Assert.IsFalse(regression.Coefficients.HasNaN()); Assert.AreEqual(3, regression.StandardErrors.Length); Assert.AreEqual(1.6402037052797314, regression.StandardErrors[0], 1e-8); Assert.AreEqual(0.026119425452145524, regression.StandardErrors[1], 1e-8); Assert.AreEqual(1.1297252500874606, regression.StandardErrors[2], 1e-8); Assert.IsFalse(regression.StandardErrors.HasNaN()); }