Quick 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.

Implement Python Function for Statistical Test P-Values

Company: Roblox

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

##### Scenario You need a utility that calculates p-values for one-sided and two-sided statistical tests. ##### Question Write a Python function `compute_p_value(stat, dist='z', df=None, alternative='two-sided')` that returns the p-value. Your code should support Z-tests and Student-t tests, and handle 'less', 'greater', and 'two-sided' alternatives. ##### Hints Use the CDF of the chosen distribution; for two-sided tests return 2*min(CDF, 1-CDF). Libraries like scipy.stats are allowed.

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.

Implement compute_p_value(stat, dist='z', df=None, alternative='two-sided') that returns the p-value for one-sided and two-sided tests. For dist='z', use the standard normal distribution. For dist='t', use the Student's t distribution with degrees of freedom df (a positive integer). The alternative can be 'less', 'greater', or 'two-sided'. Compute p-values using the CDF of the chosen distribution: for 'less' return CDF(stat); for 'greater' return 1 - CDF(stat); for 'two-sided' return 2 * min(CDF(stat), 1 - CDF(stat)). Do not use external libraries. Return a float in [0, 1].

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

  1. For the normal CDF, use erf: Phi(z) = 0.5 * (1 + erf(z / sqrt(2))).
  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).
  3. Compute the regularized incomplete beta via a continued fraction (Lentz's method).
  4. For two-sided tests, use 2 * min(CDF, 1 - CDF) and clamp results to [0,1].

Loading coding console...

Show the approach

Approach

Compute the cumulative distribution function (CDF) of the specified distribution at the test statistic. For the normal distribution, use the error function. For the t-distribution with df degrees of freedom, use the regularized incomplete beta function via a stable continued fraction (Lentz's method). The one-sided p-values are directly CDF or its complement, and the two-sided p-value is 2 * min(CDF, 1 - CDF).

Time complexity:
O(1)
Space complexity:
O(1)