/** * 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]); } }
/** * Conducts a regression on the data in the model, using all regressors. * * @return RegressionResults the structure holding all regression results * @exception ModelSpecificationException - thrown if number of observations is * less than the number of variables */ public RegressionResults regress() throws ModelSpecificationException { return regress(this.nvars); }
/** * Adds an observation to the regression model. * @param x the array with regressor values * @param y the value of dependent variable given these regressors * @exception ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ public void addObservation(final double[] x, final double y) throws ModelSpecificationException { if ((!this.hasIntercept && x.length != nvars) || (this.hasIntercept && x.length + 1 != nvars)) { throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION, x.length, nvars); } if (!this.hasIntercept) { include(MathArrays.copyOf(x, x.length), 1.0, y); } else { final double[] tmp = new double[x.length + 1]; System.arraycopy(x, 0, tmp, 1, x.length); tmp[0] = 1.0; include(tmp, 1.0, y); } ++nobs; }
double _w; this.rss_set = false; sumy = smartAdd(yi, sumy); sumsqy = smartAdd(sumsqy, yi * yi); for (int i = 0; i < x.length; i++) { if (w == 0.0) { _w = w; if (di != 0.0) { dpi = smartAdd(di, wxi * xi); final double tmp = wxi * xi / di; if (FastMath.abs(tmp) > Precision.EPSILON) { for (int k = i + 1; k < nvars; k++) { xk = x[k]; x[k] = smartAdd(xk, -xi * r[nextr]); if (di != 0.0) { r[nextr] = smartAdd(di * r[nextr], (_w * xi) * xk) / dpi; } else { r[nextr] = xk / xi; y = smartAdd(xk, -xi * rhs[i]); if (di != 0.0) { rhs[i] = smartAdd(di * rhs[i], wxi * xk) / dpi; } else { rhs[i] = xk / xi; sserr = smartAdd(sserr, w * y * y);
inverse(rinv, nreq); final double[] covmat = new double[nreq * (nreq + 1) / 2]; Arrays.fill(covmat, Double.NaN);
d[col] = 0.0; rhs[col] = 0.0; this.include(x_sing, weight, y); } else { sserr += d[col] * rhs[col] * rhs[col];
inverse(rinv, nreq); final double[] covmat = new double[nreq * (nreq + 1) / 2]; Arrays.fill(covmat, Double.NaN);
@Override MillerLRModel train() { trained = R.regress(); state = State.testing; return this; } /////////TESTING///////////
@Override void addTrain(List<Double> given, double expected, Log log) { if (dataInvalid(given)) log.warn("Data point " + given.toString() + ", " + Double.toString(expected) + " is not valid and so was not added to the training data."); else { double[] givenArr = LR.doubleListToArray(given); R.addObservation(givenArr, expected); if (state == State.testing || state == State.ready) resetTest(); state = State.training; } }
d[col] = 0.0; rhs[col] = 0.0; this.include(x_sing, weight, y); } else { sserr += d[col] * rhs[col] * rhs[col];
double _w; this.rss_set = false; sumy = smartAdd(yi, sumy); sumsqy = smartAdd(sumsqy, yi * yi); for (int i = 0; i < x.length; i++) { if (w == 0.0) { _w = w; if (di != 0.0) { dpi = smartAdd(di, wxi * xi); final double tmp = wxi * xi / di; if (Math.abs(tmp) > Precision.EPSILON) { for (int k = i + 1; k < nvars; k++) { xk = x[k]; x[k] = smartAdd(xk, -xi * r[nextr]); if (di != 0.0) { r[nextr] = smartAdd(di * r[nextr], (_w * xi) * xk) / dpi; } else { r[nextr] = xk / xi; y = smartAdd(xk, -xi * rhs[i]); if (di != 0.0) { rhs[i] = smartAdd(di * rhs[i], wxi * xk) / dpi; } else { rhs[i] = xk / xi; sserr = smartAdd(sserr, w * y * y);
inverse(rinv, nreq); final double[] covmat = new double[nreq * (nreq + 1) / 2]; Arrays.fill(covmat, Double.NaN);