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

The following examples show how to use org.apache.commons.math.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++) {
        this.addObservation(x[i], y[i]);
    }
    return;
}
 
Example 2
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 3
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);
    }
    this.tolset();

    this.singcheck();

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

    this.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 4
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);
    }
    this.tolset();

    this.singcheck();

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

    this.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);
    }
}