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 ).
Unlike linear regression, which models continuous outputs directly, Logistic Regression is a Generalized Linear Model (GLM) where:
- The target variable is assumed to follow a Bernoulli distribution: .
- We model the conditional probability that the input belongs to the positive class.
- The link function used is the non-linear logit link function (log-odds), which maps the continuous range of the linear equation to a probability range.
1.2 Mathematical Formulation & Sigmoid Activation (2 marks)
The model starts by computing an unbounded continuous score (the logit score) :
We convert this unbounded logit score into a bounded conditional probability by passing it through the Sigmoid (Logistic) Function:
Decision Boundary (1 mark)
To convert the estimated probability into a discrete class prediction , a decision threshold (conventionally ) is applied:
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 ().
Assume we have already trained the model and obtained the optimal parameters:
- Weight () =
- Bias () =
This yields the logit score equation:
Prediction Generation for 3 Different Students:
Case 1: Student A studies Hours
- Calculate Logit ():
- Apply Sigmoid:
- Threshold Classification: Since , predicted label is Fail (0).
Case 2: Student B studies Hours
- Calculate Logit ():
- Apply Sigmoid:
- Threshold Classification: Exactly at the decision boundary. By convention (), predicted label is Pass (1).
Case 3: Student C studies Hours
- Calculate Logit ():
- Apply Sigmoid:
- Threshold Classification: Since , predicted label is Pass (1).
1.4 Probability Curve & Decision Boundary Visual (2 marks)
The relationship between Study Hours () and Pass Probability () 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 study hours (). 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:
Where:
- = Actual ground truth label ( or )
- = Model predicted probability ()
- = Number of training samples
How the Log-Loss penalizes errors:
- If : Loss . As prediction , Loss . As , Loss (heavily penalizes False Negatives).
- If : Loss . As prediction , Loss . As , Loss (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 .
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:
- The gradient vector points in the direction of steepest ascent.
- Therefore, to minimize the loss, we must move in the opposite direction—the negative gradient ().
Iterative Update Equations:
At each iteration , we update the weights and bias parameters:
Where is the learning rate (step size).
- If is too small, convergence is extremely slow.
- If 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 with respect to the parameters. We use the chain rule for a single observation :
Where:
Step 1: Compute (Derivative of Loss w.r.t. Probability)
Step 2: Compute (Derivative of Sigmoid w.r.t. Logit)
As proved previously (derivative of sigmoid):
Step 3: Compute and (Derivative of Logit w.r.t. Parameters)
Since :
Step 4: Combine Derivatives via Chain Rule
Multiply the partial derivatives together:
The intermediate terms cancel out perfectly:
Similarly, for the bias :
Step 5: Average Over All Samples
Averaging the partial gradients across the entire dataset yields the final gradient equations for the optimization step:
- Inference & Significance: These mathematically elegant gradients represent the prediction error () scaled by the feature value . This means that if a prediction has zero error (), 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
| Concept | Mathematical Formula | Convexity | Primary Use Case | Optimization Solver |
|---|---|---|---|---|
| Log-Loss | Convex | Binary Classification | Gradient Descent | |
| MSE | Non-Convex (for Sigmoid) | Linear Regression | OLS (Normal Eq) or GD |
KEY TAKEAWAYS (Quick Review)
✅ Logistic Regression:
- Fits a Sigmoid curve: .
- Probability boundary of corresponds exactly to .
- Models log-odds linearly: .
✅ Loss Functions & Optimization:
- MSE cannot be used because Sigmoid makes it non-convex.
- Log-Loss is convex and derived from Maximum Likelihood Estimation (MLE).
- Gradient Descent iteratively minimizes loss by taking steps proportional to the negative gradient.
- Updates scale directly with the model's prediction errors ().
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 ()
- ✅ Show a step-by-step calculus derivation of gradients using the chain rule
- ✅ Explain the significance of the final gradient equations ()
MARK BREAKDOWN
| Component | Marks |
|---|---|
| Logistic Regression & GLM Definition | 2.0 |
| Sigmoid Formulation & Decision Boundary | 3.0 |
| Worked Case Study Examples (3 students) | 3.0 |
| Sigmoid Curve Visual & Boundary Explanation | 2.0 |
| PART 1 SUBTOTAL | 10.0 |
| Loss Function & MSE Non-Convexity Explanation | 2.0 |
| Log-Loss Formula & Likelihood Intuition | 2.0 |
| Gradient Descent Update Equations & Learning Rate () | 2.0 |
| Step-by-Step Calculus Chain Rule Derivative Proof | 4.0 |
| PART 2 SUBTOTAL | 10.0 |
| TOTAL | 20.0 |
This answer scores FULL 20 marks ✅