Skip to content

Commit 49444ee

Browse files
author
Luc Maisonobe
committed
Allow covariance to be computed for one-dimensional variables.
JIRA: MATH-939 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1453271 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7360556 commit 49444ee

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ This is a minor release: It combines bug fixes and new features.
5555
Changes to existing features were made in a backwards-compatible
5656
way such as to allow drop-in replacement of the v3.1[.1] JAR file.
5757
">
58+
<action dev="luc" type="fix" issue="MATH-939" due-to="Piotr Wydrych" >
59+
Allow covariance to be computed for one-dimensional variables.
60+
</action>
5861
<action dev="luc" type="fix" issue="MATH-938" >
5962
Fixed accuracy of 3D Line.revert().
6063
</action>

src/main/java/org/apache/commons/math3/stat/correlation/Covariance.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.math3.stat.correlation;
1818

1919
import org.apache.commons.math3.exception.MathIllegalArgumentException;
20+
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
2021
import org.apache.commons.math3.exception.util.LocalizedFormats;
2122
import org.apache.commons.math3.linear.RealMatrix;
2223
import org.apache.commons.math3.linear.BlockRealMatrix;
@@ -70,31 +71,36 @@ public Covariance() {
7071
* <p>The <code>biasCorrected</code> parameter determines whether or not
7172
* covariance estimates are bias-corrected.</p>
7273
*
73-
* <p>The input array must be rectangular with at least two columns
74+
* <p>The input array must be rectangular with at least one column
7475
* and two rows.</p>
7576
*
7677
* @param data rectangular array with columns representing covariates
7778
* @param biasCorrected true means covariances are bias-corrected
7879
* @throws MathIllegalArgumentException if the input data array is not
79-
* rectangular with at least two rows and two columns.
80+
* rectangular with at least two rows and one column.
81+
* @throws NotStrictlyPositiveException if the input data array is not
82+
* rectangular with at least one row and one column.
8083
*/
8184
public Covariance(double[][] data, boolean biasCorrected)
82-
throws MathIllegalArgumentException {
85+
throws MathIllegalArgumentException, NotStrictlyPositiveException {
8386
this(new BlockRealMatrix(data), biasCorrected);
8487
}
8588

8689
/**
8790
* Create a Covariance matrix from a rectangular array
8891
* whose columns represent covariates.
8992
*
90-
* <p>The input array must be rectangular with at least two columns
93+
* <p>The input array must be rectangular with at least one column
9194
* and two rows</p>
9295
*
9396
* @param data rectangular array with columns representing covariates
9497
* @throws MathIllegalArgumentException if the input data array is not
95-
* rectangular with at least two rows and two columns.
98+
* rectangular with at least two rows and one column.
99+
* @throws NotStrictlyPositiveException if the input data array is not
100+
* rectangular with at least one row and one column.
96101
*/
97-
public Covariance(double[][] data) throws MathIllegalArgumentException {
102+
public Covariance(double[][] data)
103+
throws MathIllegalArgumentException, NotStrictlyPositiveException {
98104
this(data, true);
99105
}
100106

@@ -105,12 +111,12 @@ public Covariance(double[][] data) throws MathIllegalArgumentException {
105111
* <p>The <code>biasCorrected</code> parameter determines whether or not
106112
* covariance estimates are bias-corrected.</p>
107113
*
108-
* <p>The matrix must have at least two columns and two rows</p>
114+
* <p>The matrix must have at least one column and two rows</p>
109115
*
110116
* @param matrix matrix with columns representing covariates
111117
* @param biasCorrected true means covariances are bias-corrected
112118
* @throws MathIllegalArgumentException if the input matrix does not have
113-
* at least two rows and two columns
119+
* at least two rows and one column
114120
*/
115121
public Covariance(RealMatrix matrix, boolean biasCorrected)
116122
throws MathIllegalArgumentException {
@@ -123,11 +129,11 @@ public Covariance(RealMatrix matrix, boolean biasCorrected)
123129
* Create a covariance matrix from a matrix whose columns
124130
* represent covariates.
125131
*
126-
* <p>The matrix must have at least two columns and two rows</p>
132+
* <p>The matrix must have at least one column and two rows</p>
127133
*
128134
* @param matrix matrix with columns representing covariates
129135
* @throws MathIllegalArgumentException if the input matrix does not have
130-
* at least two rows and two columns
136+
* at least two rows and one column
131137
*/
132138
public Covariance(RealMatrix matrix) throws MathIllegalArgumentException {
133139
this(matrix, true);
@@ -154,7 +160,7 @@ public int getN() {
154160
/**
155161
* Compute a covariance matrix from a matrix whose columns represent
156162
* covariates.
157-
* @param matrix input matrix (must have at least two columns and two rows)
163+
* @param matrix input matrix (must have at least one column and two rows)
158164
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
159165
* @return covariance matrix
160166
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
@@ -178,7 +184,7 @@ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorr
178184
/**
179185
* Create a covariance matrix from a matrix whose columns represent
180186
* covariates. Covariances are computed using the bias-corrected formula.
181-
* @param matrix input matrix (must have at least two columns and two rows)
187+
* @param matrix input matrix (must have at least one column and two rows)
182188
* @return covariance matrix
183189
* @throws MathIllegalArgumentException if matrix does not contain sufficient data
184190
* @see #Covariance
@@ -191,26 +197,31 @@ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix)
191197
/**
192198
* Compute a covariance matrix from a rectangular array whose columns represent
193199
* covariates.
194-
* @param data input array (must have at least two columns and two rows)
200+
* @param data input array (must have at least one column and two rows)
195201
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
196202
* @return covariance matrix
197203
* @throws MathIllegalArgumentException if the data array does not contain sufficient
198204
* data
205+
* @throws NotStrictlyPositiveException if the input data array is not
206+
* rectangular with at least one row and one column.
199207
*/
200208
protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected)
201-
throws MathIllegalArgumentException {
209+
throws MathIllegalArgumentException, NotStrictlyPositiveException {
202210
return computeCovarianceMatrix(new BlockRealMatrix(data), biasCorrected);
203211
}
204212

205213
/**
206214
* Create a covariance matrix from a rectangular array whose columns represent
207215
* covariates. Covariances are computed using the bias-corrected formula.
208-
* @param data input array (must have at least two columns and two rows)
216+
* @param data input array (must have at least one column and two rows)
209217
* @return covariance matrix
210218
* @throws MathIllegalArgumentException if the data array does not contain sufficient data
219+
* @throws NotStrictlyPositiveException if the input data array is not
220+
* rectangular with at least one row and one column.
211221
* @see #Covariance
212222
*/
213-
protected RealMatrix computeCovarianceMatrix(double[][] data) throws MathIllegalArgumentException {
223+
protected RealMatrix computeCovarianceMatrix(double[][] data)
224+
throws MathIllegalArgumentException, NotStrictlyPositiveException {
214225
return computeCovarianceMatrix(data, true);
215226
}
216227

@@ -268,15 +279,15 @@ public double covariance(final double[] xArray, final double[] yArray)
268279

269280
/**
270281
* Throws MathIllegalArgumentException if the matrix does not have at least
271-
* two columns and two rows.
282+
* one column and two rows.
272283
* @param matrix matrix to check
273284
* @throws MathIllegalArgumentException if the matrix does not contain sufficient data
274285
* to compute covariance
275286
*/
276287
private void checkSufficientData(final RealMatrix matrix) throws MathIllegalArgumentException {
277288
int nRows = matrix.getRowDimension();
278289
int nCols = matrix.getColumnDimension();
279-
if (nRows < 2 || nCols < 2) {
290+
if (nRows < 2 || nCols < 1) {
280291
throw new MathIllegalArgumentException(
281292
LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS,
282293
nRows, nCols);

src/test/java/org/apache/commons/math3/stat/correlation/CovarianceTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.math3.stat.correlation;
1818

1919
import org.apache.commons.math3.TestUtils;
20+
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
2021
import org.apache.commons.math3.linear.RealMatrix;
2122
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
2223
import org.apache.commons.math3.stat.descriptive.moment.Variance;
@@ -161,6 +162,16 @@ public void testConstant() {
161162
Assert.assertEquals(0d, new Covariance().covariance(noVariance, noVariance, true), Double.MIN_VALUE);
162163
}
163164

165+
/**
166+
* One column
167+
*/
168+
@Test
169+
public void testOneColumn() {
170+
RealMatrix cov = new Covariance(new double[][] {{1}, {2}}, false).getCovarianceMatrix();
171+
Assert.assertEquals(1, cov.getRowDimension());
172+
Assert.assertEquals(1, cov.getColumnDimension());
173+
Assert.assertEquals(0.25, cov.getEntry(0, 0), 1.0e-15);
174+
}
164175

165176
/**
166177
* Insufficient data
@@ -175,11 +186,10 @@ public void testInsufficientData() {
175186
} catch (IllegalArgumentException ex) {
176187
// Expected
177188
}
178-
RealMatrix matrix = new Array2DRowRealMatrix(new double[][] {{0},{1}});
179189
try {
180-
new Covariance(matrix);
181-
Assert.fail("Expecting IllegalArgumentException");
182-
} catch (IllegalArgumentException ex) {
190+
new Covariance(new double[][] {{},{}});
191+
Assert.fail("Expecting NotStrictlyPositiveException");
192+
} catch (NotStrictlyPositiveException ex) {
183193
// Expected
184194
}
185195
}

0 commit comments

Comments
 (0)