Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model.
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        addObservation(x[i], y[i]);
    }
}
 
Example 2
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds a series of observations to the regression model. The lengths of
 * x and y must be the same and x must be rectangular.
 *
 * @param x a series of observations on the independent variables
 * @param y a series of observations on the dependent variable
 * The length of x and y must be the same
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(final double[][] x,final double[] y) throws ModelSpecificationException {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    boolean obsOk=true;
    for( int i = 0 ; i < x.length; i++){
        if( x[i] == null || x[i].length == 0 ){
            obsOk = false;
        }
    }
    if( !obsOk ){
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              0, 1);
    }
    for( int i = 0 ; i < x.length ; i++){
        addData( x[i][0], y[i] );
    }
}
 
Example 3
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        this.addObservation(x[i], y[i]);
    }
    return;
}
 
Example 4
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds a series of observations to the regression model. The lengths of
 * x and y must be the same and x must be rectangular.
 *
 * @param x a series of observations on the independent variables
 * @param y a series of observations on the dependent variable
 * The length of x and y must be the same
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(final double[][] x,final double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    boolean obsOk=true;
    for( int i = 0 ; i < x.length; i++){
        if( x[i] == null || x[i].length == 0 ){
            obsOk = false;
        }
    }
    if( !obsOk ){
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              0, 1);
    }
    for( int i = 0 ; i < x.length ; i++){
        addData( x[i][0], y[i] );
    }
    return;
}
 
Example 5
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model.
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) throws ModelSpecificationException {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        addObservation(x[i], y[i]);
    }
}
 
Example 6
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model.
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) throws ModelSpecificationException {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        addObservation(x[i], y[i]);
    }
}
 
Example 7
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds a series of observations to the regression model. The lengths of
 * x and y must be the same and x must be rectangular.
 *
 * @param x a series of observations on the independent variables
 * @param y a series of observations on the dependent variable
 * The length of x and y must be the same
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(final double[][] x,final double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    boolean obsOk=true;
    for( int i = 0 ; i < x.length; i++){
        if( x[i] == null || x[i].length == 0 ){
            obsOk = false;
        }
    }
    if( !obsOk ){
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              0, 1);
    }
    for( int i = 0 ; i < x.length ; i++){
        addData( x[i][0], y[i] );
    }
}
 
Example 8
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model.
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        addObservation(x[i], y[i]);
    }
}
 
Example 9
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds a series of observations to the regression model. The lengths of
 * x and y must be the same and x must be rectangular.
 *
 * @param x a series of observations on the independent variables
 * @param y a series of observations on the dependent variable
 * The length of x and y must be the same
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(final double[][] x,final double[] y) {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    boolean obsOk=true;
    for( int i = 0 ; i < x.length; i++){
        if( x[i] == null || x[i].length == 0 ){
            obsOk = false;
        }
    }
    if( !obsOk ){
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              0, 1);
    }
    for( int i = 0 ; i < x.length ; i++){
        addData( x[i][0], y[i] );
    }
}
 
Example 10
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds multiple observations to the model.
 * @param x observations on the regressors
 * @param y observations on the regressand
 * @throws ModelSpecificationException if {@code x} is not rectangular, does not match
 * the length of {@code y} or does not contain sufficient data to estimate the model
 */
public void addObservations(double[][] x, double[] y) throws ModelSpecificationException {
    if ((x == null) || (y == null) || (x.length != y.length)) {
        throw new ModelSpecificationException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
              (x == null) ? 0 : x.length,
              (y == null) ? 0 : y.length);
    }
    if (x.length == 0) {  // Must be no y data either
        throw new ModelSpecificationException(
                LocalizedFormats.NO_DATA);
    }
    if (x[0].length + 1 > x.length) {
        throw new ModelSpecificationException(
              LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
              x.length, x[0].length);
    }
    for (int i = 0; i < x.length; i++) {
        addObservation(x[i], y[i]);
    }
}
 
Example 11
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Conducts a regression on the data in the model, using a subset of regressors.
 *
 * @param numberOfRegressors many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return RegressionResults the structure holding all regression results
 * @exception  ModelSpecificationException - thrown if number of observations is
 * less than the number of variables or number of regressors requested
 * is greater than the regressors in the model
 */
