Compute CDF from a PDF Function
Company: Uber
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given a callable function `pdf(t)` that returns the probability density of a normal distribution at value `t`. Implement a function `cdf(x)` that returns the cumulative probability:
`P(X <= x) = integral from -infinity to x of pdf(t) dt`
Requirements:
- You may call `pdf(t)` as often as needed.
- Do not call a built-in CDF function from a statistics library.
- Use a numerical method to approximate the integral.
- Your implementation should handle positive and negative `x` values.
- State any assumptions you make about numerical precision, integration bounds, and convergence.
Quick Answer: This question evaluates a candidate's skills in numerical integration, applied numerical methods, and probability/statistics by requiring construction of a cumulative distribution function from a provided PDF.
You are given the probability density function of the standard normal distribution: pdf(t) = exp(-t*t/2) / sqrt(2*pi). Implement a function solution(x) that returns the cumulative probability P(X <= x) by numerically approximating the integral of the PDF. Do not use math.erf, scipy, or any built-in CDF function from a statistics library. Your approach must work for both positive and negative x values. Assumptions for this problem: returning the answer rounded to 6 decimal places is sufficient; because the standard normal distribution is symmetric, you may compute the integral over [0, |x|] instead of integrating from negative infinity; and using a sufficiently fine even partition should be treated as converged for this precision.
Constraints
- -8.0 <= x <= 8.0
- Do not use math.erf or any statistics-library CDF
- Your answer should be accurate to within 1e-6 after rounding
Examples
Input: 0.0
Expected Output: 0.5
Explanation: By symmetry, exactly half of the probability mass lies at or below 0.
Input: 1.0
Expected Output: 0.841345
Explanation: The standard normal CDF at x = 1 is approximately 0.841345.
Input: -1.0
Expected Output: 0.158655
Explanation: Using symmetry, Phi(-1) = 1 - Phi(1) = 1 - 0.841345 = 0.158655.
Input: 1.96
Expected Output: 0.975002
Explanation: A z-score of 1.96 corresponds to a cumulative probability of about 0.975002.