Bag.st
branchjv
changeset 18071 009cf668b0ed
parent 18070 d262e3aecaca
parent 15457 af0513df339f
child 18091 abbcac10730e
--- a/Bag.st	Mon Jul 01 22:14:20 2013 +0100
+++ b/Bag.st	Tue Jul 09 22:51:30 2013 +0100
@@ -427,6 +427,30 @@
     ^ count
 ! !
 
+!Bag methodsFor:'statistical functions'!
+
+variance
+    "compute the variance over a complete data set (and not of a sample)"
+
+    |m sz sumDeltaSquares|
+
+    m := self arithmeticMean.
+    sumDeltaSquares := 0.
+    sz := 0.
+    self 
+        valuesAndCountsDo:[:val :count |
+            sumDeltaSquares :=  sumDeltaSquares + ((val - m) squared).
+            sz := sz + 1.
+        ].
+
+    ^ sumDeltaSquares / sz
+
+    "
+     TestCase assert:( #(1 1 1 2 2 2 1 1 1 2 2 2) asBag variance = #(1 1 1 2 2 2 1 1 1 2 2 2) variance).
+     TestCase assert:( #(1 1 1 1 1 0 0 0 0 0) asBag variance = #(1 1 1 1 1 0 0 0 0 0) variance).
+    "
+! !
+
 !Bag methodsFor:'testing'!
 
 includes:anObject
@@ -450,10 +474,10 @@
 !Bag class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.46 2013-07-04 15:46:20 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.46 2013-07-04 15:46:20 stefan Exp $'
 ! !