Bag.st
changeset 15457 af0513df339f
parent 15429 087ad8e8da0c
child 15686 2efc686ef383
child 18071 009cf668b0ed
--- a/Bag.st	Thu Jul 04 17:45:33 2013 +0200
+++ b/Bag.st	Thu Jul 04 17:46:20 2013 +0200
@@ -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 $'
 ! !