From 0701902122308f376dab4f2a4371600ddc05ae3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Sun, 15 Jan 2017 21:27:55 +0100 Subject: [PATCH] math.statistics: fix calculation of the harmonic mean --- basis/math/statistics/statistics-docs.factor | 5 +++-- basis/math/statistics/statistics-tests.factor | 5 ++++- basis/math/statistics/statistics.factor | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index 83cb371978..04ed406463 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -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" } } diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 8b2d5ea305..8602a3148e 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.factor @@ -13,7 +13,10 @@ IN: math.statistics.tests { 1.0 } [ { 1 1 1 } geometric-mean ] unit-test { t } [ 1000 1000 geometric-mean 1000 .01 ~ ] unit-test { t } [ 100000 100000 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 diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index fe18d0c855..15693f999f 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -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