/** * One-sample Z-test * When the population mean and standard deviation are known. * https://en.wikipedia.org/wiki/Z-test * * Hₐ - H₀ M - μ M - μ M - μ * z = ------- = ----- = ----- = ----- * σ σ SEM σ/√n * * p1 = CDF below if left tailed * = CDF above if right tailed * p2 = CDF outside * * @param number $Hₐ Alternate hypothesis (M Sample mean) * @param int $n Sample size * @param number $H₀ Null hypothesis (μ Population mean) * @param number $σ SD of population (Standard error of the mean) * * @return array [ * z => z score * p1 => one-tailed p value (left or right tail depends on how Hₐ differs from H₀) * p2 => two-tailed p value * ] */ public static function zTest($Hₐ, $n, $H₀, $σ) : array { // Calculate z score (test statistic) $sem = self::sem($σ, $n); $z = self::zScore($Hₐ, $H₀, $sem, self::Z_RAW_VALUE); // One- and two-tailed P values if ($Hₐ < $H₀) { $p1 = StandardNormal::CDF($z); } else { $p1 = StandardNormal::above($z); } $p2 = StandardNormal::outside(-abs($z), abs($z)); return ['z' => $z, 'p1' => $p1, 'p2' => $p2]; }
/** * F used within CDF * _ _ * 1 ∞ | / 1 ν \ / ν \ | * Fᵥ,ᵤ(x) = Φ(-μ) + - ∑ | pⱼIy | j + -, - | + qⱼIy | j + 1, - | | * 2 ʲ⁼⁰ |_ \ 2 2 / \ 2 / _| * * where * Φ = cumulative distribution function of the standard normal distribution * Iy(a,b) = regularized incomplete beta function * * x² * y = ------ * x² + ν * * 1 / μ² \ / μ² \ʲ * pⱼ = -- exp| - - | | - | * j! \ 2 / \ 2 / * * μ / μ² \ / μ² \ʲ * qⱼ = ------------ exp| - - | | - | * √2Γ(j + 3/2) \ 2 / \ 2 / * * @param number $x * @param int $ν Degrees of freedom * @param number $μ Noncentrality parameter * * @return number */ private static function F($x, int $ν, $μ) { Support::checkLimits(self::LIMITS, ['x' => $x, 'ν' => $ν, 'μ' => $μ]); $Φ = StandardNormal::CDF(-$μ); $y = $x ** 2 / ($x ** 2 + $ν); $sum = $Φ; $tol = 1.0E-8; $j = 0; do { $exp = exp(-1 * $μ ** 2 / 2) * ($μ ** 2 / 2) ** $j; $pⱼ = 1 / Combinatorics::factorial($j) * $exp; $qⱼ = $μ / sqrt(2) / Special::gamma($j + 3 / 2) * $exp; $I1 = Special::regularizedIncompleteBeta($y, $j + 1 / 2, $ν / 2); $I2 = Special::regularizedIncompleteBeta($y, $j + 1, $ν / 2); $delta = $pⱼ * $I1 + $qⱼ * $I2; $sum += $delta / 2; $j += 1; } while ($delta / $sum > $tol || $j < 10); return $sum; }