From 9a4e66c8a686879f859154090fd1f0bfbcfc23c1 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 4 May 2012 15:27:46 -0700 Subject: [PATCH] math.statistics: Add power-mean and quadratic-mean. --- basis/math/statistics/statistics-tests.factor | 2 ++ basis/math/statistics/statistics.factor | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 8e28a463f7..63c9bfa388 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.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 diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index 88d93d3339..aa396c894b 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -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 ;