/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { if (StatisticDistribution == null) { return(Double.NaN); // return NaN to match R's behavior } double p; switch (Tail) { case DistributionTail.TwoTail: double a = StatisticDistribution.DistributionFunction(x); double b = StatisticDistribution.ComplementaryDistributionFunction(x); double c = Math.Min(a, b); p = Math.Min(2 * c, 1); break; case DistributionTail.OneLower: p = StatisticDistribution.DistributionFunction(x); break; case DistributionTail.OneUpper: p = StatisticDistribution.ComplementaryDistributionFunction(x); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { if (Double.IsNaN(this.Statistic)) { Trace.TraceWarning("The test statistic is NaN, probably because its standard error is zero. This test is not applicable in this case as the samples do " + "not come from a normal distribution. One way to overcome this problem may be to increase the number of samples in your experiment."); return(Double.NaN); } double p; switch (Tail) { case DistributionTail.TwoTail: p = 2.0 * StatisticDistribution.DistributionFunction(Math.Abs(x)); break; case DistributionTail.OneUpper: p = StatisticDistribution.ComplementaryDistributionFunction(x); break; case DistributionTail.OneLower: p = StatisticDistribution.DistributionFunction(x); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { // TODO: Maybe unify with WilcoxonTest? double p; switch (Tail) { case DistributionTail.TwoTail: double a = StatisticDistribution.DistributionFunction(x); double b = StatisticDistribution.ComplementaryDistributionFunction(x); double c = Math.Min(a, b); p = Math.Min(2 * c, 1); break; case DistributionTail.OneLower: p = StatisticDistribution.DistributionFunction(x); break; case DistributionTail.OneUpper: p = StatisticDistribution.ComplementaryDistributionFunction(x); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { double p; switch (Tail) { case DistributionTail.TwoTail: double a = StatisticDistribution.DistributionFunction(x); double b = StatisticDistribution.ComplementaryDistributionFunction(x); p = 2 * Math.Min(a, b); break; case DistributionTail.OneUpper: p = StatisticDistribution.DistributionFunction(x); break; case DistributionTail.OneLower: p = StatisticDistribution.ComplementaryDistributionFunction(x); break; default: throw new InvalidOperationException(); } return(p); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { if (Tail == Testing.DistributionTail.TwoTail) { return(StatisticDistribution.ComplementaryDistributionFunction(Statistic)); } return(StatisticDistribution.OneSideDistributionFunction(Statistic)); }
/// <summary> /// Computes the Chi-Square Test. /// </summary> /// protected void Compute(double statistic, int degreesOfFreedom) { this.Statistic = statistic; this.StatisticDistribution = new ChiSquareDistribution(degreesOfFreedom); this.Tail = DistributionTail.OneUpper; this.PValue = StatisticDistribution.ComplementaryDistributionFunction(Statistic); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { if (StatisticDistribution == null) { return(Double.NaN); } return(StatisticDistribution.ComplementaryDistributionFunction(x)); }
/// <summary> /// Computes the two-tail probability using the Wilson-Sterne rule, /// which defines the tail of the distribution based on a ordering /// of the null probabilities of X. (Smirnoff, 2003) /// </summary> /// /// <remarks> /// References: Jeffrey S. Simonoff, Analyzing /// Categorical Data, Springer, 2003 (pg 64). /// </remarks> /// private double wilsonSterne(double x) { double mean = StatisticDistribution.Mean; if (x == mean) { return(1); } int trials = StatisticDistribution.NumberOfTrials; // Construct a map of values and point probabilities double[] probabilities = new double[trials]; for (int i = 0; i < probabilities.Length; i++) { probabilities[i] = StatisticDistribution.ProbabilityMassFunction(i); } int[] values; // Build the ordered Wilson-Sterne table probabilities.Sort(out values); // Now, compute the cumulative probability double[] cumulative = new double[trials]; cumulative[0] = probabilities[0]; for (int i = 1; i < cumulative.Length; i++) { cumulative[i] += cumulative[i - 1] + probabilities[i]; } int v = 0; for (int i = 0; i < values.Length; i++) { if (values[i] == (int)x) { v = i; while (v < probabilities.Length && probabilities[i] == probabilities[v]) { v++; } } } return(cumulative[v - 1]); }
/// <summary> /// Converts a given p-value to a test statistic. /// </summary> /// /// <param name="p">The p-value.</param> /// /// <returns>The test statistic which would generate the given p-value.</returns> /// public override double PValueToStatistic(double p) { double b; switch (Tail) { case DistributionTail.OneLower: b = StatisticDistribution.InverseDistributionFunction(p); break; case DistributionTail.OneUpper: b = StatisticDistribution.InverseDistributionFunction(1.0 - p); break; case DistributionTail.TwoTail: throw new NotSupportedException(); default: throw new InvalidOperationException(); } return b; }
/// <summary> /// Computes the two-tail probability using the Wilson-Sterne rule, /// which defines the tail of the distribution based on a ordering /// of the null probabilities of X. (Smirnoff, 2003) /// </summary> /// /// <remarks> /// References: Jeffrey S. Simonoff, Analyzing /// Categorical Data, Springer, 2003 (pg 64). /// </remarks> /// private double wilsonSterne(double x) { int trials = StatisticDistribution.NumberOfTrials; // Construct a map of values and point probabilities int[] values = new int[trials]; for (int i = 0; i < values.Length; i++) { values[i] = i; } double[] probabilities = new double[trials]; for (int i = 0; i < probabilities.Length; i++) { probabilities[i] = StatisticDistribution.ProbabilityMassFunction(i); } // Build the ordered Wilson-Sterne table Array.Sort(probabilities, values); // Now, compute the cumulative distribution double[] cumulative = new double[trials]; cumulative[0] = probabilities[0]; for (int i = 1; i < cumulative.Length; i++) { cumulative[i] += cumulative[i - 1] + probabilities[i]; } int v = 0; // Locate the desired value for (int i = 0; i < values.Length; i++) { if (values[i] == (int)x) { v = i; } } // Report the p-value return(cumulative[v]); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { double p; switch (Tail) { case DistributionTail.TwoTail: p = wilsonSterne(x); break; case DistributionTail.OneUpper: p = StatisticDistribution.ComplementaryDistributionFunction((int)x, inclusive: true); break; case DistributionTail.OneLower: p = StatisticDistribution.DistributionFunction((int)x); break; default: throw new InvalidOperationException(); } return p; }
/// <summary> /// Converts a given p-value to a test statistic. /// </summary> /// /// <param name="p">The p-value.</param> /// /// <returns>The test statistic which would generate the given p-value.</returns> /// public override double PValueToStatistic(double p) { double f; switch (Tail) { case DistributionTail.OneLower: f = StatisticDistribution.InverseDistributionFunction(p); break; case DistributionTail.OneUpper: f = StatisticDistribution.InverseDistributionFunction(1.0 - p); break; case DistributionTail.TwoTail: f = StatisticDistribution.InverseDistributionFunction(1.0 - p / 2.0); break; default: throw new InvalidOperationException(); } return(f); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { return(StatisticDistribution.DistributionFunction(x)); }
/// <summary> /// Converts a given p-value to a test statistic. /// </summary> /// /// <param name="p">The p-value.</param> /// /// <returns>The test statistic which would generate the given p-value.</returns> /// public override double PValueToStatistic(double p) { return(StatisticDistribution.InverseDistributionFunction(p)); }
/// <summary> /// Converts a given test statistic to a p-value. /// </summary> /// /// <param name="x">The value of the test statistic.</param> /// /// <returns>The p-value for the given statistic.</returns> /// public override double StatisticToPValue(double x) { return StatisticDistribution.ComplementaryDistributionFunction(x); }