2.4.8SVM, Naive Bayes & Probabilistic Models

Gaussian Naive Bayes

1,555 words7 min readdifficulty · medium3 backlinks

WHY does this method exist?

We want P(classfeatures)P(\text{class} \mid \text{features}). That is hard to model directly for continuous data. Bayes' theorem lets us flip it into things we can estimate from data:

P(yx)=P(y)P(xy)P(x)P(y \mid \mathbf{x}) = \frac{P(y)\,P(\mathbf{x}\mid y)}{P(\mathbf{x})}

  • WHAT we want: the posterior P(yx)P(y\mid\mathbf{x}).
  • WHAT we can measure: the prior P(y)P(y) (just count classes) and the likelihood P(xy)P(\mathbf{x}\mid y) (how features are distributed inside each class).

The problem: P(xy)=P(x1,x2,,xdy)P(\mathbf{x}\mid y)=P(x_1,x_2,\dots,x_d\mid y) is a joint distribution over all features — hopeless to estimate with limited data. The "Naive" trick fixes this.


The Naive assumption (the 80/20 core)

WHY is this a huge win? A joint distribution over dd features needs exponentially many parameters. Splitting it into dd separate 1-D distributions needs only O(d)O(d) parameters. It is "naive" because features are rarely truly independent — but the classifier still works surprisingly well.


The Gaussian part: modelling each P(xjy)P(x_j\mid y)

We only need two numbers per feature per class: the mean μy,j\mu_{y,j} and variance σy,j2\sigma_{y,j}^2.

HOW do we estimate μ\mu and σ2\sigma^2? (Derivation from scratch)

We use Maximum Likelihood Estimation (MLE). Take all nyn_y training points in class yy; their feature-jj values are x(1),,x(ny)x^{(1)},\dots,x^{(n_y)}. The log-likelihood of a Gaussian is:

(μ,σ2)=i=1ny[12ln(2πσ2)(x(i)μ)22σ2]\ell(\mu,\sigma^2)=\sum_{i=1}^{n_y}\left[-\tfrac12\ln(2\pi\sigma^2)-\frac{(x^{(i)}-\mu)^2}{2\sigma^2}\right]

Solve for μ\mu — set /μ=0\partial\ell/\partial\mu=0: μ=ix(i)μσ2=0    μ=1nyix(i)\frac{\partial\ell}{\partial\mu}=\sum_i \frac{x^{(i)}-\mu}{\sigma^2}=0 \;\Rightarrow\; \boxed{\mu = \frac{1}{n_y}\sum_i x^{(i)}} Why this step? The variance cancels, leaving the plain sample mean.

Solve for σ2\sigma^2 — set /σ2=0\partial\ell/\partial\sigma^2=0: σ2=i[12σ2+(x(i)μ)22σ4]=0    σ2=1nyi(x(i)μ)2\frac{\partial\ell}{\partial\sigma^2}=\sum_i\left[-\frac{1}{2\sigma^2}+\frac{(x^{(i)}-\mu)^2}{2\sigma^4}\right]=0 \;\Rightarrow\; \boxed{\sigma^2=\frac{1}{n_y}\sum_i (x^{(i)}-\mu)^2} Why this step? Multiply through by 2σ42\sigma^4 and rearrange — you get the sample variance.

So training is just: compute per-class, per-feature mean and variance. That's it.


The decision rule (put it together)

Since P(x)P(\mathbf{x}) is the same for every class, we drop it and maximise the numerator:

y^=argmaxy  P(y)j=1dP(xjy)\hat{y}=\arg\max_y \; P(y)\prod_{j=1}^{d}P(x_j\mid y)

Figure — Gaussian Naive Bayes

Worked Example 1 — one feature, two classes

Feature = height (cm). Class A (short): μA=160, σA=5\mu_A=160,\ \sigma_A=5. Class B (tall): μB=180, σB=5\mu_B=180,\ \sigma_B=5. Priors equal. Classify x=172x=172.

  • lnP(xA)(172160)2225=14450=2.88\ln P(x\mid A)\propto -\frac{(172-160)^2}{2\cdot25}=-\frac{144}{50}=-2.88 Why? Constant ln(2πσ2)\ln(2\pi\sigma^2) is identical for both classes (same σ\sigma), so it cancels.
  • lnP(xB)(172180)2225=6450=1.28\ln P(x\mid B)\propto -\frac{(172-180)^2}{2\cdot25}=-\frac{64}{50}=-1.28

1.28>2.88-1.28 > -2.88 \Rightarrow Class B (tall). Why? 172172 is closer to 180180; with equal σ\sigma and equal priors, GNB just picks the nearest mean.

Worked Example 2 — unequal variance flips intuition

Class A: μA=160,σA=20\mu_A=160,\sigma_A=20. Class B: μB=180,σB=3\mu_B=180,\sigma_B=3. Classify x=172x=172.

  • lnP(xA)=12ln(2π400)144800=4.030.18=4.21\ln P(x\mid A)=-\tfrac12\ln(2\pi\cdot400)-\frac{144}{800}=-4.03-0.18=-4.21
  • lnP(xB)=12ln(2π9)6418=2.023.56=5.58\ln P(x\mid B)=-\tfrac12\ln(2\pi\cdot9)-\frac{64}{18}=-2.02-3.56=-5.58

