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.