Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-095

Normal Approximation Error

Problem

Return the absolute error of the normal approximation to the binomial CDF at k:

error = |P(X <= k) - Phi((k - n * p) / sqrt(n * p * (1 - p)))|

Compute the exact binomial probability with a sum and the normal probability with math.erf. Return 0.0 when n <= 0 or p is outside [0, 1].

Hint

No continuity correction is applied, so some error is expected.

Test cases

case 1
in: [10,0.5,5]
expected: 0.123046875
case 2
in: [100,0.5,50]
expected: 0.039794618693589356
case 3
in: [20,0.1,2]
expected: 0.17692680518946635
case 4
in: [5,0.2,-1]
expected: 0.0126736593387341
Starter codePython
import math


def normal_approximation_error(n, p, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor