示例#1
0
        private void power(double stdDev, int samples)
        {
            this.powerAnalysis = new TTestPowerAnalysis(Hypothesis)
            {
                Samples = samples,
                Effect  = (EstimatedValue - HypothesizedValue) / stdDev,
                Size    = Size,
            };

            powerAnalysis.ComputePower();
        }
示例#2
0
        public void TTestPowerAnalysisConstructorTest3()
        {
            // Examples from R's graphical manual
            // http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=pwr:pwr.t.test

            double actual, expected;

            {
                var target = new TTestPowerAnalysis(OneSampleHypothesis.ValueIsDifferentFromHypothesis)
                {
                    Effect  = 0.2,
                    Samples = 60,
                    Size    = 0.10,
                };

                target.ComputePower();

                expected = 0.4555818;
                actual   = target.Power;
                Assert.AreEqual(expected, actual, 1e-5);
            }

            {
                var target = new TwoSampleTTestPowerAnalysis(TwoSampleHypothesis.ValuesAreDifferent)
                {
                    Effect   = 2 / 2.8,
                    Samples1 = 30,
                    Samples2 = 30,
                };

                target.ComputePower();

                expected = 0.7764889;
                actual   = target.Power;
                Assert.AreEqual(expected, actual, 1e-6);
            }

            {
                var target = new TwoSampleTTestPowerAnalysis(TwoSampleHypothesis.FirstValueIsGreaterThanSecond)
                {
                    Effect = 0.3,
                    Power  = 0.75,
                };

                target.ComputeSamples();

                expected = 120.2232016;
                actual   = target.Samples1;
                Assert.AreEqual(expected, actual, 1e-6);
                Assert.AreEqual(target.Samples1, target.Samples2);
            }
        }
示例#3
0
        public void TTestPowerAnalysisConstructorTest7()
        {
            // When creating a power analysis, we have three things we can
            // change. We can always freely configure two of those things
            // and then ask the analysis to give us the third.

            var analysis = new TTestPowerAnalysis(OneSampleHypothesis.ValueIsDifferentFromHypothesis);

            // Those are:
            double e = analysis.Effect;   // the test's minimum detectable effect size
            double n = analysis.Samples;  // the number of samples in the test
            double p = analysis.Power;    // the probability of committing a type-2 error

            // Let's set the desired effect size and the
            // number of samples so we can get the power

            analysis.Effect  = 0.2;        // we would like to detect at least 0.2 std. dev. apart
            analysis.Samples = 60;         // we would like to use at most 60 samples
            analysis.ComputePower();       // what will be the power of this test?

            double power = analysis.Power; // The power is going to be 0.33 (or 33%)

            // Let's set the desired power and the number
            // of samples so we can get the effect size

            analysis.Power   = 0.8;          // we would like to create a test with 80% power
            analysis.Samples = 60;           // we would like to use at most 60 samples
            analysis.ComputeEffect();        // what would be the minimum effect size we can detect?

            double effect = analysis.Effect; // The effect will be 0.36 standard deviations.

            // Let's set the desired power and the effect
            // size so we can get the number of samples

            analysis.Power  = 0.8; // we would like to create a test with 80% power
            analysis.Effect = 0.2; // we would like to detect at least 0.2 std. dev. apart
            analysis.ComputeSamples();

            double samples = analysis.Samples; // We would need around 199 samples.

            Assert.AreEqual(198.15082094251142, samples, 1e-10);
            Assert.AreEqual(0.36770431608203374, effect);
            Assert.AreEqual(0.33167864622935495, power);
        }
示例#4
0
        public void PowerTest()
        {
            int    samples = 5;
            double stdDev  = 1;
            double mean    = 0.2;

            {
                TTest test = new TTest(mean, stdDev: stdDev, samples: samples,
                                       alternate: OneSampleHypothesis.ValueIsSmallerThanHypothesis);

                Assert.AreEqual(4, test.StatisticDistribution.DegreesOfFreedom);
                Assert.AreEqual(0.02138791, test.Analysis.Power, 1e-6);
                Assert.AreEqual(0.2, test.Analysis.Effect);
                Assert.AreEqual(5, test.Analysis.Samples);

                TTestPowerAnalysis target = (TTestPowerAnalysis)test.Analysis;
                target.Power = 0.6;
                target.ComputeSamples();

                Assert.IsTrue(Double.IsNaN(target.Samples));
                Assert.AreEqual(0.6, target.Power, 1e-6);
                Assert.AreEqual(0.2, target.Effect);
            }


            {
                TTest test = new TTest(mean, stdDev: stdDev, samples: samples,
                                       alternate: OneSampleHypothesis.ValueIsGreaterThanHypothesis);

                Assert.AreEqual(4, test.StatisticDistribution.DegreesOfFreedom);

                Assert.AreEqual(0.2, test.Analysis.Effect);
                Assert.AreEqual(0.102444276600, test.Analysis.Power, 1e-6);
                Assert.AreEqual(5, test.Analysis.Samples, 1e-4);

                TTestPowerAnalysis target = (TTestPowerAnalysis)test.Analysis;
                target.Power = 0.6;
                target.ComputeSamples();

                Assert.AreEqual(91.444828012, target.Samples, 1e-6);
                Assert.AreEqual(0.6, target.Power, 1e-6);
                Assert.AreEqual(0.2, target.Effect);
            }


            {
                TTest test = new TTest(mean, stdDev: stdDev, samples: samples,
                                       alternate: OneSampleHypothesis.ValueIsDifferentFromHypothesis);

                Assert.AreEqual(4, test.StatisticDistribution.DegreesOfFreedom);

                Assert.AreEqual(0.2, test.Analysis.Effect);
                Assert.AreEqual(0.06426957, test.Analysis.Power, 1e-6);
                Assert.AreEqual(5, test.Analysis.Samples, 1e-4);

                TTestPowerAnalysis target = (TTestPowerAnalysis)test.Analysis;
                target.Power = 0.6;
                target.ComputeSamples();

                Assert.AreEqual(124.3957558, target.Samples, 1e-6);
                Assert.AreEqual(0.6, target.Power, 1e-6);
                Assert.AreEqual(0.2, target.Effect);
            }
        }