Height and Area of the Bell Curve

Probability DistributionsMedium+20 XP+20

Height and Area of the Bell Curve

A mill fills bags of flour, and the fill weight XXX (in grams) is modelled as normal: X∼N(μ,σ2)X \sim N(\mu, \sigma^2)X∼N(μ,σ2). For any weight xxx, quality control asks two different questions.

  1. How dense is the curve at xxx? That is the PDF value f(x)f(x)f(x), the height of the bell curve at xxx:
f(x)=1σ2π e−(x−μ)22σ2f(x) = \frac{1}{\sigma \sqrt{2\pi}}\, e^{-\frac{(x-\mu)^2}{2\sigma^2}}f(x)=σ2π​1​e−2σ2(x−μ)2​
  1. What fraction of bags weigh at most xxx? That is the CDF value F(x)=P(X≤x)F(x) = P(X \le x)F(x)=P(X≤x), the area under the curve to the left of xxx.

The normal CDF has no formula built only from arithmetic and powers. Python's standard library does ship the error function math.erf, though, and the CDF of the standard normal Z∼N(0,1)Z \sim N(0, 1)Z∼N(0,1) can be written with it:

Φ(z)=12[1+erf⁡ ⁣(z2)]\Phi(z) = \frac{1}{2}\left[1 + \operatorname{erf}\!\left(\frac{z}{\sqrt{2}}\right)\right]Φ(z)=21​[1+erf(2​z​)]

Task: write normal_pdf_cdf(x, mu, sigma) that returns the tuple (f(x), F(x)) for X∼N(μ,σ2)X \sim N(\mu, \sigma^2)X∼N(μ,σ2), each rounded to 4 decimal places.

  • sigma is the standard deviation σ\sigmaσ (always greater than 000), not the variance.
  • Φ\PhiΦ above is the CDF of N(0,1)N(0, 1)N(0,1) only; your function must work for any μ\muμ and σ\sigmaσ.
  • Use only the standard library.

For example, a mill aiming at μ=512\mu = 512μ=512 g with σ=15\sigma = 15σ=15 g wants to know how often a bag comes in at or under the 500500500 g printed on the label: that is the second number returned by normal_pdf_cdf(500, 512, 15).

Normal (Gaussian) Distribution

Mean (μ):0.0
Std Dev (σ):1.0
Limit a:-1.0
Limit b:1.0
0.000.230.46f(x)-6-4-20246x

Properties & Integration

Parameters: μ = 0.0, σ = 1.0
Interval: [-1.0, 1.0]
F(1.0) = 0.8413
F(-1.0) = 0.1587
P(-1.0 ≤ X ≤ 1.0) = F(b) - F(a)
= 0.6827 (68.3%)
  • The Normal distribution is symmetric about its mean μ.
  • Try setting the interval to μ ± σ (e.g. μ=0, σ=1, a=−1, b=1) to verify the empirical rule: approximately 68% of the area lies within 1 standard deviation.
The Normal Distribution is defined by its center μ and spread σ. The probability corresponds to the shaded area under the curve. Note that the tails extend to negative and positive infinity, though they decay extremely fast.

Examples

x = -1.5
mu = 0
sigma = 1
returns (0.1295, 0.0668)
x = 500
mu = 512
sigma = 15
returns (0.0193, 0.2119)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run