Probability & Statistics: Deep Concept and Formula Guide for ML

Probability & Statistics: Deep Concept and Formula Guide for ML

This document provides intuitive explanations alongside mathematical formulas for key statistical concepts. It explicitly breaks down the formula terms and details exactly how these concepts act as the mathematical engines behind modern Machine Learning.


1. Maximum Likelihood Estimation (MLE) & Maximum A Posteriori (MAP)

These are the two fundamental frameworks for answering the question: “How do we choose the best weights (parameters) for our neural network?”

Maximum Likelihood Estimation (MLE)

  • Concept: Finds the parameters $\theta$ that make the observed data $D$ most probable. It trusts the training data completely and assumes no prior knowledge about what the weights should look like.
  • ML Context (Optimization Objective): In ML, MLE is the core optimization objective. When you train a classification network using Cross-Entropy Loss, you are literally doing Maximum Likelihood Estimation. You are adjusting the network weights so that the probability the network assigns to the correct labels in the training set is maximized.
  • Formula Breakdown: \(\hat{\theta}_{MLE} = \arg\max_{\theta} P(D | \theta) = \arg\max_{\theta} \prod_{i=1}^n P(x_i | \theta)\)
    • $\hat{\theta}_{MLE}$: The optimal weights we want to find.
    • $\arg\max_{\theta}$: “Find the value of $\theta$ that maximizes the following expression.”
    • $P(D\theta)$: The likelihood of the entire dataset $D$ given the model weights $\theta$.
    • $\prod_{i=1}^n P(x_i\theta)$: We assume each training example $x_i$ is independent, so the total probability is the product of individual probabilities.

    How does Maximization become Minimization? (The Log-Likelihood Trick)

    1. The Underflow Problem: Multiplying thousands of tiny probabilities (e.g., $0.1 \times 0.05 \times 0.2 \dots$) quickly results in a number so small that computers round it to 0.0 (numerical underflow).
    2. The Logarithm Solution: To fix this, we take the natural logarithm ($\log$) of the probability. Because $\log(A \times B) = \log(A) + \log(B)$, the massive multiplication turns into a massive addition, which computers handle perfectly. Also, because $\log$ is a strictly increasing function, the $\theta$ that maximizes the probability is the exact same $\theta$ that maximizes the log-probability. \(\arg\max_{\theta} \prod P(x_i | \theta) \quad \text{becomes} \quad \arg\max_{\theta} \sum \log P(x_i | \theta)\)
    3. Flipping the Sign: By convention, Deep Learning libraries (like PyTorch and TensorFlow) are built to minimize loss functions, not maximize them. Maximizing a positive number is mathematically identical to minimizing its negative. So, we multiply the whole equation by $-1$. \(\arg\max_{\theta} \sum \log P(x_i | \theta) \quad \text{becomes} \quad \arg\min_{\theta} -\sum_{i=1}^n \log P(x_i | \theta)\) (This final equation, the Negative Log-Likelihood, is the exact mathematical definition of Cross-Entropy Loss!)

Maximum A Posteriori (MAP)

  • Concept: Finds the most probable parameters given the data $D$ and a prior belief $P(\theta)$. It is MLE constrained by our prior assumptions.
  • ML Context (Regularization): In ML, we often don’t trust our training data perfectly because of overfitting. MAP allows us to inject “prior” knowledge. For example, if we believe our network weights should generally be small numbers clustered around zero, we apply a Gaussian prior. Training a model with MAP estimation using a Gaussian prior is mathematically identical to training with L2 Regularization (Weight Decay).
  • Formula Breakdown: \(\hat{\theta}_{MAP} = \arg\max_{\theta} P(\theta | D) = \arg\max_{\theta} \left[ P(D | \theta) \cdot P(\theta) \right]\)
    • $P(\thetaD)$: The “Posterior” — how likely these weights are after seeing the data.
    • $P(\theta)$: The “Prior” — how likely these weights were before seeing any data.

    In log-space (our optimization objective), this becomes: \(\hat{\theta}_{MAP} = \arg\min_{\theta} \left( -\sum_{i=1}^n \log P(x_i | \theta) - \log P(\theta) \right)\)

    • $-\sum \log P(x_i\theta)$: The standard loss (e.g., Cross-Entropy / MLE).
    • $-\log P(\theta)$: The penalty term. If $P(\theta)$ is a Gaussian distribution, $-\log P(\theta)$ simplifies to $\lambda \sum \theta^2$, which is exactly the L2 weight penalty!

