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
This commit is contained in:
Phil Steitz 2009-02-16 05:04:57 +00:00
parent f24bbdd41d
commit 2d73d9f99a
1 changed files with 20 additions and 2 deletions

View File

@ -26,7 +26,7 @@ import org.apache.commons.math.stat.descriptive.moment.Variance;
* Computes covariances for pairs of arrays or columns of a matrix. * Computes covariances for pairs of arrays or columns of a matrix.
* *
* <p>The constructors that take <code>RealMatrix</code> or * <p>The constructors that take <code>RealMatrix</code> or
* <code>double[][]</code> arguments generate correlation matrices. The * <code>double[][]</code> arguments generate covariance matrices. The
* columns of the input matrices are assumed to represent variable values.</p> * columns of the input matrices are assumed to represent variable values.</p>
* *
* <p>The constructor argument <code>biasCorrected</code> determines whether or * <p>The constructor argument <code>biasCorrected</code> determines whether or
@ -34,7 +34,7 @@ import org.apache.commons.math.stat.descriptive.moment.Variance;
* *
* <p>Unbiased covariances are given by the formula</p> * <p>Unbiased covariances are given by the formula</p>
* <code>cov(X, Y) = &Sigma;[(x<sub>i</sub> - E(X))(y<sub>i</sub> - E(Y))] / (n - 1)</code> * <code>cov(X, Y) = &Sigma;[(x<sub>i</sub> - E(X))(y<sub>i</sub> - E(Y))] / (n - 1)</code>
* where <code>E(x)</code> is the mean of <code>X</code> and <code>E(Y)</code> * where <code>E(X)</code> is the mean of <code>X</code> and <code>E(Y)</code>
* is the mean of the <code>Y</code> values. * is the mean of the <code>Y</code> values.
* *
* <p>Non-bias-corrected estimates use <code>n</code> in place of <code>n - 1</code> * <p>Non-bias-corrected estimates use <code>n</code> in place of <code>n - 1</code>
@ -50,9 +50,16 @@ public class Covariance {
/** /**
* Create an empty covariance matrix. * Create an empty covariance matrix.
*/ */
/** Number of observations (length of covariate vectors) */
private final int n;
/**
* Create a Covariance with no data
*/
public Covariance() { public Covariance() {
super(); super();
covarianceMatrix = null; covarianceMatrix = null;
n = 0;
} }
/** /**
@ -105,6 +112,7 @@ public class Covariance {
*/ */
public Covariance(RealMatrix matrix, boolean biasCorrected) { public Covariance(RealMatrix matrix, boolean biasCorrected) {
checkSufficientData(matrix); checkSufficientData(matrix);
n = matrix.getRowDimension();
covarianceMatrix = computeCovariance(matrix, biasCorrected); covarianceMatrix = computeCovariance(matrix, biasCorrected);
} }
@ -131,6 +139,16 @@ public class Covariance {
return covarianceMatrix; 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 * Create a covariance matrix from a matrix whose columns represent
* covariates. * covariates.