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

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#TOO_MANY_REGRESSORS . 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 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 * @throws ModelSpecificationException if {@code nreq} is less than 1
 * or greater than the number of independent variables
 */
private double[] regcf(int nreq) throws ModelSpecificationException {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    final double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (FastMath.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 2
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 */
private double[] regcf(int nreq) {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 3
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 */
private double[] regcf(int nreq) {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 4
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 * @throws ModelSpecificationException if {@code nreq} is less than 1
 * or greater than the number of independent variables
 */
private double[] regcf(int nreq) throws ModelSpecificationException {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    final double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 5
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 */
private double[] regcf(int nreq) {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 6
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 * @throws ModelSpecificationException if {@code nreq} is less than 1
 * or greater than the number of independent variables
 */
private double[] regcf(int nreq) throws ModelSpecificationException {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    final double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 7
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 * @throws ModelSpecificationException if {@code nreq} is less than 1
 * or greater than the number of independent variables
 */
private double[] regcf(int nreq) throws ModelSpecificationException {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    final double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (Math.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 8
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The regcf method conducts the linear regression and extracts the
 * parameter vector. Notice that the algorithm can do subset regression
 * with no alteration.
 *
 * @param nreq how many of the regressors to include (either in canonical
 * order, or in the current reordered state)
 * @return an array with the estimated slope coefficients
 * @throws ModelSpecificationException if {@code nreq} is less than 1
 * or greater than the number of independent variables
 */
private double[] regcf(int nreq) throws ModelSpecificationException {
    int nextr;
    if (nreq < 1) {
        throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS);
    }
    if (nreq > this.nvars) {
        throw new ModelSpecificationException(
                LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars);
    }
    if (!this.tol_set) {
        tolset();
    }
    final double[] ret = new double[nreq];
    boolean rankProblem = false;
    for (int i = nreq - 1; i > -1; i--) {
        if (FastMath.sqrt(d[i]) < tol[i]) {
            ret[i] = 0.0;
            d[i] = 0.0;
            rankProblem = true;
        } else {
            ret[i] = rhs[i];
            nextr = i * (nvars + nvars - i - 1) / 2;
            for (int j = i + 1; j < nreq; j++) {
                ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]);
                ++nextr;
            }
        }
    }
    if (rankProblem) {
        for (int i = 0; i < nreq; i++) {
            if (this.lindep[i]) {
                ret[i] = Double.NaN;
            }
        }
    }
    return ret;
}
 
Example 9
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 10
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 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: 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 13
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 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: 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 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);
    }
}