Exemple #1
0
        public void ZTestConstructorTest()
        {
            // This example has been gathered from the Wikipedia's page about
            // the Z-Test, available from: http://en.wikipedia.org/wiki/Z-test

            // Suppose there is a text comprehension test being run across
            // a given demographic region. The mean score of the population
            // from this entire region are around 100 points, with a standard
            // deviation of 12 points.

            // There is a local school, however, whose 55 students attained
            // an average score in the test of only about 96 points. Would 
            // their scores be surprisingly that low, or could this event
            // have happened due to chance?

            // So we would like to check that a sample of
            // 55 students with a mean score of 96 points:

            int sampleSize = 55;
            double sampleMean = 96;

            // Was expected to have happened by chance in a population with
            // an hypothesized mean of 100 points and standard deviation of
            // about 12 points:

            double standardDeviation = 12;
            double hypothesizedMean = 100;


            // So we start by creating the test:
            ZTest test = new ZTest(sampleMean, standardDeviation, sampleSize,
                hypothesizedMean, OneSampleHypothesis.ValueIsSmallerThanHypothesis);

            // Now, we can check whether this result would be
            // unlikely under a standard significance level:

            bool significant  = test.Significant;

            // We can also check the test statistic and its P-Value
            double statistic = test.Statistic;
            double pvalue = test.PValue;

            Assert.AreEqual(statistic, -2.47, 0.01);
            Assert.AreEqual(pvalue, 0.0068, 0.001);

            /* This is the one-sided p-value for the null hypothesis that the 55 students 
             * are comparable to a simple random sample from the population of all test-takers.
             * The two-sided p-value is approximately 0.014 (twice the one-sided p-value).
             */

            test = new ZTest(sampleMean, standardDeviation, sampleSize, hypothesizedMean,
                OneSampleHypothesis.ValueIsDifferentFromHypothesis);

            Assert.AreEqual(test.Statistic, -2.47, 0.01);
            Assert.AreEqual(test.PValue, 0.014, 0.005);

        }
Exemple #2
0
 /// <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(ZTest.StatisticToPValue(x, Tail));
 }
Exemple #3
0
 /// <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(ZTest.PValueToStatistic(p, Tail));
 }
Exemple #4
0
        public void PValueToStatisticTest()
        {
            double p = 0.05;
            double z = 0;
            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
                Assert.AreEqual(DistributionTail.TwoTail, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = 1.96;
                Assert.AreEqual(expected, actual, 0.01);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsSmallerThanHypothesis);
                Assert.AreEqual(DistributionTail.OneLower, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = -1.6449;
                Assert.AreEqual(expected, actual, 1e-4);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsGreaterThanHypothesis);
                Assert.AreEqual(DistributionTail.OneUpper, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = 1.6449;
                Assert.AreEqual(expected, actual, 1e-4);
            }

            p = 0.95;
            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
                Assert.AreEqual(DistributionTail.TwoTail, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = 0.0627;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsSmallerThanHypothesis);
                Assert.AreEqual(DistributionTail.OneLower, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = 1.6449;
                Assert.AreEqual(expected, actual, 1e-4);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsGreaterThanHypothesis);
                Assert.AreEqual(DistributionTail.OneUpper, target.Tail);
                double actual = target.PValueToStatistic(p);
                double expected = -1.6449;
                Assert.AreEqual(expected, actual, 1e-4);
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        public void StatisticToPValueTest()
        {
            double z = 1.96;
            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
                Assert.AreEqual(DistributionTail.TwoTail, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.05;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsSmallerThanHypothesis);
                Assert.AreEqual(DistributionTail.OneLower, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.975;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsGreaterThanHypothesis);
                Assert.AreEqual(DistributionTail.OneUpper, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.025;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            z = -1.96;
            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
                Assert.AreEqual(DistributionTail.TwoTail, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.05;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsSmallerThanHypothesis);
                Assert.AreEqual(DistributionTail.OneLower, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.025;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                ZTest target = new ZTest(z, OneSampleHypothesis.ValueIsGreaterThanHypothesis);
                Assert.AreEqual(DistributionTail.OneUpper, target.Tail);
                double actual = target.StatisticToPValue(z);
                double expected = 0.975;
                Assert.AreEqual(expected, actual, 1e-5);
            }
        }
Exemple #8
0
        public void SampleSizeTest1()
        {
            // Example from http://wise.cgu.edu/powermod/computing.asp

            double mean = 104;
            double stdDev = 10;
            int samples = 25;
            double hypothesizedMean = 100;

            OneSampleHypothesis hypothesis = OneSampleHypothesis.ValueIsGreaterThanHypothesis;
            ZTest target = new ZTest(mean, stdDev, samples, hypothesizedMean, hypothesis);

            ZTestPowerAnalysis power = (ZTestPowerAnalysis)target.Analysis;

            power.Size = 0.01;
            power.Power = 0.90;
            power.ComputeSamples();

            double sampleSize = System.Math.Ceiling(power.Samples);

            Assert.AreEqual(82, sampleSize, 1e-16);
        }
Exemple #9
0
        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);
        }
Exemple #10
0
        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);
        }
Exemple #11
0
        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);
        }
Exemple #12
0
        public void ZTestConstructorTest()
        {
            /* Suppose that in a particular geographic region, the mean and standard
             * deviation of scores on a reading test are 100 points, and 12 points, 
             * respectively. Our interest is in the scores of 55 students in a particular
             * school who received a mean score of 96. We can ask whether this mean score
             * is significantly lower than the regional mean — that is, are the students
             * in this school comparable to a simple random sample of 55 students from the
             * region as a whole, or are their scores surprisingly low?
             */

            ZTest target;
            
            target = new ZTest(100, 12, 96, 55, Hypothesis.OneLower);
            Assert.AreEqual(target.Statistic, -2.47, 0.01);

            
            Assert.AreEqual(target.PValue, 0.0068, 0.001);

            /* This is the one-sided p-value for the null hypothesis that the 55 students 
             * are comparable to a simple random sample from the population of all test-takers.
             * The two-sided p-value is approximately 0.014 (twice the one-sided p-value).
             */

            target = new ZTest(100, 12, 96, 55, Hypothesis.TwoTail);
            Assert.AreEqual(target.PValue, 0.014, 0.005);

            
        }