Probability
Dependence and conditional probability
is the probability of event . Events and are dependent if knowing whether happened tells us something about whether happened, and independent otherwise. Independence is exactly the condition that the probability of both is the product of each:
In general, provided , the probability of given is
and when and are independent this reduces to .
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
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:
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:
splits into the mutually exclusive events " and " and " and not ", so the denominator can be expanded:
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:
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:
- A coin flip that is 1 for heads and 0 for tails has expected value .
- 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 be the number of girls in the two-child example: 0 with probability , 1 with probability , 2 with probability . Conditioned on at least one girl, it is 1 with probability and 2 with probability . Conditioned on the older child being a girl, it is 1 or 2 with probability 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 puts equal density on every value in the interval:
![Uniform PDF on [0, 5]](/_next/image?url=%2Fimages%2Fresearch%2Fdata-science%2Fstatistics%2Fprobability%2Funiform-pdf.png&w=1920&q=75)
![Uniform CDF on [0, 5]](/_next/image?url=%2Fimages%2Fresearch%2Fdata-science%2Fstatistics%2Fprobability%2Funiform-cdf.png&w=1920&q=75)
Normal distribution
The normal distribution is symmetric about its mean , with spread set by its standard deviation :
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 and it is the standard normal. If is standard normal then is normal with mean and standard deviation ; conversely standardises .
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

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_zThe normal distribution also underlies Student's t-distribution, which describes the standardised sample mean when 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 each have mean and standard deviation , then for large
A variable is the sum of independent variables, each 1 with probability . A Bernoulli has mean and standard deviation , so for large the binomial is approximately normal with and . Here are 10,000 draws from against that normal curve:

So to estimate the probability that a fair coin gives more than 60 heads in 100 flips, we can use 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.
Linked from
Revision log
- Migrated from data-science-notes; fixed Bayes' theorem and an expected value
Tags: Statistics