Statistics and Probability for Data Science
Statistics lets you describe data and reason about uncertainty.
Descriptive statistics
| Measure | What it tells you | Robust to outliers? |
|---|---|---|
| Mean | Average value | No |
| Median | Middle value | Yes |
| Mode | Most frequent value | Yes |
| Variance / standard deviation | Spread | No |
| IQR | Spread 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
| Distribution | Use |
|---|---|
| Normal (Gaussian) | Measurement noise, many natural quantities |
| Bernoulli / Binomial | Success or failure counts |
| Poisson | Event counts in a fixed interval |
| Uniform | Equal chance over a range |
| Exponential | Time 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
- State H0 (no effect) and H1 (effect).
- Choose a significance level (for example 0.05).
- Compute a test statistic and p-value.
- 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).