public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException {
    if (this.nobs <= numberOfRegressors) {
       throw new ModelSpecificationException(
               LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
               this.nobs, numberOfRegressors);
    }
    if( numberOfRegressors > this.nvars ){
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars);
    }

    tolset();
    singcheck();

    double[] beta = this.regcf(numberOfRegressors);

    ss();

    double[] cov = this.cov(numberOfRegressors);

    int rnk = 0;
    for (int i = 0; i < this.lindep.length; i++) {
        if (!this.lindep[i]) {
            ++rnk;
        }
    }

    boolean needsReorder = false;
    for (int i = 0; i < numberOfRegressors; i++) {
        if (this.vorder[i] != i) {
            needsReorder = true;
            break;
        }
    }
    if (!needsReorder) {
        return new RegressionResults(
                beta, new double[][]{cov}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    } else {
        double[] betaNew = new double[beta.length];
        double[] covNew = new double[cov.length];

        int[] newIndices = new int[beta.length];
        for (int i = 0; i < nvars; i++) {
            for (int j = 0; j < numberOfRegressors; j++) {
                if (this.vorder[j] == i) {
                    betaNew[i] = beta[ j];
                    newIndices[i] = j;
                }
            }
        }

        int idx1 = 0;
        int idx2;
        int _i;
        int _j;
        for (int i = 0; i < beta.length; i++) {
            _i = newIndices[i];
            for (int j = 0; j <= i; j++, idx1++) {
                _j = newIndices[j];
                if (_i > _j) {
                    idx2 = _i * (_i + 1) / 2 + _j;
                } else {
                    idx2 = _j * (_j + 1) / 2 + _i;
                }
                covNew[idx1] = cov[idx2];
            }
        }
        return new RegressionResults(
                betaNew, new double[][]{covNew}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    }
}
 
Example 12
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates sample data.  Checks that
 * <ul><li>Neither x nor y is null or empty;</li>
 * <li>The length (i.e. number of rows) of x equals the length of y</li>
 * <li>x has at least one more row than it has columns (i.e. there is
 * sufficient data to estimate regression coefficients for each of the
 * columns in x plus an intercept.</li>
 * </ul>
 *
 * @param x the [n,k] array representing the x data
 * @param y the [n,1] array representing the y data
 * @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)) {
        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 new NoDataException();
    }
    if (x[0].length + 1 > x.length) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                x.length, x[0].length);
    }
}
 
Example 13
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates sample data.  Checks that
 * <ul><li>Neither x nor y is null or empty;</li>
 * <li>The length (i.e. number of rows) of x equals the length of y</li>
 * <li>x has at least one more row than it has columns (i.e. there is
 * sufficient data to estimate regression coefficients for each of the
 * columns in x plus an intercept.</li>
 * </ul>
 *
 * @param x the [n,k] array representing the x data
 * @param y the [n,1] array representing the y data
 * @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)) {
        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 new NoDataException();
    }
    if (x[0].length + 1 > x.length) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                x.length, x[0].length);
    }
}
 
Example 14
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Conducts a regression on the data in the model, using a subset of regressors.
 *
 * @param numberOfRegressors many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return RegressionResults the structure holding all regression results
 * @exception  ModelSpecificationException - thrown if number of observations is
 * less than the number of variables or number of regressors requested
 * is greater than the regressors in the model
 */
public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException {
    if (this.nobs <= numberOfRegressors) {
       throw new ModelSpecificationException(
               LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
               this.nobs, numberOfRegressors);
    }
    if( numberOfRegressors > this.nvars ){
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars);
    }

    tolset();
    singcheck();

    double[] beta = this.regcf(numberOfRegressors);

    ss();

    double[] cov = this.cov(numberOfRegressors);

    int rnk = 0;
    for (int i = 0; i < this.lindep.length; i++) {
        if (!this.lindep[i]) {
            ++rnk;
        }
    }

    boolean needsReorder = false;
    for (int i = 0; i < numberOfRegressors; i++) {
        if (this.vorder[i] != i) {
            needsReorder = true;
            break;
        }
    }
    if (!needsReorder) {
        return new RegressionResults(
                beta, new double[][]{cov}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    } else {
        double[] betaNew = new double[beta.length];
        double[] covNew = new double[cov.length];

        int[] newIndices = new int[beta.length];
        for (int i = 0; i < nvars; i++) {
            for (int j = 0; j < numberOfRegressors; j++) {
                if (this.vorder[j] == i) {
                    betaNew[i] = beta[ j];
                    newIndices[i] = j;
                }
            }
        }

        int idx1 = 0;
        int idx2;
        int _i;
        int _j;
        for (int i = 0; i < beta.length; i++) {
            _i = newIndices[i];
            for (int j = 0; j <= i; j++, idx1++) {
                _j = newIndices[j];
                if (_i > _j) {
                    idx2 = _i * (_i + 1) / 2 + _j;
                } else {
                    idx2 = _j * (_j + 1) / 2 + _i;
                }
                covNew[idx1] = cov[idx2];
            }
        }
        return new RegressionResults(
                betaNew, new double[][]{covNew}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    }
}
 
Example 15
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates sample data.  Checks that
 * <ul><li>Neither x nor y is null or empty;</li>
 * <li>The length (i.e. number of rows) of x equals the length of y</li>
 * <li>x has at least one more row than it has columns (i.e. there is
 * sufficient data to estimate regression coefficients for each of the
 * columns in x plus an intercept.</li>
 * </ul>
 *
 * @param x the [n,k] array representing the x data
 * @param y the [n,1] array representing the y data
 * @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) throws MathIllegalArgumentException {
    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 new NoDataException();
    }
    if (x[0].length + 1 > x.length) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                x.length, x[0].length);
    }
}
 
