Implement robust one/two-sided p-value function
Company: Roblox
Role: Data Scientist
Category: Statistics & Math
Difficulty: hard
Interview Round: Technical Screen
Implement in Python a function p_value(stat, alternative, dist, df=None) that returns the p-value for one-sided and two-sided tests. Requirements: (1) alternative ∈ {'less','greater','two-sided'}; (2) dist ∈ {'z','t'} where 'z' uses the standard normal and 't' uses Student's t with degrees of freedom df; (3) for 'z', do not use external libraries—implement the CDF via math.erf/erfc to achieve at least 1e-9 relative error for |z| ≤ 8 and use numerically stable tails; (4) for 't', you may use scipy.stats.t.cdf if available, otherwise implement a reasonable approximation (e.g., incomplete beta/continued fraction) and document error bounds; (5) handle edge cases: NaN/inf inputs, df<1, and extreme |stat|; (6) include minimal unit tests: z=0 two-sided→1.0; z=1.96 two-sided≈0.0500; z=5 greater≈2.87e-7; t=2.0 with df=10 two-sided≈0.070; monotonicity checks; (7) explain how you ensure numerical stability for very small p-values.
Quick Answer: This question evaluates proficiency in statistical hypothesis testing, numerical computing, and numerical stability when implementing p-value calculations for z and t distributions, and it targets the Statistics & Math domain for a data scientist role.