x
/// <summary> /// Constructs a Chi-Square Test. /// </summary> /// <param name="statistic">The test statistic.</param> /// <param name="degreesOfFreedom">The chi-square distribution degrees of freedom.</param> /// <param name="threshold">The significance threshold. By default, 0.05 will be used.</param> /// public ChiSquareTest(double statistic, int degreesOfFreedom, double threshold) { this.Statistic = statistic; this.Threshold = threshold; this.distribution = new ChiSquareDistribution(degreesOfFreedom); this.PValue = distribution.SurvivalFunction(Statistic); }
/// <summary> /// Construct a Chi-Square Test. /// </summary> /// <param name="expected">The expected variable values.</param> /// <param name="observed">The observed variable values.</param> /// <param name="degreesOfFreedom">The chi-square distribution degrees of freedom.</param> /// <param name="threshold">The significance threshold. By default, 0.05 will be used.</param> /// public ChiSquareTest(double[] expected, double[] observed, int degreesOfFreedom, double threshold) { if (expected == null) throw new ArgumentNullException("expected"); if (observed == null) throw new ArgumentNullException("observed"); // X² = sum(o - e)² // ----- // e double sum = 0.0; for (int i = 0; i < observed.Length; i++) { double d = observed[i] - expected[i]; sum += (d * d) / expected[i]; } this.Statistic = sum; this.Threshold = threshold; this.distribution = new ChiSquareDistribution(degreesOfFreedom); this.PValue = distribution.SurvivalFunction(Statistic); }