Here is how the algorithm is implemented in the code:
There are two things which are worth being mentioned:
First,
is given background subtracted, so it is unnecessary to subtract the background again.
Second, in some certain circumstances,
can be be negative. In that case
could be equal to 0, and that's pretty bad.
To avoid such problem, the idea is to make sure
is positive, or to put a condition like: if
then
.
The errors square
are calculated the same way, except that the weight are squared before being applied: