1717package org .apache .commons .math3 .stat .correlation ;
1818
1919import org .apache .commons .math3 .exception .MathIllegalArgumentException ;
20+ import org .apache .commons .math3 .exception .NotStrictlyPositiveException ;
2021import org .apache .commons .math3 .exception .util .LocalizedFormats ;
2122import org .apache .commons .math3 .linear .RealMatrix ;
2223import 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 );
0 commit comments