How to calculate gradient for this multivariable function

368 Views Asked by At

I'm trying to calculate the gradient of the following multi-variable function:

$$ f(m,b) = \frac{1}{n}\sum_{i=0}^n {(y_i-(mx_i + b))^2}$$

I want to use Chain rule as $${(y_i-(mx_i + b))^2}$$ is a composite function. I'd like to calculate $$\frac{df}{dm}$$ and $$\frac{df}{db}$$

Can somebody show me how to derive this? Thank you.

1

There are 1 best solutions below

1
On

Let's consider the i-th component of the sum:

$$f_i(m,b)=\frac{1}{n}{(y_i-(mx_i + b))^2}$$

thus:

$$\frac{\partial f_i}{\partial m}=\frac{2}{n}{(y_i-(mx_i + b))}(-x_i)$$

$$\frac{\partial f_i}{\partial b}=\frac{2}{n}{(y_i-(mx_i + b))}(-1)$$

and then adding up.

$$\frac{\partial f}{\partial m}= \frac{2}{n}\sum_{i=0}^n {-x_i(y_i-(mx_i + b))}$$

$$\frac{\partial f}{\partial b}= \frac{2}{n}\sum_{i=0}^n {-(y_i-(mx_i + b))}$$

NOTE

The functions to derive are in the form:

$$f(t)=[g(t)]^2\implies f'(t)=2g(t)g'(t)$$