Skip to main content

SAMPLE EXAM ANSWER: Logistic Regression Classifier & Loss Minimization via Gradient Descent

Question: (i) Explain in detail about logistic regression classifier, with suitable example. [10 marks] (ii) What is loss function, explain how Gradient descent helps to Minimize the loss. [10 marks]

Marks: 20 marks | Time: 30 minutes


✅ COMPLETE EXAM ANSWER


PART 1: LOGISTIC REGRESSION CLASSIFIER IN DETAIL (10 marks)

1.1 Statistical Definition & GLM Framework (2 marks)

Logistic Regression is a supervised machine learning algorithm used for binary classification (predicting a target label y{0,1}y \in \{0, 1\}).

Unlike linear regression, which models continuous outputs directly, Logistic Regression is a Generalized Linear Model (GLM) where:

  1. The target variable is assumed to follow a Bernoulli distribution: yiBernoulli(Pi)y_i \sim \text{Bernoulli}(P_i).
  2. We model the conditional probability Pi=P(yi=1xi)P_i = P(y_i = 1 \mid \mathbf{x}_i) that the input belongs to the positive class.
  3. The link function used is the non-linear logit link function (log-odds), which maps the continuous range (,)(-\infty, \infty) of the linear equation to a [0,1][0, 1] probability range.

1.2 Mathematical Formulation & Sigmoid Activation (2 marks)

The model starts by computing an unbounded continuous score (the logit score) zz: z=wTx+b=w1x1+w2x2++wnxn+bz = \mathbf{w}^T \mathbf{x} + b = w_1 x_1 + w_2 x_2 + \dots + w_n x_n + b

We convert this unbounded logit score into a bounded conditional probability P(y=1x)P(y = 1 \mid \mathbf{x}) by passing it through the Sigmoid (Logistic) Function:

P(y=1x)=σ(z)=11+ez=11+e(wTx+b)\boxed{P(y = 1 \mid \mathbf{x}) = \sigma(z) = \frac{1}{1 + e^{-z}} = \frac{1}{1 + e^{-(\mathbf{w}^T \mathbf{x} + b)}}}

Decision Boundary (1 mark)

To convert the estimated probability into a discrete class prediction y^{0,1}\hat{y} \in \{0, 1\}, a decision threshold τ\tau (conventionally τ=0.5\tau = 0.5) is applied:

