Example usage for org.apache.commons.math3.exception.util LocalizedFormats NOT_ENOUGH_DATA_REGRESSION

List of usage examples for org.apache.commons.math3.exception.util LocalizedFormats NOT_ENOUGH_DATA_REGRESSION

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception.util LocalizedFormats NOT_ENOUGH_DATA_REGRESSION.

Prototype

LocalizedFormats NOT_ENOUGH_DATA_REGRESSION

To view the source code for org.apache.commons.math3.exception.util LocalizedFormats NOT_ENOUGH_DATA_REGRESSION.

Click Source Link

Usage

From source file:experiment.SimpleRegression_bug.java

/**
 * Performs a regression on data present in buffers and outputs a RegressionResults object.
 *
 * <p>If there are fewer than 3 observations in the model and {@code hasIntercept} is true
 * a {@code NoDataException} is thrown.  If there is no intercept term, the model must
 * contain at least 2 observations.</p>
 *
 * @return RegressionResults acts as a container of regression output
 * @throws ModelSpecificationException if the model is not correctly specified
 * @throws NoDataException if there is not sufficient data in the model to
 * estimate the regression parameters//from  www  .j a v a2  s .c o  m
 */
public RegressionResults regress() throws ModelSpecificationException, NoDataException {
    if (hasIntercept) {
        if (n < 3) {
            throw new NoDataException(LocalizedFormats.NOT_ENOUGH_DATA_REGRESSION);
        }
        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[] vcv = new double[] { 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 mse = getMeanSquareError();
            final double[] vcv = new double[] { ybar / (n - 1.0), Double.NaN, Double.NaN };
            return new RegressionResults(params, new double[][] { vcv }, true, n, 1, sumY, sumYY,
                    getSumSquaredErrors(), true, false);
        }
    } else {
        if (n < 2) {
            throw new NoDataException(LocalizedFormats.NOT_ENOUGH_DATA_REGRESSION);
        }
        if (!Double.isNaN(sumXX)) {
            final double[] vcv = new double[] { getMeanSquareError() / sumXX };
            final double[] params = new double[] { sumXY / sumXX };
            return new RegressionResults(params, new double[][] { vcv }, true, n, 1, sumY, sumYY,
                    getSumSquaredErrors(), false, false);
        } else {
            final double[] vcv = new double[] { Double.NaN };
            final double[] params = new double[] { Double.NaN };
            return new RegressionResults(params, new double[][] { vcv }, true, n, 1, Double.NaN, Double.NaN,
                    Double.NaN, false, false);
        }
    }
}