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

db4
Doug Coleman 2012-05-04 15:27:46 -07:00
parent 7ab521f0ba
commit 9a4e66c8a6
2 changed files with 8 additions and 0 deletions

View 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

View 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 ;