Probability

Updated Started

Dependence and conditional probability

P(E)P(E) is the probability of event EE. Events EE and FF are dependent if knowing whether EE happened tells us something about whether FF happened, and independent otherwise. Independence is exactly the condition that the probability of both is the product of each:

P(E,F)=P(E)P(F)(only when independent)P(E, F) = P(E)P(F) \quad \text{(only when independent)}

In general, provided P(F)>0P(F) > 0, the probability of EE given FF is

P(E∣F)=P(E,F)P(F)or equivalentlyP(E,F)=P(E∣F)P(F)P(E \mid F) = \frac{P(E, F)}{P(F)} \qquad \text{or equivalently} \qquad P(E, F) = P(E \mid F)P(F)

and when EE and FF are independent this reduces to P(E∣F)=P(E)P(E \mid F) = P(E).

Two children

A family has two children, each equally likely to be a boy or a girl. What is the probability both are girls, given that the older one is a girl? The event "both are girls and the older is a girl" is just "both are girls", so

P(Both∣Older)=P(Both)P(Older)=1/41/2=12P(\text{Both} \mid \text{Older}) = \frac{P(\text{Both})}{P(\text{Older})} = \frac{1/4}{1/2} = \frac{1}{2}

Now condition instead on "at least one is a girl". Of the four equally likely families (GG, GB, BG, BB) three qualify, and only one of those is GG:

P(Both∣At least one)=P(Both)P(At least one)=1/43/4=13P(\text{Both} \mid \text{At least one}) = \frac{P(\text{Both})}{P(\text{At least one})} = \frac{1/4}{3/4} = \frac{1}{3}

Knowing only that at least one child is a girl, a boy-and-girl family is twice as likely as two girls.

Bayes' theorem

Bayes' theorem reverses a conditional probability. Using the definition twice:

P(E∣F)=P(E,F)P(F)=P(F∣E)P(E)P(F)P(E \mid F) = \frac{P(E, F)}{P(F)} = \frac{P(F \mid E)P(E)}{P(F)}

FF splits into the mutually exclusive events "FF and EE" and "FF and not EE", so the denominator can be expanded:

P(E∣F)=P(F∣E)P(E)P(F∣E)P(E)+P(F∣¬E)P(¬E)P(E \mid F) = \frac{P(F \mid E)P(E)}{P(F \mid E)P(E) + P(F \mid \neg E)P(\neg E)}

A disease affects 1 in 10,000 people. A test detects it 99% of the time when it is present, and gives a false positive 1% of the time when it is not. Given a positive test:

P(Disease∣+)=0.99×0.00010.99×0.0001+0.01×0.9999≈0.0098\begin{aligned} P(\text{Disease} \mid +) &= \frac{0.99 \times 0.0001}{0.99 \times 0.0001 + 0.01 \times 0.9999} \\ &\approx 0.0098 \end{aligned}

Under 1%. In natural frequencies: of a million people, 100 have the disease and 99 of them test positive; of the 999,900 who don't, 9,999 test positive. So only 99 of the 10,098 positives are real.

This assumes people are tested at random. If only people with symptoms take the test, we should condition on "positive and symptomatic", and the answer would be much higher.

Random variables

A random variable is a variable whose possible values have a probability distribution. Its expected value is the average of its values weighted by their probabilities:

E(X)=∑ixiP(X=xi)E(X) = \sum_i x_i P(X = x_i)
  • A coin flip that is 1 for heads and 0 for tails has expected value 0×12+1×12=120 \times \frac{1}{2} + 1 \times \frac{1}{2} = \frac{1}{2}.
  • The number of heads in 10 flips has expected value 5.
  • A number drawn uniformly from range(10) (0 to 9) has expected value 4.5.

Random variables can be conditioned on events too. Let XX be the number of girls in the two-child example: 0 with probability 14\frac{1}{4}, 1 with probability 12\frac{1}{2}, 2 with probability 14\frac{1}{4}. Conditioned on at least one girl, it is 1 with probability 23\frac{2}{3} and 2 with probability 13\frac{1}{3}. Conditioned on the older child being a girl, it is 1 or 2 with probability 12\frac{1}{2} each.

Continuous distributions

A discrete distribution puts positive probability on individual outcomes. Over a continuum there are infinitely many points, so each individual point has probability zero. Instead we use a probability density function (PDF): the probability of a value falling in an interval is the integral of the density over that interval. The cumulative distribution function (CDF) gives the probability that the variable is less than or equal to a value.

