KatInterview

Compute P-Values for Statistical Hypothesis Tests

Arrays & HashingAuto-graded · 25 test cases
HardAuto-gradedArrays & Hashingstatisticsnumerical-methodshypothesis-testing

Last updated

Tests numerical stability in tail probability calculation, distribution function implementation, and handling of hypothesis testing edge cases.

Write a function that computes p-values for statistical hypothesis tests using either the standard normal (z) or Student's t distribution.

Your function must accept four parameters:

  1. test_statistic (float): The computed test statistic
  2. alternative_hypothesis (str): One of 'less', 'greater', or 'two-sided'
  3. distribution (str): Either 'z' (standard normal) or 't' (Student's t)
  4. degrees_of_freedom (float, optional): Required when distribution='t'; must be ≥ 1

Return Value

Return the p-value as a float, or None for invalid inputs.

P-value Definition

The p-value is calculated as follows:

  • 'greater': P(X > test_statistic), the probability that a random variable from the specified distribution exceeds the test statistic
  • 'less': P(X < test_statistic), the probability that a random variable is less than the test statistic
  • 'two-sided': For symmetric distributions, this is 2 × min(P(X > |test_statistic|), P(X < -|test_statistic|)), which equals 2 × P(X > |test_statistic|), capped at 1.0

Both the standard normal and Student's t distributions are symmetric around zero.

Requirements

For the standard normal distribution ('z'):

  • Implement the cumulative distribution function using math.erf or math.erfc
  • Do not use external statistical libraries (like scipy) for the normal distribution
  • Your implementation must achieve relative error ≤ 1e-9 for |test_statistic| ≤ 8
  • Use numerically stable methods that compute tail probabilities directly, never as 1 - cdf(x), to avoid catastrophic cancellation

For the t distribution ('t'):

  • You may use scipy.stats.t if available
  • If scipy is unavailable, implement an approximation using the incomplete beta function
  • The relationship is: P(T > t) = ½ · I_x(ν/2, ½) where x = ν/(ν + t²) for t ≥ 0

Edge Cases

  • Return None if test_statistic is NaN
  • Return None if distribution='t' and degrees_of_freedom is None, NaN, or < 1
  • If test_statistic is +∞: return 0.0 for 'greater' and 'two-sided', return 1.0 for 'less'
  • If test_statistic is -∞: return 1.0 for 'greater', return 0.0 for 'less' and 'two-sided'
  • Treat infinite degrees of freedom as equivalent to the standard normal distribution
  • Clamp all finite results to the range [0, 1]
  • Raise ValueError for invalid alternative_hypothesis or distribution values

Constraints

  • -10⁹ ≤ test_statistic ≤ 10⁹ (when finite)
  • alternative_hypothesis ∈ {'less', 'greater', 'two-sided'}
  • distribution ∈ {'z', 't'}
  • 1 ≤ degrees_of_freedom ≤ 10⁶ (when finite and valid)

Examples

Example 1

Input: compute_p_value(0, 'two-sided', 'z')

Output: 1

Example 2

Input: compute_p_value(1.96, 'two-sided', 'z')

Output: 0.04999579029644087

Example 3

Input: compute_p_value(5, 'greater', 'z')

Output: 2.866515718791946e-7

Example 4

Input: compute_p_value(2, 'two-sided', 't', 10)

Output: 0.07338803477074024

Hints

Hint 1

Always compute tail probabilities directly using the survival function, never as 1 - CDF, to avoid catastrophic cancellation when probabilities are very close to 1.

Hint 2

For the normal distribution, use erfc(z/√2)/2 to compute the upper tail P(Z > z). The two-sided p-value can be computed as erfc(|z|/√2) without an additional doubling step.

Hint 3

Both distributions are symmetric around zero. Use this property: P(X < -s) = P(X > s), so you can compute the 'less' alternative by negating the statistic and computing the 'greater' alternative.