Module 5 · Formula sheet

Back to the Module 5 guide.

Class probabilities and Bayes classification

Choose the most likely label when all mistakes count equally. For input xx, the conditional class probability is

pk(x)=P(Y=k∣X=x).p_k(x)=P(Y=k\mid X=x).

XX is the input, YY the label, kk a possible label, and ∣\mid means “given.” For KK classes, their probabilities sum to 1. With two labels, 0 and 1, p0(x)=1−p1(x)p_0(x)=1-p_1(x).

The Bayes optimal classifier uses the true probabilities:

C∗(x)=arg⁡max⁡k  pk(x).C^*(x)=\underset{k}{\arg\max}\;p_k(x).

“Arg max” returns the label with the largest probability. With two labels, choose 1 when p1(x)≥0.5p_1(x)\geq0.5 and 0 otherwise; either label is equally good at a tie, which we assign to 1.

Invented example: if p1(x)=0.8p_1(x)=0.8, predicting 1 has error probability 1−0.8=0.21-0.8=0.2; predicting 0 has error probability 0.8. The Bayes rule chooses 1. With estimated probabilities, the same decision rule gives an estimated classifier, not necessarily the true optimum.

Local averaging

Estimate a probability by counting nearby labels. For binary training labels yi∈{0,1}y_i\in\{0,1\}, choose a neighborhood N(x)\mathcal N(x) containing m>0m>0 observations near input xx:

p^1(x)=1m∑i∈N(x)yi,p^0(x)=1−p^1(x).\begin{aligned} \widehat p_1(x)&=\frac{1}{m} \sum_{i\in\mathcal N(x)}y_i,\\ \widehat p_0(x)&=1-\widehat p_1(x). \end{aligned}

The index ii selects a training row, mm is the number of selected rows, and a hat marks an estimate. Since labels are 0 or 1, the sum counts class-1 observations. Choose class 1 when p^1(x)≥0.5\widehat p_1(x)\geq0.5.

Invented neighborhood: 1,1,0,1,01,1,0,1,0 gives p^1=3/5=0.6\widehat p_1=3/5=0.6 and p^0=0.4\widehat p_0=0.4, so predict 1.

In many dimensions, small neighborhoods can be nearly empty and large ones can include dissimilar inputs: the curse of dimensionality.

Horizontal shifts

Work with the expression inside the function. If R(u)=1R(u)=1 for a≤u≤ba\leq u\leq b and 0 otherwise, then

R(x+c)=1  ⟺  a≤x+c≤b  ⟺  a−c≤x≤b−c.\begin{aligned} R(x+c)=1 &\iff a\leq x+c\leq b\\ &\iff a-c\leq x\leq b-c. \end{aligned}

a,ba,b are the original interval endpoints; cc is the amount added to the input. For c>0c>0, R(x+c)R(x+c) shifts left by cc; R(x−c)R(x-c) shifts right by cc. The height is unchanged. Included endpoints stay included.

Shared Q1: a=−2a=-2, b=2b=2, c=1c=1. Thus R(x+1)=1R(x+1)=1 on [−2−1,2−1]=[−3,1][-2-1,2-1]=[-3,1] and 0 outside. At x=−3x=-3, the inside is −3+1=−2-3+1=-2, so the value is 1.

Sigmoid identities

The sigmoid turns a real score into a probability between 0 and 1. For real number zz:

S(z)=11+e−z=ez1+ez.S(z)=\frac{1}{1+e^{-z}} =\frac{e^z}{1+e^z}.

SS names the sigmoid; e≈2.718e\approx2.718 is the exponential base. Multiply numerator and denominator of the first form by eze^z to get the second, using eze−z=1e^ze^{-z}=1.

Reverse the score to get the complementary probability:

S(−z)=11+ez=e−z1+e−z,S(-z)=\frac{1}{1+e^z} =\frac{e^{-z}}{1+e^{-z}}, 1−S(z)=S(−z).\boxed{1-S(z)=S(-z)}.

For subtraction, put 1 over the denominator 1+e−z1+e^{-z}; the numerator becomes (1+e−z)−1=e−z(1+e^{-z})-1=e^{-z}.

Shared Q2 uses xx instead of zz; the identities are identical. Check: S(0)=1/2S(0)=1/2, S(2)≈0.8808S(2)\approx0.8808, and S(−2)≈0.1192S(-2)\approx0.1192.

Logistic prediction

Add contributions, then apply the sigmoid. With dd features and fitted coefficients:

z=β^0+∑j=1dβ^jxj,z=\widehat\beta_0+ \sum_{j=1}^{d}\widehat\beta_jx_j, p^1(x)=11+e−z,p^0(x)=1−p^1(x).\begin{aligned} \widehat p_1(x)&=\frac{1}{1+e^{-z}},\\ \widehat p_0(x)&=1-\widehat p_1(x). \end{aligned}

