Collection.st
branchjv
changeset 17841 7abcc4aef871
parent 17834 04ff72c5039a
child 17845 7e0cfaac936d
--- a/Collection.st	Fri Jun 03 18:15:16 2011 +0100
+++ b/Collection.st	Wed Jun 08 22:53:07 2011 +0100
@@ -259,6 +259,7 @@
     ^ self withSize:n
 ! !
 
+
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -479,6 +480,7 @@
     ].
 ! !
 
+
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -3039,22 +3041,51 @@
 
 !Collection methodsFor:'queries'!
 
+arithmeticMean
+    "arithmetic mean value of all elements in the collection"
+
+    ^ self sum / self size
+
+    "
+     TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5).
+     TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4).
+     TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431).
+    "
+
+    "Created: / 13-04-2011 / 16:52:32 / cg"
+!
+
 average
     "average value of all elements in the collection"
 
-    ^ self sum / self size
+    ^ self arithmeticMean
 
     "
      TestCase assert:( { 1. 2. 3. 4 } average = 2.5).
     "
 
     "Created: / 18-03-2011 / 10:31:04 / cg"
+    "Modified: / 13-04-2011 / 17:09:21 / cg"
 !
 
 defaultElement
     ^  nil
 !
 
+geometricMean
+    "geometric mean value of all elements in the collection"
+
+    ^ self product raisedTo:(1/self size)
+
+    "
+     TestCase assert:( { 1. 2. 3. 4. } geometricMean closeTo: 2.21336).
+     TestCase assert:( { 1. 2. 3. 4. 5 } geometricMean closeTo: 2.60517).
+     TestCase assert:( { 13. 23. 12. 44. 55 } geometricMean closeTo: 24.41932).
+    "
+
+    "Created: / 13-04-2011 / 16:53:44 / cg"
+!
+
 largest:n
     "return the n largest elements"
 
@@ -3443,6 +3474,24 @@
       Used by functions which create a growing collection (see collect:with:, for example)"
 
     ^ self species
+!
+
+standardDeviation
+    "standard deviation value of all elements in the collection"
+
+    |m sumDeltaSquares|
+
+    m := self arithmeticMean.
+    sumDeltaSquares := self inject:0 into:[:sum :this | sum + ((this - m) squared)].
+    ^ (sumDeltaSquares / (self size - 1)) sqrt
+
+    "
+     TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5).
+     TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4).
+     TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431).
+    "
+
+    "Created: / 13-04-2011 / 17:58:47 / cg"
 ! !
 
 !Collection methodsFor:'searching'!
@@ -3902,15 +3951,15 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Collection.st 10632 2011-04-09 17:19:04Z vranyj1 $'
+    ^ '$Id: Collection.st 10643 2011-06-08 21:53:07Z vranyj1 $'
 !
 
 version_CVS
-    ^ 'Header: /var/local/cvs/stx/libbasic/Collection.st,v 1.256 2011-03-20 21:52:46 cg Exp '
+    ^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.257 2011/04/13 16:10:00 cg Exp '
 !
 
 version_SVN
-    ^ '$Id: Collection.st 10632 2011-04-09 17:19:04Z vranyj1 $'
+    ^ '$Id: Collection.st 10643 2011-06-08 21:53:07Z vranyj1 $'
 ! !
 
 Collection initialize!
@@ -3924,3 +3973,4 @@
 
 
 
+