2. Information Theory: Entropy, Cross-Entropy, & KL Divergence

These concepts measure the “surprise” or “difference” between probability distributions. They form the mathematical basis for loss functions in classification and language modeling.

Entropy ($H$)

  • Concept: Measures the inherent uncertainty or “messiness” of a true distribution $P$.
  • Formula Breakdown: \(H(P) = -\sum_x P(x) \log P(x)\)
    • $P(x)$: The true probability of event $x$.
    • $-\log P(x)$: The “surprise” of seeing $x$. Rare events have high surprise.
    • Entropy is the expected (average) surprise.

Cross-Entropy ($H(P, Q)$)

  • Concept: Measures the average surprise if we experience events from the true distribution $P$, but we believe they are coming from our model’s predicted distribution $Q$.
  • ML Context: This is the standard loss function for classification (and predicting the next word in LLMs). $P$ is the true label (usually a one-hot vector). $Q$ is your neural network’s output.
  • Formula Breakdown: \(H(P, Q) = -\sum_x P(x) \log Q(x)\)
    • $P(x)$: The true probability (e.g., 1 for the correct class, 0 for others).
    • $Q(x)$: Your model’s predicted probability for class $x$.
    • Because $P(x)=0$ for all incorrect classes, the sum collapses to just $-\log Q(\text{correct})$. Thus, minimizing Cross-Entropy simply means forcing your model to output a high probability for the correct class!

KL Divergence ($D_{KL}(P || Q)$)

  • Concept: Measures how much extra surprise is needed if you use distribution $Q$ to approximate the true distribution $P$.
  • ML Context: Used heavily in Reinforcement Learning from Human Feedback (RLHF) and Direct Preference Optimization (DPO). When fine-tuning an LLM, we add a KL Divergence penalty to prevent the new model ($Q$) from drifting too far away from the original pretrained model ($P$).
  • Formula Breakdown: \(D_{KL}(P || Q) = \sum_x P(x) \log \frac{P(x)}{Q(x)}\)
    • Crucial ML Relationship: $H(P, Q) = H(P) + D_{KL}(P Q)$. Cross-Entropy equals the true Entropy plus the KL Divergence. In ML, since the true labels ($P$) are fixed, $H(P)$ is constant. Therefore, minimizing Cross-Entropy is mathematically identical to minimizing KL Divergence between your model and the truth!

3. Confidence Intervals, Hypothesis Testing, and P-values

When evaluating models, a single metric (like “85% accuracy”) is dangerous. These tools help us quantify the reliability of our evaluations.

Confidence Intervals (CI)

  • Concept: An estimated range of values which is likely to include the “true, unseen population parameter.”
    • What does this mean? In statistics, you never have access to all the data in the universe (the population). You only have a tiny slice of it (your sample, like a test set). The “population parameter” is the true accuracy your model would have if it were evaluated on every single possible input in the universe.
    • Since you can’t measure that universal truth, you use your test set to calculate a sample accuracy, and then you draw a Confidence Interval around it.
    • A 95% Confidence Interval means that if you repeated this exact experiment (drawing a new test set and making an interval) 100 times, 95 of those computed intervals would successfully capture that true, unseen universal accuracy.
  • ML Context: You evaluate an LLM on 1,000 prompts and it gets 82% accuracy. You shouldn’t just deploy and claim 82%. You report a 95% CI of [80.5%, 83.5%]. This tells stakeholders the worst-case and best-case expected performance in production.
  • Formula Breakdown: \(\text{CI} = \bar{x} \pm Z_{1-\alpha/2} \left( \frac{s}{\sqrt{n}} \right)\)
    • $\bar{x}$: Your sample mean (e.g., your test set accuracy).
    • $s$: The standard deviation of your test set predictions.
    • $n$: The size of your test set. (Notice that as $n$ gets larger, the CI gets tighter).
    • $Z_{1-\alpha/2}$: The critical value from a normal distribution (usually 1.96 for a 95% CI).
    • How do you choose the Confidence Level (90%, 95%, 99%)?
      • This choice determines your $Z$ value (e.g., 1.64 for 90%, 1.96 for 95%, 2.58 for 99%).
      • The Trade-off: A higher confidence level (99%) means you are more certain you captured the true value, but the interval becomes much wider (and potentially less useful). A lower level (90%) gives a tighter, more precise interval, but there is a 10% chance the true value is completely outside your bounds.
      • In ML Practice: 95% ($\alpha = 0.05$) is the overwhelming industry standard for reporting metrics in ML research papers and production systems.

