[MMD]
ML Calculus Chapter 2: Optimization, Partial Derivatives, and Gradients
In Chapter 1, we covered the fundamentals of differentiation and key derivative formulas. In this post, we'll see how those derivatives are applied to optimization, and explore partial derivatives and gradients — the tools for differentiating functions with multiple variables.
This post is based on Week 2 of DeepLearning.AI's Mathematics for Machine Learning and Data Science — Calculus.
What You'll Learn#
- Optimization — the difference between local minima, local maxima, and the global minimum
- Squared Loss optimization — why the mean is the optimal solution
- Log-Loss optimization — why we use logarithms
- Partial Derivatives — differentiating multivariable functions
- Gradient — representing partial derivatives as a vector
- Finding the minimum using the gradient
What Is Optimization?#
Optimization is the process of finding the maximum or minimum of a function.
In machine learning, we always want to minimize a loss function. Training a model means reducing the loss — the measure of how badly the model is predicting the data.
Local Minima vs. Global Minimum#
| Term | Description |
|---|---|
| Local Minimum | Lower than its immediate neighbors, but not the lowest point overall (slope = 0) |
| Global Minimum | The absolute lowest point of the entire function (slope = 0) |
| Local Maximum | Higher than its immediate neighbors, but not the highest point overall (slope = 0) |
Key insight: Minima and maxima always occur where the derivative (slope) equals 0. However, a zero slope doesn't guarantee a minimum — saddle points also have a zero derivative.
Squared Loss Optimization#
Understanding Through the Power Line Problem#
Suppose you're choosing a location to build a house and want to minimize the connection cost to power lines at positions .
Cost Function:
Why we use squared distances:
- Easier to differentiate than absolute values
- Penalizes deviations in both directions equally
Finding the minimum — set the derivative to 0:
Conclusion: The optimal location that minimizes squared loss is the mean. This is the same reason why minimizing MSE in linear regression yields the least-squares estimator.
Generalizing to Points#
Machine learning interpretation: When MSE (Mean Squared Error) is used as the loss function, the prediction that minimizes it is always the mean of the target values.
Log-Loss Optimization#
Why Use Log-Loss?#
Suppose you flip a coin 10 times and get 7 heads (H) and 3 tails (T). You want to estimate the probability of heads.
Likelihood:
Finding the that maximizes is called Maximum Likelihood Estimation (MLE).
The problem: Products of probabilities get extremely small (e.g., ). With more data, the numbers become too small for computers to handle reliably.
The fix — log transformation:
Using the logarithm turns products into sums, making computation numerically stable.
Differentiate to find the maximum:
Conclusion: The MLE estimate matches the observed frequency ().
Negative Log-Loss#
In classification models, the loss function uses a negative sign:
Why:
- When , — the log-likelihood is negative
- Negating it makes the loss positive so we can minimize it
Minimizing the negative log-loss is equivalent to maximizing the original likelihood.
Partial Derivatives#
Extending to Tangent Planes#
Derivative of a 1-variable function → tangent line
Derivative of a 2-variable function → tangent plane
When differentiating a function with multiple variables, we hold all other variables constant and differentiate with respect to one variable at a time. This is called a partial derivative.
Computing Partial Derivatives#
Partial derivative with respect to (treat as a constant):
Partial derivative with respect to (treat as a constant):
Notation: or (read as "partial f, partial x")
Another Example#
Key insight: When taking the partial derivative with respect to , treat any term involving only as a constant. Since has no , its partial derivative with respect to is 0.
Gradient#
The gradient is a vector formed by collecting all partial derivatives.
For a two-variable function :
Example#
Gradient at the point :
Geometric meaning: The gradient points in the direction of steepest ascent. The negative gradient () points in the direction of steepest descent.
Finding the Minimum Using the Gradient#
Analytical Method#
For a multivariable function, the minimum occurs where all partial derivatives equal 0.
Linear Regression Example#
Fitting a line to the data points :
Loss function (MSE):
Partial derivatives:
Solving the system of equations:
This is the analytical solution to the Least Squares problem.
In practice? When datasets are large and models have many parameters, solving analytically becomes intractable. That's when Gradient Descent comes in.
Summary#
| Concept | Description |
|---|---|
| Optimization | Finding the minimum or maximum of a function |
| Global Minimum | The absolute lowest point of the entire function |
| Local Minimum | Lower than neighbors, but not the global minimum |
| Squared Loss | , minimized by the mean |
| Log-Loss | , used for probability estimation |
| Partial Derivative | Differentiate with respect to one variable, treat the rest as constants |
| Gradient | Vector of all partial derivatives, points toward steepest ascent |
| Minimum condition |
Quiz#
Q1. Find for .
Show answer
Treat as a constant:
Q2. Find the gradient of .
Show answer
Q3. What value of minimizes the loss function ?
Show answer
This is the mean of 3, 7, and 5 — confirming that squared loss is always minimized by the mean.
Q4. Which of the following statements about the gradient is correct?
- The gradient is a scalar value
- The gradient points in the direction of steepest descent
- A point where the gradient is is a candidate for a local minimum or maximum
- The gradient always points in a positive direction
Show answer
Answer: 3
- 1 ❌: The gradient is a vector (a collection of partial derivatives)
- 2 ❌: The gradient points toward ascent; the negative gradient points toward descent
- 3 ✅: When , the point is a candidate for a local min, local max, or saddle point
- 4 ❌: The gradient is a directional vector and can have negative components
In the next post, we'll cover the actual mechanics of Gradient Descent, optimization in perceptrons and neural networks, and dive into Backpropagation and Newton's Method.
// Related Posts
Probability & Statistics in Practice: Inference Problems from the ML Trenches
From probability fundamentals and Bayes' theorem to distributions, MLE/MAP, confidence intervals, and hypothesis testing — a collection of practice problems grounded in real ML and data analysis scenarios.
Probability & Statistics Coding Assignments: Building ML Statistical Tools in Python
Bayesian updates, distribution simulation, CLT verification, MLE/MAP implementation, confidence intervals, hypothesis testing, and a full A/B test pipeline — implementing probability & statistics chapters 1–4 in code.
ML Probability & Statistics Chapter 4: Confidence Intervals and Hypothesis Testing
A complete guide to confidence intervals, the t-distribution, hypothesis testing fundamentals (null/alternative hypotheses, p-values, rejection regions, statistical power), various t-tests, and A/B testing.