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 { return new RegressionResults(params, new double[][] { vcv }, true, n, 1, sumY, sumYY, getSumSquaredErrors(), true, false); 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);
/** * <p>Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard * error of the parameter estimates</a>, * usually denoted s(b<sub>i</sub>).</p> * * <p>If there are problems with an ill conditioned design matrix then the regressor * which is redundant will be assigned <code>Double.NaN</code>. </p> * * @return an array standard errors associated with parameters estimates, * null if no estimation occurred */ public double[] getStdErrorOfEstimates() { if (parameters == null) { return null; } double[] se = new double[this.parameters.length]; for (int i = 0; i < this.parameters.length; i++) { double var = this.getVcvElement(i, i); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { se[i] = FastMath.sqrt(var); continue; } se[i] = Double.NaN; } return se; }
@Override LR.ModelResult asResult() { LR.ModelResult r = new LR.ModelResult(name, framework, hasConstant(), getNumVars(), state, getNTrain(), getNTest()); if (trained != null) r.withTrainInfo("parameters", LR.doubleArrayToList(trained.getParameterEstimates()), "SSE", trained.getErrorSumSquares(), "RSquared", trained.getRSquared(), "adjRSquared", trained.getAdjustedRSquared(), "MSE", trained.getMeanSquareError(), "SSR", trained.getRegressionSumSquares(), "parameters std error", trained.getStdErrorOfEstimates(), "SST", trained.getTotalSumSquares()); if (tester.isReady()) r.withTestInfo(tester.getStatistics()); return r; } ///////////UTILS//////////
@Override Object data() { return trained.getParameterEstimates(); }
public Function<Date,Double> getPredictorFunction (double probSubestimation) { final int n = (int) regResuls.getN(); final double estimIntercept = regResuls.getParameterEstimate(0); final double estimSlope = regResuls.getParameterEstimate(1); final double tStudentNMinus2ThisConfidenceInter = new TDistribution(regResuls.getN() - 2).inverseCumulativeProbability(1-probSubestimation); final double sy = Math.sqrt(regResuls.getTotalSumSquares() / (n-2)); final double sx2 = sumSquaresOfX; // getXSumSquares() in the regression original object, but not in RegressionResults final double meanX = monitValues.values().stream().mapToDouble(e->e).sum() / monitValues.size(); if (fittingType.isLinear()) return d ->estimIntercept + estimSlope * d.getTime() + tStudentNMinus2ThisConfidenceInter * sy * Math.sqrt(1 + (1/n) + Math.pow(d.getTime() - meanX , 2) / ((n-1) * sx2)); else if (fittingType.isExponential()) return d ->Math.exp(estimIntercept + estimSlope * d.getTime() + tStudentNMinus2ThisConfidenceInter * sy * Math.sqrt(1 + (1/n) + Math.pow(d.getTime() - meanX , 2) / ((n-1) * sx2))); throw new Net2PlanException ("Unknown"); } public FITTINGTYPE getFittingType()
public Function<Date,Double> getPredictorFunctionNoConfidenceInterval () { final int n = (int) regResuls.getN(); final double estimIntercept = regResuls.getParameterEstimate(0); final double estimSlope = regResuls.getParameterEstimate(1); if (fittingType.isLinear()) return d ->estimIntercept + estimSlope * d.getTime(); else if (fittingType.isExponential()) return d ->Math.exp(estimIntercept + estimSlope * d.getTime()); throw new Net2PlanException ("Unknown"); }
final TrafficSeries.TrafficPredictor tp = tm.getFunctionPredictionSoProbSubestimationIsBounded(fittingType); final double val = tp.getPredictorFunction(probSubestimation).apply(date); varianceExplained.put(ee, tp.getRegResuls().getRSquared()); if (storeAsNewSample) tm.addValue(date, val); else
@Override double predict(List<Double> given) { if (state == State.created || state == State.training) train(); if (state == State.ready || state == State.testing) { double[] params = trained.getParameterEstimates(); double result; if (R.hasIntercept()) { result = params[0]; for (int i = 0; i < numVars; i++) result += params[i+1]*given.get(i); } else { result = 0; for (int i = 0; i < numVars; i++) result += params[i]*given.get(i); } return result; } throw new RuntimeException("Model in state '" + state.name() + "' so cannot make predictions."); }
return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, _syy+_mean, sumYY,true,false); final double[] vcv = new double[]{ _mse / _sxx }; final double[] params = new double[]{ _sxy/_sxx }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, _syy, _sse,false,false); 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);
/** * Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard * error of the parameter estimate at index</a>, * usually denoted s(b<sub>index</sub>). * * @param index Index. * @return the standard errors associated with parameters estimated at index. * @throws OutOfRangeException if {@code index} is not in the interval * {@code [0, number of parameters)}. */ public double getStdErrorOfEstimate(int index) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (index < 0 || index >= this.parameters.length) { throw new OutOfRangeException(index, 0, this.parameters.length - 1); } double var = this.getVcvElement(index, index); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { return FastMath.sqrt(var); } return Double.NaN; }
/** * Gets the Local Regression (Loess) parameter estimates. * * @param y the y array * @return the Loess parameter estimates */ public static double[] getLoessParameterEstimates(final double[] y) { int n = y.length; double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = i + 1; } return createWeigthedLinearRegression(x, y, getTricubeWeigts(n)) .regress().getParameterEstimates(); }
return new RegressionResults( beta, new double[][]{cov}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); return new RegressionResults( betaNew, new double[][]{covNew}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
/** * <p>Returns the covariance between regression parameters i and j.</p> * * <p>If there are problems with an ill conditioned design matrix then the covariance * which involves redundant columns will be assigned {@code Double.NaN}. </p> * * @param i {@code i}th regression parameter. * @param j {@code j}th regression parameter. * @return the covariance of the parameter estimates. * @throws OutOfRangeException if {@code i} or {@code j} is not in the * interval {@code [0, number of parameters)}. */ public double getCovarianceOfParameters(int i, int j) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (i < 0 || i >= this.parameters.length) { throw new OutOfRangeException(i, 0, this.parameters.length - 1); } if (j < 0 || j >= this.parameters.length) { throw new OutOfRangeException(j, 0, this.parameters.length - 1); } return this.getVcvElement(i, j); }
@Override MillerLRModel test() { state = State.ready; tester.newTestData(trained.getParameterEstimates(), hasConstant(), getNumVars(), sst, sse, getNTest()); return this; }
return new RegressionResults( beta, new double[][]{cov}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); return new RegressionResults( betaNew, new double[][]{covNew}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false);
/** * <p>Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard * error of the parameter estimates</a>, * usually denoted s(b<sub>i</sub>).</p> * * <p>If there are problems with an ill conditioned design matrix then the regressor * which is redundant will be assigned <code>Double.NaN</code>. </p> * * @return an array standard errors associated with parameters estimates, * null if no estimation occurred */ public double[] getStdErrorOfEstimates() { if (parameters == null) { return null; } double[] se = new double[this.parameters.length]; for (int i = 0; i < this.parameters.length; i++) { double var = this.getVcvElement(i, i); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { se[i] = Math.sqrt(var); continue; } se[i] = Double.NaN; } return se; }
@Override void addTest(List<Double> given, double expected, Log log) { if (!(state == State.testing)) { clearTest(); train(); } if (dataInvalid(given)) log.warn("Data point " + given.toString() + ", " + Double.toString(expected) + " is not valid and so was not added to the testing data."); else { double fact1 = nTest + 1.0; double fact2 = nTest / fact1; double dy = expected - ybar; ybar += dy / fact1; double[] params = trained.getParameterEstimates(); double rdev = expected; if (hasConstant()) { rdev -= params[0]; for (int i = 1; i < params.length; i++) rdev -= params[i] * given.get(i - 1); sst += fact2 * dy * dy; } else { for (int i = 0; i < params.length; i++) rdev -= params[i] * given.get(i); sst += expected * expected; } sse += rdev * rdev; nTest++; state = State.testing; } }
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 { return new RegressionResults(params, new double[][] { vcv }, true, n, 1, sumY, sumYY, getSumSquaredErrors(), true, false); 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);
/** * Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard * error of the parameter estimate at index</a>, * usually denoted s(b<sub>index</sub>). * * @param index Index. * @return the standard errors associated with parameters estimated at index. * @throws OutOfRangeException if {@code index} is not in the interval * {@code [0, number of parameters)}. */ public double getStdErrorOfEstimate(int index) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (index < 0 || index >= this.parameters.length) { throw new OutOfRangeException(index, 0, this.parameters.length - 1); } double var = this.getVcvElement(index, index); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { return Math.sqrt(var); } return Double.NaN; }
/** * Gets the robust loess parameter estimates. * * @param y the y array * @return the robust loess parameter estimates */ public static double[] getRobustLoessParameterEstimates(final double[] y) { int n = y.length; double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = i + 1; } SimpleRegression tricubeRegression = createWeigthedLinearRegression(x, y, getTricubeWeigts(n)); double[] residuals = new double[n]; for (int i = 0; i < n; i++) { residuals[i] = y[i] - tricubeRegression.predict(x[i]); } SimpleRegression tricubeBySquareRegression = createWeigthedLinearRegression( x, y, getTricubeBisquareWeigts(residuals)); double[] estimates = tricubeBySquareRegression.regress() .getParameterEstimates(); if (Double.isNaN(estimates[0]) || Double.isNaN(estimates[1])) { return tricubeRegression.regress().getParameterEstimates(); } return estimates; }