Hypothesis Testing & P-values

  • Concept: A formal framework to prove that an intervention (like a new architecture) actually works, rather than just getting lucky on the test set.
  • ML Context: You train a new custom Transformer (Model A) and want to prove it beats a baseline model (Model B). $H_0$ (the Null Hypothesis) is “Both models have the exact same underlying accuracy.” You run a test and get a p-value of $0.02$. This means: If the models were truly identical, there is only a 2% chance Model A would beat Model B by this much just due to test-set luck. Since 2% is less than the standard 5% threshold, you reject $H_0$ and conclude Model A is better.
  • P-value Formula Breakdown: \(\text{p-value} = P(|T| \ge |t_{obs}| \mid H_0 \text{ is true})\)
    • $T$: The random variable representing the test statistic if the null hypothesis were true.
    • $t_{obs}$: The actual difference in performance you observed between Model A and B.

4. Bootstrap & Permutation Tests (Resampling Methods)

The standard CI and p-value formulas above assume your data follows a normal distribution. In ML, metrics like F1-score, BLEU, or exact-match don’t follow nice algebraic rules. Resampling methods solve this by using raw compute power instead of elegant math.

Bootstrap (For Variance & CIs)

  • Concept: You simulate having thousands of different test sets by resampling your one test set over and over with replacement.
  • ML Context: You want a confidence interval for an F1 score. You can’t use the standard CI formula because F1 is a complex harmonic mean. Instead, you randomly sample your test data with replacement 1,000 times, calculate F1 1,000 times, and look at the spread of those results to find your 95% boundaries.
  • Algorithm & Formula Breakdown:
    1. For $b = 1$ to $B$ (e.g., $B=1000$ iterations):
      • Sample $N$ items from your test set $D$ with replacement to create a fake test set $D^*_b$.
      • Compute your metric on $D^_b$ to get $\theta^_b$ (e.g., the F1 score for this iteration).
    2. To find the variance of your metric: \(\text{Var}(\hat{\theta}) \approx \frac{1}{B-1} \sum_{b=1}^B (\theta^*_b - \bar{\theta}^*)^2\)
      • $\theta^*_b$: The score from one fake test set.
      • $\bar{\theta}^*$: The average score across all 1,000 fake test sets.

Permutation Test (For Hypothesis Testing)

  • Concept: Tests if Model A and Model B are truly different by destroying the labels that distinguish them and seeing if the difference disappears.
  • ML Context: You pool all the individual errors from Model A and Model B. You shuffle them wildly and arbitrarily assign half to “Fake Model A” and half to “Fake Model B”. You do this 10,000 times. If the true difference between Model A and B is larger than 99% of these shuffled fake differences, your models are significantly different.
  • Algorithm Breakdown: \(\text{p-value} = \frac{1}{K} \sum_{k=1}^K I(\delta_k \ge \delta_{obs})\)
    • $K$: Total number of shuffles (e.g., 10,000).
    • $\delta_{obs}$: The real performance difference you observed.
    • $\delta_k$: The performance difference on the $k$-th shuffled iteration.
    • $I(\dots)$: The Indicator Function. It equals 1 if the condition is true, and 0 if false. We are literally just counting how many times a random shuffle produced a difference bigger than our real model.

5. Bias, Variance, Irreducible Error

