Evaluate the Gaussian density f(x) = (1/(σ√(2π))) × exp(−½z²) where z=(x−μ)/σ. Four named pieces: z (the standardized value), coeff (1/(σ√(2π))), exp_term (exp(−½z²)), and their product pdf. At x=1, μ=0, σ=1 (standard normal) the density is ≈0.241971. Library: scipy.stats.norm.pdf(x, mu, sigma).

By hand

Standard normal (μ=0, σ=1): z = (1−0)/1 = 1.0. Coefficient = 1/√(2π) ≈ 0.398942. Exponential = exp(−0.5×1²) = exp(−0.5) ≈ 0.606531. pdf = 0.398942 × 0.606531 ≈ 0.241971. Each piece is a separate variable, making the two-factor structure of the formula visible in the trace.

naive.py
Replay: real traced execution (multi-file project)
import math
x = 1
mu = 0
sigma = 1
z = (x - mu) / sigma
coeff = 1 / (sigma * math.sqrt(2 * math.pi))
exp_term = math.exp(-0.5 * z ** 2)
pdf = coeff * exp_term
print('RESULT:', round(pdf, 6))
  1. import math

    1import math2x = 1
  2. x ← 1

    1import math2x = 13mu = 0
    values this step1x
  3. mu ← 0

    2x = 13mu = 04sigma = 1
    values this step0mu
  4. sigma ← 1

    3mu = 04sigma = 15z = (x - mu) / sigma
    values this step1sigma
  5. z ← 1.0

    4sigma = 15z = (x - mu) / sigma6coeff = 1 / (sigma * math.sqrt(2 * math.pi))
    values this step1.0z
  6. coeff ← 0.3989422804014327

    5z = (x - mu) / sigma6coeff = 1 / (sigma * math.sqrt(2 * math.pi))7exp_term = math.exp(-0.5 * z ** 2)
    values this step0.3989422804014327coeff
  7. exp_term ← 0.6065306597126334

    6coeff = 1 / (sigma * math.sqrt(2 * math.pi))7exp_term = math.exp(-0.5 * z ** 2)8pdf = coeff * exp_term
    values this step0.6065306597126334exp_term
  8. pdf ← 0.24197072451914337

    7exp_term = math.exp(-0.5 * z ** 2)8pdf = coeff * exp_term9print('RESULT:', round(pdf, 6))
    values this step0.24197072451914337pdf
  9. stdout ← RESULT: 0.241971

    8pdf = coeff * exp_term9print('RESULT:', round(pdf, 6))
    values this stepRESULT: 0.241971stdout

With the library

scipy.stats.norm.pdf(x, loc, scale) takes the point x first, then loc (mean μ) and scale (std σ). Returns the density value — the same formula evaluated at full floating-point precision.

library.py
from scipy.stats import norm
from dalib.display import set_display
set_display()

x = 1
mu = 0
sigma = 1
pdf = norm.pdf(x, mu, sigma)
print('x:', x, ' mu:', mu, ' sigma:', sigma)
print('RESULT:', round(pdf, 6))
x: 1  mu: 0  sigma: 1
RESULT: 0.241971

Implementation notes

  • The pdf gives a density, not a probability. f(x) can exceed 1 for narrow distributions (small σ). The probability of falling in an interval [a, b] is the integral of f, not f itself.
  • At x=μ the density is maximized: coeff = 1/(σ√(2π)), exp_term = 1 (since z=0). Density decreases symmetrically as |x−μ| grows.
  • Cross-reference: z-scores (ch02) for the standardization z=(x−μ)/σ that appears in the exponent; discrete-cdf (this chapter) for the analogous cumulative form.