Cet article n'est pas encore disponible en français. Vous lisez la version anglaise.
Derivatives, Critical Points, and Convexity of the Linear Regression Cost Function
Dernière mise à jour le
Studying the Variation of Cost Function
We will focus on studying the variation of with respect to , by fixing the parameter to zero. That will allow us to later plot in a simple 2D graph.
So for , our model becomes , and the cost function simplifies to:
# In NumPy (b=0):
def compute_cost_jw(x, y, w):
m = len(y)
return (1 / (2 * m)) * np.sum((w * x - y) ** 2)
Computing the Derivative of the Cost Function
Let’s take the derivative of with respect to w.
Which is equivalent to:
# In NumPy:
def compute_gradient_jw(x, y, w):
m = len(y)
return (1 / m) * np.sum(x * (w * x - y))
The derivation steps and the used calculus rules are described in detail in the following figure.

Step-by-Step Derivation Walkthrough (Optional)
Read this section if the previous figure wasn’t enough for you to understand the whole derivation process with every calculus rule explained in detail.
To find , we pass the derivative operator inside the summation and apply the chain rule:
Evaluating the inner derivative yields . Canceling the constants gives:
Which simplifies to our final form:
To compute the previous derivative, the following rules were applied:
Sum Rule:
Basically the rule states that the derivative of a sum of terms is equivalent to the sum of derivatives :
The Power Rule:
For a function defined with a standalone variable as , the exponent drops down to the front, and the original power on top of gets reduced by :
The Chain Rule:
Let’s say that is a composite (or nested) function defined as:
The derivative is calculated by multiplying the derivative of the outer function with respect to the inner function , by the derivative of the inner function with respect to its own variable :
The Combined Form:
By applying both the chain rule then the power rule we get:
Inner Derivative Evaluation:
In the expression , we treat as the active variable as we are deriving with respect to it, while considering and as constants since they are totally independent from .
So deriving with respect to gives us:
because the derivative of a constant is zero.
Because the rate of change of a constant multiplied by the variable is the constant itself:
Finding the Critical Point w*
To study the variation of the cost and find its minimum, we look for the critical point where the derivative equals zero

Setting the derivative to zero to find the critical point:
Expanding the terms inside the summation:
Since is a constant factor relative to the summation index , we can pull it out of the sum:
After that we can divide by the () term to isolate .
# In NumPy:
def compute_w_star(x, y):
return np.sum(x * y) / np.sum(x ** 2)
Variation Table of the Cost Function

As presented in the figure and table above, when varies from to , the derivative is negative for values of between and .
It becomes zero at the critical point , and then becomes positive after that.
The variation of directly corresponds to the sign of its derivative: when the derivative is negative on a specific interval, is decreasing, and when it is positive, is increasing.

As we know from calculus, an equation of the form is equal to zero when . The sign of depends entirely on the sign of the coefficient .
The previous equation is of the form , where:
Because is a sum of squared terms, it is strictly positive (). When the leading coefficient is positive, the sign of the expression is determined directly by comparing to the critical root :
The variation of directly corresponds to the sign of its derivative: when the derivative is negative on a specific interval, is decreasing, and when it is positive, is increasing.
Proving Mathematically that has a Convex Graph
As we’ve seen in the variation table, the graph of the cost function forms a convex shape, or a ramen soup bowl shape if you like.
A convex graph in math is defined as follows: given two points belonging to the same graph and , if we draw a line between them, it will always be either totally above the graph or totally below it.
In order to totally prove that is a convex function, we need to calculate the second derivative :
The second derivative of happens to equal , the leading coefficient of the linear equation we derived when studying the first derivative.
Since is the sum of the squares of every input example , which is totally independent of , then is always strictly positive.
This means that the first derivative , which represents the slope of every point , is always increasing.
It starts with an increasing negative slope when , nullifies and intersects with the horizontal axis at (the critical point), and then keeps increasing with a positive slope for all values of .

The fact that is positive for all inputs , and is equal to zero at point and changes its sign, mathematically proves that is convex and that the point is its global, and only, minimum.
The left panel shows the parabola on the Student Marks dataset, with the minimum marked at . The right panel confirms the derivative crosses zero exactly at that point.

Studying the Variations of the Cost Function
Now, let’s suppose that is equal to zero.
The model becomes .
And the cost function simplifies to:
# In NumPy (w=0):
def compute_cost_jb(x, y, b):
m = len(y)
return (1 / (2 * m)) * np.sum((b - y) ** 2)
Let’s quickly calculate the derivative of , using the same calculus rules as before.

# In NumPy:
def compute_gradient_jb(x, y, b):
m = len(y)
return (1 / m) * np.sum(b - y)
Finding the Critical Point b*
To study the variation of , we need to find its critical point, the point where the derivative . Following the same derivation steps as before, we find that the critical point is actually the mean of the target variable :
# In NumPy:
def compute_b_star(y):
return np.mean(y)

Variation Table of the Cost Function

As presented in the figure and table above, when varies from to , the derivative is negative for values of between and . It becomes zero at the critical point , and then becomes positive after that.
The variation of directly corresponds to the sign of its derivative. When the derivative is negative on a specific interval, is decreasing, and when it is positive, is increasing.

As we know from calculus, an equation of the form is equal to zero when . The sign of depends entirely on the sign of the coefficient .
The previous equation is of the form , where:
Because represents the number of training examples , it is strictly positive (). When the leading coefficient is positive, the sign of the expression is determined directly by comparing to the critical root :
Proving Mathematically that has a Convex Graph
As we did with , let’s also prove that is convex following the same steps.
The first step is to calculate the second derivative :
Unlike the study of where the second derivative matched the linear coefficient , here the second derivative is a constant equal to 1.
Since 1 is strictly positive, the second derivative is always positive and entirely independent of both and the dataset values.
This means that the first derivative , which represents the slope of every point , is always increasing.
It starts with an increasing negative slope when , nullifies and intersects with the horizontal axis at (the critical point), and then keeps increasing with a positive slope for all values of .

The fact that is positive for all inputs , and is equal to zero at point and changes its sign, also mathematically proves that is convex, and that the point is its global, and only, minimum.
The left panel shows the parabola on the Student Marks dataset, with the minimum at . The right panel confirms the derivative crosses zero at that point.

Conclusion
In this article we’ve studied and proved that both and form a convex 2D bowl with a single and guaranteed global minimum and respectively.
In the first part we set to only study how varies with respect to .
And in the second onewe set to only study how varies with respect to .
But we never studied the full picture of it, when both and vary simultaneously.
In other words, we studied and but we’ve never actually studied the full cost function .
In the next article we will do that, and even scale the solution to any number of input features using the normal equation, and finally we will introduce the most important algorithm in machine learning “Gradient Descent”.Read Part 3: Gradient Descent Explained →
Just joining the series? Start from the beginning: Part 1: Linear Regression From Scratch
Cet article vous a plu ?
Je suis ouvert à de nouveaux postes, en télétravail ou à l'international. Si quelque chose vous a parlé ou si vous souhaitez collaborer, j'adorerais en discuter.