xjx_j is feature jj of input xx, β^j\widehat\beta_j its fitted weight, β^0\widehat\beta_0 the fitted intercept, and zz the score. Hats mean estimated from data. p^k(x)\widehat p_k(x) estimates the chance of class kk; e≈2.718e\approx2.718.

Shared Q5: supplied coefficients are −6,0.05,1-6,0.05,1; supplied inputs are 40 study hours and GPA 3.5.

z=−6+0.05(40)+1(3.5)=−0.5,p^1=11+e0.5≈0.3775.\begin{aligned} z&=-6+0.05(40)+1(3.5)=-0.5,\\ \widehat p_1&=\frac{1}{1+e^{0.5}}\approx0.3775. \end{aligned}

Estimated chance of an A: 37.8%. The probability is for this student; the training sample size is not an extra divisor.

Odds and logit

Odds compare the chance of class 1 with the chance of class 0. For probability q=P(Y=1∣X=x)q=P(Y=1\mid X=x), with 0<q<10<q<1:

odds=q1−q,logit⁡(q)=ln⁡ ⁣(q1−q).\begin{aligned} \text{odds}&=\frac{q}{1-q},\\ \operatorname{logit}(q)&=\ln\!\left(\frac{q}{1-q}\right). \end{aligned}

ln⁡\ln is the natural logarithm. Logistic regression makes the log-odds a weighted score:

z=β0+∑j=1dβjxj,logit⁡(q)=z.z=\beta_0+\sum_{j=1}^{d}\beta_jx_j, \qquad \operatorname{logit}(q)=z.

dd counts features, xjx_j is feature jj, βj\beta_j its coefficient, and β0\beta_0 the intercept. Equivalently,

q1−q=ez,q=ez1+ez.\frac{q}{1-q}=e^z, \qquad q=\frac{e^z}{1+e^z}.

Shared Q6 proof: exponentiate ln⁡(q/(1−q))=z\ln(q/(1-q))=z to get q/(1−q)=ezq/(1-q)=e^z. Multiply by 1−q1-q to get q=ez−ezqq=e^z-e^zq. Collect terms: q(1+ez)=ezq(1+e^z)=e^z. Divide by 1+ez>01+e^z>0 to obtain q=ez/(1+ez)q=e^z/(1+e^z). In reverse, 1−q=1/(1+ez)1-q=1/(1+e^z), so q/(1−q)=ezq/(1-q)=e^z and its log is zz.

Coefficient meaning: holding other features fixed, increasing xjx_j by 1 adds βj\beta_j to log-odds and multiplies odds by eβje^{\beta_j}. The probability change is not constant.

Numerical check: q=0.8q=0.8 gives odds 0.8/0.2=40.8/0.2=4, log-odds ln⁡4≈1.3863\ln4\approx1.3863, and the inverse gives eln⁡4/(1+eln⁡4)=4/5=0.8e^{\ln4}/(1+e^{\ln4})=4/5=0.8.

Solving for a target probability

Turn the target chance back into a score, then solve a linear equation. Suppose one input hh can change while all others stay fixed:

z=a+bh,q=11+e−z.z=a+bh,\qquad q=\frac{1}{1+e^{-z}}.

aa is the intercept plus all fixed contributions, b≠0b\neq0 is the coefficient of hh, and qq is the predicted class-1 probability. For target probability 0<t<10<t<1:

h=ln⁡(t/(1−t))−ab.\boxed{h=\frac{\ln(t/(1-t))-a}{b}}.

tt is the requested probability, and ln⁡\ln is the natural logarithm. At t=0.5t=0.5, ln⁡(t/(1−t))=0\ln(t/(1-t))=0, so h=−a/bh=-a/b.

Shared Q5: keep GPA at 3.5. The fixed contribution is a=−6+1(3.5)=−2.5a=-6+1(3.5)=-2.5; the hours coefficient is b=0.05b=0.05. For t=0.5t=0.5:

h=0−(−2.5)0.05=50 hours.h=\frac{0-(-2.5)}{0.05}=50\text{ hours}.

Check: z=−2.5+0.05(50)=0z=-2.5+0.05(50)=0, so q=1/(1+e0)=0.5q=1/(1+e^0)=0.5. If b=0b=0, changing hh cannot change the model’s probability.

Probability thresholds

A probability becomes a label only after choosing a cutoff. Let q=P(Y=1∣X=x)q=P(Y=1\mid X=x), with logistic score zz and sigmoid relation q=1/(1+e−z)q=1/(1+e^{-z}). For threshold 0<t<10<t<1:

