Why does 2/n * sigma convert to 2*error/n in python

I’m just trying to work out the raionale for the conversion from the formula to the python code:

Why does this convert to:
deriv = 2*error/len_data
rather than
(2/len_data) * error

Apologies if this is obvious.

Hi @bullchris2,

First, you don’t ever need to apologize for asking questions when you are learning. That’s just how anybody learns. :blush:

In the MSE derivative formula, let’s just replace the summation of errors with a notation J. So the derivative formula can be written as \frac{2}n\times{J} , which is also the equivalent of \frac{2\times{J} }n. That’s why it converts to the python code 2*error/len_data rather than (2/len_data) * error.

I hope this helps, but honestly, I’m not sure which step tripped you. Let me know if you have further questions.

Honestly, it’s just the algebra, it’s been a while. I guess I’m still not sure why they’re equivalent? Sorry, I really feel like I’m being thick, but there’s definitely a multiplication algebra rule I’ve forgotton.

Again, no need to apologize. I really admire you for not afraid to admit you don’t know something. That’s very brave.

If it’s algebra you need a refresher on, I recommend Khan Academy, they have free quality courses on Math.

For this particular question, I will break it down step by step. If you still don’t get it, it would be helpful to point out which step is the one you tripped on:

  • Step 1: For our convenience, let’s replace \sum_{i=1}^{n}{a_{0}+a_{1}x_{1}^{i} - y^{i}} with a notation J. Basically we are assigning the result of the summation to a variable J.
  • Step 2: Rewrite the derivative formula with notation J. So that:
    \frac{d}{da_{0}}MSE(a_{0}, a_{1}) = \frac{2}{n}\times{J} .
  • Step 3 :\frac{d}{da_{0}}MSE(a_{0}, a_{1}) = \frac{2}{n}\times{J} = 2\times\frac{1}{n}\times{J} = \frac{2\times{J}}{n} .
  • Step 4: Convert the formula to python code: deriv = 2*J/n, in our case J is error.

Hope this helps and I didn’t offend you by accident.