From 1a59d2f44fc3ada99633038a93120c8c73d217e5 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 31 Oct 2005 05:41:17 +0000 Subject: [PATCH] statistics tweaks --- contrib/math/TODO.txt | 4 ++-- contrib/math/statistics.factor | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/contrib/math/TODO.txt b/contrib/math/TODO.txt index cc1a723d74..c394efa7ec 100644 --- a/contrib/math/TODO.txt +++ b/contrib/math/TODO.txt @@ -12,7 +12,8 @@ Nice to have: - infinite limits, sums, products - finding roots with Newton's method - solving ODEs with Runge-Kutta - - matrices: singular value decomposition, eigenvalues, LU decomposition + - matrices: singular value decomposition, eigenvalues, LU decomposition, + polar decomposition - square root of a matrix, e^matrix - finding roots of polynomials - Algebra: @@ -32,7 +33,6 @@ Nice to have: - minimal and characteristic polynomials of algebraic numbers - norm and trace of algebraic numbers - minimal and characteristic polynomials of matrices - - eigenvalues of matrices - Graphs: - minimum spanning trees - Logic: diff --git a/contrib/math/statistics.factor b/contrib/math/statistics.factor index 78582b201c..ac52361941 100644 --- a/contrib/math/statistics.factor +++ b/contrib/math/statistics.factor @@ -5,10 +5,14 @@ USING: kernel math sequences ; #! arithmetic mean, sum divided by length [ sum ] keep length / ; -: geo-mean ( seq -- n ) +: geometric-mean ( seq -- n ) #! geometric mean, nth root of product [ product ] keep length swap nth-root ; +: harmonic-mean ( seq -- n ) + #! harmonic mean, reciprocal of sum of reciprocals. + [ recip ] map sum recip ; + : median ( seq -- n ) #! middle number if odd, avg of two middle numbers if even number-sort dup length dup even? [ @@ -19,7 +23,7 @@ USING: kernel math sequences ; : range ( seq -- n ) #! max - min - number-sort [ first ] keep pop swap - ; + dup first 2dup [ min ] reduce >r [ max ] reduce r> - ; : var ( seq -- ) #! variance, normalize by N-1