I'm trying to calculate the total sum of squares using python. I know that the formula of TSS is: [enter image description here][1]
I created a code to do it:
from statistics import mean
x = ([3,1,3,1,3,13])
def tss(a):
m = mean(a)
for i in a:
i += ((i-m)**2)
return (i)
print(tss(x))
The problem is: Its keeping returning me 94, but i know that the correct answer is 102. I don't have a clue of what i did wrong. Can anybody help me? [1]: https://i.stack.imgur.com/Alx6r.png
i
is resetting each time it goes through the loop. So on the last loop your function erases all the previous sums, setsi
to 13, then adds the square of the difference between 13 and the mean toi
(which is now 13), returning 94. You need a different variable to track the sum, so it doesn't get lost each loop. You want: