Search code examples
pythonsumcalculustss

Total Sum of Squares (TSS) in python


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.sstatic.net/Alx6r.png


Solution

  • i is resetting each time it goes through the loop. So on the last loop your function erases all the previous sums, sets i to 13, then adds the square of the difference between 13 and the mean to i (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:

    from statistics import mean
    
    x = ([3,1,3,1,3,13])
    
    def tss(a):
        m = mean(a)
        n = 0
        for i in a:
            n += ((i-m)**2)
        return (n)
    
    print(tss(x))
    '''
    
    @mateen's answer is more pythonic and will perform better than a loop, but I don't think you'll get the understanding from it. Welcome to python!