Module 6 · Formula sheet

Back to the Module 6 guide.

Bayes and joint densities

Weight each class by how common it is and how well it fits the input. Let Y=kY=k name a class, xx be an observed continuous input, πk=P(Y=k)\pi_k=P(Y=k) its prior, and fk(x)f_k(x) the density of XX within that class. There are KK classes.

The joint density in xx and discrete label kk is

g(x,k)=πkfk(x).g(x,k)=\pi_kf_k(x).

Its area over all xx is πk\pi_k. A conditional density fkf_k has area 1. Density is curve height; the probability of a continuous input interval is its area, and P(X=x)=0P(X=x)=0 at an exact point.

The posterior, or class probability after seeing xx, is

P(Y=k∣X=x)=πkfk(x)∑ℓ=1Kπℓfℓ(x).P(Y=k\mid X=x)= \frac{\pi_kf_k(x)}{\sum_{\ell=1}^{K}\pi_\ell f_\ell(x)}.

ℓ\ell runs through all class labels. For equal mistake costs, choose

C(x)=arg⁡max⁡k{πkfk(x)}.C(x)=\arg\max_k\{\pi_kf_k(x)\}.

C(x)C(x) is the predicted label; arg max returns the label with the largest weight. The denominator is the same for every candidate class.

Invented numbers: priors 0.8,0.20.8,0.2 and densities 0.1,0.30.1,0.3 give weights 0.08,0.060.08,0.06. Class 1’s posterior is 0.06/(0.08+0.06)≈0.4290.06/(0.08+0.06)\approx0.429; predict class 0.

Q3: labels y∈{−1,+1}y\in\{-1,+1\}, means 3y3y, shared variance 1, and priors 1/21/2 give

g(x,y)=122πexp⁡ ⁣[−(x−3y)22].g(x,y)=\frac{1}{2\sqrt{2\pi}} \exp\!\left[-\frac{(x-3y)^2}{2}\right].

Univariate LDA

Model one bell curve per class with a shared spread. For input XX and class Y=kY=k:

X∣Y=k∼N(μk,σ2),X\mid Y=k\sim\mathcal N(\mu_k,\sigma^2), fk(x)=1σ2πexp⁡ ⁣[−(x−μk)22σ2].f_k(x)=\frac{1}{\sigma\sqrt{2\pi}} \exp\!\left[-\frac{(x-\mu_k)^2}{2\sigma^2}\right].

μk\mu_k is the class mean; σ2>0\sigma^2>0 is the common variance; σ\sigma is the standard deviation. Here π\pi is the mathematical constant and πk=P(Y=k)>0\pi_k=P(Y=k)>0 is the class prior. exp⁡(a)=ea\exp(a)=e^a.

Instead of comparing weighted densities πkfk(x)\pi_kf_k(x), compare linear discriminant scores:

δk(x)=μkxσ2−μk22σ2+log⁡πk.\delta_k(x)=\frac{\mu_kx}{\sigma^2} -\frac{\mu_k^2}{2\sigma^2}+\log\pi_k.

log⁡\log is the natural logarithm. Predict the label with the largest score. A discriminant score is not itself a probability.

For classes 0 and 1 with μ1>μ0\mu_1>\mu_0, equal mistake costs give input boundary

γ=μ0+μ12+σ2μ1−μ0log⁡π0π1.\begin{aligned} \gamma&=\frac{\mu_0+\mu_1}{2}\\ &\quad+\frac{\sigma^2}{\mu_1-\mu_0} \log\frac{\pi_0}{\pi_1}. \end{aligned}

Predict 1 for x≥γx\geq\gamma, otherwise 0. Equal priors give log⁡(1)=0\log(1)=0, hence the midpoint. If the means are reversed, reverse the direction of the threshold rule. If they coincide, identical densities leave only the priors to compare.

Q7: means −1,+1-1,+1, variance 1/41/4, and priors 1/2,1/21/2,1/2 give γ=(−1+1)/2=0\gamma=(-1+1)/2=0. Q3: means −3,+3-3,+3 and equal priors also give γ=0\gamma=0, using labels −1,+1-1,+1.

Estimating LDA from data

Count classes, average within classes, then pool the within-class spread. Let training pair (xi,yi)(x_i,y_i) contain input xix_i and true label yiy_i. There are NN observations and KK classes; NkN_k is class kk‘s count. Hats mean estimates.

π^k=NkN,μ^k=1Nk∑i:yi=kxi.\widehat\pi_k=\frac{N_k}{N}, \qquad \widehat\mu_k=\frac1{N_k}\sum_{i:y_i=k}x_i.

