Midterm practice · Formula sheet

Back to the practice module

Use the question’s probability event, units, and noise assumptions before substituting numbers. In N(μ,σ2)N(\mu,\sigma^2), the second parameter is variance. A hat marks an estimate or prediction.

Bayes rule

Use for Problems 1, 3, 4. To minimize expected mistakes with equal error costs, choose the most probable class at each input.

Let pk(x)=P(Y=k∣X=x)p_k(x)=P(Y=k\mid X=x) be the conditional probability of class kk at predictor value xx. For multiple classes, CB(x)=arg⁡max⁡kpk(x)C_B(x)=\arg\max_k p_k(x): the label with largest probability. For two classes,

CB(x)={1,p1(x)≥1/2,0,p1(x)<1/2.C_B(x)=\begin{cases} 1,&p_1(x)\ge1/2,\\ 0,&p_1(x)<1/2. \end{cases}

Either class is optimal at a tie; this module uses 1. If given p0(x)p_0(x) instead, predict 1 when p0(x)≤1/2p_0(x)\le1/2. Respect any stated domain. Check: p1=0.7p_1=0.7 gives prediction 1 and conditional error 1−0.7=0.31-0.7=0.3.

With false-positive cost cFP>0c_{FP}>0 and false-negative cost cFN>0c_{FN}>0, assuming correct decisions cost zero, predict 1 when

p1(x)≥cFPcFP+cFN.p_1(x)\ge\frac{c_{FP}}{c_{FP}+c_{FN}}.

Why: the expected cost of predicting 1 is cFP(1−p1)c_{FP}(1-p_1); predicting 0 costs cFNp1c_{FN}p_1. Choose the smaller. Invented check: costs 1 and 4 give a threshold 1/(1+4)=0.21/(1+4)=0.2.

Classification errors

Use for Problems 3, 4. Positive means actual or predicted class 1. TPTP counts correct positives; FNFN counts positives predicted 0; FPFP counts negatives predicted 1; TNTN counts correct negatives.

Actual classPredict 1Predict 0
1TPFN
0FPTN
TPR=TPTP+FN.\mathrm{TPR}=\frac{TP}{TP+FN}. FNR=FNTP+FN.\mathrm{FNR}=\frac{FN}{TP+FN}. FPR=FPFP+TN.\mathrm{FPR}=\frac{FP}{FP+TN}. TPR+FNR=1.\mathrm{TPR}+\mathrm{FNR}=1.

Rates require a nonzero actual-class denominator. Sheet check: TP=4,FN=3,FP=2,TN=4TP=4,FN=3,FP=2,TN=4 gives FNR=3/7\mathrm{FNR}=3/7 and FPR=2/6=1/3\mathrm{FPR}=2/6=1/3.

For a sigmoid probability, σ(z)=1/(1+e−z)\sigma(z)=1/(1+e^{-z}) is increasing and σ(0)=1/2\sigma(0)=1/2. Thus the decision boundary is z=0z=0. The favored class is the event inside the supplied probability. In Problem 3, P(Y=0∣x)=σ(2+x1−x2)P(Y=0\mid x)=\sigma(2+x_1-x_2), so z>0z>0 predicts 0, and the boundary is x2=x1+2x_2=x_1+2.

Flipping all predictions swaps the two columns of the table; it does not change actual class totals.

Expectation and dependence

Use for Problem 4. If Y=h(X)+εY=h(X)+\varepsilon with ε\varepsilon independent of XX, E[ε]=μE[\varepsilon]=\mu, and Var⁡(ε)=σ2\operatorname{Var}(\varepsilon)=\sigma^2, then

f(x)=E[Y∣X=x]f(x)=E[Y\mid X=x] =h(x)+μ,=h(x)+\mu, E[(Y−f(x))2∣X=x]=σ2.E[(Y-f(x))^2\mid X=x]=\sigma^2.

Here hh is the supplied deterministic rule, ff is the best squared-error prediction, and the last quantity is irreducible MSE. Sheet check: h(x)=x+x2h(x)=x+x^2 and N(1,2)N(1,2) noise give f(x)=x+x2+1f(x)=x+x^2+1 and irreducible MSE 2. A marginal noise distribution alone does not imply independence.

For random variables U,VU,V with finite, nonzero variances, let sU,sVs_U,s_V be their standard deviations. Then

Cov⁡(U,V)=E[UV]−E[U]E[V],\begin{aligned} &\operatorname{Cov}(U,V)\\ &\quad=E[UV]-E[U]E[V], \end{aligned} Corr⁡(U,V)=Cov⁡(U,V)sUsV.\operatorname{Corr}(U,V)=\frac{\operatorname{Cov}(U,V)}{s_Us_V}.

Independence implies zero covariance. Zero covariance does not imply independence. Counterexample: XX uniform on {−1,0,1}\{-1,0,1\} and Z=X2Z=X^2 are dependent, but E[X]=E[XZ]=0E[X]=E[XZ]=0, so their covariance and correlation are zero. Correlation alone also does not establish causation.

Model flexibility

Use for Problems 4, 6. To measure new-data error, use predictions on observations that were not used for fitting. For mm evaluation pairs (xi,yi)(x_i,y_i) and fitted rule f^\hat f,

MSE=1m∑i=1m(yi−f^(xi))2.MSE=\frac1m\sum_{i=1}^{m}(y_i-\hat f(x_i))^2.

Select flexibility using validation MSE or cross-validation; reserve a final test set for evaluation after selection. Invented check: validation MSEs 4, 2, 3 for increasing flexibility favor the middle model.

At fixed input xx, for a new independent response YY and fitted model f^D\hat f_D trained on random dataset DD,

E[(Y−f^D(x))2∣X=x]E[(Y-\hat f_D(x))^2\mid X=x] =Bias⁡(x)2+Var⁡D(f^D(x))+σ2(x).\begin{aligned} &=\operatorname{Bias}(x)^2\\ &\quad+\operatorname{Var}_D(\hat f_D(x))\\ &\quad+\sigma^2(x). \end{aligned}

Here f(x)=E[Y∣X=x]f(x)=E[Y\mid X=x], Bias⁡(x)=ED[f^D(x)]−f(x)\operatorname{Bias}(x)=E_D[\hat f_D(x)]-f(x), and σ2(x)=Var⁡(Y∣X=x)\sigma^2(x)=\operatorname{Var}(Y\mid X=x) is irreducible noise. The expectation averages over training datasets and fresh response noise. Overfitting by a very flexible model is associated with high variance, not high bias.

When an enlarged model contains the old model and both minimize ordinary training RSS on the same data,

RSStrain,new≤RSStrain,old.RSS_{\text{train,new}}\le RSS_{\text{train,old}}.

It can match the old model by setting new coefficients to zero. Equality is possible. There is no corresponding general guarantee for test RSS or for an arbitrary subgroup’s RSS; Problem 6’s unchanged male fit follows from that model’s separate group parameters.

Coin flips

Use for Problem 5. Assume nn independent flips, each with heads probability pp. With hh heads and n−hn-h tails,

P(one specified order)=ph(1−p)n−h,\begin{aligned} &P(\text{one specified order})\\ &\quad=p^h(1-p)^{n-h}, \end{aligned} P(exactly h heads)=(nh)ph(1−p)n−h.\begin{aligned} &P(\text{exactly }h\text{ heads})\\ &\quad=\binom nh p^h(1-p)^{n-h}. \end{aligned}

The binomial coefficient (nh)=n!/[h!(n−h)!]\binom nh=n!/[h!(n-h)!] counts choices of head positions. Factorial n!=n(n−1)⋯1n!=n(n-1)\cdots1, with 0!=10!=1.

Sheet check: P(H,H,T)=p2(1−p)P(H,H,T)=p^2(1-p); 20 heads in 30 flips has probability 30,045,015 p20(1−p)1030{,}045{,}015\,p^{20}(1-p)^{10}. Use the counting factor only when different orders qualify.

ROC and AUC

Use for Problem 8. Let YY be the true label and Y^\hat Y the prediction. An ROC curve plots

P(Y^=1∣Y=0)⏟horizontal: FPR\underbrace{P(\hat Y=1\mid Y=0)}_{\text{horizontal: FPR}} P(Y^=1∣Y=1)⏟vertical: TPR\underbrace{P(\hat Y=1\mid Y=1)}_{\text{vertical: TPR}}

as a threshold or classifier parameter varies. AUC is the area under this curve in the unit square.

If an independent coin predicts 1 with probability pp, both conditional probabilities are pp:

(FPR,TPR)=(p,p).(\mathrm{FPR},\mathrm{TPR})=(p,p).

Varying pp from 0 to 1 traces the diagonal, whose triangular area is AUC=1/2\mathrm{AUC}=1/2. Check: p=0.7p=0.7 gives (0.7,0.7)(0.7,0.7) and FNR 1−0.7=0.31-0.7=0.3. A fixed pp is one point; the family gives the curve. With no label-related information, population AUC cannot systematically rise above or fall below 1/21/2; sample estimates can fluctuate by chance.

Least squares · Fit an intercept and slope

Use this for a straight-line prediction y^=β^0+β^1x\hat y=\hat\beta_0+\hat\beta_1x. The intercept β^0\hat\beta_0 is the fitted response at x=0x=0; the slope β^1\hat\beta_1 is the change in the fitted response for a one-unit increase in xx.

For nn supplied points (xi,yi)(x_i,y_i), calculate the means:

xˉ=1n∑i=1nxi,yˉ=1n∑i=1nyi.\bar x=\frac1n\sum_{i=1}^n x_i,\qquad \bar y=\frac1n\sum_{i=1}^n y_i.

Measure horizontal spread and joint variation:

Sxx=∑i=1n(xi−xˉ)2S_{xx}=\sum_{i=1}^n(x_i-\bar x)^2

Sxy=∑i=1n(xi−xˉ)(yi−yˉ).S_{xy}=\sum_{i=1}^n(x_i-\bar x)(y_i-\bar y).

When Sxx>0S_{xx}>0, the coefficients minimizing squared residuals are:

β^1=SxySxx\hat\beta_1=\frac{S_{xy}}{S_{xx}}

β^0=yˉ−β^1xˉ.\hat\beta_0=\bar y-\hat\beta_1\bar x.

Use it. Problem 2 gives Sxy=16S_{xy}=16, Sxx=10S_{xx}=10, xˉ=1\bar x=1, and yˉ=0\bar y=0. Thus β^1=16/10=1.6\hat\beta_1=16/10=1.6 and β^0=0−(1.6)(1)=−1.6\hat\beta_0=0-(1.6)(1)=-1.6.

The fitted line estimates the mean response. If the model’s noise has known nonzero mean mm, the fitted intercept estimates β0+m\beta_0+m; see shifted noise.

Fit quality · RSS, RSE, and R²

For observation ii, the residual is ei=yi−y^ie_i=y_i-\hat y_i, where yiy_i is observed and y^i\hat y_i is fitted. With nn observations:

RSS=∑i=1nei2.\mathrm{RSS}=\sum_{i=1}^n e_i^2.

RSS is total squared prediction error on the observations being evaluated. For a simple linear model with a fitted intercept and slope, the residual standard error is:

RSE=RSSn−2.\mathrm{RSE}=\sqrt{\frac{\mathrm{RSS}}{n-2}}.

The denominator uses n−2n-2 residual degrees of freedom because two coefficients were estimated. RSE has the same units as the response.

Let yˉ\bar y be the observed response mean. The total sum of squares is the error from always predicting that mean:

TSS=∑i=1n(yi−yˉ)2.\mathrm{TSS}=\sum_{i=1}^n(y_i-\bar y)^2.

For TSS>0\mathrm{TSS}>0, the fraction of baseline squared error removed by the fit is:

R2=1−RSSTSS.R^2=1-\frac{\mathrm{RSS}}{\mathrm{TSS}}.

Use it. Problem 2 has RSS=0.4\mathrm{RSS}=0.4, n=5n=5, and TSS=26\mathrm{TSS}=26. Thus RSE=0.4/3≈0.3651\mathrm{RSE}=\sqrt{0.4/3}\approx0.3651 and R2=1−0.4/26≈0.9846R^2=1-0.4/26\approx0.9846.

Coefficient uncertainty · Known Gaussian noise variance

Assume Yi=β0+β1xi+εiY_i=\beta_0+\beta_1x_i+\varepsilon_i, with fixed observed xix_i and independent errors εi∼N(0,σ2)\varepsilon_i\sim N(0,\sigma^2). Here σ>0\sigma>0 is the supplied noise standard deviation, nn is the observation count, xˉ\bar x is the predictor mean, and Sxx=∑i(xi−xˉ)2>0S_{xx}=\sum_i(x_i-\bar x)^2>0.

The standard errors describe how much the fitted coefficients vary over repeated samples:

SE(β^1)=σSxx\mathrm{SE}(\hat\beta_1)=\frac{\sigma}{\sqrt{S_{xx}}}

SE(β^0)=σ1n+xˉ2Sxx.\mathrm{SE}(\hat\beta_0)=\sigma\sqrt{\frac1n+\frac{\bar x^2}{S_{xx}}}.

For either coefficient βj\beta_j (j=0j=0 for intercept, j=1j=1 for slope), a 95% confidence interval is:

β^j±1.96 SE(β^j).\hat\beta_j\pm1.96\,\mathrm{SE}(\hat\beta_j).

The normal multiplier 1.96 is rounded. Over repeated samples, this interval-building procedure covers the true fixed coefficient about 95% of the time.

Use it. Problem 2 supplies variance 8s28s^2, so σ=8 s\sigma=\sqrt8\,s. With Sxx=10S_{xx}=10, the slope standard error is 4/5 s\sqrt{4/5}\,s. Its estimate is 1.6, so its interval is 1.6±1.964/5 s1.6\pm1.96\sqrt{4/5}\,s.

If noise variance is instead unknown and estimated from residuals, an exact small-sample interval uses the appropriate tt multiplier and estimated standard errors. Problems 2 and 7 request intervals as functions of the supplied noise parameters, so use their given variances here.

Hypothesis tests · Compare a proposed coefficient with its interval

For a two-sided 5% test of H0:βj=bH_0:\beta_j=b, bb is the proposed coefficient, β^j\hat\beta_j is its estimate, and SE(β^j)\mathrm{SE}(\hat\beta_j) is its standard error. Under independent Gaussian errors with known variance, use:

z=β^j−bSE(β^j).z=\frac{\hat\beta_j-b}{\mathrm{SE}(\hat\beta_j)}.

Reject when ∣z∣>1.96|z|>1.96. Equivalently, reject when the proposed value lies outside the 95% interval:

∣β^j−b∣>1.96 SE(β^j).|\hat\beta_j-b|>1.96\,\mathrm{SE}(\hat\beta_j).

Fail to reject when the inequality is reversed or equal. This means there is insufficient evidence against the proposed value at this level; it does not prove the null hypothesis.

Use it. In Problem 7, the slope estimate is 2, the proposed slope is 0, and its standard error is w/10w/\sqrt{10}. Reject when 2>1.96w/102>1.96w/\sqrt{10}, or 0<w<210/1.96≈3.22680<w<2\sqrt{10}/1.96\approx3.2268.

Interactions · Substitute the group indicator

Let xx be a numeric predictor and g∈{0,1}g\in\{0,1\} a group indicator. In the fitted model:

y^=b0+b1x+b2g+b3xg,\hat y=b_0+b_1x+b_2g+b_3xg,

the bjb_j are fitted coefficients. Substitute the group value to read its line:

g=0:y^0=b0+b1xg=0:\quad \hat y_0=b_0+b_1x

g=1:y^1=(b0+b2)+(b1+b3)x.g=1:\quad \hat y_1=(b_0+b_2)+(b_1+b_3)x.

The prediction difference is y^1−y^0=b2+b3x\hat y_1-\hat y_0=b_2+b_3x. If b3≠0b_3\ne0, the lines meet at x=−b2/b3x=-b_2/b_3.

Use it. Problem 6 gives b0=35b_0=35, b1=5b_1=5, b2=−15b_2=-15, and b3=5b_3=5. The lines are 35+5x35+5x and 20+10x20+10x; their crossing is x=−(−15)/5=3x=-(-15)/5=3.

Adding a term such as b4x2gb_4x^2g leaves the g=0g=0 group linear and permits curvature for g=1g=1. When both fits minimize ordinary unpenalized RSS on the same training data, the extended model includes the old model at b4=0b_4=0, so:

RSSextended≤RSSoriginal.\mathrm{RSS}_{\text{extended}}\le\mathrm{RSS}_{\text{original}}.

These fully interacted models allow independent group fits. With unconstrained coefficients, adding curvature only for g=1g=1 leaves the minimum g=0g=0 training RSS unchanged. If the g=0g=0 line is identifiable (at least two distinct training xx values), its predictions and its RSS on the same test points also stay unchanged. Total test RSS has no guaranteed ordering.

Shifted noise · A nonzero mean changes the intercept

Suppose the model is Y=β0+β1X+εY=\beta_0+\beta_1X+\varepsilon, with noise independent of XX and ε∼N(m,σ2)\varepsilon\sim N(m,\sigma^2). Here mm is its known mean and σ2\sigma^2 its variance. Then:

E[Y∣X=x]=(β0+m)+β1x.E[Y\mid X=x]=(\beta_0+m)+\beta_1x.

An ordinary fitted intercept α^0\hat\alpha_0 estimates the mean-response intercept α0=β0+m\alpha_0=\beta_0+m. Recover the original model’s intercept by subtracting the known noise mean:

β^0=α^0−m.\hat\beta_0=\hat\alpha_0-m.

If the interval for α0\alpha_0 is [L,U][L,U], the interval for β0\beta_0 is [L−m,U−m][L-m,U-m]. Its width is unchanged because mm is a known constant. The slope estimate and its uncertainty are unchanged for the same observations.

Use it. Problem 7’s printed data give α^0=0\hat\alpha_0=0. When the known noise mean is m=1m=1, the original intercept estimate is 0−1=−10-1=-1. Its 95% interval is −1±1.96w/5-1\pm1.96w/\sqrt5, where ww is the supplied noise standard deviation.

If mm is unknown, these observations identify the sum β0+m\beta_0+m, not its two components separately.