/// <summary> /// Creates a new test for two ROC curves. /// </summary> /// /// <param name="curve1">The first ROC curve.</param> /// <param name="curve2">The second ROC curve.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two areas.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoReceiverOperatingCurveTest(ReceiverOperatingCharacteristic curve1, ReceiverOperatingCharacteristic curve2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { this.Curve1 = curve1; this.Curve2 = curve2; double[] Vx1 = curve1.NegativeAccuracies; double[] Vy1 = curve1.PositiveAccuracies; double[] Vx2 = curve2.NegativeAccuracies; double[] Vy2 = curve2.PositiveAccuracies; double covx = Measures.Covariance(Vx1, Vx2); double covy = Measures.Covariance(Vy1, Vy2); double cov = covx / Vx1.Length + covy / Vy1.Length; this.EstimatedValue1 = curve1.Area; this.EstimatedValue2 = curve2.Area; this.ObservedDifference = EstimatedValue1 - EstimatedValue2; this.HypothesizedDifference = hypothesizedDifference; this.Variance1 = curve1.Variance; this.Variance2 = curve2.Variance; this.OverallVariance = Variance1 + Variance2 - 2 * cov; this.StandardError = System.Math.Sqrt(OverallVariance); // Compute Z statistic double z = (ObservedDifference - HypothesizedDifference) / StandardError; Compute(z, alternate); }
public TwoSampleHypothesisTestResult TestHypothesis(IEnumerable <double> sample1, IEnumerable <double> sample2, double hypothesizedDifference, TwoSampleHypothesis alternateHypothesis, double alpha) { var sample1Collection = sample1 as ICollection <double> ?? sample1.ToArray(); var sample2Collection = sample2 as ICollection <double> ?? sample2.ToArray(); ITwoSampleNormalDistributionHypothesisTest test; if (sample1Collection.Count < 30 || sample2Collection.Count < 30) { test = new TwoSampleTTestHypothesisTest(); } else { test = new TwoSampleZTestHypothesisTest(); } return(test .TestHypothesis( sample1Collection, sample2Collection, hypothesizedDifference, alternateHypothesis, alpha)); }
public static bool RunConcurrentPerformanceComparison(int numIterations, int degreeParallelism, Action firstOperation, Action secondOperation, double hypothesizedDifference = 0, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent, bool outputDetails = false) { return RunConcurrentPerformanceComparison(numIterations, degreeParallelism, "first", firstOperation, "second", secondOperation, hypothesizedDifference, hypothesis, outputDetails); }
/// <summary> /// Tests whether two samples comes from the /// same distribution without assuming normality. /// </summary> /// /// <param name="sample1">The first sample.</param> /// <param name="sample2">The second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// <param name="exact">True to compute the exact distribution. May require a significant /// amount of processing power for large samples (n > 12). If left at null, whether to /// compute the exact or approximate distribution will depend on the number of samples. /// Default is null.</param> /// <param name="adjustForTies">Whether to account for ties when computing the /// rank statistics or not. Default is true.</param> /// public MannWhitneyWilcoxonTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent, bool?exact = null, bool adjustForTies = true) { this.NumberOfSamples1 = sample1.Length; this.NumberOfSamples2 = sample2.Length; int n = NumberOfSamples1 + NumberOfSamples2; // Concatenate both samples and rank them double[] samples = sample1.Concatenate(sample2); double[] rank = samples.Rank(adjustForTies: true); // Split the rankings back and sum Rank1 = rank.Get(0, NumberOfSamples1); Rank2 = rank.Get(NumberOfSamples1, 0); // Compute rank sum statistic this.RankSum1 = Rank1.Sum(); this.RankSum2 = Rank2.Sum(); // It is not necessary to compute the sum for both ranks. The sum of ranks in the second // sample can be determined from the first, since the sum of all the ranks equals n(n+1)/2 Accord.Diagnostics.Debug.Assert(RankSum2 == n * (n + 1) / 2 - RankSum1); // The U statistic can be obtained from the sum of the ranks in the sample, // minus the smallest value it can take (i.e. minus (n1 * (n1 + 1)) / 2.0), // meaning there is an wasy way to convert from W to U: // Compute Mann-Whitney's U statistic from the rank sum // as in Zar, Jerrold H. Biostatistical Analysis, 1998: this.Statistic1 = RankSum1 - (NumberOfSamples1 * (NumberOfSamples1 + 1)) / 2.0; // U1 this.Statistic2 = RankSum2 - (NumberOfSamples2 * (NumberOfSamples2 + 1)) / 2.0; // U2 // Again, it would not be necessary to compute U2 due the relation: Accord.Diagnostics.Debug.Assert(Statistic1 + Statistic2 == NumberOfSamples1 * NumberOfSamples2); Accord.Diagnostics.Debug.Assert(Statistic1 == MannWhitneyDistribution.MannWhitneyU(Rank1)); Accord.Diagnostics.Debug.Assert(Statistic2 == MannWhitneyDistribution.MannWhitneyU(Rank2)); // http://users.sussex.ac.uk/~grahamh/RM1web/WilcoxonHandoout2011.pdf // https://onlinecourses.science.psu.edu/stat464/node/38 // http://www.real-statistics.com/non-parametric-tests/wilcoxon-rank-sum-test/ // http://personal.vu.nl/R.Heijungs/QM/201516/stat/bs/documents/Supplement%2016B%20-%20Wilcoxon%20Mann-Whitney%20Small%20Sample%20Test.pdf // http://www.real-statistics.com/non-parametric-tests/wilcoxon-rank-sum-test/wilcoxon-rank-sum-exact-test/ // The smaller value of U1 and U2 is the one used when using significance tables this.Statistic = (NumberOfSamples1 < NumberOfSamples2) ? Statistic1 : Statistic2; this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.StatisticDistribution = new MannWhitneyDistribution(Rank1, Rank2, exact) { Correction = (Tail == DistributionTail.TwoTail) ? ContinuityCorrection.Midpoint : ContinuityCorrection.KeepInside }; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Constructs a Z test. /// </summary> /// /// <param name="sample1">The first data sample.</param> /// <param name="sample2">The second data sample.</param> /// <param name="hypothesizedDifference">The hypothesized sample difference.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoSampleZTest(double[] sample1, double[] sample2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { int samples1 = sample1.Length; int samples2 = sample2.Length; if (samples1 < 30 || samples2 < 30) { System.Diagnostics.Trace.TraceWarning( "Warning: running a Z test for less than 30 samples. Consider running a Student's T Test instead."); } double mean1 = Tools.Mean(sample1); double mean2 = Tools.Mean(sample2); double var1 = Tools.Variance(sample1, mean1); double var2 = Tools.Variance(sample2, mean2); double sqStdError1 = var1 / sample1.Length; double sqStdError2 = var2 / sample2.Length; this.Compute(mean1, mean2, sqStdError1, sqStdError2, hypothesizedDifference, alternate); this.power(var1, var2, sample1.Length, sample2.Length); }
/// <summary> /// Tests whether two samples comes from the /// same distribution without assuming normality. /// </summary> /// /// <param name="sample1">The first sample.</param> /// <param name="sample2">The second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public MannWhitneyWilcoxonTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { int n1 = sample1.Length; int n2 = sample2.Length; int n = n1 + n2; // Concatenate both samples and rank them double[] samples = sample1.Concatenate(sample2); double[] rank = Accord.Statistics.Tools.Rank(samples); // Split the rankings back and sum Rank1 = rank.Submatrix(0, n1 - 1); Rank2 = rank.Submatrix(n1, n - 1); double t1 = RankSum1 = Rank1.Sum(); double t2 = RankSum2 = Rank2.Sum(); // Estimated values for t under the null double t1max = n1 * n2 + (n1 * (n1 + 1)) / 2.0; double t2max = n1 * n2 + (n2 * (n2 + 1)) / 2.0; // Diff in observed t and estimated t double u1 = Statistic1 = t1max - t1; double u2 = Statistic2 = t2max - t2; double hypothesizedValue = (n1 * n2) / 2.0; Compute(u1, rank, n1, n2, alternate); }
/// <summary> /// Tests whether the means of two samples are different. /// </summary> /// <param name="mean1">The first sample's mean.</param> /// <param name="mean2">The second sample's mean.</param> /// <param name="var1">The first sample's variance.</param> /// <param name="var2">The second sample's variance.</param> /// <param name="samples1">The number of observations in the first sample.</param> /// <param name="samples2">The number of observations in the second sample.</param> /// <param name="assumeEqualVariances">True assume equal variances, false otherwise. Default is true.</param> /// <param name="hypothesizedDifference">The hypothesized sample difference.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> public TwoSampleTTest(double mean1, double var1, int samples1, double mean2, double var2, int samples2, bool assumeEqualVariances = true, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { Compute(mean1, var1, samples1, mean2, var2, samples2, hypothesizedDifference, assumeEqualVariances, alternate); }
public static bool RunConcurrentPerformanceComparison(int numIterations, int degreeParallelism, Action firstOperation, Action secondOperation, double hypothesizedDifference = 0, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent, bool outputDetails = false) { return(RunConcurrentPerformanceComparison(numIterations, degreeParallelism, "first", firstOperation, "second", secondOperation, hypothesizedDifference, hypothesis, outputDetails)); }
/// <summary> /// Computes the two sample sign test. /// </summary> /// protected void Compute(int positive, int total, TwoSampleHypothesis 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 TwoSampleHypothesis.ValuesAreDifferent: binomialHypothesis = OneSampleHypothesis.ValueIsDifferentFromHypothesis; break; case TwoSampleHypothesis.FirstValueIsGreaterThanSecond: binomialHypothesis = OneSampleHypothesis.ValueIsSmallerThanHypothesis; break; case TwoSampleHypothesis.FirstValueIsSmallerThanSecond: binomialHypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis; break; default: throw new InvalidOperationException(); } base.Compute(positive, total, 0.5, binomialHypothesis); }
/// <summary> /// Creates a new sign test for two samples. /// </summary> /// /// <param name="sample1">The first sample of observations.</param> /// <param name="sample2">The second sample of observations.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoSampleSignTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate) { if (sample1.Length != sample2.Length) { throw new DimensionMismatchException("sample2", "Both samples should be of the same size."); } int positive = 0; int negative = 0; for (int i = 0; i < sample1.Length; i++) { double d = sample2[i] - sample1[i]; if (d > 0) { positive++; } else if (d < 0) { negative++; } } Compute(positive, positive + negative, alternate); }
/// <summary> /// Creates a new paired t-test. /// </summary> /// /// <param name="sample1">The observations in the first sample.</param> /// <param name="sample2">The observations in the second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public PairedTTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { if (sample1.Length != sample2.Length) { throw new DimensionMismatchException("sample2", "Paired samples must have the same size."); } int n = sample1.Length; double[] delta = new double[sample1.Length]; for (int i = 0; i < delta.Length; i++) { delta[i] = sample1[i] - sample2[i]; } double mean = delta.Mean(); double std = delta.StandardDeviation(); StandardError = std / Math.Sqrt(n); ObservedDifference = mean; Mean1 = sample1.Mean(); Mean2 = sample2.Mean(); SampleSize = n; Statistic = ObservedDifference / StandardError; StatisticDistribution = new TDistribution(n - 1); this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Creates a new Two-Table Mean Kappa test. /// </summary> /// /// <param name="matrices1">The first group of contingency tables.</param> /// <param name="matrices2">The second group of contingency tables.</param> /// <param name="assumeEqualVariances">True to assume equal variances, false otherwise. Default is true.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two average Kappa values.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoAverageKappaTest(GeneralConfusionMatrix[] matrices1, GeneralConfusionMatrix[] matrices2, double hypothesizedDifference = 0, bool assumeEqualVariances = true, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { double[] kappas1 = new double[matrices1.Length]; for (int i = 0; i < matrices1.Length; i++) { kappas1[i] = matrices1[i].Kappa; } double[] kappas2 = new double[matrices1.Length]; for (int i = 0; i < matrices2.Length; i++) { kappas2[i] = matrices2[i].Kappa; } double meanKappa1 = kappas1.Mean(); double meanKappa2 = kappas2.Mean(); Variance1 = kappas1.Variance(meanKappa1); Variance2 = kappas2.Variance(meanKappa2); int kappaSamples1 = matrices1.Length; int kappaSamples2 = matrices2.Length; base.Compute( meanKappa1, Variance1, kappaSamples1, meanKappa2, Variance2, kappaSamples2, hypothesizedDifference, assumeEqualVariances, alternate); }
/// <summary> /// Creates a new Z-Test for two sample proportions. /// </summary> /// /// <param name="proportion1">The proportion of success observations in the first sample.</param> /// <param name="sampleSize1">The total number of observations in the first sample.</param> /// <param name="proportion2">The proportion of success observations in the second sample.</param> /// <param name="sampleSize2">The total number of observations in the second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoProportionZTest(double proportion1, int sampleSize1, double proportion2, int sampleSize2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { int x1 = (int)Math.Round(proportion1 * sampleSize1); int x2 = (int)Math.Round(proportion2 * sampleSize2); Compute(x1, sampleSize1, x2, sampleSize2, alternate); }
/// <summary> /// Tests whether the means of two samples are different. /// </summary> /// <param name="sample1">The first sample.</param> /// <param name="sample2">The second sample.</param> /// <param name="hypothesizedDifference">The hypothesized sample difference.</param> /// <param name="assumeEqualVariances">True to assume equal variances, false otherwise. Default is true.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> public TwoSampleZTest(DescriptiveResult sample1, DescriptiveResult sample2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { // References: http://en.wikipedia.org/wiki/Student's_t-test#Worked_examples Compute(sample1.Mean, sample2.Mean, sample1.Variance / sample1.Count, sample2.Variance / sample2.Count, hypothesizedDifference, alternate); }
public BothLatencyValidator( double alpha, TwoSampleHypothesis alternateHypothesis, // TODO: P2 - per the link, appears this may be the number of standard deviations? http://accord-framework.net/docs/html/T_Accord_Statistics_Testing_Power_TwoSampleTTestPowerAnalysis.htm double minimumDetectableDifferenceDesired = 0.00001, // Some number larger than 0 double testStatisticalPower = 0.8 // the test power that we want. 0.8 is a standard - 0.9 is more conservative -- 1 - probability of rejecting the null hypothesis when the null hypothesis is actually false ) : this(alpha, alternateHypothesis, 0.Percent(), minimumDetectableDifferenceDesired, testStatisticalPower) { }
/// <summary> /// Computes the Z test. /// </summary> protected void Compute(double statistic, TwoSampleHypothesis alternate) { Statistic = statistic; StatisticDistribution = NormalDistribution.Standard; Hypothesis = alternate; PValue = StatisticToPValue(Statistic); }
/// <summary> /// Computes the F-test. /// </summary> /// protected void Compute(double statistic, int d1, int d2, TwoSampleHypothesis alternate) { this.Statistic = statistic; this.StatisticDistribution = new FDistribution(d1, d2); this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Computes the Mann-Whitney-Wilcoxon test. /// </summary> /// protected void Compute(double statistic, double[] ranks, int n1, int n2, TwoSampleHypothesis alternate) { this.Statistic = statistic; this.StatisticDistribution = new MannWhitneyDistribution(ranks, n1, n2); this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Computes the Z test. /// </summary> /// protected void Compute(double observedDifference, double hypothesizedDifference, double standardError, TwoSampleHypothesis alternate) { this.ObservedDifference = observedDifference; this.HypothesizedDifference = hypothesizedDifference; this.StandardError = standardError; // Compute Z statistic double z = (ObservedDifference - HypothesizedDifference) / StandardError; Compute(z, alternate); }
public GeneralHypothesisTest(string firstLabel, DescriptiveResult sample1, string secondLabel, DescriptiveResult sample2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { int samples1 = sample1.Count; int samples2 = sample2.Count; HypothesizedDifference = Math.Abs(hypothesizedDifference); var s1 = new SampleInfo { Name = firstLabel, Count = sample1.Count, Mean = sample1.Mean, StdDev = sample1.StdDev }; var s2 = new SampleInfo { Name = secondLabel, Count = sample2.Count, Mean = sample2.Mean, StdDev = sample2.StdDev }; Result = new ComparisonResult { FirstSample = s1, SecondSample = s2, Hypothesis = alternate, HypothesizedDifference = HypothesizedDifference }; if (samples1 < 30 || samples2 < 30) { _tTest = new TwoSampleTTest(sample1, sample2, false, HypothesizedDifference, alternate); Result.Confidence = _tTest.Confidence; Result.ObservedDifference = _tTest.ObservedDifference; Result.Significant = _tTest.Significant; Result.Size = _tTest.Size; Result.StandardError = _tTest.StandardError; } else { _zTest = new TwoSampleZTest(sample1, sample2, HypothesizedDifference, alternate); Result.Confidence = _zTest.Confidence; Result.ObservedDifference = _zTest.ObservedDifference; Result.Significant = _zTest.Significant; Result.Size = _zTest.Size; Result.StandardError = _zTest.StandardError; } }
/// <summary> /// Computes the Z test. /// </summary> /// protected void Compute(double statistic, TwoSampleHypothesis alternate) { this.Statistic = statistic; this.StatisticDistribution = NormalDistribution.Standard; this.Hypothesis = alternate; this.Tail = (DistributionTail)alternate; this.PValue = StatisticToPValue(Statistic); this.OnSizeChanged(); }
/// <summary> /// Computes the Z test. /// </summary> /// protected void Compute(double value1, double value2, double squareStdError1, double squareStdError2, double hypothesizedDifference, TwoSampleHypothesis alternate) { this.EstimatedValue1 = value1; this.EstimatedValue2 = value2; double diff = value1 - value2; double stdError = Math.Sqrt(squareStdError1 + squareStdError2); Compute(diff, hypothesizedDifference, stdError, alternate); }
/// <summary> /// Creates a new Two-Table Mean Kappa test. /// </summary> /// /// <param name="meanKappa1">The average kappa value for the first group of contingency tables.</param> /// <param name="meanKappa2">The average kappa value for the second group of contingency tables.</param> /// <param name="varKappa1">The kappa's variance in the first group of tables.</param> /// <param name="varKappa2">The kappa's variance in the first group of tables.</param> /// <param name="kappaSamples1">The number of contingency tables averaged in the first group.</param> /// <param name="kappaSamples2">The number of contingency tables averaged in the second group.</param> /// <param name="assumeEqualVariances">True to assume equal variances, false otherwise. Default is true.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two Kappa values.</param> /// public TwoAverageKappaTest(double meanKappa1, double varKappa1, int kappaSamples1, double meanKappa2, double varKappa2, int kappaSamples2, double hypothesizedDifference = 0, bool assumeEqualVariances = true, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { Variance1 = varKappa1; Variance2 = varKappa2; base.Compute( meanKappa1, varKappa1, kappaSamples1, meanKappa2, varKappa2, kappaSamples2, hypothesizedDifference, assumeEqualVariances, alternate); }
public static TwoSampleHypothesisTestResult TestHypothesis( this ITwoSampleNormalDistributionHypothesisTest source, BenchmarkResults.BeforeAndAfter resultMeasurement, double hypothesizedDifference, TwoSampleHypothesis alternateHypothesis, double alpha) { return(source.TestHypothesis( resultMeasurement.Baseline.GetResultRuns().Select(run => MeasurementExtensions.GetAverageNanoseconds(run)), resultMeasurement.Treatment.GetResultRuns().Select(run => run.GetAverageNanoseconds()), hypothesizedDifference, alternateHypothesis, alpha)); }
public TwoSampleHypothesisTestResult TestHypothesis(IEnumerable <double> sample1, IEnumerable <double> sample2, double hypothesizedDifference, TwoSampleHypothesis alternateHypothesis, double alpha) { var test = new TwoSampleZTest( sample1.ToArray(), sample2.ToArray(), hypothesizedDifference: hypothesizedDifference, alternate: alternateHypothesis); test.Size = alpha; return(new TwoSampleHypothesisTestResult( test.Significant, test.GetConfidenceInterval(1 - alpha), test.ObservedDifference)); }
/// <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> /// <param name="proportion">The proportion of observations in the second group /// when compared to the first group. A proportion of 2:1 results in twice more /// samples in the second group than in the first. Default is 1.</param> /// /// <returns>The required number of samples.</returns> /// public static TwoSampleZTestPowerAnalysis GetSampleSize(double delta, double standardDeviation = 1, double proportion = 1.0, double power = 0.8, double alpha = 0.05, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var analysis = new TwoSampleZTestPowerAnalysis(hypothesis) { Effect = (delta) / standardDeviation, Size = alpha, Power = power, }; analysis.ComputeSamples(proportion); return(analysis); }
public static ComparisonResult RunConcurrentPerformanceComparison(int numIterations, int degreeParallelism, string firstLabel, Action firstOperation, string secondLabel, Action secondOperation, double hypothesizedDifference = 0, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var result1 = RunConcurrentPerformanceTest(numIterations, degreeParallelism, firstOperation); var result2 = RunConcurrentPerformanceTest(numIterations, degreeParallelism, secondOperation); var comparison = new GeneralHypothesisTest(firstLabel, result1.DescriptiveResult, secondLabel, result2.DescriptiveResult, hypothesizedDifference, hypothesis); comparison.Result.FirstSample.Details = result1; comparison.Result.SecondSample.Details = result2; return(comparison.Result); }
/// <summary> /// Creates a new Two-Table Kappa test. /// </summary> /// /// <param name="kappa1">The kappa value for the first contingency table to test.</param> /// <param name="kappa2">The kappa value for the second contingency table to test.</param> /// <param name="var1">The variance of the kappa value for the first contingency table to test.</param> /// <param name="var2">The variance of the kappa value for the second contingency table to test.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two Kappa values.</param> /// public TwoMatrixKappaTest(double kappa1, double var1, double kappa2, double var2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { this.EstimatedValue1 = kappa1; this.EstimatedValue2 = kappa2; this.Variance1 = var1; this.Variance2 = var2; this.OverallVariance = Variance1 + Variance2; double diff = Math.Abs(EstimatedValue1 - EstimatedValue2); double stdError = Math.Sqrt(OverallVariance); Compute(diff, hypothesizedDifference, stdError, alternate); }
/// <summary> /// Creates a new Two-Table Kappa test. /// </summary> /// /// <param name="matrix1">The first contingency table to test.</param> /// <param name="matrix2">The second contingency table to test.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two Kappa values.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoMatrixKappaTest(GeneralConfusionMatrix matrix1, GeneralConfusionMatrix matrix2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { this.EstimatedValue1 = matrix1.Kappa; this.EstimatedValue2 = matrix2.Kappa; this.Variance1 = matrix1.Variance; this.Variance2 = matrix2.Variance; this.OverallVariance = Variance1 + Variance2; double diff = Math.Abs(EstimatedValue1 - EstimatedValue2); double stdError = Math.Sqrt(OverallVariance); Compute(diff, hypothesizedDifference, stdError, alternate); }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="sampleSize1">The number of observations in the first sample.</param> /// <param name="sampleSize2">The number of observations in the second 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 TwoSampleZTestPowerAnalysis GetEffectSize(int sampleSize1, int sampleSize2, double power = 0.8, double alpha = 0.05, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var analysis = new TwoSampleZTestPowerAnalysis(hypothesis) { Samples1 = sampleSize1, Samples2 = sampleSize2, Size = alpha, Power = power, }; analysis.ComputeEffect(); return(analysis); }
public void WilcoxonSignedRankTestConstructorTest() { // Example from http://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test double[] sample1 = { 125, 115, 130, 140, 140, 115, 140, 125, 140, 135 }; double[] sample2 = { 110, 122, 125, 120, 140, 124, 123, 137, 135, 145 }; TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent; var target = new TwoSampleWilcoxonSignedRankTest(sample1, sample2, alternate); // Wikipedia uses an alternate definition for the W statistic. The framework // uses the positive W statistic. There is no difference, if the proper // respective statistical tables (or distributions) are followed. Assert.AreEqual(27, target.Statistic); Assert.IsFalse(target.Significant); }
public static ComparisonResult RunConcurrentPerformanceComparison(int numIterations, int degreeParallelism, string firstLabel, Action firstOperation, string secondLabel, Action secondOperation, double hypothesizedDifference = 0, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var result1 = RunConcurrentPerformanceTest(numIterations, degreeParallelism, firstOperation); var result2 = RunConcurrentPerformanceTest(numIterations, degreeParallelism, secondOperation); var comparison = new GeneralHypothesisTest(firstLabel, result1.DescriptiveResult, secondLabel, result2.DescriptiveResult, hypothesizedDifference, hypothesis); comparison.Result.FirstSample.Details = result1; comparison.Result.SecondSample.Details = result2; return comparison.Result; }
/// <summary> /// Creates a new sign test for two samples. /// </summary> /// /// <param name="sample1">The first sample of observations.</param> /// <param name="sample2">The second sample of observations.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoSampleSignTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate) { if (sample1.Length != sample2.Length) throw new DimensionMismatchException("sample2", "Both samples should be of the same size."); int positive = 0; int negative = 0; for (int i = 0; i < sample1.Length; i++) { double d = sample2[i] - sample1[i]; if (d > 0) positive++; else if (d < 0) negative++; } Compute(positive, positive + negative, alternate); }
/// <summary> /// Computes the Z-test for two sample proportions. /// </summary> /// protected void Compute( int successes1, int trials1, int successes2, int trials2, TwoSampleHypothesis alternate) { double p1 = successes1 / (double)trials1; double p2 = successes2 / (double)trials2; double p = (successes1 + successes2) / (double)(trials1 + trials2); EstimatedValue1 = p1; EstimatedValue2 = p2; ObservedDifference = p1 - p2; StandardError = Math.Sqrt(p * (1 - p) * (1.0 / trials1 + 1.0 / trials2)); double z = ObservedDifference / StandardError; Compute(z, alternate); }
public static bool RunConcurrentPerformanceComparison(int numIterations, int degreeParallelism, string firstLabel, Action firstOperation, string secondLabel, Action secondOperation, double hypothesizedDifference = 0, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent, bool outputDetails = false) { var comparison = RunConcurrentPerformanceComparison(numIterations, degreeParallelism, firstLabel, firstOperation, secondLabel, secondOperation, hypothesizedDifference, hypothesis); if (outputDetails) { Console.WriteLine(comparison.ToString()); Console.WriteLine("-----{0}-----", firstLabel); Console.WriteLine(comparison.FirstSample.Details.ToString()); Console.WriteLine("-----{0}-----", secondLabel); Console.WriteLine(comparison.SecondSample.Details.ToString()); } return comparison.Significant; }
/// <summary> /// Tests whether the medians of two paired samples are different. /// </summary> /// /// <param name="sample1">The first sample.</param> /// <param name="sample2">The second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoSampleWilcoxonSignedRankTest(double[] sample1, double[] sample2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { if (sample1.Length != sample2.Length) throw new DimensionMismatchException("sample2", "Both samples should be of the same size."); int[] signs = new int[sample1.Length]; double[] diffs = new double[sample1.Length]; // 1. Compute absolute difference and sign function for (int i = 0; i < sample1.Length; i++) { double d = sample1[i] - sample2[i]; signs[i] = Math.Sign(d); diffs[i] = Math.Abs(d); } this.Hypothesis = alternate; Compute(signs, diffs, (DistributionTail)alternate); }
public GeneralHypothesisTest(DescriptiveResult sample1, DescriptiveResult sample2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) : this("first", sample1, "second", sample2, hypothesizedDifference, alternate) { }
/// <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="variance1">The first sample variance.</param> /// <param name="variance2">The second sample variance.</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="proportion">The proportion of observations in the second group /// when compared to the first group. A proportion of 2:1 results in twice more /// samples in the second group than in the first. Default is 1.</param> /// <param name="hypothesis">The alternative hypothesis (research hypothesis) to be tested.</param> /// /// <returns>The required number of samples.</returns> /// public static TwoSampleTTestPowerAnalysis GetSampleSize(double delta, double variance1, double variance2, double proportion = 1.0, double power = 0.8, double alpha = 0.05, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { double standardDeviation = Math.Sqrt((variance1 + variance2) / 2.0); var analysis = new TwoSampleTTestPowerAnalysis(hypothesis) { Effect = delta / standardDeviation, Size = alpha, Power = power, }; analysis.ComputeSamples(proportion); return analysis; }
/// <summary> /// Creates a new Two-Table Mean Kappa test. /// </summary> /// /// <param name="matrices1">The first group of contingency tables.</param> /// <param name="matrices2">The second group of contingency tables.</param> /// <param name="assumeEqualVariances">True to assume equal variances, false otherwise. Default is true.</param> /// <param name="hypothesizedDifference">The hypothesized difference between the two average Kappa values.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoAverageKappaTest(GeneralConfusionMatrix[] matrices1, GeneralConfusionMatrix[] matrices2, double hypothesizedDifference = 0, bool assumeEqualVariances = true, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { double[] kappas1 = new double[matrices1.Length]; for (int i = 0; i < matrices1.Length; i++) kappas1[i] = matrices1[i].Kappa; double[] kappas2 = new double[matrices1.Length]; for (int i = 0; i < matrices2.Length; i++) kappas2[i] = matrices2[i].Kappa; double meanKappa1 = kappas1.Mean(); double meanKappa2 = kappas2.Mean(); Variance1 = kappas1.Variance(meanKappa1); Variance2 = kappas2.Variance(meanKappa2); int kappaSamples1 = matrices1.Length; int kappaSamples2 = matrices2.Length; base.Compute( meanKappa1, Variance1, kappaSamples1, meanKappa2, Variance2, kappaSamples2, hypothesizedDifference, assumeEqualVariances, alternate); }
/// <summary> /// Creates a new sign test for two samples. /// </summary> /// /// <param name="positiveSamples">The number of positive samples (successes).</param> /// <param name="totalSamples">The total number of samples (trials).</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoSampleSignTest(int positiveSamples, int totalSamples, TwoSampleHypothesis alternate) { Compute(positiveSamples, totalSamples, alternate); }
/// <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> /// <param name="proportion">The proportion of observations in the second group /// when compared to the first group. A proportion of 2:1 results in twice more /// samples in the second group than in the first. Default is 1.</param> /// /// <returns>The required number of samples.</returns> /// public static TwoSampleZTestPowerAnalysis GetSampleSize(double delta, double standardDeviation = 1, double proportion = 1.0, double power = 0.8, double alpha = 0.05, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var analysis = new TwoSampleZTestPowerAnalysis(hypothesis) { Effect = (delta) / standardDeviation, Size = alpha, Power = power, }; analysis.ComputeSamples(proportion); return analysis; }
/// <summary> /// Estimates the number of samples necessary to attain the /// required power level for the given effect size. /// </summary> /// /// <param name="sampleSize1">The number of observations in the first sample.</param> /// <param name="sampleSize2">The number of observations in the second 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 TwoSampleZTestPowerAnalysis GetEffectSize(int sampleSize1, int sampleSize2, double power = 0.8, double alpha = 0.05, TwoSampleHypothesis hypothesis = TwoSampleHypothesis.ValuesAreDifferent) { var analysis = new TwoSampleZTestPowerAnalysis(hypothesis) { Samples1 = sampleSize1, Samples2 = sampleSize2, Size = alpha, Power = power, }; analysis.ComputeEffect(); return analysis; }
/// <summary> /// Creates a new <see cref="ZTestPowerAnalysis"/>. /// </summary> /// /// <param name="hypothesis">The hypothesis tested.</param> /// public TwoSampleZTestPowerAnalysis(TwoSampleHypothesis hypothesis) : base((DistributionTail)hypothesis) { }
/// <summary> /// Creates a new Z-Test for two sample proportions. /// </summary> /// /// <param name="successes1">The number of successes in the first sample.</param> /// <param name="trials1">The total number of trials (observations) in the first sample.</param> /// <param name="successes2">The number of successes in the second sample.</param> /// <param name="trials2">The total number of trials (observations) in the second sample.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> /// public TwoProportionZTest(int successes1, int trials1, int successes2, int trials2, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { Compute(successes1, trials1, successes2, trials2, alternate); }