From 790663295242345019d7887d312deadec83d5ca3 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 2 Oct 2012 17:59:47 -0700 Subject: [PATCH] math.matrices: Add some combinators for iterating over matrices. Add covariance matrix. --- basis/math/matrices/matrices-tests.factor | 32 +++++++++++++++++++++++ basis/math/matrices/matrices.factor | 10 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/basis/math/matrices/matrices-tests.factor b/basis/math/matrices/matrices-tests.factor index f4ba962f80..7547e78652 100644 --- a/basis/math/matrices/matrices-tests.factor +++ b/basis/math/matrices/matrices-tests.factor @@ -282,3 +282,35 @@ IN: math.matrices.tests { 6 12 18 } { 7 14 21 } } } [ { 5 6 7 } { 1 2 3 } outer ] unit-test + + +CONSTANT: test-points { + { 80 27 89 } { 80 27 88 } { 75 25 90 } + { 62 24 87 } { 62 22 87 } { 62 23 87 } + { 62 24 93 } { 62 24 93 } { 58 23 87 } + { 58 18 80 } { 58 18 89 } { 58 17 88 } + { 58 18 82 } { 58 19 93 } { 50 18 89 } + { 50 18 86 } { 50 19 72 } { 50 19 79 } + { 50 20 80 } { 56 20 82 } { 70 20 91 } +} + +{ + { + { 84+2/35 22+23/35 24+4/7 } + { 22+23/35 9+104/105 6+87/140 } + { 24+4/7 6+87/140 28+5/7 } + } +} [ + test-points sample-cov-matrix +] unit-test + +{ + { + { 80+8/147 21+85/147 23+59/147 } + { 21+85/147 9+227/441 6+15/49 } + { 23+59/147 6+15/49 27+17/49 } + } +} [ + test-points cov-matrix +] unit-test + diff --git a/basis/math/matrices/matrices.factor b/basis/math/matrices/matrices.factor index 3f594c87d2..e5f3dcfe1b 100644 --- a/basis/math/matrices/matrices.factor +++ b/basis/math/matrices/matrices.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays columns kernel locals math math.bits math.functions math.order math.vectors sequences -sequences.private fry ; +sequences.private fry math.statistics ; IN: math.matrices ! Matrices @@ -196,3 +196,11 @@ IN: math.matrices : cartesian-matrix-column-map ( m quot -- m' ) [ cols first2 ] prepose cartesian-matrix-map ; inline + +: cov-matrix-ddof ( m ddof -- cov ) + '[ _ cov-ddof ] cartesian-matrix-column-map ; inline + +: cov-matrix ( m -- cov ) 0 cov-matrix-ddof ; inline + +: sample-cov-matrix ( m -- cov ) 1 cov-matrix-ddof ; inline +