y^={1if P(y=1x)0.5(equivalent to z0)0if P(y=1x)<0.5(equivalent to z<0)\boxed{\hat{y} = \begin{cases} 1 & \text{if } P(y = 1 \mid \mathbf{x}) \ge 0.5 \quad (\text{equivalent to } z \ge 0) \\ 0 & \text{if } P(y = 1 \mid \mathbf{x}) < 0.5 \quad (\text{equivalent to } z < 0) \end{cases}}


1.3 Worked Example (Pass/Fail Classification) (3 marks)

Problem: Predict whether a student Passes (1) or Fails (0) an exam based on Hours Studied (xx).

Assume we have already trained the model and obtained the optimal parameters:

  • Weight (w1w_1) = 1.51.5
  • Bias (bb) = 4.5-4.5

This yields the logit score equation: z=1.5x4.5z = 1.5x - 4.5

Prediction Generation for 3 Different Students:

Case 1: Student A studies x=2x = 2 Hours
  1. Calculate Logit (zz): zA=1.5(2)4.5=3.04.5=1.5z_A = 1.5(2) - 4.5 = 3.0 - 4.5 = \mathbf{-1.5}
  2. Apply Sigmoid: P(y=1x=2)=11+e(1.5)=11+e1.511+4.481689=15.4816890.1824 (18.24%)P(y = 1 \mid x = 2) = \frac{1}{1 + e^{-(-1.5)}} = \frac{1}{1 + e^{1.5}} \approx \frac{1}{1 + 4.481689} = \frac{1}{5.481689} \approx \mathbf{0.1824 \ (18.24\%)}
  3. Threshold Classification: Since 0.1824<0.50.1824 < 0.5, predicted label is Fail (0).
Case 2: Student B studies x=3x = 3 Hours
  1. Calculate Logit (zz): zB=1.5(3)4.5=4.54.5=0.0z_B = 1.5(3) - 4.5 = 4.5 - 4.5 = \mathbf{0.0}
  2. Apply Sigmoid: P(y=1x=3)=11+e0=11+1=0.5000 (50.00%)P(y = 1 \mid x = 3) = \frac{1}{1 + e^{-0}} = \frac{1}{1 + 1} = \mathbf{0.5000 \ (50.00\%)}
  3. Threshold Classification: Exactly at the decision boundary. By convention (0.5\ge 0.5), predicted label is Pass (1).
Case 3: Student C studies x=4x = 4 Hours
  1. Calculate Logit (zz): zC=1.5(4)4.5=6.04.5=1.5z_C = 1.5(4) - 4.5 = 6.0 - 4.5 = \mathbf{1.5}
  2. Apply Sigmoid: P(y=1x=4)=11+e1.511+0.223130=11.2231300.8176 (81.76%)P(y = 1 \mid x = 4) = \frac{1}{1 + e^{-1.5}} \approx \frac{1}{1 + 0.223130} = \frac{1}{1.223130} \approx \mathbf{0.8176 \ (81.76\%)}
  3. Threshold Classification: Since 0.81760.50.8176 \ge 0.5, predicted label is Pass (1).

1.4 Probability Curve & Decision Boundary Visual (2 marks)

The relationship between Study Hours (xx) and Pass Probability (PP) forms the characteristic S-shaped curve:

P(Pass) ↑
1.00 ┤ * * * Student C (4h, 81.76%)
│ *
0.75 ┤ *
│ *
0.50 ┤───────────────*──────────── [Decision Boundary at x = 3.0 Hours]
│ * Student B (3h, 50.00%)
0.25 ┤ *
│ * Student A (2h, 18.24%)
0.00 ┴─*─*─*─┴─────┴─────┴─────┴─────┴─────┴─→ Hours Studied (x)
0 1 2 3 4 5 6
  • Inference: The threshold is reached exactly at x=3.0x = 3.0 study hours (z=0,P=0.5z = 0, P = 0.5). If a student studies less than 3 hours, they are predicted to fail; if they study 3 or more hours, they are predicted to pass.

PART 2: LOSS FUNCTION & MINIMIZATION VIA GRADIENT DESCENT (10 marks)

2.1 What is a Loss Function? (2 marks)

A Loss Function (or cost function) is a mathematical objective function that quantifies the difference between a model's predicted outputs and the actual ground-truth labels. It maps the error of a model's parameters to a single scalar value.

Why We Cannot Use Mean Squared Error (MSE) for Logistic Regression:

In linear regression, MSE is convex. However, if we apply MSE to Logistic Regression, squaring the non-linear Sigmoid output results in a non-convex cost function with numerous local minima. Gradient descent can easily get trapped in sub-optimal local minima.

The Solution: Binary Cross-Entropy Loss (Log-Loss):

Derived from the Maximum Likelihood Estimation (MLE) of a Bernoulli process, we use Log-Loss, which is guaranteed to be convex:

J(w,b)=1Ni=1N[yiln(pi)+(1yi)ln(1pi)]\boxed{\mathcal{J}(\mathbf{w}, b) = -\frac{1}{N} \sum_{i=1}^{N} \left[ y_i \ln(p_i) + (1 - y_i) \ln(1 - p_i) \right]}

Where:

  • yiy_i = Actual ground truth label (00 or 11)
  • pip_i = Model predicted probability (σ(zi)\sigma(z_i))
  • NN = Number of training samples
How the Log-Loss penalizes errors:
  • If yi=1y_i = 1: Loss =ln(pi)= -\ln(p_i). As prediction pi1p_i \rightarrow 1, Loss 0\rightarrow 0. As pi0p_i \rightarrow 0, Loss \rightarrow \infty (heavily penalizes False Negatives).
  • If yi=0y_i = 0: Loss =ln(1pi)= -\ln(1 - p_i). As prediction pi0p_i \rightarrow 0, Loss 0\rightarrow 0. As pi1p_i \rightarrow 1, Loss \rightarrow \infty (heavily penalizes False Positives).

2.2 Gradient Descent Minimization Mechanics (3 marks)

Gradient Descent is an iterative numerical optimization algorithm used to find the global minimum of a convex cost function J(w,b)\mathcal{J}(\mathbf{w}, b).

The Physical Intuition:

Imagine being at the top of a mountain basin in thick fog. To find the bottom of the basin, you can feel the slope of the ground beneath your feet and take a step in the direction of the steepest descent (downward).

In mathematical terms:

  1. The gradient vector wJ\nabla_{\mathbf{w}} \mathcal{J} points in the direction of steepest ascent.
  2. Therefore, to minimize the loss, we must move in the opposite direction—the negative gradient (wJ-\nabla_{\mathbf{w}} \mathcal{J}).

Iterative Update Equations:

At each iteration tt, we update the weights and bias parameters:

w(t+1)=w(t)ηwJ(w,b)\boxed{\mathbf{w}^{(t+1)} = \mathbf{w}^{(t)} - \eta \nabla_{\mathbf{w}} \mathcal{J}(\mathbf{w}, b)}

b(t+1)=b(t)ηJb\boxed{b^{(t+1)} = b^{(t)} - \eta \frac{\partial \mathcal{J}}{\partial b}}

Where η>0\eta > 0 is the learning rate (step size).

  • If η\eta is too small, convergence is extremely slow.
  • If η\eta is too large, the algorithm can overshoot the minimum and diverge.

2.3 Step-by-Step Calculus Derivation of Gradients (5 marks)

To implement the update rules, we derive the partial derivatives of the Cost Function J\mathcal{J} with respect to the parameters. We use the chain rule for a single observation ii:

Jiwj=Jipipiziziwj\frac{\partial \mathcal{J}_i}{\partial w_j} = \frac{\partial \mathcal{J}_i}{\partial p_i} \cdot \frac{\partial p_i}{\partial z_i} \cdot \frac{\partial z_i}{\partial w_j}

Where:

  • Ji=[yiln(pi)+(1yi)ln(1pi)]\mathcal{J}_i = - \left[ y_i \ln(p_i) + (1 - y_i) \ln(1 - p_i) \right]
  • pi=σ(zi)=11+ezip_i = \sigma(z_i) = \frac{1}{1 + e^{-z_i}}
  • zi=wTxi+bz_i = \mathbf{w}^T \mathbf{x}_i + b

Step 1: Compute Jipi\frac{\partial \mathcal{J}_i}{\partial p_i} (Derivative of Loss w.r.t. Probability)

Jipi=ddpi[yiln(pi)+(1yi)ln(1pi)]\frac{\partial \mathcal{J}_i}{\partial p_i} = -\frac{d}{dp_i} \left[ y_i \ln(p_i) + (1 - y_i) \ln(1 - p_i) \right]

Jipi=[yipi1yi1pi]=yi(1pi)+pi(1yi)pi(1pi)\frac{\partial \mathcal{J}_i}{\partial p_i} = - \left[ \frac{y_i}{p_i} - \frac{1 - y_i}{1 - p_i} \right] = \frac{-y_i(1 - p_i) + p_i(1 - y_i)}{p_i(1 - p_i)}

Jipi=yi+yipi+piyipipi(1pi)=piyipi(1pi)\frac{\partial \mathcal{J}_i}{\partial p_i} = \frac{-y_i + y_i p_i + p_i - y_i p_i}{p_i(1 - p_i)} = \mathbf{\frac{p_i - y_i}{p_i(1 - p_i)}}


Step 2: Compute pizi\frac{\partial p_i}{\partial z_i} (Derivative of Sigmoid w.r.t. Logit)

As proved previously (derivative of sigmoid): pizi=σ(zi)(1σ(zi))=pi(1pi)\frac{\partial p_i}{\partial z_i} = \sigma(z_i)(1 - \sigma(z_i)) = \mathbf{p_i(1 - p_i)}


Step 3: Compute ziwj\frac{\partial z_i}{\partial w_j} and zib\frac{\partial z_i}{\partial b} (Derivative of Logit w.r.t. Parameters)

Since zi=w1xi,1+w2xi,2++wjxi,j+bz_i = w_1 x_{i,1} + w_2 x_{i,2} + \dots + w_j x_{i,j} + b: ziwj=xi,j\frac{\partial z_i}{\partial w_j} = \mathbf{x_{i,j}}

zib=1\frac{\partial z_i}{\partial b} = \mathbf{1}


Step 4: Combine Derivatives via Chain Rule

Multiply the partial derivatives together: Jiwj=[piyipi(1pi)][pi(1pi)]xi,j\frac{\partial \mathcal{J}_i}{\partial w_j} = \left[ \frac{p_i - y_i}{p_i(1 - p_i)} \right] \cdot \left[ p_i(1 - p_i) \right] \cdot x_{i,j}

The intermediate pi(1pi)p_i(1 - p_i) terms cancel out perfectly: Jiwj=(piyi)xi,j\frac{\partial \mathcal{J}_i}{\partial w_j} = \mathbf{(p_i - y_i) x_{i,j}}

Similarly, for the bias bb: Jib=[piyipi(1pi)][pi(1pi)]1=(piyi)\frac{\partial \mathcal{J}_i}{\partial b} = \left[ \frac{p_i - y_i}{p_i(1 - p_i)} \right] \cdot \left[ p_i(1 - p_i) \right] \cdot 1 = \mathbf{(p_i - y_i)}


Step 5: Average Over All NN Samples

Averaging the partial gradients across the entire dataset yields the final gradient equations for the optimization step:

Jwj=1Ni=1N(piyi)xi,j\boxed{\frac{\partial \mathcal{J}}{\partial w_j} = \frac{1}{N} \sum_{i=1}^{N} (p_i - y_i) x_{i,j}}

Jb=1Ni=1N(piyi)\boxed{\frac{\partial \mathcal{J}}{\partial b} = \frac{1}{N} \sum_{i=1}^{N} (p_i - y_i)}

  • Inference & Significance: These mathematically elegant gradients represent the prediction error (piyip_i - y_i) scaled by the feature value xi,jx_{i,j}. This means that if a prediction has zero error (pi=yip_i = y_i), the gradient becomes zero, and no updates are made. If the error is large, the parameter updates are proportional to the magnitude of that error.

SUMMARY PARADIGMS COMPARISON TABLE

ConceptMathematical FormulaConvexityPrimary Use CaseOptimization Solver
Log-Loss1N[yln(p)+(1y)ln(1p)]-\frac{1}{N} \sum [y \ln(p) + (1-y)\ln(1-p)]ConvexBinary ClassificationGradient Descent
MSE1n(yy^)2\frac{1}{n} \sum (y - \hat{y})^2Non-Convex (for Sigmoid)Linear RegressionOLS (Normal Eq) or GD

KEY TAKEAWAYS (Quick Review)

Logistic Regression:

  1. Fits a Sigmoid curve: P(y=1x)=11+ezP(y=1 \mid \mathbf{x}) = \frac{1}{1 + e^{-z}}.
  2. Probability boundary of τ=0.5\tau = 0.5 corresponds exactly to z=0z = 0.
  3. Models log-odds linearly: ln(odds)=wTx+b\ln(\text{odds}) = \mathbf{w}^T\mathbf{x} + b.

Loss Functions & Optimization:

  1. MSE cannot be used because Sigmoid makes it non-convex.
  2. Log-Loss is convex and derived from Maximum Likelihood Estimation (MLE).
  3. Gradient Descent iteratively minimizes loss by taking steps proportional to the negative gradient.
  4. Updates scale directly with the model's prediction errors (piyip_i - y_i).

ANSWER CHECKLIST

  • ✅ Define Logistic Regression as a Generalized Linear Model (GLM)
  • ✅ Write the standard Sigmoid function and decision boundary threshold
  • ✅ Provide a worked prediction pass/fail example for 3 distinct students
  • ✅ Sketch or describe the S-shaped sigmoid probability curve
  • ✅ Define a loss function and prove why MSE cannot be used for logistic classification
  • ✅ Write the formal Binary Cross-Entropy (Log-Loss) equation
  • ✅ Explain the physical intuition of Gradient Descent
  • ✅ Write down iterative weight and bias update equations with learning rate (η\eta)
  • ✅ Show a step-by-step calculus derivation of gradients using the chain rule
  • ✅ Explain the significance of the final gradient equations (piyip_i - y_i)

MARK BREAKDOWN

ComponentMarks
Logistic Regression & GLM Definition2.0
Sigmoid Formulation & Decision Boundary3.0
Worked Case Study Examples (3 students)3.0
Sigmoid Curve Visual & Boundary Explanation2.0
PART 1 SUBTOTAL10.0
Loss Function & MSE Non-Convexity Explanation2.0
Log-Loss Formula & Likelihood Intuition2.0
Gradient Descent Update Equations & Learning Rate (η\eta)2.0
Step-by-Step Calculus Chain Rule Derivative Proof4.0
PART 2 SUBTOTAL10.0
TOTAL20.0

This answer scores FULL 20 marks