Example 16
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Conducts a regression on the data in the model, using a subset of regressors.
 *
 * @param numberOfRegressors many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return RegressionResults the structure holding all regression results
 * @exception  ModelSpecificationException - thrown if number of observations is
 * less than the number of variables or number of regressors requested
 * is greater than the regressors in the model
 */
public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException {
    if (this.nobs <= numberOfRegressors) {
       throw new ModelSpecificationException(
               LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
               this.nobs, numberOfRegressors);
    }
    if( numberOfRegressors > this.nvars ){
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars);
    }

    tolset();
    singcheck();

    double[] beta = this.regcf(numberOfRegressors);

    ss();

    double[] cov = this.cov(numberOfRegressors);

    int rnk = 0;
    for (int i = 0; i < this.lindep.length; i++) {
        if (!this.lindep[i]) {
            ++rnk;
        }
    }

    boolean needsReorder = false;
    for (int i = 0; i < numberOfRegressors; i++) {
        if (this.vorder[i] != i) {
            needsReorder = true;
            break;
        }
    }
    if (!needsReorder) {
        return new RegressionResults(
                beta, new double[][]{cov}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    } else {
        double[] betaNew = new double[beta.length];
        double[] covNew = new double[cov.length];

        int[] newIndices = new int[beta.length];
        for (int i = 0; i < nvars; i++) {
            for (int j = 0; j < numberOfRegressors; j++) {
                if (this.vorder[j] == i) {
                    betaNew[i] = beta[ j];
                    newIndices[i] = j;
                }
            }
        }

        int idx1 = 0;
        int idx2;
        int _i;
        int _j;
        for (int i = 0; i < beta.length; i++) {
            _i = newIndices[i];
            for (int j = 0; j <= i; j++, idx1++) {
                _j = newIndices[j];
                if (_i > _j) {
                    idx2 = _i * (_i + 1) / 2 + _j;
                } else {
                    idx2 = _j * (_j + 1) / 2 + _i;
                }
                covNew[idx1] = cov[idx2];
            }
        }
        return new RegressionResults(
                betaNew, new double[][]{covNew}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    }
}
 
Example 17
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Conducts a regression on the data in the model, using a subset of regressors.
 *
 * @param numberOfRegressors many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return RegressionResults the structure holding all regression results
 * @exception  ModelSpecificationException - thrown if number of observations is
 * less than the number of variables or number of regressors requested
 * is greater than the regressors in the model
 */
public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException {
    if (this.nobs <= numberOfRegressors) {
       throw new ModelSpecificationException(
               LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
               this.nobs, numberOfRegressors);
    }
    if( numberOfRegressors > this.nvars ){
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars);
    }

    tolset();
    singcheck();

    double[] beta = this.regcf(numberOfRegressors);

    ss();

    double[] cov = this.cov(numberOfRegressors);

    int rnk = 0;
    for (int i = 0; i < this.lindep.length; i++) {
        if (!this.lindep[i]) {
            ++rnk;
        }
    }

    boolean needsReorder = false;
    for (int i = 0; i < numberOfRegressors; i++) {
        if (this.vorder[i] != i) {
            needsReorder = true;
            break;
        }
    }
    if (!needsReorder) {
        return new RegressionResults(
                beta, new double[][]{cov}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    } else {
        double[] betaNew = new double[beta.length];
        double[] covNew = new double[cov.length];

        int[] newIndices = new int[beta.length];
        for (int i = 0; i < nvars; i++) {
            for (int j = 0; j < numberOfRegressors; j++) {
                if (this.vorder[j] == i) {
                    betaNew[i] = beta[ j];
                    newIndices[i] = j;
                }
            }
        }

        int idx1 = 0;
        int idx2;
        int _i;
        int _j;
        for (int i = 0; i < beta.length; i++) {
            _i = newIndices[i];
            for (int j = 0; j <= i; j++, idx1++) {
                _j = newIndices[j];
                if (_i > _j) {
                    idx2 = _i * (_i + 1) / 2 + _j;
                } else {
                    idx2 = _j * (_j + 1) / 2 + _i;
                }
                covNew[idx1] = cov[idx2];
            }
        }
        return new RegressionResults(
                betaNew, new double[][]{covNew}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    }
}
 
Example 18
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Conducts a regression on the data in the model, using a subset of regressors.
 *
 * @param numberOfRegressors many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return RegressionResults the structure holding all regression results
 * @exception  ModelSpecificationException - thrown if number of observations is
 * less than the number of variables or number of regressors requested
 * is greater than the regressors in the model
 */
public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException {
    if (this.nobs <= numberOfRegressors) {
       throw new ModelSpecificationException(
               LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
               this.nobs, numberOfRegressors);
    }
    if( numberOfRegressors > this.nvars ){
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars);
    }

    tolset();
    singcheck();

    double[] beta = this.regcf(numberOfRegressors);

    ss();

    double[] cov = this.cov(numberOfRegressors);

    int rnk = 0;
    for (int i = 0; i < this.lindep.length; i++) {
        if (!this.lindep[i]) {
            ++rnk;
        }
    }

    boolean needsReorder = false;
    for (int i = 0; i < numberOfRegressors; i++) {
        if (this.vorder[i] != i) {
            needsReorder = true;
            break;
        }
    }
    if (!needsReorder) {
        return new RegressionResults(
                beta, new double[][]{cov}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    } else {
        double[] betaNew = new double[beta.length];
        double[] covNew = new double[cov.length];

        int[] newIndices = new int[beta.length];
        for (int i = 0; i < nvars; i++) {
            for (int j = 0; j < numberOfRegressors; j++) {
                if (this.vorder[j] == i) {
                    betaNew[i] = beta[ j];
                    newIndices[i] = j;
                }
            }
        }

        int idx1 = 0;
        int idx2;
        int _i;
        int _j;
        for (int i = 0; i < beta.length; i++) {
            _i = newIndices[i];
            for (int j = 0; j <= i; j++, idx1++) {
                _j = newIndices[j];
                if (_i > _j) {
                    idx2 = _i * (_i + 1) / 2 + _j;
                } else {
                    idx2 = _j * (_j + 1) / 2 + _i;
                }
                covNew[idx1] = cov[idx2];
            }
        }
        return new RegressionResults(
                betaNew, new double[][]{covNew}, true, this.nobs, rnk,
                this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
    }
}
 
Example 19
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates sample data.  Checks that
 * <ul><li>Neither x nor y is null or empty;</li>
 * <li>The length (i.e. number of rows) of x equals the length of y</li>
 * <li>x has at least one more row than it has columns (i.e. there is
 * sufficient data to estimate regression coefficients for each of the
 * columns in x plus an intercept.</li>
 * </ul>
 *
 * @param x the [n,k] array representing the x data
 * @param y the [n,1] array representing the y data
 * @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) throws MathIllegalArgumentException {
    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 new NoDataException();
    }
    if (x[0].length + 1 > x.length) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                x.length, x[0].length);
    }
}
 
Example 20
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validates sample data.  Checks that
 * <ul><li>Neither x nor y is null or empty;</li>
 * <li>The length (i.e. number of rows) of x equals the length of y</li>
 * <li>x has at least one more row than it has columns (i.e. there is
 * sufficient data to estimate regression coefficients for each of the
 * columns in x plus an intercept.</li>
 * </ul>
 *
 * @param x the [n,k] array representing the x data
 * @param y the [n,1] array representing the y data
 * @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) throws MathIllegalArgumentException {
    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 new NoDataException();
    }
    if (x[0].length + 1 > x.length) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS,
                x.length, x[0].length);
    }
}