Implement Python Function for Statistical Test P-Values
Company: Roblox
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in statistical hypothesis testing, p-value interpretation, and implementing distribution-based calculations (Z and Student-t) in code, and it falls under the Coding & Algorithms domain for data scientist roles.
Constraints
- dist is 'z' or 't'
- alternative is 'less', 'greater', or 'two-sided'
- For dist='t', df is a positive integer (1 <= df <= 10^6)
- stat is a finite float (|stat| <= 1e6)
- Use only the Python standard library
- Return value within absolute error 1e-9 of the true p-value
Examples
Input:
Expected Output: 1.0
Input:
Expected Output: 0.75
Hints
- For the normal CDF, use erf: Phi(z) = 0.5 * (1 + erf(z / sqrt(2))).
- For the t-distribution CDF, use the regularized incomplete beta function: if t >= 0, F(t) = 1 - 0.5 * I_{nu/(nu + t^2)}(nu/2, 1/2); else F(t) = 0.5 * I_{nu/(nu + t^2)}(nu/2, 1/2).
- Compute the regularized incomplete beta via a continued fraction (Lentz's method).
- For two-sided tests, use 2 * min(CDF, 1 - CDF) and clamp results to [0,1].