Cumulative standard deviation: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 2,296:
New data 7 so S.D. now = 1.399708
New data 9 so S.D. now = 2.000000
</pre>
 
=={{header|Lobster}}==
<lang Lobster>
// Stats computes a running mean and variance
// See Knuth TAOCP vol 2, 3rd edition, page 232
 
class Stats:
M = 0.0
S = 0.0
n = 0
def incl(x):
n += 1
if n == 1:
M = x
else:
let mm = (x - M)
M += mm / n
S += mm * (x - M)
def mean(): return M
//def variance(): return (if n > 1.0: S / (n - 1.0) else: 0.0) // Bessel's correction
def variance(): return (if n > 0.0: S / n else: 0.0)
def stddev(): return sqrt(variance())
def count(): return n
def test_stdv() -> float:
let v = [2,4,4,4,5,5,7,9]
let s = Stats {}
for(v) x: s.incl(x+0.0)
print concat_string(["Mean: ", string(s.mean()), ", Std.Deviation: ", string(s.stddev())], "")
 
test_stdv()
</lang>
{{out}}
<pre>
Mean: 5.0, Std.Deviation: 2.0
</pre>
 
E
22

edits