/// <summary> /// Computes the power for a test with givens values of /// <see cref="IPowerAnalysis.Effect">effect size</see> and <see cref="IPowerAnalysis.Samples"> /// number of samples</see> under <see cref="IPowerAnalysis.Size"/>. /// </summary> /// /// <returns> /// The power for the test under the given conditions. /// </returns> /// public override void ComputePower() { double samples = (Samples1 * Samples2) / (double)(Samples1 + Samples2); double Za = ZTest.PValueToStatistic(Size, Tail); double Zb = Za - Math.Sqrt(samples) * Effect; Power = NormalDistribution.Standard.ComplementaryDistributionFunction(Zb); }
/// <summary> /// Gets the recommended sample size for the test to attain /// the power indicating in <see cref="IPowerAnalysis.Power"/> considering /// values of <see cref="IPowerAnalysis.Effect"/> and <see cref="IPowerAnalysis.Size"/>. /// </summary> /// /// <returns> /// Recommended sample size for attaining the given /// <see cref="IPowerAnalysis.Power"/> for size effect <see cref="IPowerAnalysis.Effect"/> /// under the given <see cref="IPowerAnalysis.Size"/>. /// </returns> /// public override void ComputeSamples() { double Za = ZTest.PValueToStatistic(Size, Tail); double Zb = NormalDistribution.Standard .InverseDistributionFunction(Power); double n = (Za + Zb) / Effect; Samples = n * n; }
/// <summary> /// Computes the minimum detectable effect size for the test /// considering the power given in <see cref="IPowerAnalysis.Power"/>, the /// number of samples in <see cref="IPowerAnalysis.Samples"/> and the significance /// level <see cref="IPowerAnalysis.Size"/>. /// </summary> /// /// <returns> /// The minimum detectable <see cref="IPowerAnalysis.Effect">effect /// size</see> for the test under the given conditions. /// </returns> /// public override void ComputeEffect() { double Za = ZTest.PValueToStatistic(Size, Tail); double Zb = NormalDistribution.Standard .InverseDistributionFunction(Power); double n = Math.Sqrt(Samples1 + Samples2); double d = (Za + Zb) / n; Effect = d; }
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); } }