The shared estimated variance is

σ^2=∑k=1K∑i:yi=k(xi−μ^k)2N−K.\widehat\sigma^2= \frac{\sum_{k=1}^{K}\sum_{i:y_i=k} (x_i-\widehat\mu_k)^2}{N-K}.

Each difference uses its own class’s mean. If the class sample variances sk2s_k^2 are given, the equivalent rule is

σ^2=∑k=1K(Nk−1)sk2N−K,\widehat\sigma^2= \frac{\sum_{k=1}^{K}(N_k-1)s_k^2}{N-K},

where sk2=∑i:yi=k(xi−μ^k)2/(Nk−1)s_k^2=\sum_{i:y_i=k}(x_i-\widehat\mu_k)^2/(N_k-1) when Nk≥2N_k\geq2. The denominator N−KN-K accounts for estimating one mean per class. The pooled-deviations rule needs each class represented, N>KN>K, and a positive result to use as an LDA variance.

Invented data: class 0 inputs −2,0-2,0; class 1 inputs 2,42,4. Then N=4N=4, K=2K=2, each Nk=2N_k=2; priors are 2/4=0.52/4=0.5. Means are (−2+0)/2=−1(-2+0)/2=-1 and (2+4)/2=3(2+4)/2=3. Squared within-class deviations sum to 1+1+1+1=41+1+1+1=4, so pooled variance is 4/(4−2)=24/(4-2)=2.

Insert these estimates into the LDA score to classify new inputs.

Multivariate LDA

Several inputs make a cloud rather than a one-dimensional bell curve. Let x\mathbf x contain pp input values, μk\boldsymbol\mu_k contain the pp class means, and Σ\Sigma be the shared nonsingular p×pp\times p covariance matrix. Covariance records input spreads and how inputs move together.

Define the adjusted squared distance

uk=x−μk,dk2=ukTΣ−1uk.\begin{aligned} \mathbf u_k&=\mathbf x-\boldsymbol\mu_k,\\ d_k^2&=\mathbf u_k^T\Sigma^{-1}\mathbf u_k. \end{aligned}

Then the class density is

fk(x)=exp⁡(−dk2/2)(2π)p/2∣Σ∣1/2.f_k(\mathbf x)= \frac{\exp(-d_k^2/2)}{(2\pi)^{p/2}|\Sigma|^{1/2}}.

uk\mathbf u_k is the offset from the class center; TT denotes transpose; Σ−1\Sigma^{-1} adjusts for spread and correlation; ∣Σ∣|\Sigma| is the determinant; π\pi is the mathematical constant. With class priors πk=P(Y=k)\pi_k=P(Y=k), predict the class with largest πkfk(x)\pi_kf_k(\mathbf x).

For p=1p=1, Σ=σ2\Sigma=\sigma^2 and dk2=(x−μk)2/σ2d_k^2=(x-\mu_k)^2/\sigma^2, giving the usual univariate normal density. Shared covariance gives linear class boundaries: lines for two inputs, flat surfaces in higher dimensions.

Confusion matrix rates

Name the actual class before choosing the denominator. Positive means class 1; negative means class 0. These symbols are counts:

PredictionActual 0Actual 1
Predict 0True negative (TN)False negative (FN)
Predict 1False positive (FP)True positive (TP)

Among actual negatives:

FPR=FPFP+TN,TNR=TNFP+TN=1−FPR.\begin{aligned} FPR&=\frac{FP}{FP+TN},\\ TNR&=\frac{TN}{FP+TN}=1-FPR. \end{aligned}

Among actual positives:

FNR=FNFN+TP,TPR=TPFN+TP=1−FNR.\begin{aligned} FNR&=\frac{FN}{FN+TP},\\ TPR&=\frac{TP}{FN+TP}=1-FNR. \end{aligned}

The denominator must be nonzero. Overall, with N=TP+TN+FP+FNN=TP+TN+FP+FN:

Error rate=FP+FNN.\text{Error rate}=\frac{FP+FN}{N}.

Slide training counts: TN=9644TN=9644, FN=252FN=252, FP=23FP=23, TP=81TP=81. Hence FPR=23/9667≈0.238%FPR=23/9667\approx0.238\%, FNR=252/333≈75.68%FNR=252/333\approx75.68\%, TPR=81/333≈24.32%TPR=81/333\approx24.32\%, and training error =(23+252)/10000=2.75%=(23+252)/10000=2.75\%. These describe the training data; test performance needs new observations.

