I'm attempting to implement a recursive standard deviation function in Python. I believe this is the correct formula; however, I consistently get different answers compared to the built-in numpy std.
def standard_deviation(l):
std = 0
mean = 0
for i in range(len(l)):
mean = (i * mean + l[i]) / (i + 1)
std = math.sqrt( ( (i * std ** 2) + (l[i] - mean) ** 2 )
/ (i + 1))
return std