← back

Normal Distribution PDF Calculator

#80 · Probability · Medium

⊣ Solve on deep-ml.com

Problem

Calculate the probability density function (PDF) value of a normal (Gaussian) distribution at a given point x, with specified mean and standard deviation.

Solution

1
2
3
4
5
6
7
import math

def normal_pdf(x, mean=0, std=1):
    coefficient = 1 / (std * math.sqrt(2 * math.pi))
    exponent = -0.5 * ((x - mean) / std) ** 2
    pdf = coefficient * math.exp(exponent)
    return round(pdf, 4)

Explanation

  1. The normal distribution PDF formula is: f(x) = (1 / (sigma sqrt(2pi))) exp(-0.5 ((x - mu) / sigma)^2).
  2. The coefficient normalizes the distribution so the total area under the curve is 1.
  3. The exponent determines how quickly the probability drops off from the mean.
  4. The result is the probability density (not probability) at point x.
  5. The standard normal distribution has mean=0 and std=1.

Complexity

  • Time: O(1)
  • Space: O(1)