Accueil Blog Projets CV Contact

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

Sidali Assoul Sidali Assoul 12 min de lecture

Dernière mise à jour le

Studying the Variation of Cost Function J(w)J(w)

We will focus on studying the variation of J(w)J(w) with respect to ww, by fixing the parameter bb to zero. That will allow us to later plot J(w)J(w) in a simple 2D graph.

So for b=0b=0, our model becomes fw(x)=wxf_{w}(x) = wx, and the cost function simplifies to:

J(w)=12mi=1m(wx(i)y(i))2J(w) = \frac{1}{2m} \sum_{i=1}^{m} (wx^{(i)} - y^{(i)} )^2

# 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 dJdw\frac{dJ}{dw}

Let’s take the derivative of J(w)J(w) with respect to w.

dJdw=1mi=1mx(i)(wx(i)y(i))\frac{dJ}{dw} = \frac{1}{m} \sum_{i=1}^{m} x^{(i)}(wx^{(i)} - y^{(i)} )

Which is equivalent to:

dJdw=1mi=1mx(i)(fw(x(i))y(i))\frac{dJ}{dw} = \frac{1}{m} \sum_{i=1}^{m} x^{(i)}(f_{w}(x^{(i)}) - y^{(i)} )
# 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.

Derivative of Cost Function

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 dJ(w)dw\frac{dJ(w)}{dw}, we pass the derivative operator inside the summation and apply the chain rule:

dJ(w)dw=12mi=1m2ddw(wx(i)y(i))Inner Derivative(wx(i)y(i))21\frac{dJ(w)}{dw} = \frac{1}{2m} \sum_{i=1}^{m} 2 \cdot \underbrace{\frac{d}{dw}(w x^{(i)} - y^{(i)})}_{\text{Inner Derivative}} \cdot (w x^{(i)} - y^{(i)})^{2-1}

Evaluating the inner derivative yields ddw(wx(i)y(i))=x(i)\frac{d}{dw}(w x^{(i)} - y^{(i)}) = x^{(i)}. Canceling the constants gives:

dJ(w)dw=12m2i=1mx(i)(wx(i)y(i))1\frac{dJ(w)}{dw} = \frac{1}{\cancel{2}m} \cdot \cancel{2} \sum_{i=1}^{m} x^{(i)} \cdot (w x^{(i)} - y^{(i)})^1

Which simplifies to our final form: dJ(w)dw=1mi=1mx(i)(wx(i)y(i))\frac{dJ(w)}{dw} = \frac{1}{m} \sum_{i=1}^{m} x^{(i)}(w x^{(i)} - y^{(i)})

To compute the previous derivative, the following rules were applied:

Sum Rule:

Basically the rule states that the derivative of a sum of terms u(w)u(w) is equivalent to the sum of derivatives dudw\frac{du}{dw}:

ddwi=1mu(w)=i=1mdu(w)dw\frac{d}{dw} \sum_{i=1}^{m} u(w) = \sum_{i=1}^{m} \frac{du(w)}{dw}

The Power Rule:

For a function G(u)G(u) defined with a standalone variable uu as G(u)=unG(u)=u^n, the exponent nn drops down to the front, and the original power on top of uu gets reduced by 11:

G(u)=un    dGdu=nun1G(u) = u^n \implies \frac{dG}{du} = n \cdot u^{n-1}

The Chain Rule:

Let’s say that G(u(w))G(u(w)) is a composite (or nested) function defined as:

G(u(w))=un(w)G(u(w))= u^n(w) wu(w)G(u(w))w \to u(w) \to G(u(w))

The derivative is calculated by multiplying the derivative of the outer function dGdG with respect to the inner function uu, by the derivative of the inner function uu with respect to its own variable ww:

ddw[G(u(w))]=dGdududw\frac{d}{dw}[G(u(w))] = \frac{dG}{du} \cdot \frac{du}{dw}

The Combined Form:

By applying both the chain rule then the power rule we get:

ddw[u(w)]n=n[u(w)]n1 Power function dGdudu(w)dwInner Functiondudw\frac{d}{dw} [u(w)]^n = \underbrace{n \cdot [u(w)]^{n-1}}_{\text{ Power function } \frac{dG}{du} } \cdot \underbrace{\frac{du(w)}{dw}}_{\text{Inner Function} \frac{du}{dw} }

Inner Derivative Evaluation:

In the expression u(w)=wx(i)y(i)u(w) = w x^{(i)} - y^{(i)}, we treat ww as the active variable as we are deriving with respect to it, while considering x(i)x^{(i)} and y(i)y^{(i)} as constants since they are totally independent from ww.

So deriving uu with respect to ww gives us:

