Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/07/17 17:03:02 (6 years ago)
Author:
gkronber
Message:

#2789 added Finbarr O'Sullivan smoothing spline code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MathNetNumerics-Exploration-2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/Splines.cs

    r15450 r15457  
    7070          "B-Spline Smoothing",
    7171          "Penalized Regression Spline (alglib)",
    72           "CUBGCV"
     72          "CUBGCV",
     73          "Finnbar O'Sullivan (sbart)"
    7374      }.Select(s => new StringValue(s)));
    7475
     
    172173              break;
    173174            }
     175          case "Finnbar O'Sullivan (sbart)": {
     176              SBART.SBART_Report report;
     177              var lambda = ((IValueParameter<DoubleValue>)Parameters["Lambda"]).Value.Value; // controls extent of smoothing
     178              var model =
     179                SBART.CalculateSBART(x, y,
     180                Problem.ProblemData.TargetVariable, inputVars, (float)lambda, out report);
     181              var targetVar = Problem.ProblemData.TargetVariable;
     182              var problemData = (IRegressionProblemData)Problem.ProblemData.Clone();
     183              Results.Add(new Result("Solution", model.CreateRegressionSolution(problemData)));
     184              Results.Add(new Result("GCV", new DoubleValue(report.gcv)));
     185              Results.Add(new Result("Smoothing parameter (optimized for GCV)", new DoubleValue(report.smoothingParameter)));
     186              break;
     187
     188            }
    174189          case "B-Spline Smoothing": {
    175190              BSplineSmoothing(x, y, inputVars);
     
    205220    public static IRegressionEnsembleModel CalculatePenalizedRegressionSpline(double[] x, double[] y, double lambda, string targetVar, string[] inputVars,
    206221      out double avgTrainRMSE,
    207       out double cvRMSE) {
     222      out double cvRMSE, out double[] predictions) {
    208223      int info;
    209224      alglib.spline1dinterpolant interpolant;
     
    218233      double[] x_loo = new double[(folds - 1) * foldSize];
    219234      double[] y_loo = new double[(folds - 1) * foldSize];
     235      double[] w_loo = Enumerable.Repeat(1.0, x_loo.Length).ToArray();
    220236
    221237      var models = new List<IRegressionModel>();
     
    225241      int nTest = 0;
    226242
     243      predictions = new double[y.Length];
    227244
    228245      for (int i = 0; i < folds; i++) {
     
    232249          Array.Copy(y, (i + 1) * foldSize, y_loo, i * foldSize, (folds - i - 1) * foldSize);
    233250        }
    234         alglib.spline1dfitpenalized(x_loo, y_loo, n, lambda, out info, out interpolant, out rep);
    235         Debug.Assert(y_loo.All(yi => yi != 0.0));
    236         Debug.Assert(x_loo.All(xi => xi != 0.0));
     251        alglib.spline1dfitpenalizedw(x_loo, y_loo, w_loo, n, lambda, out info, out interpolant, out rep);
     252        //Debug.Assert(y_loo.All(yi => yi != 0.0));
     253        //Debug.Assert(x_loo.All(xi => xi != 0.0));
    237254
    238255        // training error
    239256        for (int j = 0; j < x_loo.Length; j++) {
    240           var y_pred = alglib.spline1dcalc(interpolant, x[i]);
     257          var y_pred = alglib.spline1dcalc(interpolant, x[j]);
    241258          double res = y[j] - y_pred;
    242259          sumTrainSE += res * res;
     
    245262        // test
    246263        for (int j = i * foldSize; j < (i + 1) * foldSize; j++) {
    247           var y_pred = alglib.spline1dcalc(interpolant, x[i]);
    248           double res = y[j] - y_pred;
     264          predictions[j] = alglib.spline1dcalc(interpolant, x[j]);
     265          double res = y[j] - predictions[j];
    249266          sumTestSE += res * res;
    250267          nTest++;
     
    630647      int n = x.Length;
    631648      var range = x[n - 1] - x[0];
    632       var binSize = range / n / 20;
     649      var binSize = range / n / 10;
    633650      {
    634651        // binning
     
    759776
    760777    public double GetEstimatedValue(double xx) {
     778      xx = (xx - offset) * scale;
    761779      int n = a.Length;
    762780      if (xx <= x[1]) {
     
    791809        product = product.Zip(dataset.GetDoubleValues(factor, rows), (pi, fi) => pi * fi).ToArray();
    792810      }
    793       foreach (var xx in product.Select(xi => (xi - offset) * scale)) {
     811      foreach (var xx in product) {
    794812        yield return GetEstimatedValue(xx);
    795813      }
Note: See TracChangeset for help on using the changeset viewer.