]> gitweb.factorcode.org Git - factor.git/commitdiff
math.statistics: Add power-mean and quadratic-mean.
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 4 May 2012 22:27:46 +0000 (15:27 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 4 May 2012 22:27:46 +0000 (15:27 -0700)
basis/math/statistics/statistics-tests.factor
basis/math/statistics/statistics.factor

index 8e28a463f7ec83cee021bf2c48ea850ae70906c5..63c9bfa388a91c299434c94b76fe9185ce78f348 100644 (file)
@@ -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
index 88d93d3339b7f15287b21e3c800687e99b1d0749..aa396c894b5233c9000e0d993777eae376e89050 100644 (file)
@@ -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 ;