Ignore:
Timestamp:
11/06/17 13:12:41 (4 years ago)
Author:
gkronber
Message:

#2789 more tests with CV and automatic determination of smoothing parameter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MathNetNumerics-Exploration-2789/Main/Program.cs

    r15449 r15450  
    1212  class Program {
    1313    static void Main(string[] args) {
    14       var xs = HeuristicLab.Common.SequenceGenerator.GenerateSteps(-3.5, 3.5, 0.1, includeEnd: true).ToList();
    15       var ys = xs.Select(xi => 1.0 / Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi)).ToArray(); // 1.0 / (Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi))).ToArray();
     14      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider();
     15      var problemData = provider.LoadData(provider.GetDataDescriptors().First(dd => dd.Name.Contains("Poly")));
     16      // var provider = new HeuristicLab.Problems.Instances.DataAnalysis.RegressionRealWorldInstanceProvider();
     17      // var problemData = provider.LoadData(provider.GetDataDescriptors().First(dd => dd.Name.Contains("Chem")));
    1618
    17       int n = xs.Count();
    18       alglib.hqrndstate state;
    19       alglib.hqrndseed(1234, 5678, out state);
    20       var ys_noise = ys.Select(yi => yi + alglib.hqrndnormal(state) * 0.1).ToList();
     19      var gam = new GAM();
     20      gam.MaxIterations = 10;
     21      gam.MaxInteractions = 3;
     22      gam.Problem.ProblemData = problemData;
     23      gam.Start();
    2124
    22       CubicSplineGCV.CubGcvReport report;
    23       var model = CubicSplineGCV.CalculateCubicSpline(
    24         xs.ToArray(), ys_noise.ToArray(), "y", new string[] { "x" }, out report);
     25      var solution = (IRegressionSolution)gam.Results["Ensemble solution"].Value;
    2526
    26       Console.WriteLine("Smoothing Parameter (= RHO/(RHO + 1) {0}", report.smoothingParameter);
    27       Console.WriteLine("Estimated DOF of RSS {0}", report.estimatedRSSDegreesOfFreedom);
    28       Console.WriteLine("GCV {0}", report.generalizedCrossValidation);
    29       Console.WriteLine("Mean squared residual {0}", report.meanSquareResiudal);
    30       Console.WriteLine("Estimate of true MSE at data points {0}", report.estimatedTrueMeanSquaredErrorAtDataPoints);
    31       Console.WriteLine("Estimate of error variance {0}", report.estimatedErrorVariance);
    32       Console.WriteLine("Mean square value of DF(I) {0}", report.meanSquareOfDf);
    33 
    34       OnlineCalculatorError error;
    35       var ys_smoothed = xs.Select(xi => model.GetEstimatedValue(xi)).ToArray();
    36       var mse = OnlineMeanSquaredErrorCalculator.Calculate(ys, ys_smoothed, out error);
    37       Console.WriteLine("MSE(ys, ys_smooth) = {0}", mse);
    38       mse = OnlineMeanSquaredErrorCalculator.Calculate(ys, ys_noise, out error);
    39       Console.WriteLine("MSE(ys, ys_noise) = {0}", mse);
    40 
    41       Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    42 
    43       for (int i = 0; i < n; i++) {
    44         Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
    45           xs[i], ys[i], ys_smoothed[i], ys_noise[i], ys_smoothed[i] + 1.96 * report.se[i], ys_smoothed[i] - 1.96 * report.se[i]);
    46       }
     27      Console.WriteLine("RMSE (train) {0}", solution.TrainingRootMeanSquaredError);
     28      Console.WriteLine("RMSE (test) {0}", solution.TestRootMeanSquaredError);
    4729    }
    4830  }
Note: See TracChangeset for help on using the changeset viewer.