Standard Deviation
In statistics and probability theory, the standard deviation (SD) (represented by the Greek letter sigma, σ) measures the amount of variation or dispersion from the average.
... yup, straight from Wikipedia.
To fully understand this, lets look at the formula, then some code for it.
So, breaking it down, the Standard Deviation is a measure of how spread out numbers are. It is the square root of the Variance. Variance is just the squared differences from the Mean... Yes, the mean is just a fancy way of saying average.
So, let's code this!!!
def stdev(s): import math def average(s): return sum(s) * (1.0 / len(s)) avg = average(s) variance = map(lambda x: (x - avg)**2, s) standard_deviation = squareroot(average(variance)) return ("The mean is, ", avg, ". The Standard Deviation is ", standard_deviation)
Like we Said in previous posts, we try to stay away from Python built in functions(the math libraries at least). So, Let's code the Square Root function!!!
def goodenough(g,dvalue): d=abs(g**2-dvalue) return (d<0.0001) def average(d,e): return (d+e)/2.0 def improve(g,dvalue): return average(g,dvalue/g) def improveloop(g,dvalue): if (goodenough(g,dvalue)): return g else: return improveloop(improve(g,dvalue),dvalue) def squareroot(f): return improveloop(1.0,f)
Test in Python2.7.2! I'll soon update it to be compatible with Python 3.*












