CalcPath
ProgressProblem setsFeedback
← Question sheet

Statistics · Solutions

Logistic Regression

12 questions · about 100 minutes

Work this sheet on paper, showing every step — the method is what is being tested, and the solutions are written so you can check your route rather than just your answer. Name every result you use (“because σ′=σ(1−σ)”, “by the score equations”, “by Wilks' theorem”) and check its hypotheses before invoking a likelihood-ratio or Wald test. No calculator is needed: every answer has an exact form such as ln3 or e1/5, and any critical value you need is printed with the question. Full worked solutions are on a separate page; each question also has its own reveal, so you can check one answer without seeing the rest.

  1. 1.Computation

    A linear probability model is fitted by least squares to whether a household owns a car (Y=1) against household income x in thousands, giving

    p^​=−51​+201​x.

    1. (a)Find the range of incomes over which the fitted value is a valid probability, and evaluate the fit at x=2 and at x=30.
    2. (b)For a household with x=8, write down the two values the error ε=Y−p can take and the probability of each, and compute the error variance. Repeat for x=14.
    3. (c)Say which of the three failures of the linear probability model each of (a) and (b) illustrates. Which of the three cannot be repaired by adjusting standard errors, and why not?

    Solution

    (a) Solve the two inequalities. p^​≥0 requires 20x​≥51​, that is x≥4; and p^​≤1 requires 20x​≤56​, that is x≤24. The fit is a probability only for incomes between $4,000 and $24,000.

    At x=2: p^​=−51​+101​=−101​, a negative probability. At x=30: p^​=−51​+23​=1013​, a probability of 130%. Both are statements that cannot be true of any household.

    (b) The error is two-valued. At x=8 the model probability is p=−51​+208​=51​. Since Y is Bernoulli(p), the error ε=Y−p equals 1−p=54​ with probability p=51​, and equals −p=−51​ with probability 1−p=54​. Its variance is

    p(1−p)=51​⋅54​=254​.

    Check directly: E(ε)=54​⋅51​−51​⋅54​=0 and E(ε2)=2516​⋅51​+251​⋅54​=12520​=254​. ✓

    At x=14: p=−51​+2014​=21​. The error is ±21​, each with probability 21​, and the variance is 21​⋅21​=41​.

    (c) Rank the failures. Part (a) is the range failure: the fitted line leaves [0,1]. Part (b) shows the other two at once. The variance is 254​ at one income and 41​ at another, so the errors are heteroscedastic; and at each income the error takes only two values, so it is not normal.

    Heteroscedasticity and non-normality are the familiar complaints: by Gauss–Markov the least-squares estimates stay unbiased, only the standard errors are wrong, and weighted least squares or robust standard errors repair them. The range failure is different in kind. A fitted probability of 1013​ is not an imprecise estimate of something — the quantity being estimated cannot exceed 1 — so no confidence interval around it helps. That failure forces a new functional form, which is why the log-odds is modelled instead of the probability.

  2. 2.Proof

    Let σ(z)=1+e−z1​. Prove each statement directly from this formula.

    1. (a)σ(−z)=1−σ(z) for every real z, and consequently σ′(z)=σ(z)σ(−z).
    2. (b)σ′(z)≤41​ for every z, with equality only at z=0.
    3. (c)σ′′(z)=σ′(z)(1−2σ(z)). Deduce where σ is convex, where it is concave, and where its inflection point lies, and say what this means for the effect of a predictor on a probability.

    Solution

    (a) Symmetry, then the derivative. By the formula, σ(−z)=1+ez1​. Multiply numerator and denominator by e−z:

    σ(−z)=e−z+1e−z​.

    On the other side, 1−σ(z)=1+e−z(1+e−z)−1​=1+e−ze−z​, the same expression. So σ(−z)=1−σ(z).

    For the derivative, write σ(z)=(1+e−z)−1 and apply the chain rule:

    σ′(z)=−(1+e−z)−2⋅(−e−z)=1+e−z1​⋅1+e−ze−z​=σ(z)σ(−z),

    using the expression for σ(−z) just found. Equivalently σ′=σ(1−σ).

    (b) Complete the square. Put p=σ(z). Since e−z>0, the denominator exceeds 1, so 0<p<1. Then

    σ′(z)=p(1−p)=41​−(p−21​)2≤41​,

    with equality exactly when p=21​. Now σ(z)=21​ means 1+e−z=2, so e−z=1 and z=0. Both factors of σ′ are positive, so σ is strictly increasing and takes the value 21​ only once, at z=0. Hence the bound 41​ is attained only there.

    (c) Differentiate σ′=σ−σ2. By the product/chain rule,

    σ′′=σ′−2σσ′=σ′(1−2σ).

    Since σ′>0 everywhere, the sign of σ′′ is the sign of 1−2σ(z). For z<0 we have σ(z)<21​ (strictly increasing, σ(0)=21​), so σ′′>0 and σ is convex there; for z>0, σ′′<0 and σ is concave. The sign changes at z=0, so the inflection point is (0,21​) — the same point at which, by (b), the slope is largest.

    For a logistic model this says: a unit change in a predictor moves the probability most when the current probability is near 21​, and the effect shrinks towards zero as the probability approaches 0 or 1. The steepest possible probability change per unit of log-odds is 41​, and it is only available at the centre. ■

  3. 3.Computation

    A logistic model for whether a seedling survives its first winter, in terms of its stem diameter x in millimetres, is fitted as

    logit(p^​)=−2ln3+(ln3)x.

    Give every answer exactly.

    1. (a)Compute the fitted odds and the fitted probability of survival at x=0,1,2,3,4.
    2. (b)State the odds ratio for a 1 mm increase in diameter and for a 2 mm increase, and explain why neither depends on the starting diameter.
    3. (c)Compute the change in fitted probability from x=1 to 2, from 2 to 3, and from 3 to 4, and reconcile the pattern you find with (b).

    Solution

    (a) Work on the odds scale first. The linear predictor is η=(x−2)ln3, so the odds are

    1−p^​p^​​=eη=e(x−2)ln3=3x−2.

    At x=0,1,2,3,4 this gives odds 91​, 31​, 1, 3, 9. Converting with p^​=1+oddsodds​:

    p^​=101​,41​,21​,43​,109​.

    (For instance at x=1: 1+1/31/3​=4/31/3​=41​.)

    (b) Coefficients are log odds ratios. A 1 mm increase adds β1​=ln3 to the log-odds, so it multiplies the odds by eln3=3; a 2 mm increase adds 2ln3 and multiplies them by e2ln3=9. The reason it is independent of the starting point is that the odds are eη and eη+β1​=eηeβ1​: the ratio of new odds to old is eβ1​ whatever η was. The table in (a) shows it — 91​→31​→1→3→9 is a factor of 3 at every step.

    (c) The probability steps are not constant.

    21​−41​=41​,43​−21​=41​,109​−43​=203​.

    The first two steps are equal because they are mirror images about p=21​: σ(−z)=1−σ(z) places 41​ and 43​ symmetrically. The third step is smaller because the curve is flattening as the probability nears 1 — the same ln3 on the log-odds scale buys less and less probability. There is no contradiction with (b): the odds ratio is exactly 3 at each step, and it is the probability that has no fixed exchange rate against the coefficient.

  4. 4.Computation

    Write p1​ for the probability of an outcome in a treated group and p2​ for the probability in a control group.

    1. (a)In a trial, 41​ of control patients recover and 21​ of treated patients recover. Compute the relative risk and the odds ratio for treatment. Which of the two is the logistic regression coefficient for treatment built from, and what is that coefficient exactly?
    2. (b)Repeat for a rare outcome: a side effect occurs in 1001​ of controls and 1002​ of treated patients.
    3. (c)Prove that in general OR=RR⋅1−p1​1−p2​​, and deduce that whenever treatment raises the probability, the odds ratio exceeds the relative risk, with the gap small exactly when both probabilities are small. Confirm the formula against (a) and (b).

    Solution

    (a) The relative risk is RR=p2​p1​​=1/41/2​=2. The odds are 1/21/2​=1 for treated and 3/41/4​=31​ for control, so OR=1/31​=3. Treatment doubles the risk but triples the odds.

    A logistic coefficient is a log odds ratio, never a log relative risk: with treatment coded x=1, β1​=lnOR=ln3.

    (b) RR=1/1002/100​=2 again. The odds are 98/1002/100​=982​=491​ and 99/1001/100​=991​, so

    OR=1/991/49​=4999​≈2.02.

    For a rare outcome the two measures nearly agree; for the common outcome in (a) they did not.

    (c) Rearrange the definition.

    OR=p2​/(1−p2​)p1​/(1−p1​)​=p2​p1​​⋅1−p1​1−p2​​=RR⋅1−p1​1−p2​​.

    If p1​>p2​ then 1−p2​>1−p1​>0, so the correction factor exceeds 1 and OR>RR: an odds ratio reported in the language of risk always overstates a beneficial-looking effect. The factor 1−p1​1−p2​​ tends to 1 as p1​,p2​→0, which is the rare-outcome case in which the two agree.

    Checks: in (a) the factor is 1/23/4​=23​ and 2⋅23​=3 ✓; in (b) it is 98/10099/100​=9899​ and 2⋅9899​=4999​ ✓.

  5. 5.Critique

    A logistic regression of loan default on years of employment x reports β^​1​=−21​ with standard error 51​. The analyst's report contains the three claims below. Each is wrong. For each, name the error precisely, then give the correct statement with exact numbers. Use 2 as the two-sided 5% critical value wherever one is needed.

    1. (a)“Each extra year of employment lowers the probability of default by 0.5, so five years of employment reduce it by 2.5 — or to zero, whichever comes first.”
    2. (b)“The 95% interval for β1​ is −21​±2⋅51​=(−109​,−101​). Exponentiating the point estimate and the half-width gives the odds-ratio interval e−1/2±e2/5.”
    3. (c)“The odds ratio per year is e−1/2≈0.61, so a borrower with one more year of employment is about 39% less likely to default.”

    Solution

    (a) A coefficient is a change in log-odds, not in probability. The correct multiplicative statement is: each extra year multiplies the odds of default by e−1/2, and five years multiply them by e−5/2. The probability change has no fixed value — it depends on where you start — and it can be bounded. By the mean value theorem, for some ξ between η and η−21​,

    σ(η)−σ(η−21​)=21​σ′(ξ)≤21​⋅41​=81​,

    since σ′=σ(1−σ)≤41​. So no single year can move the probability by more than 81​, let alone 21​. For instance from p=21​ (where η=0) one more year gives p=σ(−21​)=1+e1/21​, which exceeds 31​ because e1/2<2: a drop of less than 61​. And the probability can never reach zero, since σ takes values in (0,1); it only approaches it.

    (b) The interval is built correctly and transformed incorrectly. On the log-odds scale β^​1​ is approximately normal, so (−109​,−101​) is right. To move to the odds-ratio scale, apply exp to the two endpoints: because exp is strictly increasing, β1​ lies in the interval exactly when eβ1​ lies in (e−9/10,e−1/10), so the coverage is unchanged.

    The proposed interval e−1/2±e2/5 is not even a set of odds ratios: its lower end is e−1/2−e2/5<0, since e2/5>1>e−1/2, and an odds ratio is positive. The correct interval is not symmetric about e−1/2; rather e−9/10⋅e−1/10=e−1=(e−1/2)2, so e−1/2 is its geometric centre. Consistency check: the Wald statistic is z=1/5−1/2​=−25​, with ∣z∣>2; the log-odds interval excludes 0 and the odds-ratio interval excludes 1, as they must.

    (c) An odds ratio is not a relative risk. “Less likely” is a statement about probabilities, and by the identity OR=RR⋅1−p1​1−p2​​ the two agree only when default is rare. The correct sentence is “one more year multiplies the odds of default by e−1/2”. To see the size of the discrepancy, take a borrower whose current probability is 21​: after one more year the odds are e−1/2, so the probability is 1+e1/21​ and the relative risk is

    RR=1+e1/22​>e−1/2,

    because 2e1/2>1+e1/2 reduces to e1/2>1. The risk falls by less than 39%; the odds fall by 39%. Only for a borrower whose default probability is already tiny would the two readings coincide.

  6. 6.Computation

    An intercept-only logistic model, logit(pi​)=β for every i, is fitted to n=12 binary observations of which s=9 are successes. You may use e4/3≈3.79.

    1. (a)Write the log-likelihood ℓ(β) in terms of s, n and β, show that the score equation reduces to σ(β)=s/n, and solve it exactly.
    2. (b)Starting from β(0)=0, take one Newton step. Does it overshoot or undershoot the exact maximiser? Justify without a calculator.
    3. (c)Evaluate the Hessian at the maximiser and deduce the standard error of β^​. Write the Wald statistic for H0​:β=0 exactly and decide the test with 2 as the critical value.
    4. (d)Compute the deviance of the fitted model exactly.

    Solution

    (a) Sum the Bernoulli contributions. With every linear predictor equal to β,

    ℓ(β)=i=1∑n​[yi​β−ln(1+eβ)]=sβ−nln(1+eβ).

    Differentiating, ℓ′(β)=s−n1+eβeβ​=s−nσ(β), which is the score equation ∑i​(yi​−pi​)=0 with all pi​ equal. Setting it to zero gives σ(β^​)=ns​=43​, so β^​=logit(43​)=ln1/43/4​=ln3. Because ℓ is strictly concave (its second derivative −nσ(1−σ) is negative), this stationary point is the unique global maximum.

    (b) One Newton step. At β(0)=0 every pi​=σ(0)=21​. The gradient is s−n⋅21​=9−6=3 and the Hessian is −nσ(1−σ)=−12⋅41​=−3. Newton's update is

    β(1)=β(0)−ℓ′′(0)ℓ′(0)​=0−−33​=1.

    The exact maximiser is ln3, and ln3>1 because 3>e. So the step undershoots, landing at 1 against the target ln3≈1.099 — most of the way in a single step, as expected near the optimum of a concave function; a second step would close almost all of the remaining gap.

    (c) Curvature at the maximiser. With σ(β^​)=43​,

    ℓ′′(β^​)=−n⋅43​⋅41​=−49​.

    The observed information is 49​, and the standard error is its inverse square root: SE(β^​)=94​​=32​. (In general SE=1/np^​(1−p^​)​, the familiar binomial standard error transported to the log-odds scale.)

    The Wald statistic is z=SEβ^​​=2/3ln3​=23​ln3. It exceeds 2 exactly when ln3>34​, that is when 3>e4/3≈3.79, which is false. So z≈1.65<2 and we do not reject β=0 at the 5% level: nine successes in twelve trials is consistent with a fair coin.

    (d) Deviance. At β^​=ln3 we have 1+eβ^​=4, so

    ℓ(β^​)=9ln3−12ln4=9ln3−24ln2,D=−2ℓ(β^​)=48ln2−18ln3.

    Check by the other route: for an intercept-only fit ℓ(β^​)=slnns​+(n−s)lnnn−s​=9ln43​+3ln41​=9ln3−18ln2−6ln2=9ln3−24ln2 ✓. Numerically D≈33.27−19.78=13.5.

  7. 7.Proof

    A logistic model contains an intercept and a single binary predictor xi​∈{0,1}: logit(pi​)=β0​+β1​xi​. Write n1​ for the number of observations with xi​=1 and s1​ for the number of successes among them; define n0​,s0​ likewise for xi​=0. Assume 0<s0​<n0​ and 0<s1​<n1​.

    1. (a)Write out the two score equations X⊤(y−p^​)=0 for this design, and deduce that at the maximum likelihood fit ∑i:xi​=1​p^​i​=s1​ and ∑i:xi​=0​p^​i​=s0​.
    2. (b)Hence prove that the fitted probability in each group equals that group's observed success proportion, and that β^​1​ is exactly the logarithm of the sample odds ratio s0​/(n0​−s0​)s1​/(n1​−s1​)​.
    3. (c)In a study of 100 patients, 40 received a new drug and 30 of them recovered; of the 60 controls, 15 recovered. Write down β^​0​, β^​1​ and the two fitted probabilities exactly. Then explain where the argument in (b) breaks if s1​=n1​.

    Solution

    (a) Two columns, two equations. The design matrix has a column of ones and the indicator column (xi​). The score equations X⊤(y−p^​)=0 are therefore

    i=1∑n​(yi​−p^​i​)=0andi=1∑n​xi​(yi​−p^​i​)=i:xi​=1∑​(yi​−p^​i​)=0.

    The second says ∑xi​=1​p^​i​=∑xi​=1​yi​=s1​. Subtracting it from the first leaves ∑xi​=0​(yi​−p^​i​)=0, so ∑xi​=0​p^​i​=s0​. In words: with an intercept in the model, the fitted probabilities sum to the observed successes overall, and with an indicator in the model they do so within the indicated group as well.

    (b) Same predictor, same fitted probability. Every observation with xi​=1 has the same linear predictor β^​0​+β^​1​, hence the same fitted probability, call it p^​(1)​; every observation with xi​=0 has p^​(0)​=σ(β^​0​). The sums in (a) become n1​p^​(1)​=s1​ and n0​p^​(0)​=s0​, so

    p^​(1)​=n1​s1​​,p^​(0)​=n0​s0​​.

    Applying the logit, β^​0​=logit(n0​s0​​)=lnn0​−s0​s0​​ and β^​0​+β^​1​=lnn1​−s1​s1​​. Subtracting,

    β^​1​=lnn1​−s1​s1​​−lnn0​−s0​s0​​=lns0​/(n0​−s0​)s1​/(n1​−s1​)​,

    the log of the sample odds ratio. This stationary point is the maximum: both groups are present, so X has full rank, and the assumption 0<sj​<nj​ places every fitted probability strictly inside (0,1), so the log-likelihood is strictly concave and its unique stationary point is the global maximiser. ■

    (c) Numbers. p^​(1)​=4030​=43​ with odds 3; p^​(0)​=6015​=41​ with odds 31​. Hence

    β^​0​=ln31​=−ln3,β^​1​=ln3−(−ln3)=2ln3=ln9,

    an odds ratio of 9 for the drug. Check: β^​0​+β^​1​=ln3=logit(43​) ✓.

    If s1​=n1​ — every treated patient recovers — then (a) demands p^​(1)​=1, which no finite β^​0​+β^​1​ can deliver because σ never reaches 1. The score equations then have no solution: this is separation on the indicator, the likelihood increases without bound as β1​→∞, and the maximum likelihood estimate does not exist. The sample odds ratio ⋯n1​/0​ is infinite for the same reason.

  8. 8.Computation

    Three nested models of the same binary response are fitted. Model A has only an intercept; Model B adds one predictor; Model C adds three further predictors to B. Their deviances are DA​=200, DB​=190 and DC​=184. Critical values at the 5% level: χ12​:3.841, χ32​:7.815, χ42​:9.488.

    1. (a)Test B against A, C against B, and C against A. For each, state the statistic, its degrees of freedom, the theorem that justifies the reference distribution, and the conclusion.
    2. (b)Explain why DC​≤DB​≤DA​ had to hold whatever the data, and why the three statistics in (a) satisfy the identity you notice.
    3. (c)In Model B the predictor's coefficient is β^​1​=53​ with standard error 51​. Compute the Wald statistic and compare its square with the likelihood-ratio statistic for the same hypothesis. Should the two agree exactly?
    4. (d)Using 2 as the critical value, give a 95% confidence interval for the odds ratio per unit of the predictor, and show that it extends further above e3/5 than below it, by an exact factor.

    Solution

    (a) Likelihood-ratio tests. For nested models differing by k parameters, Dred​−Dfull​→χk2​ under the null that the extra coefficients are zero — Wilks' theorem, quoted for twice the log-likelihood ratio.

    B against A: 200−190=10 on 1 degree of freedom; 10>3.841, reject — the first predictor carries information.

    C against B: 190−184=6 on 3 degrees of freedom; 6<7.815, do not reject — the three extra predictors, taken together, add nothing detectable beyond B.

    C against A: 200−184=16 on 4 degrees of freedom; 16>9.488, reject — C is better than the empty model, but (from the previous test) that is because it contains B's predictor.

    (b) Nesting. Each model's parameter space contains the previous one's — set the new coefficients to zero and you recover the smaller model — so the maximised log-likelihood can only rise when parameters are added, and D=−2ℓ(β^​) can only fall. The identity 10+6=16 holds because all three statistics are differences of the same three deviances: (DA​−DB​)+(DB​−DC​)=DA​−DC​. The degrees of freedom add in the same way, 1+3=4.

    (c) Wald versus likelihood ratio. z=1/53/5​=3, so z2=9, against the likelihood-ratio statistic 10 for the same hypothesis β1​=0. Both are asymptotically χ12​ under the null, but they are different functions of the data — one uses the curvature at the maximum, the other the height difference between two maxima — and they agree only in the limit. A gap of 9 versus 10 is unremarkable; both reject comfortably.

    (d) Build on the log-odds scale, then exponentiate the endpoints. The interval for β1​ is 53​±2⋅51​=(51​,1), and because exp is increasing the odds-ratio interval is (e1/5,e) with the same coverage.

    The gaps on either side of the point estimate e3/5 are

    e3/5−e1/5=e1/5(e2/5−1),e−e3/5=e3/5(e2/5−1),

    so the upper gap is exactly e2/5 times the lower one. The interval is symmetric on the log scale (the point estimate is its geometric centre, since e1/5⋅e=e6/5=(e3/5)2) and skewed to the right on the odds-ratio scale, because exp is convex. That skew is a feature, not an error: the sampling distribution of eβ^​1​ is itself right-skewed.

  9. 9.Sketch

    A continuous predictor is cut into five bins of 90 observations each, with midpoints x=1,2,3,4,5; the success counts in the bins are 10,18,30,45,60. A second predictor, cut into four bins with midpoints x=1,2,3,4, gives observed success proportions 21​, 32​, 98​, 6564​.

    A sketch with no numbers beside it earns nothing here: every plotted point must carry its exact coordinates.

    1. (a)For the first predictor, compute the empirical odds and log-odds in each bin exactly, and sketch the log-odds against x. Say what feature of the sketch supports linearity in the logit and what slope it suggests.
    2. (b)On a second set of axes, sketch the observed proportions against x for the same predictor. Explain why this plot, which is visibly curved, is not evidence against the model.
    3. (c)Repeat (a) for the second predictor. Describe the curvature you find, identify its exact form, and say how the model should be amended.

    Solution

    (a) Odds first, then logs. The proportions are 9010​=91​, 9018​=51​, 9030​=31​, 9045​=21​, 9060​=32​. The odds p/(1−p) are

    81​,41​,21​,1,2,

    and the log-odds are −3ln2, −2ln2, −ln2, 0, ln2. Every successive difference is exactly ln2, so the five points are collinear.

    The sketch: horizontal axis x from 0 to 6, vertical axis log-odds from −2.5 to 1 (mark ln2≈0.69 as a tick). Plot the five points and draw the straight line through them; it crosses the axis at (4,0) and has slope ln2. The feature that supports linearity in the logit is the constant vertical spacing — equal steps in x give equal steps in log-odds — and the common step estimates β1​≈ln2, with β0​≈−4ln2 from the intercept of the line.

    (b) The same data on the probability scale. The points are (1,91​),(2,51​),(3,31​),(4,21​),(5,32​); the successive rises are 454​, 152​, 61​, 61​ — increasing, then levelling — which is the lower half of an S. Draw the axes with p from 0 to 1, plot the five points, and draw through them the curve p=σ((x−4)ln2), which passes through all five exactly and flattens toward the ceiling p=1 (dashed) to the right.

    This curvature is not evidence against anything: the model asserts linearity in the logit, and a linear logit produces exactly an S-shaped probability curve. Checking straightness on the probability scale tests a model nobody proposed. That is why the diagnostic is made on the log-odds scale, where (a) shows a perfect line.

    (c) The second predictor is curved where it matters. Odds: 1/21/2​=1, 1/32/3​=2, 1/98/9​=8, 1/6564/65​=64. Log-odds: 0, ln2, 3ln2, 6ln2. The successive differences are ln2, 2ln2, 3ln2 — themselves increasing by a constant ln2 — so the log-odds are a quadratic in x: indeed 0,1,3,6 are 2x(x−1)​ at x=1,2,3,4, so

    logit(p)=2ln2​x(x−1).

    Sketch the four points on axes with log-odds from 0 to 4.5; they bend upward, convex, with no straight line passing near all four (a line through the first two, slope ln2, would predict 2ln2 at x=3 against the observed 3ln2, and 3ln2 at x=4 against 6ln2). The remedy is the one ordinary regression would use: add an x2 term, or a spline, so that the logit is linear in the transformed predictor. Here the quadratic term removes the curvature exactly.

  10. 10.Computation

    A fitted model is applied to seven cases. The three cases with Y=1 receive fitted probabilities 0.9, 0.6, 0.3; the four cases with Y=0 receive 0.8, 0.5, 0.2, 0.1. A case is classified positive when its fitted probability exceeds the threshold c.

    1. (a)At c=21​ write down TP, FP, TN, FN and compute sensitivity, specificity, precision and accuracy. Repeat at c=0.7, and check the direction of every change against what the threshold trade-off requires.
    2. (b)Compute the AUC exactly, using its interpretation as a probability about a randomly chosen positive–negative pair.
    3. (c)List the point (1−specificity, sensitivity) at every threshold at which the classification changes, as c sweeps from 1 down to 0; join them into the ROC curve, and verify that the area under your polygon agrees with (b).
    4. (d)A second model returns exactly half of every fitted probability above. State its AUC and its confusion matrix at c=21​. What does the comparison show about what AUC does and does not measure?

    Solution

    (a) Count, then divide. At c=21​ the positives 0.9 and 0.6 are called positive and 0.3 is not, so TP=2, FN=1; among the negatives only 0.8 is called positive, so FP=1, TN=3. Then

    sensitivity=32​,specificity=43​,precision=32​,accuracy=75​.

    At c=0.7 only 0.9 and 0.8 exceed the threshold: TP=1, FN=2, FP=1, TN=3, giving sensitivity 31​, specificity 43​, precision 21​, accuracy 74​.

    Raising c moved one positive case (0.6) from predicted-positive to predicted-negative and moved no negative case, so sensitivity fell and specificity stayed put — non-increasing and non-decreasing respectively, exactly as the trade-off allows. Accuracy fell from 75​ to 74​; note the do-nothing baseline of calling everything negative already scores 74​.

    (b) Count concordant pairs. There are 3×4=12 positive–negative pairs. The positive scored 0.9 beats all four negatives; 0.6 beats 0.5,0.2,0.1 (three); 0.3 beats 0.2,0.1 (two). Nine of twelve pairs are ordered correctly, so

    AUC=129​=43​.

    There are no ties, so no half-credit is needed.

    (c) Walk down the sorted scores. In decreasing order the scores are 0.9(+), 0.8(−), 0.6(+), 0.5(−), 0.3(+), 0.2(−), 0.1(−). Start at c=1 with nothing called positive: the point is (0,0). As c drops past each score that case becomes positive; a positive case raises sensitivity by 31​, a negative case raises 1−specificity by 41​:

    (0,0)→(0,31​)→(41​,31​)→(41​,32​)→(21​,32​)→(21​,1)→(43​,1)→(1,1).

    The ROC curve is this staircase: up, right, up, right, up, right, right. Draw it on the unit square with the chance diagonal dashed; the curve lies above the diagonal throughout.

    Area: on the four columns of width 41​ the staircase sits at heights 31​, 32​, 1, 1, so the area is 41​(31​+32​+1+1)=41​⋅3=43​ ✓. Rule the square into a 4×3 grid of cells of area 121​: each cell corresponds to one (negative, positive) pair, and the cell lies under the curve exactly when the positive outscores the negative. Nine cells under the curve, nine concordant pairs — the two computations are the same count.

    (d) Halving preserves order. The second model's scores are 0.45,0.3,0.15 for the positives and 0.4,0.25,0.1,0.05 for the negatives; every pairwise comparison is unchanged, so its AUC is still 43​ and its ROC curve is identical. But at c=21​ nothing exceeds the threshold: TP=0, FN=3, FP=0, TN=4, sensitivity 0, specificity 1, accuracy 74​, precision undefined (00​). It never finds a single case.

    AUC measures ranking — whether positives tend to score above negatives — and is blind to the scale of the probabilities. Calibration, whether a case scored 0.45 really has a 45% chance, is a separate property, and the second model fails it at every case while matching the first model's AUC exactly. If the probabilities themselves will be used, or a threshold will be chosen from them, calibration must be checked separately.

  11. 11.Proof

    Four observations of the model logit(pi​)=β0​+β1​xi​ are (x,y)=(1,0), (2,0), (3,1), (4,1). Write β=(β0​,β1​)⊤ and xi​=(1,xi​)⊤.

    1. (a)Show that b=(−5,2)⊤ completely separates the data.
    2. (b)Write ℓ(tb) explicitly as a function of t>0 and prove that it increases to 0 as t→∞. Then prove that ℓ(β)<0 for every finite β, and conclude that the maximum likelihood estimate does not exist.
    3. (c)A fifth observation (2,1) is added. Prove that no vector now separates the data, and show that ℓ(β)≤−2ln2 for every β, so that the supremum in (b) is no longer approachable.
    4. (d)Describe what software fitting the original four observations by Newton's method would report, and explain why running more iterations is not the remedy.

    Solution

    (a) Check the sign at every observation. xi⊤​b=−5+2xi​ takes the values −3,−1,1,3 at x=1,2,3,4: negative at both observations with y=0 and positive at both with y=1. That is the definition of complete separation.

    (b) Along the ray tb. The linear predictors are ηi​=t(−5+2xi​)=−3t,−t,t,3t. An observation with y=0 contributes ln(1−pi​)=−ln(1+eηi​), and one with y=1 contributes lnpi​=−ln(1+e−ηi​). Substituting,

    ℓ(tb)=−ln(1+e−3t)−ln(1+e−t)−ln(1+e−t)−ln(1+e−3t)=−2ln(1+e−t)−2ln(1+e−3t).

    As t increases, e−t and e−3t decrease to 0, so each logarithm decreases to ln1=0 and ℓ(tb) increases. It is always negative, since each logarithm is of a number exceeding 1; and using ln(1+u)≤u for u≥0,

    0>ℓ(tb)≥−2e−t−2e−3t⟶0,

    so ℓ(tb)→0 from below.

    For any finite β every pi​=σ(xi⊤​β) lies strictly inside (0,1), so every contribution lnpi​ or ln(1−pi​) is strictly negative and ℓ(β)<0. Combining: the supremum of ℓ is 0, it is approached along the ray, and it is attained nowhere. A maximiser would have to attain the supremum, so none exists. ■

    (c) Two outcomes at one predictor value. Separation would need a b with x⊤b<0 at (2,0) and x⊤b>0 at (2,1), but both observations share x=(1,2)⊤, so the same number would have to be both negative and positive. No separating vector exists.

    For the bound: the two observations at x=2 share a fitted probability p, and together they contribute

    ln(1−p)+lnp=ln(p(1−p))≤ln41​=−2ln2,

    since p(1−p)≤41​ for p∈(0,1). The other three contributions are negative, so ℓ(β)≤−2ln2 for every β. The likelihood can no longer be driven towards 0 by any choice of coefficients: the conflicting pair at x=2 costs at least 2ln2 whatever the fit does elsewhere. ■

    (d) What the output looks like. Each Newton iteration moves further out along a separating direction, so the reported coefficients grow with the number of iterations — a slope in the tens or twenties, an intercept of the opposite sign and comparable size — and the iteration halts only when the change in ℓ falls below the tolerance, since ℓ is flattening towards 0. The fitted probabilities are numerically 0 and 1, and the weights pi​(1−pi​) are numerically 0, so X⊤WX is nearly singular and its inverse — the standard errors — is enormous. Software may add a warning about fitted probabilities of 0 or 1.

    More iterations cannot help because there is nothing to converge to: (b) shows the maximum does not exist, so any finite stopping point is arbitrary. The remedies are to change the problem — penalise the likelihood (Firth's correction or a ridge penalty, which make the penalised objective attain a finite maximum), drop or merge the offending predictor, or collect data that overlaps as in (c).

  12. 12.Critique

    Each of the three modelling decisions below contains an error. Identify it precisely, explain why it matters, and state what should have been done. Where a number is involved, correct it exactly.

    1. (a)“A Poisson regression of monthly complaints per store has coefficient ln2 on the number of checkouts. So each additional checkout adds ln2≈0.69 complaints to the expected monthly count.”
    2. (b)“Across stores the variance of the monthly complaint count is about six times its mean. Since over-dispersion leaves the Poisson coefficients roughly unbiased, the reported standard errors and p-values can be used as they are. In particular the checkout coefficient has z=3, so it is significant.”
    3. (c)“Time spent on a web page, in minutes, is positive and right-skewed, so we fitted a Poisson regression with a log link: the log link guarantees positive predictions, and the skew is handled by the distribution.”

    Solution

    (a) The log link makes coefficients multiplicative. In the model logE(Y)=η the expected count is eη, and increasing the checkout count by one replaces η by η+ln2, so it multiplies the expected count by eln2=2. The correct sentence is “each additional checkout doubles the expected number of complaints”. The additive change is not ln2 but E(Y) itself: from an expected 3 complaints the extra checkout adds 3, from 10 it adds 10. This is the same misreading as treating a logistic coefficient as a change in probability — the coefficient lives on the scale of the link, and only there is its effect constant.

    (b) Unbiased coefficients do not rescue the inference. A Poisson response has variance equal to its mean, and the standard errors are computed on that assumption. If the true variance is about 6 times the mean, the actual sampling variance of β^​ is about 6 times what the model claims, so every reported standard error is too small by a factor of about 6​ and every z-statistic too large by the same factor. The corrected statistic is

    z=6​3​=26​​≈1.22,

    well short of 2: the checkout effect is not significant at the 5% level once the dispersion is honoured. This is the count-data twin of heteroscedasticity in the linear model — estimates fine, standard errors wrong. The remedy is a negative binomial regression, which estimates a dispersion parameter, or at least a quasi-Poisson or robust standard error that scales the Poisson ones by ϕ^​​.

    (c) The link is right and the distribution is wrong. The Poisson distribution lives on the counts 0,1,2,…; time on a page is continuous, and a model for it that assigns probability only to whole minutes is not a model of the data. The mistake shows up concretely through the variance–mean relation, which is not invariant to the unit of measurement: a page with mean 2 minutes and Poisson-consistent variance 2 (min2) has, in seconds, mean 120 and variance 2×602=7200, not 120. A model whose fit depends on whether you recorded seconds or minutes is measuring the units, not the behaviour.

    The positive-predictions argument is correct as far as it goes — a log link does keep E(Y)>0 — but link and distribution are separate choices in a generalised linear model and the second was made badly. The right choices are ordinary regression on ln(time), or a gamma GLM with a log link, whose variance is proportional to the square of the mean: mean 2, variance 4ϕ in minutes becomes mean 120, variance 14400ϕ=ϕ⋅1202 in seconds — the same model in both units, as a model for a continuous quantity must be.

← Back to the question sheet

CalcPath·Free university mathematics

SubjectsProblem setsProgressFeedbackAbout