Q4, spam is positive: FPR is legitimate email incorrectly marked spam, divided by all actual legitimate email. FNR is missed spam divided by all actual spam. TPR is caught spam divided by all actual spam.

Count mistakes with indicators

For observation ii, let yiy_i be its true label and C(xi)C(x_i) its predicted label. An indicator 1{A}\mathbf1\{A\} equals 1 if statement AA is true, otherwise 0:

FP=∑i=1N1{C(xi)=1}1{yi=0},FP=\sum_{i=1}^{N} \mathbf1\{C(x_i)=1\}\mathbf1\{y_i=0\}, FN=∑i=1N1{C(xi)=0}1{yi=1}.FN=\sum_{i=1}^{N} \mathbf1\{C(x_i)=0\}\mathbf1\{y_i=1\}.

Both switches must be on to count a row. The slides’ equality switch δ(a)=1\delta(a)=1 for a=0a=0, otherwise 0, serves the same purpose.

Gaussian tail errors

Standardize the boundary, then take the area on the wrong side. Suppose classes 0 and 1 have normal means μ0,μ1\mu_0,\mu_1, shared standard deviation σ>0\sigma>0, and the rule predicts 1 for x≥γx\geq\gamma.

The standard-normal cumulative distribution function Φ(z)=P(Z≤z)\Phi(z)=P(Z\leq z) gives the area to the left of zz for Z∼N(0,1)Z\sim\mathcal N(0,1). The standardized boundary for class kk is

zk=γ−μkσ.z_k=\frac{\gamma-\mu_k}{\sigma}.

For class density fk(x)f_k(x), conditional error rates are

FPR=∫γ∞f0(x) dx=1−Φ(z0),\begin{aligned} FPR&=\int_\gamma^\infty f_0(x)\,dx\\ &=1-\Phi(z_0), \end{aligned} FNR=∫−∞γf1(x) dx=Φ(z1).\begin{aligned} FNR&=\int_{-\infty}^\gamma f_1(x)\,dx\\ &=\Phi(z_1). \end{aligned}

Then TPR=1−FNRTPR=1-FNR.

Each conditional density has total area 1. An integral adds curve area, giving probability.

Q3: use negative-class mean −3-3, σ=1\sigma=1, and boundary 00. Then z=[0−(−3)]/1=3z=[0-(-3)]/1=3. The 99.7% empirical rule gives one tail ≈(1−0.997)/2=0.0015\approx(1-0.997)/2=0.0015. Exact normal area is 1−Φ(3)≈0.0013501-\Phi(3)\approx0.001350.

Q7: use means −1,+1-1,+1, σ=1/2\sigma=1/2, and boundary 00. Then z0=[0−(−1)]/(1/2)=2z_0=[0-(-1)]/(1/2)=2 and z1=(0−1)/(1/2)=−2z_1=(0-1)/(1/2)=-2. The practice approximation gives

FPR≈FNR≈1−0.9542=0.023,\begin{aligned} FPR\approx FNR&\approx\frac{1-0.954}{2}\\ &=0.023, \end{aligned} TPR≈1−0.023=0.977.TPR\approx1-0.023=0.977.

Exact Gaussian values are FPR=FNR≈0.022750FPR=FNR\approx0.022750 and TPR≈0.977250TPR\approx0.977250. Use either exact values or the stated empirical approximation consistently.

Population error and expected counts

Conditional rates have different denominators; use priors to combine them. Let π0=P(Y=0)\pi_0=P(Y=0) and π1=P(Y=1)\pi_1=P(Y=1) be actual class frequencies, with π0+π1=1\pi_0+\pi_1=1. Let FPR=P(Y^=1∣Y=0)FPR=P(\widehat Y=1\mid Y=0) and FNR=P(Y^=0∣Y=1)FNR=P(\widehat Y=0\mid Y=1), where Y^\widehat Y is the predicted label.

P(false positive)=π0FPR,P(\text{false positive})=\pi_0FPR, P(false negative)=π1FNR,P(\text{false negative})=\pi_1FNR, Error=π0FPR+π1FNR.\boxed{\text{Error}=\pi_0FPR+\pi_1FNR.}

For a batch of NN observations drawn from this population, EE denotes an expected count:

E[FP]=Nπ0FPR,E[FN]=Nπ1FNR.\begin{aligned} E[FP]&=N\pi_0FPR,\\ E[FN]&=N\pi_1FNR. \end{aligned}

