I am reading calculating variance and see a formula variance = sum( (vals - mean(vals))**2 ) implemented in Python like:
def variance(values, mean):
return sum([(x-mean)**2 for x in values])
I used this implementation and also walked through it step by step in a notebook and got the same answer. However, when I used <np.array>.var(), I got a different result. Is the numpy implementation different? Is the function stated earlier incorrect?
With a randomly generated 4-element array nums like:
array([[0.10193815],
[0.99458125],
[0.57937386],
[0.27331835]])
nums.var() = 0.11502594279263058
variance(nums, nums.mean()) = 0.4601037711705223