Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs @ 14888

Last change on this file since 14888 was 14888, checked in by gkronber, 8 years ago

#2699: re-added calculation of leave one out cv estimate

File size: 8.5 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis.KernelRidgeRegression {
31  [StorableClass]
32  [Item("KernelRidgeRegressionModel", "A kernel ridge regression model")]
33  public sealed class KernelRidgeRegressionModel : RegressionModel {
34    public override IEnumerable<string> VariablesUsedForPrediction {
35      get { return allowedInputVariables; }
36    }
37
38    [Storable]
39    private readonly string[] allowedInputVariables;
40    public string[] AllowedInputVariables {
41      get { return allowedInputVariables; }
42    }
43
44
45    [Storable]
46    public double LooCvRMSE { get; private set; }
47
48    [Storable]
49    private readonly double[] alpha;
50
51    [Storable]
52    private readonly double[,] trainX; // it is better to store the original training dataset completely because this is more efficient in persistence
53
54    [Storable]
55    private readonly ITransformation<double>[] scaling;
56
57    [Storable]
58    private readonly ICovarianceFunction kernel;
59
60    [Storable]
61    private readonly double lambda;
62
63    [Storable]
64    private readonly double yOffset; // implementation works for zero-mean, unit-variance target variables
65
66    [Storable]
67    private readonly double yScale;
68
69    [StorableConstructor]
70    private KernelRidgeRegressionModel(bool deserializing) : base(deserializing) { }
71    private KernelRidgeRegressionModel(KernelRidgeRegressionModel original, Cloner cloner)
72      : base(original, cloner) {
73      // shallow copies of arrays because they cannot be modified
74      allowedInputVariables = original.allowedInputVariables;
75      alpha = original.alpha;
76      trainX = original.trainX;
77      scaling = original.scaling;
78      lambda = original.lambda;
79      LooCvRMSE = original.LooCvRMSE;
80
81      yOffset = original.yOffset;
82      yScale = original.yScale;
83      if (original.kernel != null)
84        kernel = cloner.Clone(original.kernel);
85    }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new KernelRidgeRegressionModel(this, cloner);
88    }
89
90    public KernelRidgeRegressionModel(IDataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows,
91      bool scaleInputs, ICovarianceFunction kernel, double lambda = 0.1) : base(targetVariable) {
92      if (kernel.GetNumberOfParameters(allowedInputVariables.Count()) > 0) throw new ArgumentException("All parameters in the kernel function must be specified.");
93      name = ItemName;
94      description = ItemDescription;
95      this.allowedInputVariables = allowedInputVariables.ToArray();
96      var trainingRows = rows.ToArray();
97      this.kernel = (ICovarianceFunction)kernel.Clone();
98      this.lambda = lambda;
99      try {
100        if (scaleInputs)
101          scaling = CreateScaling(dataset, trainingRows);
102        trainX = ExtractData(dataset, trainingRows, scaling);
103        var y = dataset.GetDoubleValues(targetVariable, trainingRows).ToArray();
104        yOffset = y.Average();
105        yScale = 1.0 / y.StandardDeviation();
106        for (int i = 0; i < y.Length; i++) {
107          y[i] -= yOffset;
108          y[i] *= yScale;
109        }
110        int info;
111        int n = trainX.GetLength(0);
112        alglib.densesolverreport denseSolveRep;
113        var gram = BuildGramMatrix(trainX, lambda);
114        var l = new double[n, n]; Array.Copy(gram, l, l.Length);
115
116        // cholesky decomposition
117        var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);
118        if (res == false) throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?");
119
120        alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha);
121        if (info != 1) throw new ArgumentException("Could not create model.");
122
123
124        {
125          // for LOO-CV we need to build the inverse of the gram matrix
126          alglib.matinvreport rep;
127          var invG = l;   // rename
128          alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep);         
129          if (info != 1) throw new ArgumentException("Could not invert Gram matrix.");
130          var ssqLooError = 0.0;
131          for (int i = 0; i < n; i++) {
132            var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha);
133            var looPred_i = pred_i - alpha[i] / invG[i, i];
134            var error = (y[i] - looPred_i) / yScale;
135            ssqLooError += error * error;
136          }
137          LooCvRMSE = Math.Sqrt(ssqLooError / n);
138        }
139      } catch (alglib.alglibexception ae) {
140        // wrap exception so that calling code doesn't have to know about alglib implementation
141        throw new ArgumentException("There was a problem in the calculation of the kernel ridge regression model", ae);
142      }
143    }
144
145
146    #region IRegressionModel Members
147    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
148      var newX = ExtractData(dataset, rows, scaling);
149      var dim = newX.GetLength(1);
150      var cov = kernel.GetParameterizedCovarianceFunction(new double[0], Enumerable.Range(0, dim).ToArray());
151
152      var pred = new double[newX.GetLength(0)];
153      for (int i = 0; i < pred.Length; i++) {
154        double sum = 0.0;
155        for (int j = 0; j < alpha.Length; j++) {
156          sum += alpha[j] * cov.CrossCovariance(trainX, newX, j, i);
157        }
158        pred[i] = sum / yScale + yOffset;
159      }
160      return pred;
161    }
162    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
163      return new RegressionSolution(this, new RegressionProblemData(problemData));
164    }
165    #endregion
166
167    #region helpers
168    private double[,] BuildGramMatrix(double[,] data, double lambda) {
169      var n = data.GetLength(0);
170      var dim = data.GetLength(1);
171      var cov = kernel.GetParameterizedCovarianceFunction(new double[0], Enumerable.Range(0, dim).ToArray());
172      var gram = new double[n, n];
173      // G = (K + λ I)
174      for (var i = 0; i < n; i++) {
175        for (var j = i; j < n; j++) {
176          gram[i, j] = gram[j, i] = cov.Covariance(data, i, j); // symmetric matrix
177        }
178        gram[i, i] += lambda;
179      }
180      return gram;
181    }
182
183    private ITransformation<double>[] CreateScaling(IDataset dataset, int[] rows) {
184      var trans = new ITransformation<double>[allowedInputVariables.Length];
185      int i = 0;
186      foreach (var variable in allowedInputVariables) {
187        var lin = new LinearTransformation(allowedInputVariables);
188        var max = dataset.GetDoubleValues(variable, rows).Max();
189        var min = dataset.GetDoubleValues(variable, rows).Min();
190        lin.Multiplier = 1.0 / (max - min);
191        lin.Addend = -min / (max - min);
192        trans[i] = lin;
193        i++;
194      }
195      return trans;
196    }
197
198    private double[,] ExtractData(IDataset dataset, IEnumerable<int> rows, ITransformation<double>[] scaling = null) {
199      double[][] variables;
200      if (scaling != null) {
201        variables =
202          allowedInputVariables.Select((var, i) => scaling[i].Apply(dataset.GetDoubleValues(var, rows)).ToArray())
203            .ToArray();
204      } else {
205        variables =
206        allowedInputVariables.Select(var => dataset.GetDoubleValues(var, rows).ToArray()).ToArray();
207      }
208      int n = variables.First().Length;
209      var res = new double[n, variables.Length];
210      for (int r = 0; r < n; r++)
211        for (int c = 0; c < variables.Length; c++) {
212          res[r, c] = variables[c][r];
213        }
214      return res;
215    }
216    #endregion
217  }
218}
Note: See TracBrowser for help on using the repository browser.