Y^={1q≥t,0q<t.\widehat Y= \begin{cases} 1 & q\geq t,\\ 0 & q<t. \end{cases}

Y^\widehat Y is the predicted label; tt is the minimum chance required to predict 1. Because the sigmoid increases with its score:

q≥t  ⟺  z≥ln⁡ ⁣(t1−t).q\geq t \iff z\geq\ln\!\left(\frac{t}{1-t}\right).

At t=0.5t=0.5, predict 1 exactly when z≥0z\geq0. At equality, this convention assigns the tie to 1.

Invented example: q=0.6q=0.6 predicts 1 at threshold 0.5 but predicts 0 at threshold 0.7. Changing the threshold changes the label, not the probability estimate.

Maximum likelihood

Fit coefficients that assign high probabilities to the observed labels. For independent training rows i=1,…,ni=1,\ldots,n, let yi∈{0,1}y_i\in\{0,1\} be the actual label and pi=P(Y=1∣X=xi;β)p_i=P(Y=1\mid X=x_i;\boldsymbol\beta) the proposed class-1 probability under coefficient vector β\boldsymbol\beta.

For each row, use pip_i if yi=1y_i=1, or 1−pi1-p_i if yi=0y_i=0. Multiply the factors:

L(β)=∏i=1npiyi(1−pi)1−yi.L(\boldsymbol\beta)= \prod_{i=1}^{n}p_i^{y_i}(1-p_i)^{1-y_i}.

nn counts rows; ∏\prod means multiply. The exponent 0 makes the unused factor equal 1. Maximum likelihood estimation chooses

β^=arg⁡max⁡β  L(β).\widehat{\boldsymbol\beta} =\underset{\boldsymbol\beta}{\arg\max}\;L(\boldsymbol\beta).

The hat marks fitted coefficients; “arg max” returns the coefficients giving the largest likelihood. Inputs and observed labels stay fixed while the candidate coefficients change.

Invented example: actual labels 1,0,11,0,1 and proposed class-1 probabilities 0.8,0.3,0.60.8,0.3,0.6 give

L=0.8(1−0.3)(0.6)=0.336.L=0.8(1-0.3)(0.6)=0.336.

The middle row actually has label 0, so its factor is 1−0.3=0.71-0.3=0.7.

Logistic coefficient tests

Compare the fitted effect with its uncertainty. To test coefficient jj:

H0:βj=0,H1:βj≠0,H_0:\beta_j=0, \qquad H_1:\beta_j\neq0, zj=β^j−0SE⁡(β^j).z_j=\frac{\widehat\beta_j-0} {\operatorname{SE}(\widehat\beta_j)}.

βj\beta_j is the unknown population coefficient, β^j\widehat\beta_j its fitted estimate, and SE⁡\operatorname{SE} its estimated sample-to-sample variation. The test statistic zjz_j is the estimate’s distance from zero in standard errors. This is distinct from the weighted score used to predict probabilities.

Under H0H_0 and suitable large-sample conditions, zjz_j is approximately standard normal. The module uses the approximate two-sided rule:

∣zj∣>2⟹reject H0.|z_j|>2\quad\Longrightarrow\quad\text{reject }H_0.

Otherwise fail to reject, including at exactly ∣zj∣=2|z_j|=2. Failure to reject does not prove zero; rejection does not prove causation.

Invented fit: β^j=0.6\widehat\beta_j=0.6, SE⁡=0.2\operatorname{SE}=0.2. Then zj=0.6/0.2=3z_j=0.6/0.2=3, so reject zero. In a multiple-input model, this tests the coefficient while the other modeled inputs are held fixed.

Multiclass logistic regression

One weighted score per class; divide each exponential by their total. For KK classes and dd input features:

zk=β0k+∑j=1dβjkxj,z_k=\beta_{0k}+\sum_{j=1}^{d}\beta_{jk}x_j, pk(x)=ezk∑ℓ=1Kezℓ.p_k(x)=\frac{e^{z_k}}{\sum_{\ell=1}^{K}e^{z_\ell}}.

kk selects a class, jj selects a feature, β0k\beta_{0k} is that class’s intercept, and βjk\beta_{jk} is its coefficient for feature xjx_j. The index ℓ\ell includes every class in the denominator; pk(x)p_k(x) is the class-kk probability. ee is the exponential base. This conversion is called softmax.

Probabilities sum to 1. Predict the class with the largest pk(x)p_k(x), equivalently the largest score zkz_k. Fit coefficients by maximizing the product of probabilities assigned to actual training labels.

Invented scores: 0,ln⁡2,ln⁡30,\ln2,\ln3 give weights 1,2,31,2,3 and total 6. Probabilities are 1/6,2/6,3/61/6,2/6,3/6; predict class 3.

Denominator check: with all KK classes in the sum, do not add another 1. In reference-class notation, one class has score 0 and weight e0=1e^0=1; its denominator can be written as 1 plus the exponentials for the other K−1K-1 classes.