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;
|
||||
|
||||
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.linear.NonSquareMatrixException;
|
||||
import org.apache.commons.math.linear.RealMatrix;
|
||||
import org.apache.commons.math.linear.Array2DRowRealMatrix;
|
||||
import org.apache.commons.math.linear.RealVector;
|
||||
|
@ -87,20 +92,21 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
* @param data input data array
|
||||
* @param nobs number of observations (rows)
|
||||
* @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) {
|
||||
if (data == null) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (data.length != nobs * (nvars + 1)) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.INVALID_REGRESSION_ARRAY, data.length, nobs, nvars);
|
||||
throw new DimensionMismatchException(data.length, nobs * (nvars + 1));
|
||||
}
|
||||
if (nobs <= nvars) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS);
|
||||
throw new NumberIsTooSmallException(nobs, nvars, false);
|
||||
}
|
||||
double[] y = new double[nobs];
|
||||
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.
|
||||
*
|
||||
* @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) {
|
||||
if (y == null) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (y.length == 0) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NO_DATA);
|
||||
throw new NoDataException();
|
||||
}
|
||||
this.Y = new ArrayRealVector(y);
|
||||
}
|
||||
|
@ -158,16 +163,16 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
* specifying a model including an intercept term.
|
||||
* </p>
|
||||
* @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) {
|
||||
if (x == null) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NULL_NOT_ALLOWED);
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (x.length == 0) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NO_DATA);
|
||||
throw new NoDataException();
|
||||
}
|
||||
if (noIntercept) {
|
||||
this.X = new Array2DRowRealMatrix(x, true);
|
||||
|
@ -176,9 +181,7 @@ public abstract class AbstractMultipleLinearRegression implements
|
|||
final double[][] xAug = new double[x.length][nVars + 1];
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
if (x[i].length != nVars) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
|
||||
x[i].length, nVars);
|
||||
throw new DimensionMismatchException(x[i].length, nVars);
|
||||
}
|
||||
xAug[i][0] = 1.0d;
|
||||
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 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) {
|
||||
if ((x == null) || (y == null) || (x.length != y.length)) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
|
||||
(x == null) ? 0 : x.length,
|
||||
(y == null) ? 0 : y.length);
|
||||
if ((x == null) || (y == null)) {
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
if (x.length != y.length) {
|
||||
throw new DimensionMismatchException(y.length, x.length);
|
||||
}
|
||||
if (x.length == 0) { // Must be no y data either
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NO_DATA);
|
||||
throw new NoDataException();
|
||||
}
|
||||
if (x[0].length + 1 > x.length) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
|
||||
x.length, x[0].length);
|
||||
throw new MathIllegalArgumentException(
|
||||
LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
|
||||
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 covariance the [n,n] array representing the covariance matrix
|
||||
* @throws IllegalArgumentException if the number of rows in x is not equal
|
||||
* to the number of rows in covariance or covariance is not square.
|
||||
* @throws DimensionMismatchException if the number of rows in x is not equal
|
||||
* to the number of rows in covariance
|
||||
* @throws NonSquareMatrixException if the covariance matrix is not square
|
||||
*/
|
||||
protected void validateCovarianceData(double[][] x, double[][] covariance) {
|
||||
if (x.length != covariance.length) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, x.length, covariance.length);
|
||||
throw new DimensionMismatchException(x.length, covariance.length);
|
||||
}
|
||||
if (covariance.length > 0 && covariance.length != covariance[0].length) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.NON_SQUARE_MATRIX,
|
||||
covariance.length, covariance[0].length);
|
||||
throw new NonSquareMatrixException(covariance.length, covariance[0].length);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
package org.apache.commons.math.stat.regression;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.exception.OutOfRangeException;
|
||||
import org.apache.commons.math.distribution.TDistribution;
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
|
@ -137,7 +136,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
} else {
|
||||
if( hasIntercept ){
|
||||
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 dy = y - ybar;
|
||||
sumXX += dx * dx * fact2;
|
||||
|
@ -176,7 +175,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
if (n > 0) {
|
||||
if (hasIntercept) {
|
||||
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 dy = y - ybar;
|
||||
sumXX -= dx * dx * fact2;
|
||||
|
@ -609,9 +608,9 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
* Bivariate Normal Distribution</a>.</p>
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -639,15 +638,14 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
* <code>Double.NaN</code>.
|
||||
* </li>
|
||||
* <li><code>(0 < alpha < 1)</code>; otherwise an
|
||||
* <code>IllegalArgumentException</code> is thrown.
|
||||
* <code>OutOfRangeException</code> is thrown.
|
||||
* </li></ul></p>
|
||||
*
|
||||
* @param alpha the desired significance level
|
||||
* @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)
|
||||
throws MathException {
|
||||
public double getSlopeConfidenceInterval(final double alpha) {
|
||||
if (alpha >= 1 || alpha <= 0) {
|
||||
throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL,
|
||||
alpha, 0, 1);
|
||||
|
@ -676,9 +674,10 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
* <code>Double.NaN</code>.</p>
|
||||
*
|
||||
* @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);
|
||||
return 2d * (1.0 - distribution.cumulativeProbability(
|
||||
FastMath.abs(getSlope()) / getSlopeStdErr()));
|
||||
|
@ -724,16 +723,16 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
if( FastMath.abs( sumXX ) > Precision.SAFE_MIN ){
|
||||
final double[] params = new double[]{ getIntercept(), getSlope() };
|
||||
final double mse = getMeanSquareError();
|
||||
final double _syy = sumYY + sumY * sumY / (n);
|
||||
final double _syy = sumYY + sumY * sumY / n;
|
||||
final double[] vcv = new double[]{
|
||||
mse * (xbar *xbar /sumXX + 1.0 / (n)),
|
||||
mse * (xbar *xbar /sumXX + 1.0 / n),
|
||||
-xbar*mse/sumXX,
|
||||
mse/sumXX };
|
||||
return new RegressionResults(
|
||||
params, new double[][]{vcv}, true, n, 2,
|
||||
sumY, _syy, getSumSquaredErrors(),true,false);
|
||||
}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[] vcv = new double[]{
|
||||
ybar / (n - 1.0),
|
||||
|
@ -797,7 +796,7 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
if( variablesToInclude[0] != 1 && variablesToInclude[0] != 0 ){
|
||||
throw new OutOfRangeException( variablesToInclude[0],0,1 );
|
||||
}
|
||||
final double _mean = sumY * sumY / (n);
|
||||
final double _mean = sumY * sumY / n;
|
||||
final double _syy = sumYY + _mean;
|
||||
if( variablesToInclude[0] == 0 ){
|
||||
//just the mean
|
||||
|
@ -809,8 +808,8 @@ public class SimpleRegression implements Serializable, UpdatingMultipleLinearReg
|
|||
|
||||
}else if( variablesToInclude[0] == 1){
|
||||
//final double _syy = sumYY + sumY * sumY / ((double) n);
|
||||
final double _sxx = sumXX + sumX * sumX / (n);
|
||||
final double _sxy = sumXY + sumX * sumY / (n);
|
||||
final double _sxx = sumXX + sumX * sumX / n;
|
||||
final double _sxy = sumXY + sumX * sumY / n;
|
||||
final double _sse = FastMath.max(0d, _syy - _sxy * _sxy / _sxx);
|
||||
final double _mse = _sse/((n-1));
|
||||
if( !Double.isNaN(_sxx) ){
|
||||
|
|
Loading…
Reference in New Issue