0

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

https://i.stack.imgur.com/YjYhf.png

user235059
  • 21
  • 3
  • 1
    Where exactly is the recursive call? – razdi Mar 19 '19 at 23:12
  • Possible duplicate of [Computing Standard Deviation in a stream](https://stackoverflow.com/questions/5543651/computing-standard-deviation-in-a-stream) – razdi Mar 19 '19 at 23:23
  • @KaranRazdan The only similarities between this question and that question is that someone is using a standard deviation in python. It is not even remotely a duplicate of the question you linked. – DragonBobZ Mar 19 '19 at 23:32
  • @DragonBobZ The question links an image of what the user is trying to achieve. That image is talking about the online calculation of standard deviation, which is discussed in the linked post. – razdi Mar 19 '19 at 23:35
  • @KaranRazdan Do you think that copying and pasting a code snippet that bears little resemblance to what this user has already attempted is a good way for them to learn from their mistakes and understand what's wrong with their approach? – DragonBobZ Mar 19 '19 at 23:39
  • @DragonBobZ Not at all. That is the reason I asked for the recursive call as the first comment. I was supposed to point the user to the question rather than marking it as duplicate and hence I had retracted the flag as well. – razdi Mar 20 '19 at 00:09
  • I'm not convinced by the formula for std in the link. In particular, I think what they've written as sigma_k uses the difference of points from mu_(k+1) and so isn't actually sigma_k (I don't know what it is). – joel Mar 20 '19 at 00:25

0 Answers0