Changeset 15457 for branches/MathNetNumerics-Exploration-2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/Splines.cs
- Timestamp:
- 11/07/17 17:03:02 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MathNetNumerics-Exploration-2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/Splines.cs
r15450 r15457 70 70 "B-Spline Smoothing", 71 71 "Penalized Regression Spline (alglib)", 72 "CUBGCV" 72 "CUBGCV", 73 "Finnbar O'Sullivan (sbart)" 73 74 }.Select(s => new StringValue(s))); 74 75 … … 172 173 break; 173 174 } 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 } 174 189 case "B-Spline Smoothing": { 175 190 BSplineSmoothing(x, y, inputVars); … … 205 220 public static IRegressionEnsembleModel CalculatePenalizedRegressionSpline(double[] x, double[] y, double lambda, string targetVar, string[] inputVars, 206 221 out double avgTrainRMSE, 207 out double cvRMSE ) {222 out double cvRMSE, out double[] predictions) { 208 223 int info; 209 224 alglib.spline1dinterpolant interpolant; … … 218 233 double[] x_loo = new double[(folds - 1) * foldSize]; 219 234 double[] y_loo = new double[(folds - 1) * foldSize]; 235 double[] w_loo = Enumerable.Repeat(1.0, x_loo.Length).ToArray(); 220 236 221 237 var models = new List<IRegressionModel>(); … … 225 241 int nTest = 0; 226 242 243 predictions = new double[y.Length]; 227 244 228 245 for (int i = 0; i < folds; i++) { … … 232 249 Array.Copy(y, (i + 1) * foldSize, y_loo, i * foldSize, (folds - i - 1) * foldSize); 233 250 } 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)); 237 254 238 255 // training error 239 256 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]); 241 258 double res = y[j] - y_pred; 242 259 sumTrainSE += res * res; … … 245 262 // test 246 263 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]; 249 266 sumTestSE += res * res; 250 267 nTest++; … … 630 647 int n = x.Length; 631 648 var range = x[n - 1] - x[0]; 632 var binSize = range / n / 20;649 var binSize = range / n / 10; 633 650 { 634 651 // binning … … 759 776 760 777 public double GetEstimatedValue(double xx) { 778 xx = (xx - offset) * scale; 761 779 int n = a.Length; 762 780 if (xx <= x[1]) { … … 791 809 product = product.Zip(dataset.GetDoubleValues(factor, rows), (pi, fi) => pi * fi).ToArray(); 792 810 } 793 foreach (var xx in product .Select(xi => (xi - offset) * scale)) {811 foreach (var xx in product) { 794 812 yield return GetEstimatedValue(xx); 795 813 }
Note: See TracChangeset
for help on using the changeset viewer.