/** * Gets the tricube bisquare weigths. * * @param residuals the residuals array * @return the tricube bisquare weigths * @todo The word "weight" is misspelled in the method name. */ public static double[] getTricubeBisquareWeigts(final double[] residuals) { int n = residuals.length; double[] weights = getTricubeWeigts(n); double[] weights2 = new double[n]; double s6 = median(abs(residuals)) * 6; for (int i = 2; i < n; i++) { double k = Math.pow(1 - Math.pow(residuals[i] / s6, 2), 2); if (k > 0) { weights2[i] = (1 / k) * weights[i]; } else { weights2[i] = Double.MAX_VALUE; } } weights2[0] = weights2[1] = weights2[2]; return weights2; }
/** * 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(); }
/** * 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; }