Simplfy variance of an image Today we learned variance of an image in my class. Its formula like below:
$\sigma^2 = \frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}(f(x,y)-m)^2$
$m = \frac{1}{M \cdot N} \sum\limits_{x = 0}^{M-1}\sum\limits_{y=0}^{N-1}f(x,y)$
if we expand variance formula,
$\sigma = \frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}f(x,y)^2 +  \frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}-2\cdot m \cdot f(x,y) + \frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}m^2$
we can replace $\frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}f(x,y)^2$ with $M \cdot N \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}\left(\frac{f(x,y)}{M \cdot N}\right)^2$ and it is $M\cdot N \cdot m^2 $.
Also we can say same things for second term $\frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}-2 \cdot m \cdot f(x,y) = -2\cdot m \cdot m$
and for last term $\frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}m^2 = m^2$
if we arrange all equations:
$\sigma = M\cdot N \cdot m^2 - 2m^2 +m^2$
$\sigma = m^2 \cdot(M \cdot N - 1 )$
result should be like I indicated I hope.
Teacher gave us an example and tried whether it works or not and it does not work. Here the example:
$$
f(x,y) = 
\begin{bmatrix}
    0       & 0 & 1 & 1 & 2  \\
    1       &2 & 3 & 0 & 1 \\
    3       & 3 & 2 & 2 & 0 \\
    2       & 3 & 1 & 0 & 0 \\
    1       &1 & 3 & 2 & 2 
 \end{bmatrix}
$$
its mean value $m = 1.44 $ and variance $\sigma = 1.1264$ I calculated them with python.
But when I use my formula, I found $\sigma = 49.76 $
I think I did everything correct but it does not work. Can you see my mistake ?
Note: Teacher gave us first equation but I wanted to simplfy it.
 A: 
we can replace $\frac{1}{M \cdot N} \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}f(x,y)^2$ with $M \cdot N \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}(\frac{f(x,y)}{M \cdot N})^2$ and it is $M\cdot N \cdot m^2 $

No, you cannot. In general, $a^2+b^2$ is not the same as $(a+b)^2$.
In particular, that means
$$MNm^2 = MN\left(\sum_{x=0}^{M-1}\sum_{y=0}^{N-1}\frac{f(x,y)}{M \cdot N}\right)^2\neq M \cdot N \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}\left(\frac{f(x,y)}{M \cdot N}\right)^2.$$
The sum of square is not the same as the square of sums.

Also, here is a way to see, immediatelly, that something must be wrong with your equation: apply it on a very simple case.
Take $M=1, N=2$, and set $f(0,0)=f(0,1)=1$.
Then, applying the original equations, you get
$$m=\frac12(1+1)=1\\
\sigma = \frac12((1-1)^2+(1-1)^2) = 0$$
but if you apply your derived equations, you get
$$\sigma = m^2(MN-1)=1^2(1\cdot 2 - 1) = 1\neq 0.$$
So clearly, something went wrong. Now, if you want to find what is wrong in the derivation, you can use this same very simple example. If you plug in the concrete numbers, the equality will have to break at a step - the step where you made a mistake.
In your case, you can see that the step I highlight above is precisely the step where you made the mistake, and where the simple case also breaks. In particular:
$$M \cdot N \cdot \sum\limits_{x=0}^{M-1}\sum\limits_{y=0}^{N-1}(\frac{f(x,y)}{M \cdot N})^2 = 1\cdot 2\cdot\left(\left(\frac1{2\cdot 1}\right)^2+\left(\frac1{2\cdot 1}\right)^2\right)= 1$$
while
$$M\cdot N \cdot m^2 = 1\cdot 2 \cdot 1^2 = 2$$
so our simple case does in fact show where you went wrong.

A second way to see immediately that your result is wrong is slightly more involved in that it needs a little more experience and intuition (which stems, always, from experience). The term variance is used in mathematics almost exclusively to describe how some mathematical object (random variable, function, sample, whatever) deviates from its average value. In particular, the common property of a variance is the following:

If you shift an object (i.e., change all its values by a fixed number), then the variance should not change.

You can see this in your definition of variance. If you replace the function $f$ with $g$, defined as $g(x,y)=f(x,y)+C$ for some constant $C$, then $m$ for the function $g$ will also change by $C$, so $m_g = m_f+C$. But then, in the equation for variance, the inner summands will be the same, because $g(x,y)-m_g = f(x,y)+C-(m_f+C)=f(x,y)-m_f+C-C=f(x,y)-m_f$. So, the variance of $g$ will be the same as the variance of $f$.
But your final equation for $\sigma$, which states that $\sigma=m^2(MN-1)$, does not have this property. If I replace $f$ with $g$, the value $m$ changes, and thus, the value $m^2(MN-1)$ also changes, but the value of $\sigma$ should not change.