4.21>5.58-4.21 > -5.58 \Rightarrow Class A, even though 172172 is closer to B's mean! Why this step? B is very "tight" (σ=3\sigma=3): a value 12 units away is extremely unlikely for B. The 12ln(2πσ2)-\tfrac12\ln(2\pi\sigma^2) term and the variance in the denominator both penalise confident-but-far predictions. This is why GNB decision boundaries are curved (quadratic), not just midpoints.

Worked Example 3 — two features

Point x=(x1,x2)\mathbf{x}=(x_1,x_2). GNB scores lnP(y)(x1μy1)22σy12(x2μy2)22σy22+consty\ln P(y)-\frac{(x_1-\mu_{y1})^2}{2\sigma_{y1}^2}-\frac{(x_2-\mu_{y2})^2}{2\sigma_{y2}^2}+\text{const}_y. Why? The independence assumption turns the joint into a sum of per-feature log-terms — each feature votes separately.



Recall Feynman: explain to a 12-year-old

Imagine each class is a person throwing darts at a number line, aiming for their favourite spot (the mean). Some throwers are shaky (big spread) and some are super precise (small spread). A new dart lands. You guess which thrower threw it by asking: "For each thrower, how surprised would I be if THEY threw it here?" You pick the thrower who'd be least surprised — considering both how close it is to their target and how precise they usually are. The precise thrower gets little credit for far-away darts; the shaky one is forgiven. That "least surprised" rule is Gaussian Naive Bayes.


Flashcards

What core assumption makes Naive Bayes "naive"?
Features are conditionally independent given the class, so P(xy)=jP(xjy)P(\mathbf{x}\mid y)=\prod_j P(x_j\mid y).
Why does the naive assumption help computationally?
It reduces a joint distribution (exponential parameters) to dd separate 1-D distributions (O(d)O(d) parameters).
In Gaussian NB, what distribution models each P(xjy)P(x_j\mid y)?
A univariate normal with class/feature-specific mean μy,j\mu_{y,j} and variance σy,j2\sigma_{y,j}^2.
What are the MLE estimates for μ\mu and σ2\sigma^2?
μ=1nxi\mu=\frac1n\sum x_i (sample mean) and σ2=1n(xiμ)2\sigma^2=\frac1n\sum(x_i-\mu)^2 (sample variance, divide by nn).
Why do we drop P(x)P(\mathbf{x}) in the decision rule?
It's the same for all classes, so it doesn't change the argmax.
Why take logs of the probabilities?
Products of many small numbers underflow; log is monotonic so argmax is preserved and products become sums.
When does GNB reduce to "nearest class mean"?
When all classes share equal variance and equal priors.
Why is the GNB decision boundary generally quadratic?
The (xμ)2/2σ2(x-\mu)^2/2\sigma^2 and 12lnσ2-\tfrac12\ln\sigma^2 terms differ per class, giving a quadratic (curved) boundary.
What is var_smoothing for?
A small value added to variances to prevent division by zero when a feature has near-zero variance.
Full GNB decision rule (log form)?
y^=argmaxy[lnP(y)+j(12ln(2πσy,j2)(xjμy,j)22σy,j2)]\hat y=\arg\max_y[\ln P(y)+\sum_j(-\tfrac12\ln(2\pi\sigma_{y,j}^2)-\frac{(x_j-\mu_{y,j})^2}{2\sigma_{y,j}^2})].

Connections

Concept Map

flip via Bayes

needs

needs

estimated by

joint too hard

conditional independence

reduces params to O d

continuous features

needs two params

estimated via

solve dl/dmu = 0

solve dl/dsigma2 = 0

classify via

Posterior P y given x

Bayes Theorem

Prior P y

Likelihood P x given y

Count classes

Naive Assumption

Product of P xj given y

Per-feature 1-D distributions

Gaussian Likelihood

Mean and Variance

Maximum Likelihood MLE

Sample Mean

Sample Variance

Argmax over classes

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Gaussian Naive Bayes ka core idea simple hai: har class ko ek bell-curve (normal distribution) maan lo. Training ke time bas har class ke andar, har feature ka mean aur variance nikaal lo — bas itna hi training hai! Phir jab naya point aata hai, to poochte ho: "Kaunsi class ki bell-curve ne is value ko sabse zyada easily produce kiya hoga?" Jis class ke liye point sabse kam surprising hai, wahi answer hai.

"Naive" isliye kehte hain kyunki hum assume karte hain ki saare features ek-dusre se independent hain (given the class). Real life mein ye galat hota hai, par phir bhi kaam karta hai — kyunki humein sirf sahi class choose karni hai, exact probability nahi chahiye. Is assumption se joint distribution tut ke chhote-chhote 1-D Gaussians ka product ban jaata hai, jo estimate karna aasan hai.

Ek important baat (Example 2 dekho): sirf "nearest mean" wala rule tabhi chalta hai jab sab classes ka variance aur prior barabar ho. Agar koi class bahut tight (chhota sigma) hai, to usse thoda door wala point bhi bahut unlikely ho jaata hai — isliye decision boundary curved (quadratic) banti hai. Aur practical tip: probabilities ko multiply mat karo, log leke add karo, warna numbers itne chhote ho jaate hain ki computer 0 bana deta hai (underflow).

Test yourself — SVM, Naive Bayes & Probabilistic Models

Connections