/// <summary> /// Computes the one sample sign test. /// </summary> /// protected void Compute(int positive, int total, OneSampleHypothesis alternate) { this.Hypothesis = alternate; // The underlying test is to check whether the probability // value from the samples are higher than or lesser than 0.5, // thus the actual Binomial test hypothesis is inverted: OneSampleHypothesis binomialHypothesis; switch (alternate) { case OneSampleHypothesis.ValueIsDifferentFromHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; break; case OneSampleHypothesis.ValueIsGreaterThanHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsSmallerThanHypothesis; break; case OneSampleHypothesis.ValueIsSmallerThanHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; break; default: throw new InvalidOperationException(); } base.Compute(positive, total, 0.5, binomialHypothesis); }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="sampleKappa">The estimated Kappa statistic.</param> /// <param name="standardError">The standard error of the kappa statistic. If the test is /// being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error should be computed with the null hypothesis parameter set to true</see>.</param> /// <param name="hypothesizedKappa">The hypothesized value for the Kappa statistic.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest(double sampleKappa, double standardError, double hypothesizedKappa, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { Variance = standardError * standardError; Compute(sampleKappa, hypothesizedKappa, standardError, alternate); }
/// <summary> /// Tests the probability of two outcomes in a series of experiments. /// </summary> /// /// <param name="trials">The experimental trials.</param> /// <param name="hypothesizedProbability">The hypothesized occurrence probability.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public BinomialTest(bool[] trials, double hypothesizedProbability = 0.5, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (trials == null) { throw new ArgumentNullException("trials"); } if (hypothesizedProbability < 0 || hypothesizedProbability > 1.0) { throw new ArgumentOutOfRangeException("hypothesizedProbability"); } int successes = 0; for (int i = 0; i < trials.Length; i++) { if (trials[i]) { successes++; } } Compute(successes, trials.Length, hypothesizedProbability, alternate); }
/// <summary> /// Creates a new <see cref="ReceiverOperatingCurveTest"/>. /// </summary> /// /// <param name="curve">The curve to be tested.</param> /// <param name="hypothesizedValue">The hypothesized value for the ROC area.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public ReceiverOperatingCurveTest(ReceiverOperatingCharacteristic curve, double hypothesizedValue = 0.5, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { this.Curve = curve; Compute(curve.Area, hypothesizedValue, curve.StandardError, alternate); }
/// <summary> /// Tests the null hypothesis that the population mean is equal to a specified value. /// </summary> /// /// <param name="statistic">The test statistic.</param> /// <param name="degreesOfFreedom">The degrees of freedom for the test distribution.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to test.</param> /// public TTest( double statistic, double degreesOfFreedom, OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { Compute(statistic, degreesOfFreedom, hypothesis); }
/// <summary> /// Creates a new <see cref="ReceiverOperatingCurveTest"/>. /// </summary> /// /// <param name="curve">The curve to be tested.</param> /// <param name="hypothesizedValue">The hypothesized value for the ROC area.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public ReceiverOperatingCurveTest(ReceiverOperatingCharacteristic curve, double hypothesizedValue = 0.5, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { this.Curve = curve; Compute(curve.Area, hypothesizedValue, curve.StandardError, alternate); }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="matrix">The contingency table to test.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest( GeneralConfusionMatrix matrix, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsGreaterThanHypothesis ) : this(matrix, 0, alternate) { }
/// <summary> /// Constructs a Z test. /// </summary> /// /// <param name="statistic">The test statistic, as given by (x-μ)/SE.</param> /// <param name="alternate">The alternate hypothesis to test.</param> /// public ZTest(double statistic, OneSampleHypothesis alternate) { this.EstimatedValue = statistic; this.HypothesizedValue = 0; this.StandardError = 1; this.Compute(statistic, alternate); }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="sampleKappa">The estimated Kappa statistic.</param> /// <param name="standardError">The standard error of the kappa statistic. If the test is /// being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error should be computed with the null hypothesis parameter set to true</see>.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest( double sampleKappa, double standardError, OneSampleHypothesis alternate // = OneSampleHypothesis.ValueIsGreaterThanHypothesis ) : this(sampleKappa, standardError, 0, alternate) { }
/// <summary> /// Tests the null hypothesis that the population mean is equal to a specified value. /// </summary> /// /// <param name="mean">The sample's mean value.</param> /// <param name="stdDev">The standard deviation for the samples.</param> /// <param name="samples">The number of observations in the sample.</param> /// <param name="hypothesizedMean">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TTest(double mean, double stdDev, int samples, double hypothesizedMean = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { double stdError = Accord.Statistics.Tools.StandardError(samples, stdDev); Compute(samples, mean, hypothesizedMean, stdError, alternate); power(stdDev, samples); }
/// <summary> /// Computes the Fisher's exact test. /// </summary> /// protected void Compute(double statistic, int populationSize, int n, int m, OneSampleHypothesis alternate) { this.Statistic = statistic; this.StatisticDistribution = new HypergeometricDistribution(populationSize, m, n); this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); }
public void TTestConstructorTest() { // mean = 0.5, var = 1 double[] sample = { -0.849886940156521, 3.53492346633185, 1.22540422494611, 0.436945126810344, 1.21474290382610, 0.295033941700225, 0.375855651783688, 1.98969760778547, 1.90903448980048, 1.91719241342961 }; // Null Hypothesis: Values are equal // Alternative : Values are different double hypothesizedMean = 0; OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; TTest target = new TTest(sample, hypothesizedMean, hypothesis); Assert.AreEqual(3.1254485381338246, target.Statistic); Assert.AreEqual(OneSampleHypothesis.ValueIsDifferentFromHypothesis, target.Hypothesis); Assert.AreEqual(DistributionTail.TwoTail, target.Tail); Assert.AreEqual(0.012210924322697769, target.PValue); Assert.IsTrue(target.Significant); // Null hypothesis: value is smaller than hypothesis // Alternative : value is greater than hypothesis // If the null hypothesis states that the population parameter is less // than zero (or a constant), the z-score that rejects the null is always // positive and greater than the score set for the rejection condition. hypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; target = new TTest(sample, hypothesizedMean, hypothesis); // z-score is positive: Assert.AreEqual(3.1254485381338246, target.Statistic); Assert.AreEqual(OneSampleHypothesis.ValueIsGreaterThanHypothesis, target.Hypothesis); // right tail Assert.AreEqual(DistributionTail.OneUpper, target.Tail); // right tail Assert.AreEqual(0.0061054621613488846, target.PValue); Assert.IsTrue(target.Significant); // null should be rejected // Null hypothesis: value is greater than hypothesis // Alternative: value is smaller than hypothesis // If the null hypothesis states that the population parameter is // greater than zero (or a constant), the z-score that rejects the // null is always negative and less than the score set for the // rejection condition. hypothesis = OneSampleHypothesis.ValueIsSmallerThanHypothesis; target = new TTest(sample, hypothesizedMean, hypothesis); // z-score is positive: Assert.AreEqual(3.1254485381338246, target.Statistic); Assert.AreEqual(OneSampleHypothesis.ValueIsSmallerThanHypothesis, target.Hypothesis); // left tail Assert.AreEqual(DistributionTail.OneLower, target.Tail); // left tail Assert.AreEqual(0.99389453783865112, target.PValue); Assert.IsFalse(target.Significant); // null cannot be rejected }
/// <summary> /// Tests the null hypothesis that the population mean is equal to a specified value. /// </summary> /// /// <param name="estimatedValue">The estimated value (θ).</param> /// <param name="standardError">The standard error of the estimation (SE).</param> /// <param name="hypothesizedValue">The hypothesized value (θ').</param> /// <param name="degreesOfFreedom">The degrees of freedom for the test distribution.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TTest( double estimatedValue, double standardError, double degreesOfFreedom, double hypothesizedValue, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis ) { Compute(estimatedValue, standardError, degreesOfFreedom, hypothesizedValue, alternate); }
/// <summary> /// Computes the Z test. /// </summary> /// protected void Compute(double statistic, OneSampleHypothesis alternate) { this.StatisticDistribution = NormalDistribution.Standard; this.Statistic = statistic; this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Computes the Binomial test. /// </summary> /// protected void Compute(double statistic, int m, double p, OneSampleHypothesis alternate) { this.Statistic = statistic; this.StatisticDistribution = new BinomialDistribution(m, p); this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Tests the probability of two outcomes in a series of experiments. /// </summary> /// /// <param name="successes">The number of successes in the trials.</param> /// <param name="trials">The total number of experimental trials.</param> /// <param name="hypothesizedProbability">The hypothesized occurrence probability.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public BinomialTest(int successes, int trials, double hypothesizedProbability = 0.5, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (successes > trials) throw new ArgumentOutOfRangeException("successes"); if (hypothesizedProbability < 0 || hypothesizedProbability > 1.0) throw new ArgumentOutOfRangeException("hypothesizedProbability"); Compute(successes, trials, hypothesizedProbability, alternate); }
/// <summary> /// Computes the Z test. /// </summary> /// protected void Compute(double estimatedValue, double hypothesizedValue, double stdError, OneSampleHypothesis alternate) { this.HypothesizedValue = hypothesizedValue; this.EstimatedValue = estimatedValue; this.StandardError = stdError; // Compute Z statistic double z = (estimatedValue - hypothesizedValue) / StandardError; Compute(z, alternate); }
/// <summary> /// Computes the T-test. /// </summary> /// private void Compute(double estimatedValue, double stdError, double degreesOfFreedom, double hypothesizedValue, OneSampleHypothesis alternate) { this.EstimatedValue = estimatedValue; this.StandardError = stdError; this.HypothesizedValue = hypothesizedValue; double df = degreesOfFreedom; double t = (EstimatedValue - hypothesizedValue) / StandardError; Compute(t, df, alternate); }
/// <summary> /// Tests the null hypothesis that the population mean is equal to a specified value. /// </summary> /// /// <param name="sample">The data samples from which the test will be performed.</param> /// <param name="hypothesizedMean">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TTest(double[] sample, double hypothesizedMean = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { int n = sample.Length; double mean = Accord.Statistics.Tools.Mean(sample); double stdDev = Accord.Statistics.Tools.StandardDeviation(sample, mean); double stdError = Accord.Statistics.Tools.StandardError(n, stdDev); Compute(n, mean, hypothesizedMean, stdError, alternate); power(stdDev, n); }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="sampleSize">The number of observations in the sample.</param> /// <param name="power">The desired power level. Default is 0.8.</param> /// <param name="alpha">The desired significance level. Default is 0.05.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to be tested.</param> /// /// <returns>The required number of samples.</returns> /// public static ZTestPowerAnalysis GetEffectSize(int sampleSize, double power = 0.8, double alpha = 0.05, OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { var analysis = new ZTestPowerAnalysis(hypothesis) { Samples = sampleSize, Size = alpha, Power = power, }; analysis.ComputeEffect(); return(analysis); }
/// <summary> /// Constructs a Z test. /// </summary> /// /// <param name="sampleMean">The sample's mean.</param> /// <param name="sampleStdDev">The sample's standard deviation.</param> /// <param name="hypothesizedMean">The hypothesized value for the distribution's mean.</param> /// <param name="samples">The sample's size.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public ZTest(double sampleMean, double sampleStdDev, int samples, double hypothesizedMean = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (samples < 30) { Trace.TraceWarning("Warning: running a Z test for less than 30 samples. Consider running a Student's T Test instead."); } double stdError = Measures.StandardError(samples, sampleStdDev); Compute(sampleMean, hypothesizedMean, stdError, alternate); power(sampleStdDev, samples); }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="delta">The minimum detectable difference.</param> /// <param name="standardDeviation">The difference standard deviation.</param> /// <param name="power">The desired power level. Default is 0.8.</param> /// <param name="alpha">The desired significance level. Default is 0.05.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to be tested.</param> /// /// <returns>The required number of samples.</returns> /// public static ZTestPowerAnalysis GetSampleSize(double delta, double standardDeviation = 1, double power = 0.8, double alpha = 0.05, OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { ZTestPowerAnalysis analysis = new ZTestPowerAnalysis(hypothesis) { Effect = (delta) / standardDeviation, Size = alpha, Power = power, }; analysis.ComputeSamples(); return(analysis); }
/// <summary> /// Constructs a Z test. /// </summary> /// /// <param name="samples">The data samples from which the test will be performed.</param> /// <param name="hypothesizedMean">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public ZTest(double[] samples, double hypothesizedMean = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { double mean = Measures.Mean(samples); double stdDev = Measures.StandardDeviation(samples, EstimatedValue); double stdError = Measures.StandardError(samples.Length, stdDev); if (samples.Length < 30) { Trace.TraceWarning("Warning: running a Z test for less than 30 samples. Consider running a Student's T Test instead."); } this.Compute(mean, hypothesizedMean, stdError, alternate); this.power(stdDev, samples.Length); }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="matrix">The contingency table to test.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// <param name="hypothesizedWeightedKappa">The hypothesized value for the Kappa statistic. If the test /// is being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error will be computed with the null hypothesis parameter set to true</see>.</param> /// public KappaTest(WeightedConfusionMatrix matrix, double hypothesizedWeightedKappa, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (hypothesizedWeightedKappa == 0) { // Use the null hypothesis variance Compute(matrix.WeightedKappa, hypothesizedWeightedKappa, matrix.WeightedStandardErrorUnderNull, alternate); Variance = matrix.WeightedVarianceUnderNull; } else { // Use the default variance Compute(matrix.WeightedKappa, hypothesizedWeightedKappa, matrix.WeightedStandardError, alternate); Variance = matrix.WeightedVariance; } }
public void PowerTest2() { // Example from http://www.cyclismo.org/tutorial/R/power.html double mean = 6.5; double stdDev = 2; int samples = 20; double hypothesizedMean = 5; OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); IPowerAnalysis power = target.Analysis; Assert.AreEqual(0.918362, power.Power, 1e-6); }
/// <summary> /// Tests the null hypothesis that the sample median is equal to a hypothesized value. /// </summary> /// /// <param name="sample">The data samples from which the test will be performed.</param> /// <param name="hypothesizedMedian">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public SignTest(double[] sample, double hypothesizedMedian = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { int positive = 0; int negative = 0; for (int i = 0; i < sample.Length; i++) { double d = sample[i] - hypothesizedMedian; if (d > 0) positive++; else if (d < 0) negative++; } Compute(positive, positive + negative, alternate); }
/// <summary> /// Tests the null hypothesis that the sample median is equal to a hypothesized value. /// </summary> /// /// <param name="sample">The data samples from which the test will be performed.</param> /// <param name="hypothesizedMedian">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public WilcoxonSignedRankTest(double[] sample, double hypothesizedMedian = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { int[] signs = new int[sample.Length]; double[] diffs = new double[sample.Length]; // 1. Compute absolute difference and sign function for (int i = 0; i < sample.Length; i++) { double d = sample[i] - hypothesizedMedian; signs[i] = Math.Sign(d); diffs[i] = Math.Abs(d); } this.Hypothesis = alternate; Compute(signs, diffs, (DistributionTail)alternate); }
/// <summary> /// Tests the null hypothesis that the sample median is equal to a hypothesized value. /// </summary> /// /// <param name="sample">The data samples from which the test will be performed.</param> /// <param name="hypothesizedMedian">The constant to be compared with the samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public WilcoxonSignedRankTest(double[] sample, double hypothesizedMedian = 0, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { int[] signs = new int[sample.Length]; double[] diffs = new double[sample.Length]; // 1. Compute absolute difference and sign function for (int i = 0; i < sample.Length; i++) { double d = sample[i] - hypothesizedMedian; signs[i] = Math.Sign(d); diffs[i] = Math.Abs(d); } this.Hypothesis = alternate; Compute(signs, diffs, (DistributionTail)alternate); }
public void EffectSizeTest2() { // Example from http://wise.cgu.edu/powermod/computing.asp double mean = 104; double stdDev = 10; int samples = 25; double hypothesizedMean = 100; OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); ZTestPowerAnalysis power = (ZTestPowerAnalysis)target.Analysis; power.Size = 0.05; power.Power = 0.80; power.ComputeEffect(); Assert.AreEqual(0.56, power.Effect, 0.001); }
public void ZTestConstructorTest3() { // Example from http://science.kennesaw.edu/~jdemaio/1107/hypothesis_testing.htm double mean = 53; double stdDev = 8; int samples = 49; double hypothesizedMean = 58; // Null Hypothesis: mean = 58 // Alternative : mean > 58 OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); Assert.AreEqual(-4.38, target.Statistic, 0.01); Assert.AreEqual(OneSampleHypothesis.ValueIsGreaterThanHypothesis, target.Hypothesis); Assert.AreEqual(DistributionTail.OneUpper, target.Tail); Assert.IsFalse(target.Significant); }
public void PowerTest1() { // Example from http://wise.cgu.edu/powermod/computing.asp double mean = 105; double stdDev = 10; int samples = 25; double hypothesizedMean = 100; OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); var power = target.Analysis; Assert.AreEqual(0.804, power.Power, 1e-3); mean = 90; target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); power = target.Analysis; Assert.AreEqual(0.0, power.Power, 1e-3); }
public void ZTestConstructorTest4() { // Example from http://science.kennesaw.edu/~jdemaio/1107/hypothesis_testing.htm double mean = 51.5; double stdDev = 10; int samples = 100; double hypothesizedMean = 50; // Null Hypothesis: mean = 58 // Alternative : mean != 58 OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis); Assert.AreEqual(1.5, target.Statistic, 0.01); Assert.AreEqual(OneSampleHypothesis.ValueIsDifferentFromHypothesis, target.Hypothesis); Assert.AreEqual(DistributionTail.TwoTail, target.Tail); Assert.IsFalse(target.Significant); }
/// <summary> /// Constructs a new Fisher's exact test. /// </summary> /// /// <param name="matrix">The matrix to be tested.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public FisherExactTest(ConfusionMatrix matrix, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (matrix == null) { throw new ArgumentNullException("matrix"); } int[,] mat = matrix.Matrix; int a = mat[0, 0]; int b = mat[0, 1]; int c = mat[1, 0]; int N = matrix.Samples; // total number in population int k = a; // number of success in sample int m = a + b; // number of success in population int n = a + c; // sample size Compute(k, N, n, m, alternate); }
/// <summary> /// Computes the one sample sign test. /// </summary> /// protected void Compute(int positive, int total, OneSampleHypothesis alternate) { this.Hypothesis = alternate; // The underlying test is to check whether the probability // value from the samples are higher than or lesser than 0.5, // thus the actual Binomial test hypothesis is inverted: OneSampleHypothesis binomialHypothesis; switch (alternate) { case OneSampleHypothesis.ValueIsDifferentFromHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; break; case OneSampleHypothesis.ValueIsGreaterThanHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsSmallerThanHypothesis; break; case OneSampleHypothesis.ValueIsSmallerThanHypothesis: binomialHypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; break; default: throw new InvalidOperationException(); } base.Compute(positive, total, 0.5, binomialHypothesis); }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="sampleKappa">The estimated Kappa statistic.</param> /// <param name="standardError">The standard error of the kappa statistic. If the test is /// being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error should be computed with the null hypothesis parameter set to true</see>.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest(double sampleKappa, double standardError, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsGreaterThanHypothesis) : this(sampleKappa, standardError, 0, alternate) { }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="matrix">The contingency table to test.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest(GeneralConfusionMatrix matrix, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsGreaterThanHypothesis) : this(matrix, 0, alternate) { }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="matrix">The contingency table to test.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// <param name="hypothesizedWeightedKappa">The hypothesized value for the Kappa statistic. If the test /// is being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error will be computed with the null hypothesis parameter set to true</see>.</param> /// public KappaTest(WeightedConfusionMatrix matrix, double hypothesizedWeightedKappa, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { if (hypothesizedWeightedKappa == 0) { // Use the null hypothesis variance Compute(matrix.WeightedKappa, hypothesizedWeightedKappa, matrix.WeightedStandardErrorUnderNull, alternate); Variance = matrix.WeightedVarianceUnderNull; } else { // Use the default variance Compute(matrix.WeightedKappa, hypothesizedWeightedKappa, matrix.WeightedStandardError, alternate); Variance = matrix.WeightedVariance; } }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="sampleSize">The number of observations in the sample.</param> /// <param name="power">The desired power level. Default is 0.8.</param> /// <param name="alpha">The desired significance level. Default is 0.05.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to be tested.</param> /// /// <returns>The required number of samples.</returns> /// public static ZTestPowerAnalysis GetEffectSize(int sampleSize, double power = 0.8, double alpha = 0.05, OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { var analysis = new ZTestPowerAnalysis(hypothesis) { Samples = sampleSize, Size = alpha, Power = power, }; analysis.ComputeEffect(); return analysis; }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="delta">The minimum detectable difference.</param> /// <param name="standardDeviation">The difference standard deviation.</param> /// <param name="power">The desired power level. Default is 0.8.</param> /// <param name="alpha">The desired significance level. Default is 0.05.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to be tested.</param> /// /// <returns>The required number of samples.</returns> /// public static ZTestPowerAnalysis GetSampleSize(double delta, double standardDeviation = 1, double power = 0.8, double alpha = 0.05, OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { ZTestPowerAnalysis analysis = new ZTestPowerAnalysis(hypothesis) { Effect = (delta) / standardDeviation, Size = alpha, Power = power, }; analysis.ComputeSamples(); return analysis; }
/// <summary> /// Creates a new Kappa test. /// </summary> /// /// <param name="sampleKappa">The estimated Kappa statistic.</param> /// <param name="standardError">The standard error of the kappa statistic. If the test is /// being used to assert independency between two raters (i.e. testing the null hypothesis /// that the underlying Kappa is zero), then the <see cref="AsymptoticKappaVariance(GeneralConfusionMatrix)"> /// standard error should be computed with the null hypothesis parameter set to true</see>.</param> /// <param name="hypothesizedKappa">The hypothesized value for the Kappa statistic.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test. If the /// hypothesized kappa is left unspecified, a one-tailed test will be used. Otherwise, the /// default is to use a two-sided test.</param> /// public KappaTest(double sampleKappa, double standardError, double hypothesizedKappa, OneSampleHypothesis alternate = OneSampleHypothesis.ValueIsDifferentFromHypothesis) { Variance = standardError * standardError; Compute(sampleKappa, hypothesizedKappa, standardError, alternate); }
/// <summary> /// Creates a new <see cref="ZTestPowerAnalysis"/>. /// </summary> /// /// <param name="hypothesis">The hypothesis tested.</param> /// public ZTestPowerAnalysis(OneSampleHypothesis hypothesis) : base((DistributionTail)hypothesis) { }
/// <summary> /// Tests the null hypothesis that the sample median is equal to a hypothesized value. /// </summary> /// /// <param name="positiveSamples">The number of positive samples.</param> /// <param name="totalSamples">The total number of samples.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public SignTest(int positiveSamples, int totalSamples, OneSampleHypothesis alternate) { Compute(positiveSamples, totalSamples, alternate); }