This is the holy grail of understanding why ML models fail to generalize to unseen data.

Concept: The Expected Mean Squared Error (MSE) of any model can be perfectly decomposed into three distinct mathematical components.

  • Irreducible Error ($\sigma^2$): Noise in the true data. Even the universe’s most perfect model cannot predict this. (e.g., trying to predict a coin flip).
  • Bias: Error caused by the model being too simple to capture the true patterns (Underfitting).
  • Variance: Error caused by the model being too complex and memorizing the exact noise in the training set (Overfitting).
  • ML Context: In Deep Learning, adding more layers/parameters decreases Bias but increases Variance. Techniques like Dropout, Data Augmentation, and Weight Decay deliberately increase a tiny bit of Bias in order to massively decrease Variance, resulting in a lower total error.

Formula Breakdown: Let the true, perfect universe function be $y = f(x) + \epsilon$, where $\epsilon$ is random noise. Let our ML model’s prediction be $\hat{f}(x)$. \(E\left[ (y - \hat{f}(x))^2 \right] = \text{Bias}(\hat{f}(x))^2 + \text{Var}(\hat{f}(x)) + \sigma^2\) Where:

  • $E[\dots]$: The Expected Value (the average over all possible training sets).
  • Bias Formula: \(\text{Bias}(\hat{f}(x)) = E[\hat{f}(x)] - f(x)\)
    • $E[\hat{f}(x)]$: The average prediction our model would make if we trained it on infinite different training sets.
    • $f(x)$: The true answer. Bias is how far off our average model is from the truth.
  • Variance Formula: \(\text{Var}(\hat{f}(x)) = E\left[ (\hat{f}(x) - E[\hat{f}(x)])^2 \right]\)
    • This measures how wildly our model’s predictions fluctuate if we just slightly change the training data.

6. Calibration and Uncertainty

Safety-critical ML systems (healthcare, finance, self-driving) must know when they do not know.

Calibration

  • Concept: A model is calibrated if its predicted probabilities match the empirical frequencies of correctness.
  • ML Context: Modern deep neural networks are notoriously poorly calibrated (often overconfident). If a network says “99% sure this is a dog,” it might only be right 70% of the time. We use techniques like Temperature Scaling post-training to fix this.
  • Formula Breakdown (Expected Calibration Error - ECE): \(\text{ECE} = \sum_{m=1}^M \frac{|B_m|}{N} \left| \text{acc}(B_m) - \text{conf}(B_m) \right|\)
    • $M$: The number of confidence bins (e.g., 10 bins: 0-10%, 10-20%, etc.).
    • $B_m$: The set of all predictions that fell into bin $m$.
    • $N$: Total number of samples.
    • $\text{acc}(B_m)$: The actual true accuracy of the predictions in that bin.
    • $\text{conf}(B_m)$: The average confidence the model outputted for that bin.
    • (If a model is perfectly calibrated, $\text{acc} = \text{conf}$ for every bin, and ECE is 0).

Uncertainty (Aleatoric vs. Epistemic)

  • Concept: Total predictive uncertainty is the sum of data noise (Aleatoric) and model ignorance (Epistemic).
  • ML Context:
    • Aleatoric (Data Noise): An autonomous car’s camera gets mud on it. The image is inherently bad. Gathering more training data won’t fix this specific blur.
    • Epistemic (Model Ignorance): The car encounters a person juggling on a unicycle. It is uncertain because its weights have never been optimized for this. Training it on more diverse data will reduce this uncertainty. We often measure epistemic uncertainty by using Ensembles or Monte Carlo Dropout (running the model 10 times and seeing if the predictions disagree with each other).
  • Formula Breakdown (Predictive Variance): \(\text{Var}(y | x) = \underbrace{\sigma^2(x)}_{\text{Aleatoric}} + \underbrace{\text{Var}_{\theta}(\hat{y} | x)}_{\text{Epistemic}}\)
    • $\sigma^2(x)$: The inherent variance/noise at input $x$.
    • $\text{Var}_{\theta}(\hat{y}x)$: The variance of the predictions caused by our uncertainty about the true optimal weights $\theta$.