du(w)dw=ddw(wx(i))ddw(y(i))=x(i)0=x(i)\frac{du(w)}{dw} = \frac{d}{dw}(w x^{(i)}) - \frac{d}{dw}(y^{(i)}) = x^{(i)} - 0 = x^{(i)}

ddw(y(i))=0\frac{d}{dw}(y^{(i)}) = 0 because the derivative of a constant is zero.

ddw(wx(i))=x(i)\frac{d}{dw}(wx^{(i)}) = x^{(i)} Because the rate of change of a constant multiplied by the variable ww is the constant itself:

ddw(C1w)=C1\frac{d}{dw}(C1 w ) = C1

Finding the Critical Point w*

To study the variation of the cost J(w)J(w) and find its minimum, we look for the critical point where the derivative equals zero dJ(w)dw=0\frac{dJ(w)}{dw} =0

Deriving the critical point w*, which nullifies the derivative of the cost J

dJ(w)dw=1mi=1mx(i)(wx(i)y(i))\frac{dJ(w)}{dw} = \frac{1}{m} \sum_{i=1}^{m} x^{(i)} \left( w x^{(i)} - y^{(i)} \right)

Setting the derivative to zero to find the critical point:

dJ(w)dw=0    i=1mx(i)(wx(i)y(i))=0\frac{dJ(w)}{dw} = 0 \implies \sum_{i=1}^{m} x^{(i)} \left( w x^{(i)} - y^{(i)} \right) = 0

Expanding the terms inside the summation:

    i=1mw(x(i))2i=1mx(i)y(i)=0    i=1mw(x(i))2=i=1mx(i)y(i)\implies \sum_{i=1}^{m} w \left( x^{(i)} \right)^2 - \sum_{i=1}^{m} x^{(i)} y^{(i)} = 0 \implies \sum_{i=1}^{m} w \left( x^{(i)} \right)^2 = \sum_{i=1}^{m} x^{(i)} y^{(i)}

Since ww is a constant factor relative to the summation index ii, we can pull it out of the sum:

dJ(w)dw=0    wi=1m(x(i))2=i=1mx(i)y(i)\frac{dJ(w)}{dw} = 0 \implies w \sum_{i=1}^{m} \left( x^{(i)} \right)^2 = \sum_{i=1}^{m} x^{(i)} y^{(i)}

After that we can divide by the (i=1m(x(i))2\sum_{i=1}^{m} \left( x^{(i)} \right)^2) term to isolate ww.

So the Critical point is: w where dJ(w)dw=0    w=i=1mx(i)y(i)i=1m(x(i))2\text{So the Critical point is: } w^* \text{ where } \frac{dJ(w^*)}{dw} = 0 \implies w^* = \frac{\sum_{i=1}^{m} x^{(i)} y^{(i)}}{\sum_{i=1}^{m} \left( x^{(i)} \right)^2}
# In NumPy:
def compute_w_star(x, y):
    return np.sum(x * y) / np.sum(x ** 2)

Variation Table of the Cost Function J(w)J(w)

Variation Table of the Cost function J(w)

ww50ww+50dJ(w)dw0+J(w50)J(w+50)J(w)J(w)\begin{array}{|c|ccc|} \hline w & w^* - 50 & w^* & w^* + 50 \\ \hline \frac{dJ(w)}{dw} & - & 0 & + \\ \hline & J(w^* - 50) & & J(w^* + 50) \\ J(w) & \searrow & & \nearrow \\ & & J(w^*) & \\ \hline \end{array} w=5.913J(w50)=27758.43J(w)=11.58J(w+50)=27758.43\begin{aligned} w^* &= 5.913\dots \\ J(w^* - 50) &= 27758.43\dots \\ J(w^*) &= 11.58\dots \\ J(w^* + 50) &= 27758.43\dots \end{aligned}

As presented in the figure and table above, when ww varies from w50w^* - 50 to w+50w^* + 50, the derivative dJdw\frac{dJ}{dw} is negative for values of ww between w50w^* - 50 and ww^*.

It becomes zero at the critical point ww^*, and then becomes positive after that.

The variation of J(w)J(w) directly corresponds to the sign of its derivative: when the derivative is negative on a specific interval, J(w)J(w) is decreasing, and when it is positive, J(w)J(w) is increasing.

Critical Point equation is of the form Aw - B = 0

As we know from calculus, an equation of the form AwB=0Aw - B = 0 is equal to zero when w=B/Aw^* = B/A. The sign of AwBAw - B depends entirely on the sign of the coefficient AA.

dJ(w)dw=0    wi=1m(x(i))2i=1mx(i)y(i)=0\frac{dJ(w)}{dw} = 0 \implies w\sum_{i=1}^{m} \left( x^{(i)} \right)^2 - \sum_{i=1}^{m} x^{(i)} y^{(i)} = 0

The previous equation is of the form AwB=0Aw - B = 0, where:

