Changed deprecated MathRuntimeException in package stat.regression
JIRA: MATH-459 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1239842 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ab3935c4c9
commit
3109c12ec8
|
@ -16,8 +16,13 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.stat.regression;
|
package org.apache.commons.math.stat.regression;
|
||||||
|
|
||||||
import org.apache.commons.math.MathRuntimeException;
|
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||||
|
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||||
|
import org.apache.commons.math.exception.NoDataException;
|
||||||
|
import org.apache.commons.math.exception.NullArgumentException;
|
||||||
|
import org.apache.commons.math.exception.NumberIsTooSmallException;
|
||||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||||
|
import org.apache.commons.math.linear.NonSquareMatrixException;
|
||||||
import org.apache.commons.math.linear.RealMatrix;
|
import org.apache.commons.math.linear.RealMatrix;
|
||||||
import org.apache.commons.math.linear.Array2DRowRealMatrix;
|
import org.apache.commons.math.linear.Array2DRowRealMatrix;
|
||||||
import org.apache.commons.math.linear.RealVector;
|
import org.apache.commons.math.linear.RealVector;
|
||||||
|
@ -87,20 +92,21 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
* @param data input data array
|
* @param data input data array
|
||||||
* @param nobs number of observations (rows)
|
* @param nobs number of observations (rows)
|
||||||
* @param nvars number of independent variables (columns, not counting y)
|
* @param nvars number of independent variables (columns, not counting y)
|
||||||
* @throws IllegalArgumentException if the preconditions are not met
|
* @throws NullArgumentException if the data array is null
|
||||||
|
* @throws DimensionMismatchException if the length of the data array is not equal
|
||||||
|
* to <code>nobs * (nvars + 1)</code>
|
||||||
|
* @throws NumberIsTooSmallException if <code>nobs</code> is smaller than
|
||||||
|
* <code>nvars</code>
|
||||||
*/
|
*/
|
||||||
public void newSampleData(double[] data, int nobs, int nvars) {
|
public void newSampleData(double[] data, int nobs, int nvars) {
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NullArgumentException();
|
||||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
|
||||||
}
|
}
|
||||||
if (data.length != nobs * (nvars + 1)) {
|
if (data.length != nobs * (nvars + 1)) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new DimensionMismatchException(data.length, nobs * (nvars + 1));
|
||||||
LocalizedFormats.INVALID_REGRESSION_ARRAY, data.length, nobs, nvars);
|
|
||||||
}
|
}
|
||||||
if (nobs <= nvars) {
|
if (nobs <= nvars) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NumberIsTooSmallException(nobs, nvars, false);
|
||||||
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS);
|
|
||||||
}
|
}
|
||||||
double[] y = new double[nobs];
|
double[] y = new double[nobs];
|
||||||
final int cols = noIntercept ? nvars: nvars + 1;
|
final int cols = noIntercept ? nvars: nvars + 1;
|
||||||
|
@ -123,16 +129,15 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
* Loads new y sample data, overriding any previous data.
|
* Loads new y sample data, overriding any previous data.
|
||||||
*
|
*
|
||||||
* @param y the array representing the y sample
|
* @param y the array representing the y sample
|
||||||
* @throws IllegalArgumentException if y is null or empty
|
* @throws NullArgumentException if y is null
|
||||||
|
* @throws NoDataException if y is empty
|
||||||
*/
|
*/
|
||||||
protected void newYSampleData(double[] y) {
|
protected void newYSampleData(double[] y) {
|
||||||
if (y == null) {
|
if (y == null) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NullArgumentException();
|
||||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
|
||||||
}
|
}
|
||||||
if (y.length == 0) {
|
if (y.length == 0) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NoDataException();
|
||||||
LocalizedFormats.NO_DATA);
|
|
||||||
}
|
}
|
||||||
this.Y = new ArrayRealVector(y);
|
this.Y = new ArrayRealVector(y);
|
||||||
}
|
}
|
||||||
|
@ -158,16 +163,16 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
* specifying a model including an intercept term.
|
* specifying a model including an intercept term.
|
||||||
* </p>
|
* </p>
|
||||||
* @param x the rectangular array representing the x sample
|
* @param x the rectangular array representing the x sample
|
||||||
* @throws IllegalArgumentException if x is null, empty or not rectangular
|
* @throws NullArgumentException if x is null
|
||||||
|
* @throws NoDataException if x is empty
|
||||||
|
* @throws DimensionMismatchException if x is not rectangular
|
||||||
*/
|
*/
|
||||||
protected void newXSampleData(double[][] x) {
|
protected void newXSampleData(double[][] x) {
|
||||||
if (x == null) {
|
if (x == null) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NullArgumentException();
|
||||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
|
||||||
}
|
}
|
||||||
if (x.length == 0) {
|
if (x.length == 0) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NoDataException();
|
||||||
LocalizedFormats.NO_DATA);
|
|
||||||
}
|
}
|
||||||
if (noIntercept) {
|
if (noIntercept) {
|
||||||
this.X = new Array2DRowRealMatrix(x, true);
|
this.X = new Array2DRowRealMatrix(x, true);
|
||||||
|
@ -176,9 +181,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
final double[][] xAug = new double[x.length][nVars + 1];
|
final double[][] xAug = new double[x.length][nVars + 1];
|
||||||
for (int i = 0; i < x.length; i++) {
|
for (int i = 0; i < x.length; i++) {
|
||||||
if (x[i].length != nVars) {
|
if (x[i].length != nVars) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new DimensionMismatchException(x[i].length, nVars);
|
||||||
LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
|
|
||||||
x[i].length, nVars);
|
|
||||||
}
|
}
|
||||||
xAug[i][0] = 1.0d;
|
xAug[i][0] = 1.0d;
|
||||||
System.arraycopy(x[i], 0, xAug[i], 1, nVars);
|
System.arraycopy(x[i], 0, xAug[i], 1, nVars);
|
||||||
|
@ -198,24 +201,27 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
*
|
*
|
||||||
* @param x the [n,k] array representing the x data
|
* @param x the [n,k] array representing the x data
|
||||||
* @param y the [n,1] array representing the y data
|
* @param y the [n,1] array representing the y data
|
||||||
* @throws IllegalArgumentException if any of the checks fail
|
* @throws NullArgumentException if {@code x} or {@code y} is null
|
||||||
*
|
* @throws DimensionMismatchException if {@code x} and {@code y} do not
|
||||||
|
* have the same length
|
||||||
|
* @throws NoDataException if {@code x} or {@code y} are zero-length
|
||||||
|
* @throws MathIllegalArgumentException if the number of rows of {@code x}
|
||||||
|
* is not larger than the number of columns + 1
|
||||||
*/
|
*/
|
||||||
protected void validateSampleData(double[][] x, double[] y) {
|
protected void validateSampleData(double[][] x, double[] y) {
|
||||||
if ((x == null) || (y == null) || (x.length != y.length)) {
|
if ((x == null) || (y == null)) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NullArgumentException();
|
||||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
|
}
|
||||||
(x == null) ? 0 : x.length,
|
if (x.length != y.length) {
|
||||||
(y == null) ? 0 : y.length);
|
throw new DimensionMismatchException(y.length, x.length);
|
||||||
}
|
}
|
||||||
if (x.length == 0) { // Must be no y data either
|
if (x.length == 0) { // Must be no y data either
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NoDataException();
|
||||||
LocalizedFormats.NO_DATA);
|
|
||||||
}
|
}
|
||||||
if (x[0].length + 1 > x.length) {
|
if (x[0].length + 1 > x.length) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new MathIllegalArgumentException(
|
||||||
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
|
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
|
||||||
x.length, x[0].length);
|
x.length, x[0].length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,18 +231,16 @@ public abstract class AbstractMultipleLinearRegression implements
|
||||||
*
|
*
|
||||||
* @param x the [n,k] array representing the x sample
|
* @param x the [n,k] array representing the x sample
|
||||||
* @param covariance the [n,n] array representing the covariance matrix
|
* @param covariance the [n,n] array representing the covariance matrix
|
||||||
* @throws IllegalArgumentException if the number of rows in x is not equal
|
* @throws DimensionMismatchException if the number of rows in x is not equal
|
||||||
* to the number of rows in covariance or covariance is not square.
|
* to the number of rows in covariance
|
||||||
|
* @throws NonSquareMatrixException if the covariance matrix is not square
|
||||||
*/
|
*/
|
||||||
protected void validateCovarianceData(double[][] x, double[][] covariance) {
|
protected void validateCovarianceData(double[][] x, double[][] covariance) {
|
||||||
if (x.length != covariance.length) {
|
if (x.length != covariance.length) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new DimensionMismatchException(x.length, covariance.length);
|
||||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, x.length, covariance.length);
|
|
||||||
}
|
}
|
||||||
if (covariance.length > 0 && covariance.length != covariance[0].length) {
|
if (covariance.length > 0 && covariance.length != covariance[0].length) {
|
||||||
throw MathRuntimeException.createIllegalArgumentException(
|
throw new NonSquareMatrixException(covariance.length, covariance[0].length);
|
||||||
LocalizedFormats.NON_SQUARE_MATRIX,
|
|
||||||
covariance.length, covariance[0].length);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
package org.apache.commons.math.stat.regression;
|
package org.apache.commons.math.stat.regression;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import org.apache.commons.math.MathException;
|
|
||||||
import org.apache.commons.math.exception.OutOfRangeException;
|
import org.apache.commons.math.exception.OutOfRangeException;
|
||||||
import org.apache.commons.math.distribution.TDistribution;
|
import org.apache.commons.math.distribution.TDistribution;
|
||||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||||
|
@ -137,7 +136,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
} else {
|
} else {
|
||||||
if( hasIntercept ){
|
if( hasIntercept ){
|
||||||
final double fact1 = 1.0 + n;
|
final double fact1 = 1.0 + n;
|
||||||
final double fact2 = (n) / (1.0 + n);
|
final double fact2 = n / (1.0 + n);
|
||||||
final double dx = x - xbar;
|
final double dx = x - xbar;
|
||||||
final double dy = y - ybar;
|
final double dy = y - ybar;
|
||||||
sumXX += dx * dx * fact2;
|
sumXX += dx * dx * fact2;
|
||||||
|
@ -176,7 +175,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
if (hasIntercept) {
|
if (hasIntercept) {
|
||||||
final double fact1 = n - 1.0;
|
final double fact1 = n - 1.0;
|
||||||
final double fact2 = (n) / (n - 1.0);
|
final double fact2 = n / (n - 1.0);
|
||||||
final double dx = x - xbar;
|
final double dx = x - xbar;
|
||||||
final double dy = y - ybar;
|
final double dy = y - ybar;
|
||||||
sumXX -= dx * dx * fact2;
|
sumXX -= dx * dx * fact2;
|
||||||
|
@ -609,9 +608,9 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
* Bivariate Normal Distribution</a>.</p>
|
* Bivariate Normal Distribution</a>.</p>
|
||||||
*
|
*
|
||||||
* @return half-width of 95% confidence interval for the slope estimate
|
* @return half-width of 95% confidence interval for the slope estimate
|
||||||
* @throws MathException if the confidence interval can not be computed.
|
* @throws OutOfRangeException if the confidence interval can not be computed.
|
||||||
*/
|
*/
|
||||||
public double getSlopeConfidenceInterval() throws MathException {
|
public double getSlopeConfidenceInterval() {
|
||||||
return getSlopeConfidenceInterval(0.05d);
|
return getSlopeConfidenceInterval(0.05d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -639,15 +638,14 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
* <code>Double.NaN</code>.
|
* <code>Double.NaN</code>.
|
||||||
* </li>
|
* </li>
|
||||||
* <li><code>(0 < alpha < 1)</code>; otherwise an
|
* <li><code>(0 < alpha < 1)</code>; otherwise an
|
||||||
* <code>IllegalArgumentException</code> is thrown.
|
* <code>OutOfRangeException</code> is thrown.
|
||||||
* </li></ul></p>
|
* </li></ul></p>
|
||||||
*
|
*
|
||||||
* @param alpha the desired significance level
|
* @param alpha the desired significance level
|
||||||
* @return half-width of 95% confidence interval for the slope estimate
|
* @return half-width of 95% confidence interval for the slope estimate
|
||||||
* @throws MathException if the confidence interval can not be computed.
|
* @throws OutOfRangeException if the confidence interval can not be computed.
|
||||||
*/
|
*/
|
||||||
public double getSlopeConfidenceInterval(final double alpha)
|
public double getSlopeConfidenceInterval(final double alpha) {
|
||||||
throws MathException {
|
|
||||||
if (alpha >= 1 || alpha <= 0) {
|
if (alpha >= 1 || alpha <= 0) {
|
||||||
throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
|
throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
|
||||||
alpha, 0, 1);
|
alpha, 0, 1);
|
||||||
|
@ -676,9 +674,10 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
* <code>Double.NaN</code>.</p>
|
* <code>Double.NaN</code>.</p>
|
||||||
*
|
*
|
||||||
* @return significance level for slope/correlation
|
* @return significance level for slope/correlation
|
||||||
* @throws MathException if the significance level can not be computed.
|
* @throws org.apache.commons.math.exception.MaxCountExceededException
|
||||||
|
* if the significance level can not be computed.
|
||||||
*/
|
*/
|
||||||
public double getSignificance() throws MathException {
|
public double getSignificance() {
|
||||||
TDistribution distribution = new TDistribution(n - 2);
|
TDistribution distribution = new TDistribution(n - 2);
|
||||||
return 2d * (1.0 - distribution.cumulativeProbability(
|
return 2d * (1.0 - distribution.cumulativeProbability(
|
||||||
FastMath.abs(getSlope()) / getSlopeStdErr()));
|
FastMath.abs(getSlope()) / getSlopeStdErr()));
|
||||||
|
@ -724,16 +723,16 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
if( FastMath.abs( sumXX ) > Precision.SAFE_MIN ){
|
if( FastMath.abs( sumXX ) > Precision.SAFE_MIN ){
|
||||||
final double[] params = new double[]{ getIntercept(), getSlope() };
|
final double[] params = new double[]{ getIntercept(), getSlope() };
|
||||||
final double mse = getMeanSquareError();
|
final double mse = getMeanSquareError();
|
||||||
final double _syy = sumYY + sumY * sumY / (n);
|
final double _syy = sumYY + sumY * sumY / n;
|
||||||
final double[] vcv = new double[]{
|
final double[] vcv = new double[]{
|
||||||
mse * (xbar *xbar /sumXX + 1.0 / (n)),
|
mse * (xbar *xbar /sumXX + 1.0 / n),
|
||||||
-xbar*mse/sumXX,
|
-xbar*mse/sumXX,
|
||||||
mse/sumXX };
|
mse/sumXX };
|
||||||
return new RegressionResults(
|
return new RegressionResults(
|
||||||
params, new double[][]{vcv}, true, n, 2,
|
params, new double[][]{vcv}, true, n, 2,
|
||||||
sumY, _syy, getSumSquaredErrors(),true,false);
|
sumY, _syy, getSumSquaredErrors(),true,false);
|
||||||
}else{
|
}else{
|
||||||
final double[] params = new double[]{ sumY/(n), Double.NaN };
|
final double[] params = new double[]{ sumY / n, Double.NaN };
|
||||||
//final double mse = getMeanSquareError();
|
//final double mse = getMeanSquareError();
|
||||||
final double[] vcv = new double[]{
|
final double[] vcv = new double[]{
|
||||||
ybar / (n - 1.0),
|
ybar / (n - 1.0),
|
||||||
|
@ -797,7 +796,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
if( variablesToInclude[0] != 1 && variablesToInclude[0] != 0 ){
|
if( variablesToInclude[0] != 1 && variablesToInclude[0] != 0 ){
|
||||||
throw new OutOfRangeException( variablesToInclude[0],0,1 );
|
throw new OutOfRangeException( variablesToInclude[0],0,1 );
|
||||||
}
|
}
|
||||||
final double _mean = sumY * sumY / (n);
|
final double _mean = sumY * sumY / n;
|
||||||
final double _syy = sumYY + _mean;
|
final double _syy = sumYY + _mean;
|
||||||
if( variablesToInclude[0] == 0 ){
|
if( variablesToInclude[0] == 0 ){
|
||||||
//just the mean
|
//just the mean
|
||||||
|
@ -809,8 +808,8 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
||||||
|
|
||||||
}else if( variablesToInclude[0] == 1){
|
}else if( variablesToInclude[0] == 1){
|
||||||
//final double _syy = sumYY + sumY * sumY / ((double) n);
|
//final double _syy = sumYY + sumY * sumY / ((double) n);
|
||||||
final double _sxx = sumXX + sumX * sumX / (n);
|
final double _sxx = sumXX + sumX * sumX / n;
|
||||||
final double _sxy = sumXY + sumX * sumY / (n);
|
final double _sxy = sumXY + sumX * sumY / n;
|
||||||
final double _sse = FastMath.max(0d, _syy - _sxy * _sxy / _sxx);
|
final double _sse = FastMath.max(0d, _syy - _sxy * _sxy / _sxx);
|
||||||
final double _mse = _sse/((n-1));
|
final double _mse = _sse/((n-1));
|
||||||
if( !Double.isNaN(_sxx) ){
|
if( !Double.isNaN(_sxx) ){
|
||||||
|
|
Loading…
Reference in New Issue