]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: fix calculation of the harmonic mean
authorBjörn Lindqvist <bjourne@gmail.com>
Sun, 15 Jan 2017 20:27:55 +0000 (21:27 +0100)
committerBjörn Lindqvist <bjourne@gmail.com>
Sun, 15 Jan 2017 20:27:55 +0000 (21:27 +0100)
basis/math/statistics/statistics-docs.factor
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index 83cb37197873f0fb9d141da154a652180cb3f4a0..04ed406463eba466f6d9b14c47ecd16e689443d4 100644 (file)
@@ -12,7 +12,7 @@ HELP: harmonic-mean
 { $values { "seq" sequence } { "x" "a non-negative real number" } }
 { $description "Computes the harmonic mean of the elements in " { $snippet "seq" } ". The harmonic mean is appropriate when the average of rates is desired." }
 { $notes "Positive reals only." }
-{ $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } harmonic-mean ." "6/11" } }
+{ $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } harmonic-mean ." "18/11" } }
 { $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
 
 HELP: kth-smallest
@@ -24,7 +24,8 @@ HELP: mean
 { $values { "seq" sequence } { "x" "a non-negative real number" } }
 { $description "Computes the arithmetic mean of the elements in " { $snippet "seq" } "." }
 { $examples { $example "USING: math.statistics prettyprint ;" "{ 1 2 3 } mean ." "2" } }
-{ $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." } ;
+{ $errors "Throws a " { $link signal-error. } " (divide by zero) if the sequence is empty." }
+{ $see-also geometric-mean harmonic-mean } ;
 
 HELP: median
 { $values { "seq" sequence } { "x" "a non-negative real number" } }
index 8b2d5ea305abf355fd2e34912d1503a24663bf87..8602a3148e9678b949007bd8f6f6da11053f8962 100644 (file)
@@ -13,7 +13,10 @@ IN: math.statistics.tests
 { 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
+
+{ 1 } [ { 1 1 1 } harmonic-mean ] unit-test
+{ 12/7 } [ { 1 2 4 } 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
 { 15+1/2 } [ { 4 8 15 16 23 42 } 0.2 trimmed-mean ] unit-test
index fe18d0c855f1345f55f2af163ef2afc15ba11828..15693f999fd47198dd40b4e40cf81c89d7320301 100644 (file)
@@ -35,7 +35,7 @@ IN: math.statistics
     [ [ log ] map-sum ] [ length ] bi /f e^ ; inline
 
 : harmonic-mean ( seq -- x )
-    [ recip ] map-sum recip ; inline
+    [ [ recip ] map-sum ] [ length swap / ] bi ; inline
 
 : contraharmonic-mean ( seq -- x )
     [ sum-of-squares ] [ sum ] bi / ; inline