math.statistics: Get smart about the geometric mean. Calculate it in logspace so we can use addition of small logarithms and multiplication instead of the nth root of a giant product.

db4
Doug Coleman 2014-09-26 13:33:04 -07:00
parent aae6fb2ae3
commit e4e6f34f0e
2 changed files with 5 additions and 3 deletions

View 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

View 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