From 2d73d9f99a9c7ac71d65b571164ddb37537de4a5 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Mon, 16 Feb 2009 05:04:57 +0000 Subject: [PATCH] Changed to persist and expose the number of observations in the source dataset. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@744800 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/stat/correlation/Covariance.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/math/stat/correlation/Covariance.java b/src/java/org/apache/commons/math/stat/correlation/Covariance.java index 8eaa0bba1..1d40ae076 100644 --- a/src/java/org/apache/commons/math/stat/correlation/Covariance.java +++ b/src/java/org/apache/commons/math/stat/correlation/Covariance.java @@ -26,7 +26,7 @@ import org.apache.commons.math.stat.descriptive.moment.Variance; * Computes covariances for pairs of arrays or columns of a matrix. * *

The constructors that take RealMatrix or - * double[][] arguments generate correlation matrices. The + * double[][] arguments generate covariance matrices. The * columns of the input matrices are assumed to represent variable values.

* *

The constructor argument biasCorrected determines whether or @@ -34,7 +34,7 @@ import org.apache.commons.math.stat.descriptive.moment.Variance; * *

Unbiased covariances are given by the formula

* cov(X, Y) = Σ[(xi - E(X))(yi - E(Y))] / (n - 1) - * where E(x) is the mean of X and E(Y) + * where E(X) is the mean of X and E(Y) * is the mean of the Y values. * *

Non-bias-corrected estimates use n in place of n - 1 @@ -50,9 +50,16 @@ public class Covariance { /** * Create an empty covariance matrix. */ + /** Number of observations (length of covariate vectors) */ + private final int n; + + /** + * Create a Covariance with no data + */ public Covariance() { super(); covarianceMatrix = null; + n = 0; } /** @@ -105,6 +112,7 @@ public class Covariance { */ public Covariance(RealMatrix matrix, boolean biasCorrected) { checkSufficientData(matrix); + n = matrix.getRowDimension(); covarianceMatrix = computeCovariance(matrix, biasCorrected); } @@ -131,6 +139,16 @@ public class Covariance { return covarianceMatrix; } + /** + * Returns the number of observations (length of covariate vectors) + * + * @return number of observations + */ + + public int getN() { + return n; + } + /** * Create a covariance matrix from a matrix whose columns represent * covariates.