Skip to main content

Statistics and Probability for Data Science

Statistics lets you describe data and reason about uncertainty.

Descriptive statistics​

MeasureWhat it tells youRobust to outliers?
MeanAverage valueNo
MedianMiddle valueYes
ModeMost frequent valueYes
Variance / standard deviationSpreadNo
IQRSpread of the middle 50%Yes
import numpy as np

data = np.array([12, 15, 14, 10, 98, 13, 16])
print(data.mean(), np.median(data), data.std(ddof=1))
q1, q3 = np.percentile(data, [25, 75])
print("IQR:", q3 - q1) # 98 is an outlier: median and IQR barely move

Common distributions​

DistributionUse
Normal (Gaussian)Measurement noise, many natural quantities
Bernoulli / BinomialSuccess or failure counts
PoissonEvent counts in a fixed interval
UniformEqual chance over a range
ExponentialTime between events

The Central Limit Theorem: the mean of many independent samples is approximately normal, whatever the original distribution.

Probability rules​

P(A or B) = P(A) + P(B) - P(A and B)
P(A and B) = P(A) * P(B | A)
Bayes: P(A | B) = P(B | A) * P(A) / P(B)

Example. A test is 99% sensitive and 95% specific; the condition affects 1% of people. If you test positive, the chance you have it is

P(D | +) = 0.99*0.01 / (0.99*0.01 + 0.05*0.99) = 0.0099 / 0.0594 = about 16.7%

Low base rates make positives much less certain than they look.

Hypothesis testing​

  1. State H0 (no effect) and H1 (effect).
  2. Choose a significance level (for example 0.05).
  3. Compute a test statistic and p-value.
  4. Reject H0 if p < alpha.
from scipy import stats

a = [82, 85, 88, 90, 79, 84]
b = [75, 78, 80, 77, 74, 79]
t, p = stats.ttest_ind(a, b)
print(f"t={t:.2f}, p={p:.4f}")
CAUTION

A p-value is not the probability that H0 is true. Report effect sizes and confidence intervals, and avoid testing many hypotheses without correction.

Confidence intervals​

A 95% confidence interval is a range produced by a procedure that would contain the true value in 95% of repeated samples. Wider intervals mean more uncertainty (small samples, high variance).