]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: Get smart about the geometric mean. Calculate it in logspace so...
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 26 Sep 2014 20:33:04 +0000 (13:33 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 26 Sep 2014 20:34:25 +0000 (13:34 -0700)
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index 25ef951c66475e68c8c7e0c08c0e320478e8e24d..f082711d2d6172292cdd2c5db693d621251b5513 100644 (file)
@@ -1,4 +1,4 @@
-USING: assocs kernel math math.functions math.statistics sequences
+USING: arrays assocs kernel math math.functions math.statistics sequences
 math.order tools.test math.vectors ;
 FROM: math.ranges => [a,b] ;
 IN: math.statistics.tests
@@ -8,9 +8,11 @@ IN: math.statistics.tests
 [ 1 ] [ { 1 } mean ] unit-test
 [ 0 ] [ { } mean ] unit-test
 [ 3/2 ] [ { 1 2 } mean ] unit-test
-[ 0 ] [ { 0 0 0 } geometric-mean ] unit-test
+[ 0.0 ] [ { 0 0 0 } geometric-mean ] unit-test
 [ t ] [ { 2 2 2 2 } geometric-mean 2.0 .0001 ~ ] unit-test
 [ 1.0 ] [ { 1 1 1 } geometric-mean ] unit-test
+[ t ] [ 1000 1000 <array> geometric-mean 1000 .01 ~ ] unit-test
+[ t ] [ 100000 100000 <array> geometric-mean 100000 .01 ~ ] unit-test
 [ 1/3 ] [ { 1 1 1 } harmonic-mean ] unit-test
 [ 5+1/4 ] [ { 1 3 5 7 } contraharmonic-mean ] unit-test
 [ 18 ] [ { 4 8 15 16 23 42 } 0 trimmed-mean ] unit-test
index 5a618ea168c9d98a939d71ced035e1025f44f337..bf1abf70172cd049ad73fdd6efa0f1f57402723a 100644 (file)
@@ -33,7 +33,7 @@ IN: math.statistics
     [ sum-of-squares ] [ length ] bi / sqrt ; inline
 
 : geometric-mean ( seq -- x )
-    [ length ] [ product ] bi nth-root ; inline
+    [ [ log ] map-sum ] [ length ] bi /f e^ ; inline
 
 : harmonic-mean ( seq -- x )
     [ recip ] map-sum recip ; inline