Q7: equal priors 0.5,0.50.5,0.5 and the approximate rates 0.023,0.0230.023,0.023 give error 0.5(0.023)+0.5(0.023)=0.023=2.3%0.5(0.023)+0.5(0.023)=0.023=2.3\%. An invented batch of N=1000N=1000 has expected FP =1000(0.5)(0.023)=11.5=1000(0.5)(0.023)=11.5 and the same expected FN, or 23 expected mistakes total.

Expected counts can be fractional; observed counts cannot. For a dataset, using its observed class proportions and observed conditional rates recovers (FP+FN)/N(FP+FN)/N exactly.

Shifted Gaussian inputs

Adding a constant moves the center without changing the spread. Suppose

X=aY+ε,ε∣Y=k∼N(m,s2).\begin{aligned} X&=aY+\varepsilon,\\ \varepsilon\mid Y=k&\sim\mathcal N(m,s^2). \end{aligned}

Y=kY=k is a class label; aa is a supplied multiplier; mm is the noise mean and s2s^2 its variance. The same noise distribution is assumed in each class. Substituting Y=kY=k gives

X∣Y=k∼N(ak+m,s2).X\mid Y=k\sim\mathcal N(ak+m,s^2).

With s>0s>0, its density is

fk(x)=1s2πexp⁡ ⁣[−(x−ak−m)22s2].f_k(x)=\frac1{s\sqrt{2\pi}} \exp\!\left[-\frac{(x-ak-m)^2}{2s^2}\right].

Q7 supplies a=2a=2, m=−1m=-1, s2=1/4s^2=1/4, and k∈{0,1}k\in\{0,1\}. Thus means are 2(0)−1=−12(0)-1=-1 and 2(1)−1=12(1)-1=1, with standard deviation s=1/4=1/2s=\sqrt{1/4}=1/2 in both classes:

fk(x)=2πexp⁡[−2(x+1−2k)2].f_k(x)=\sqrt{\frac2\pi}\exp[-2(x+1-2k)^2].

Equal transmitted frequencies give π0=π1=1/2\pi_0=\pi_1=1/2. The Gaussian’s second parameter is variance, so do not use 1/41/4 as the standard deviation.

Thresholds and ROC

The threshold chooses the balance of misses and false alarms. Let p1(x)=P(Y=1∣X=x)p_1(x)=P(Y=1\mid X=x) be the posterior for class 1 and τ\tau a probability threshold:

Cτ(x)={1,p1(x)≥τ,0,p1(x)<τ.C_\tau(x)=\begin{cases} 1,&p_1(x)\geq\tau,\\ 0,&p_1(x)<\tau. \end{cases}

Equal costs for false positives and false negatives give τ=0.5\tau=0.5. Lowering τ\tau can increase TPR and FPR while decreasing FNR; raising it reverses those changes. Counts may stay unchanged between observed scores.

For univariate LDA with class means μ1>μ0\mu_1>\mu_0, common variance σ2>0\sigma^2>0, priors π0,π1>0\pi_0,\pi_1>0, and 0<τ<10<\tau<1, the equivalent input boundary is

γτ=μ0+μ12+σ2μ1−μ0[log⁡π0π1+log⁡τ1−τ].\begin{aligned} \gamma_\tau&=\frac{\mu_0+\mu_1}{2}\\ &\quad+\frac{\sigma^2}{\mu_1-\mu_0} \left[\log\frac{\pi_0}{\pi_1} +\log\frac{\tau}{1-\tau}\right]. \end{aligned}

Predict 1 for x≥γτx\geq\gamma_\tau. τ\tau is a probability; γτ\gamma_\tau has the input’s units. log⁡\log is the natural logarithm.

Q7 model, invented threshold: means −1,+1-1,+1, variance 1/41/4, equal priors, and τ=0.2\tau=0.2 give

γ0.2=0+1/42log⁡0.20.8=18log⁡(0.25)≈−0.1733.\begin{aligned} \gamma_{0.2} &=0+\frac{1/4}{2}\log\frac{0.2}{0.8}\\ &=\frac18\log(0.25)\approx-0.1733. \end{aligned}

Moving left from the usual boundary 0 predicts more positives, catching more actual ones and also more actual zeros.

An ROC curve plots FPRFPR horizontally and TPR=1−FNRTPR=1-FNR vertically while varying the threshold. Here FPR is the fraction of actual negatives flagged positive; TPR is the fraction of actual positives caught. Each point is one threshold. The desirable corner is (0,1)(0,1).

AUC is the area under the ROC curve: 1 for perfect separation and 0.5 for a diagonal curve. It summarizes separation across thresholds; costs and acceptable error rates still determine which threshold to use.