Latticework

Command Palette

Search for a command to run...

Probability

Distributions

16 min

Explanation

A binomial distribution models the number of successes in n independent yes/no trials, each with success probability p — like the number of heads in 10 coin flips. Its PMF:

import math

def binomial_pmf(n, k, p):
    return math.comb(n, k) * p**k * (1 - p)**(n - k)

print(round(binomial_pmf(10, 5, 0.5), 4))   # 0.2461 -- P(exactly 5 heads in 10 flips)

math.comb(n, k) counts the number of distinct ways to choose which k of the n trials were successes.

Try it

Summing binomial_pmf(3, k, 0.5) over every possible k from 0 to 3 always gives exactly 1.0 -- a useful sanity check for any PMF you compute.

Loading editor…
Explanation

A continuous random variable (like height, or a stock return) can take any value in a range, not just discrete outcomes — so instead of "the probability of exactly this value" (which is technically 0 for any single point), you work with a PDF (probability density function), where area under the curve between two points gives the probability of landing in that range.

The normal distribution (bell curve) is the most common:

import math

def normal_pdf(x, mu, sigma):
    return (1 / (sigma * math.sqrt(2 * math.pi))) * math.exp(-((x - mu) ** 2) / (2 * sigma ** 2))

print(round(normal_pdf(0, 0, 1), 4))   # 0.3989 -- peak of the standard normal
Exercise

Write `binomial_pmf(n, k, p)`, returning P(X = k) for a Binomial(n, p) random variable. Formula: C(n, k) × p^k × (1-p)^(n-k) — use `math.comb`.

Exercise

Write `normal_pdf(x, mu, sigma)`, returning the normal distribution's probability density at `x`. Formula: (1 / (σ√(2π))) × e^(−(x−μ)² / (2σ²)).

Quiz

For a standard normal distribution (mean 0, std 1), where is the PDF at its highest point?

Checkpoint

You can compute binomial and normal probabilities from their formulas, and understand the discrete-PMF vs continuous-PDF distinction.