Mean squared error minimization

82 Views Asked by At

I'm studying machine learning right now and I have find to following exercise:

We define the mean squared error of a number $x \in \mathbb{R}$ , where $a_{1}...,a_{n} \in \mathbb{R}$ $$f(x)= \frac{1}{n}\sum_{i=1}^{n}(x-a_{i})^2$$

For which $x \in \mathbb{R}$ is the MSE minimal?

I guess that I should compute the first derivative of $f(x)$ and to check it for extrema and to make a curve discussion but I'm already struggling in the very beginning how to find $f(x)'$. Thank you in advance.

1

There are 1 best solutions below

2
On BEST ANSWER

Given function: $$f(x)=\frac{1}{n}\left(x-a_1\right)^2+\frac{1}{n}\left(x-a_2\right)^2+...+\frac{1}{n}\left(x-a_n\right)^2$$ Now, $$f'(x)=\frac{\partial}{\partial x}\left(\frac{1}{n}\left(x-a_1\right)^2+\frac{1}{n}\left(x-a_2\right)^2+...+\frac{1}{n}\left(x-a_n\right)^2\right)$$

$$\Rightarrow f'(x)=\frac{\partial}{\partial x}\left(\frac{1}{n}\left(x-a_1\right)^2\right)+\frac{\partial}{\partial x}\left(\frac{1}{n}\left(x-a_2\right)^2\right)+...+\frac{\partial}{\partial x}\left(\frac{1}{n}\left(x-a_n\right)^2\right)$$

$$\Rightarrow f'(x)=\frac{1}{n}\left(2\left(x-a_1\right)\right)+\frac{1}{n}\left(2\left(x-a_2\right)\right)+...+\frac{1}{n}\left(2\left(x-a_n\right)\right)$$

$$\Rightarrow f'(x)=\left(\frac{2x}{n}-\frac{2a_1}{n}\right)+\left(\frac{2x}{n}-\frac{2a_2}{n}\right)+...+\left(\frac{2x}{n}-\frac{2a_n}{n}\right)$$

$$\Rightarrow f'(x)=n\left(\frac{2x}{n}\right)-\left(\frac{2a_1}{n}+\frac{2a_2}{n}+...+\frac{2a_n}{n}\right)$$

$$\Rightarrow f'(x)=2x-\frac{2}{n}(a_1+a_2+...+a_n)$$.

Setting $f'(x)=0$ gives $2x-\frac{2}{n}(a_1+a_2+...+a_n)=0$, which gives $x=\frac{1}{n}(a_1+a_2+...+a_n)$.

$f''(x)=\frac{\partial}{\partial x}\left(f'(x)\right)=2>0$ for all values of $x$.

Hence, $x=\frac{1}{n}(a_1+a_2+...+a_n)$ is the point at which $f(x)$ is minimum.