Uniform distribution

The uniform distribution on [a,b][a, b] puts equal density on every value in the interval:

f(x)={1b−aa≤x≤b0otherwiseF(x)={0x<ax−ab−aa≤x≤b1x>bf(x) = \begin{cases} \frac{1}{b-a} & a \leq x \leq b \\ 0 & \text{otherwise} \end{cases} \qquad F(x) = \begin{cases} 0 & x < a \\ \frac{x-a}{b-a} & a \leq x \leq b \\ 1 & x > b \end{cases}
Uniform PDF on [0, 5]
Uniform CDF on [0, 5]

Normal distribution

The normal distribution is symmetric about its mean μ\mu, with spread set by its standard deviation σ\sigma:

f(x∣μ,σ)=1σ2πe−(x−μ)22σ2f(x \mid \mu, \sigma) = \frac{1}{\sigma \sqrt{2 \pi}} e^{-\frac{(x-\mu)^2}{2 \sigma^2}}
def normal_pdf(x: float, mu: float = 0, sigma: float = 1) -> float:
    return math.exp(-((x - mu) ** 2) / 2 / sigma**2) / (math.sqrt(2 * math.pi) * sigma)

With μ=0\mu = 0 and σ=1\sigma = 1 it is the standard normal. If ZZ is standard normal then X=σZ+μX = \sigma Z + \mu is normal with mean μ\mu and standard deviation σ\sigma; conversely Z=X−μσZ = \frac{X - \mu}{\sigma} standardises XX.

The CDF has no elementary closed form, but can be written with the error function:

def normal_cdf(x: float, mu: float = 0, sigma: float = 1) -> float:
    return (1 + math.erf((x - mu) / math.sqrt(2) / sigma)) / 2
Normal PDFs for several values of mu and sigma
Normal CDFs for several values of mu and sigma

Its inverse has no simple form either, but normal_cdf is continuous and strictly increasing, so a binary search finds it:

def inverse_normal_cdf(p: float, mu: float = 0, sigma: float = 1, tolerance: float = 0.00001) -> float:
    if mu != 0 or sigma != 1:
        return mu + sigma * inverse_normal_cdf(p, tolerance=tolerance)
    low_z, hi_z = -10.0, 10.0
    while hi_z - low_z > tolerance:
        mid_z = (low_z + hi_z) / 2
        if normal_cdf(mid_z) < p:
            low_z = mid_z
        else:
            hi_z = mid_z
    return mid_z

The normal distribution also underlies Student's t-distribution, which describes the standardised sample mean when σ\sigma has to be estimated from the sample.

The central limit theorem

The average of a large number of independent, identically distributed random variables is approximately normally distributed. If x1,…,xnx_1, \ldots, x_n each have mean μ\mu and standard deviation σ\sigma, then for large nn

1n(x1+⋯+xn)  ∼˙  N(μ,σn)equivalently(x1+⋯+xn)−nμσn  ∼˙  N(0,1)\frac{1}{n}(x_1 + \cdots + x_n) \;\dot\sim\; N\left(\mu, \frac{\sigma}{\sqrt{n}}\right) \qquad\text{equivalently}\qquad \frac{(x_1 + \cdots + x_n) - n\mu}{\sigma\sqrt{n}} \;\dot\sim\; N(0, 1)

A Binomial(n,p)\text{Binomial}(n, p) variable is the sum of nn independent Bernoulli(p)\text{Bernoulli}(p) variables, each 1 with probability pp. A Bernoulli has mean pp and standard deviation p(1−p)\sqrt{p(1-p)}, so for large nn the binomial is approximately normal with μ=np\mu = np and σ=np(1−p)\sigma = \sqrt{np(1-p)}. Here are 10,000 draws from Binomial(100,0.75)\text{Binomial}(100, 0.75) against that normal curve:

Histogram of Binomial(100, 0.75) samples against the normal approximation

So to estimate the probability that a fair coin gives more than 60 heads in 100 flips, we can use N(50,5)N(50, 5) instead of the binomial CDF. This is the basis of the tests in Hypothesis Testing.

Adapted from Joel Grus, Data Science from Scratch (2nd edition), chapter 6.

Original notebook

Linked from

Revision log

  1. Migrated from data-science-notes; fixed Bayes' theorem and an expected value

Tags: Statistics