It took a while, but I got it to work. Again there was a nod in the direction of the internet (mostly here), the source of all my knowledge. I'll just throw up the code as it ought to be fairly self explanatory, but then I always say that.
<><><>
def make_av():
def std_dev(value):
from math import sqrt
std_dev.tot += 1 # Calculate necessary values
std_dev.sum += value
std_dev.sq_sum += (value * value)
if std_dev.tot < 2: # Cant have sd with less than 2 packets
return 0.0
# Return a tuple of the running mean followed by the running standard deviation
return (std_dev.sum / std_dev.tot, sqrt(abs((std_dev.tot * std_dev.sq_sum - std_dev.sum**2) / (std_dev.tot *(std_dev.tot-1)))))
std_dev.tot, std_dev.sum, std_dev.sq_sum = [0.0]*3 # Reset variables
return (std_dev)
<><><>
So you create an instance of make_av() and input the value you wish to get the averages of. The function returns a tuple of the mean and standard deviation. Done
No comments:
Post a Comment