A=i=1m(x(i))2;B=i=1mx(i)y(i);w=BAA = \sum_{i=1}^{m} (x^{(i)})^2 \quad ; B = \sum_{i=1}^{m} x^{(i)} y^{(i)} ; \quad w^* = \frac{B}{A}

Because AA is a sum of squared terms, it is strictly positive (A>0A > 0). When the leading coefficient AA is positive, the sign of the expression is determined directly by comparing ww to the critical root ww^*:

A>0    {AwB<0when w<wAwB=0when w=wAwB>0when w>wA > 0 \implies \begin{cases} Aw - B < 0 & \text{when } w < w^* \\ Aw - B = 0 & \text{when } w = w^* \\ Aw - B > 0 & \text{when } w > w^* \end{cases}

The variation of J(w)J(w) directly corresponds to the sign of its derivative: when the derivative is negative on a specific interval, J(w)J(w) is decreasing, and when it is positive, J(w)J(w) is increasing.

Proving Mathematically that J(w)J(w) has a Convex Graph

As we’ve seen in the variation table, the graph of the cost function J(w)J(w) 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 P1P_1 and P2P_2, 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 J(w)J(w) is a convex function, we need to calculate the second derivative d2J(w)d2w\frac{d^{2}J(w)}{d^{2}w}:

dJ(w)dw=1mi=1mx(i)(wx(i)y(i))\frac{dJ(w)}{dw} = \frac{1}{m} \sum_{i=1}^{m} x^{(i)} ( wx^{(i)} - y^{(i)} ) d2J(w)d2w=1mi=1m(x(i))2=A\frac{d^{2}J(w)}{d^{2}w} = \frac{1}{m} \sum_{i=1}^{m} (x^{(i)})^2 = A

The second derivative of J(w)J(w) happens to equal AA, the leading coefficient of the linear equation we derived when studying the first derivative.

Since AA is the sum of the squares of every input example x(i)x^{(i)}, which is totally independent of ww, then A=d2J(w)d2wA = \frac{d^{2}J(w)}{d^{2}w} is always strictly positive.

This means that the first derivative dJ(w)dw\frac{dJ(w)}{dw}, which represents the slope of every point (w,J(w))(w,J(w)), is always increasing.

It starts with an increasing negative slope when w<ww < w^*, nullifies and intersects with the horizontal axis at w=ww = w^* (the critical point), and then keeps increasing with a positive slope for all values of w>ww > w^*.

Variation Table of the Second derivative of the cost

The fact that d2J(w)d2w\frac{d^{2}J(w)}{d^{2}w} is positive for all inputs ww, and dJ(w)dw\frac{dJ(w)}{dw} is equal to zero at point (w,J(w))(w^*,J(w^*)) and changes its sign, mathematically proves that J(w)J(w) is convex and that the point (w,J(w))(w^*,J(w^*)) is its global, and only, minimum.

The left panel shows the J(w)J(w) parabola on the Student Marks dataset, with the minimum marked at ww^*. The right panel confirms the derivative dJdw\frac{dJ}{dw} crosses zero exactly at that point.

J(w) parabola and its derivative dJ/dw plotted on the Student Marks dataset

Studying the Variations of the Cost Function J(b)J(b)

Now, let’s suppose that ww is equal to zero.

The model fw,b(x)f_{w,b}(x) becomes fb(x)=bf_{b}(x) = b.

And the cost function simplifies to: J(b)=12mi=1m(by(i))2J(b) = \frac{1}{2m} \sum_{i=1}^{m} (b - y^{(i)} )^2

# 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 J(b)J(b), using the same calculus rules as before.

 Derivative of Cost Function J(b)

dJ(b)db=1mi=1m(by(i))\frac{dJ(b)}{db} = \frac{1}{m} \sum_{i=1}^{m} (b - y^{(i)} )

# 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 J(b)J(b), we need to find its critical point, the point (b,J(b))(b,J(b)) where the derivative dJdb=0\frac{dJ}{db}=0. Following the same derivation steps as before, we find that the critical point bb^* is actually the mean yˉ\bar{y} of the target variable yy:

b=yˉ=1mi=1my(i)b^* = \bar{y} = \frac{1}{m} \sum_{i=1}^{m} y^{(i)}

# In NumPy:
def compute_b_star(y):
    return np.mean(y)

Critical Point b* of the cost function J(b)

Variation Table of the Cost Function J(b)J(b)

Variation Table of Cost Function J(b)

bb50bb+50dJ(b)db0+J(b50)J(b+50)J(b)J(b)\begin{array}{|c|ccc|} \hline b & b^* - 50 & b^* & b^* + 50 \\ \hline \frac{dJ(b)}{db} & - & 0 & + \\ \hline & J(b^* - 50) & & J(b^* + 50) \\ J(b) & \searrow & & \nearrow \\ & & J(b^*) & \\ \hline \end{array} b24.4J(b50)1351.6J(b)101.6J(b+50)1351.6\begin{aligned} b^* &\approx 24.4 \\ J(b^* - 50) &\approx 1351.6 \\ J(b^*) &\approx 101.6 \\ J(b^* + 50) &\approx 1351.6 \end{aligned}

As presented in the figure and table above, when bb varies from b50b^* - 50 to b+50b^* + 50, the derivative dJdb\frac{dJ}{db} is negative for values of bb between b50b^* - 50 and bb^*. It becomes zero at the critical point bb^*, and then becomes positive after that.

The variation of J(b)J(b) directly corresponds to the sign of its derivative. When the derivative is negative on a specific interval, J(b)J(b) is decreasing, and when it is positive, J(b)J(b) is increasing.

Critical Point equation is of the form Ab - B = 0

As we know from calculus, an equation of the form AbB=0Ab - B = 0 is equal to zero when b=B/Ab^* = B/A. The sign of AbBAb - B depends entirely on the sign of the coefficient AA.

dJ(b)db=0    mbi=1my(i)=0\frac{dJ(b)}{db} = 0 \implies m \cdot b - \sum_{i=1}^{m} y^{(i)} = 0

The previous equation is of the form AbB=0Ab - B = 0, where:

A=m;B=i=1my(i);b=BAA = m \quad ; B = \sum_{i=1}^{m} y^{(i)} ; \quad b^* = \frac{B}{A}

Because AA represents the number of training examples mm, it is strictly positive (A>0A > 0). When the leading coefficient AA is positive, the sign of the expression is determined directly by comparing bb to the critical root bb^*:

A>0    {AbB<0when b<bAbB=0when b=bAbB>0when b>bA > 0 \implies \begin{cases} Ab - B < 0 & \text{when } b < b^* \\ Ab - B = 0 & \text{when } b = b^* \\ Ab - B > 0 & \text{when } b > b^* \end{cases}

Proving Mathematically that J(b)J(b) has a Convex Graph

As we did with J(w)J(w), let’s also prove that J(b)J(b) is convex following the same steps.

The first step is to calculate the second derivative d2J(b)db2\frac{d^{2}J(b)}{db^{2}}:

dJ(b)db=1mi=1m(by(i))\frac{dJ(b)}{db} = \frac{1}{m} \sum_{i=1}^{m} ( b - y^{(i)} ) d2J(b)db2=1mi=1m(1)=1mm=1\frac{d^{2}J(b)}{db^{2}} = \frac{1}{m} \sum_{i=1}^{m} (1) = \frac{1}{m} \cdot m = 1

Unlike the study of J(w)J(w) where the second derivative matched the linear coefficient AA, 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 bb and the dataset values.

This means that the first derivative dJ(b)db\frac{dJ(b)}{db}, which represents the slope of every point (b,J(b))(b,J(b)), is always increasing.

It starts with an increasing negative slope when b<bb < b^*, nullifies and intersects with the horizontal axis at b=bb = b^* (the critical point), and then keeps increasing with a positive slope for all values of b>bb > b^*.

Variation Table of the Second derivative of the cost J(b)

The fact that d2J(b)db2\frac{d^{2}J(b)}{db^{2}} is positive for all inputs bb, and dJ(b)db\frac{dJ(b)}{db} is equal to zero at point (b,J(b))(b^*,J(b^*)) and changes its sign, also mathematically proves that J(b)J(b) is convex, and that the point (b,J(b))(b^*,J(b^*)) is its global, and only, minimum.

The left panel shows the J(b)J(b) parabola on the Student Marks dataset, with the minimum at b=yˉ24.4b^* = \bar{y} \approx 24.4. The right panel confirms the derivative crosses zero at that point.

J(b) parabola and its derivative dJ/db plotted on the Student Marks dataset

Conclusion

In this article we’ve studied and proved that both J(w)J(w) and J(b)J(b) form a convex 2D bowl with a single and guaranteed global minimum (w,J(w))(w^*,J(w^*)) and (b,J(b))(b^*,J(b^*)) respectively.

In the first part we set b=0b=0 to only study how JJ varies with respect to ww.

And in the second onewe set w=0w=0 to only study how JJ varies with respect to bb.

But we never studied the full picture of it, when both ww and bb vary simultaneously.

In other words, we studied J(w)J(w) and J(b)J(b) but we’ve never actually studied the full cost function J(w,b)J(w,b).

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.

Se connecter sur LinkedIn
Sidali Assoul

Écrit par

Sidali Assoul

Ingénieur logiciel avec 5 ans d'expérience en développement web et mobile full stack, un bilan éprouvé en data science, recherche et solutions IA modernes, avec une approche orientée business pour livrer des produits à forte valeur ajoutée.