ML Notes
Essential statistics concepts for ML interviews: hypothesis testing, distributions, sampling, Bayesian thinking, and probability theory.
Statistics is fundamental to understanding and implementing machine learning algorithms. Here's what you need to know for interviews.
Probability Fundamentals
Basic Concepts
Probability = likelihood of event occurring (0 to 1)
Joint Probability: P(A and B) = P(A) * P(B|A)
Marginal Probability: P(A) = Σ P(A and B) for all B
Conditional Probability: P(A|B) = P(A and B) / P(B)
Independent Events: P(A|B) = P(A) if events are independent
Bayes' Theorem
Real-world example: Spam detection
Distributions
Normal Distribution
Key properties:
- Bell-shaped curve
- Mean = Median = Mode
- 68% of data within 1 std dev
- 95% of data within 2 std devs
- 99.7% of data within 3 std devs
Used for:
- Continuous variables (heights, test scores)
- Central Limit Theorem (sample means approximate normal)
- Assumption in many ML algorithms
Binomial Distribution
Poisson Distribution
Used for count data (emails per day, accidents per year), rare events, approximates binomial when n large and p small.
Uniform Distribution
All values equally likely. Used for random sampling, baseline comparisons, initial weight distributions.
Hypothesis Testing
Framework
- State hypotheses
- H0 (null): no effect or difference
- H1 (alternative): there is an effect
- Choose significance level (α) - Typical: 0.05 (5%), More strict: 0.01
- Calculate test statistic - Depends on test type (t-test, z-test, chi-square, etc.)
- Determine p-value - If p < α: reject H0; If p ≥ α: fail to reject H0
- Conclusion - Based on p-value and interpret in context
Common Tests
T-Test: Compare means of two groups
from scipy import stats
# Independent samples t-test
t_stat, p_value = stats.ttest_ind(group1, group2)
# Paired t-test (same subjects, before/after)
t_stat, p_value = stats.ttest_rel(before, after)
if p_value < 0.05:
print("Significant difference between groups")
else:
print("No significant difference")Chi-Square Test: Compare categorical variables
from scipy.stats import chi2_contingency
contingency_table = pd.crosstab(var1, var2)
chi2, p_value, dof, expected = chi2_contingency(contingency_table)ANOVA: Compare means across multiple groups
from scipy.stats import f_oneway
f_stat, p_value = f_oneway(group1, group2, group3)Correlation Test: Check if variables are related
from scipy.stats import pearsonr, spearmanr
# Pearson (parametric)
r, p_value = pearsonr(x, y)
# Spearman (non-parametric, rank-based)
rho, p_value = spearmanr(x, y)Type I and Type II Errors
Type I Error (False Positive): Conclude effect exists when it doesn't. Probability = α. Example: flagging legitimate email as spam
Type II Error (False Negative): Conclude no effect when one exists. Probability = β. Power = 1 - β
Tradeoff: Decreasing α increases β. Can't minimize both simultaneously. Choose based on problem context.
Sampling Methods
Random Sampling
Every sample equally likely, no bias on average, used by stratified sampling.
Stratified Sampling
- Divide population into strata (groups)
- Sample proportionally from each stratum
- Maintains population distribution
Example: Sample from 60% male, 40% female population → Ensure sample is also 60% male, 40% female
Advantages: More representative, reduces variance, better for imbalanced populations
Importance Sampling
Sample more from important regions, weight samples by probability of selection, used in ML for balanced datasets.
Central Limit Theorem
Key insight: Sample means follow normal distribution
Process:
- Take random sample of size n from population
- Calculate sample mean
- Repeat many times
- Distribution of sample means ≈ normal
Implications:
- Works even if population not normal
- Enables statistical inference (confidence intervals, hypothesis tests)
- Sample size n ≥ 30 usually sufficient
Why important for ML:
- Justifies using normal distribution for statistics
- Foundation for many statistical tests
- Explains why averaging models works
Confidence Intervals
Definition
Confidence interval: range of values likely to contain parameter
95% CI = [lower_bound, upper_bound]
"We're 95% confident the true value is in this range"
NOT: "95% probability true value is in this range" (The true value is fixed)
Calculation
CI = point_estimate ± critical_value * standard_error
For mean with normal distribution:
CI = x̄ ± 1.96 * (σ / √n)
For proportion:
CI = p̂ ± 1.96 * √(p̂(1-p̂)/n)
Bayesian Thinking
Frequentist vs Bayesian
FREQUENTIST:
- Probability = long-run frequency
- Parameters fixed but unknown
- Confidence intervals based on repeated experiments
BAYESIAN:
- Probability = degree of belief
- Parameters are random variables
- Prior beliefs + data → posterior beliefs
Bayesian Inference
| 1. Start with prior belief | P(θ) |
| 2. See data | P(data|θ) |
| 3. Update to posterior | P(θ|data) |
| Example | Is coin fair? |
| Prior | P(fair) = 50% |
| Observe | 10 heads out of 10 flips |
| Posterior | P(fair|10 heads) = very low (0.1%) |
Advantage: Incorporates prior knowledge
A/B Testing Statistics
Sample Size Calculation
Need to know:
- Baseline conversion rate (p)
- Minimum detectable effect (lift)
- Confidence level (typically 95%)
- Statistical power (typically 80%)
from scipy.stats import norm
def sample_size(p, effect_size, alpha=0.05, power=0.80):
z_alpha = norm.ppf(1 - alpha/2)
z_beta = norm.ppf(power)
n = ((z_alpha + z_beta)**2 * p * (1-p)) / (effect_size**2)
return n
# Example: baseline 5%, detect 10% lift
n = sample_size(0.05, 0.005) # ~36,000 samplesStatistical Test for A/B Test
Common Interview Questions
Q: What's the difference between correlation and causation?
A: Correlation means two variables move together. Causation means one causes the other. Correlation ≠ Causation. Could be coincidence, third variable causing both, reverse causation, or non-linear relationship.
Example: Ice cream sales & drowning deaths are correlated → Both caused by summer weather, not causation
Q: Explain Simpson's Paradox
A: Trend reverses when data is aggregated/disaggregated
Example: Treatment A vs B success rates
- Overall: A > B
- But by subgroup: A < B in each subgroup
- Caused by different group sizes
Q: What's the power of a statistical test?
A: Power = 1 - β = probability of detecting effect when it exists
- Power = 80% typical target
- Depends on: effect size, sample size, significance level
- Larger effect → higher power
- Larger sample → higher power
- Higher significance level → higher power
Q: Why collect more data?
A: Reduces standard error and variance
- Smaller confidence intervals
- More powerful hypothesis tests
- Better model generalization
- Smoother estimates
Summary
Key statistics for ML:
- Probability and Bayes' theorem
- Common distributions (normal, binomial)
- Hypothesis testing framework
- Confidence intervals
- A/B testing for experiments
- Understanding errors and power
Practice calculating these by hand during interviews to show mastery.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Statistics for Machine Learning Interviews.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, interview, preparation, statistics, for
Related Machine Learning Topics