Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/RadialBasisFunctions/RadialBasisRegression.cs @ 14386

Last change on this file since 14386 was 14386, checked in by bwerth, 7 years ago

#2699 moved RadialBasisFunctions from Problems.SurrogateProblem to Algorithms.DataAnalysis

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  /// <summary>
34  /// Linear regression data analysis algorithm.
35  /// </summary>
36  [Item("Radial Basis Function Regression (RBF-R)", "Radial basis function regression data analysis algorithm (uses for ALGLIB).")]
37  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 100)]
38  [StorableClass]
39  public sealed class RadialBasisRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
40    private const string RadialBasisRegressionModelResultName = "RBF regression solution";
41
42
43    #region Parameternames
44    private const string Kernelname = "Kernel";
45    #endregion
46
47    #region Paramterproperties
48    public ValueParameter<IKernelFunction<double[]>> KernelParameter
49    {
50      get { return Parameters[Kernelname] as ValueParameter<IKernelFunction<double[]>>; }
51    }
52    #endregion
53
54    #region Properties
55    public IKernelFunction<double[]> Kernel
56    {
57      get { return KernelParameter.Value; }
58    }
59
60    #endregion
61
62    [StorableConstructor]
63    private RadialBasisRegression(bool deserializing) : base(deserializing) { }
64    private RadialBasisRegression(RadialBasisRegression original, Cloner cloner)
65      : base(original, cloner) {
66    }
67    public RadialBasisRegression() {
68      Problem = new RegressionProblem();
69      Parameters.Add(new ValueParameter<IKernelFunction<double[]>>(Kernelname, "The radial basis function"));
70      var kernel = new PolysplineKernel<double[]>();
71      KernelParameter.Value = kernel;
72      kernel.BetaParameter.Value.Value = 1;
73    }
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() { }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new RadialBasisRegression(this, cloner);
79    }
80
81    #region regression
82    protected override void Run() {
83      double loocvrmse, cvRmsError;
84      var solution = CreateRadialBasisRegressionSolution(Problem.ProblemData, Kernel, out loocvrmse, out cvRmsError);
85      Results.Add(new Result(RadialBasisRegressionModelResultName, "The RBF regression solution.", solution));
86      Results.Add(new Result("LOOCVRMSE", "The root of the mean of squared errors of a leave-one-out-cross-validation on the trainingsset (This is not the RSME on the trainingset)", new DoubleValue(loocvrmse)));
87      Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear regression solution via cross validation.", new DoubleValue(cvRmsError)));
88    }
89
90    public static IConfidenceRegressionSolution CreateRadialBasisRegressionSolution(IRegressionProblemData problemData, IKernelFunction<double[]> kernel, out double loocvRmsError, out double cvRmsError) {
91      var model = new RadialBasisFunctionModel(problemData.Dataset, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.TrainingIndices, kernel);
92      loocvRmsError = model.LeaveOneOutCrossValidationRootMeanSquaredError();
93      cvRmsError = Math.Sqrt(model.GetEstimatedValues(problemData.Dataset, problemData.TestIndices)
94        .Zip(problemData.TargetVariableTestValues, (a, b) => (a - b) * (a - b))
95        .Sum());
96      var solution = (RadialBasisFunctionRegressionSolution)model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());
97      solution.Model.Name = "Radial Basis Regression Model";
98      solution.Name = "Radial Basis Regression Solution";
99      return solution;
100    }
101    #endregion
102
103    #region helpers
104    #endregion
105  }
106}
Note: See TracBrowser for help on using the repository browser.