Permalink
Browse files

math.statistics: Add power-mean and quadratic-mean.

  • Loading branch information...
erg committed May 4, 2012
1 parent 2bc643c commit 01e8d15cfbd646d3d9335de578d8acd01049e620
Showing with 8 additions and 0 deletions.
  1. +2 −0 basis/math/statistics/statistics-tests.factor
  2. +6 −0 basis/math/statistics/statistics.factor
@@ -2,6 +2,8 @@ USING: assocs kernel math math.functions math.statistics sequences
math.order tools.test math.vectors ;
IN: math.statistics.tests
+[ 3 ] [ { 1 2 3 4 5 } 1 power-mean ] unit-test
+[ t ] [ { 1 2 3 4 5 } [ 2 power-mean ] [ quadratic-mean ] bi 1e-10 ~ ] unit-test
[ 1 ] [ { 1 } mean ] unit-test
[ 3/2 ] [ { 1 2 } mean ] unit-test
[ 0 ] [ { 0 0 0 } geometric-mean ] unit-test
@@ -5,9 +5,15 @@ math.functions math.order math.vectors sequences
sequences.private sorting fry arrays grouping sets ;
IN: math.statistics
+: power-mean ( seq p -- x )
+ [ '[ _ ^ ] map-sum ] [ [ length / ] [ recip ^ ] bi* ] 2bi ;
+
: mean ( seq -- x )
[ sum ] [ length ] bi / ;
+: quadratic-mean ( seq -- x ) ! root-mean-square
+ [ [ sq ] map-sum ] [ length ] bi / sqrt ;
+
: geometric-mean ( seq -- x )
[ length ] [ product ] bi nth-root ;

0 comments on commit 01e8d15

Please sign in to comment.