Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs @ 13119

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

#2497: added hidden parameter to turn on/off scaling of input variables in Gaussian process models

File size: 14.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 {
31  /// <summary>
32  /// Represents a Gaussian process model.
33  /// </summary>
34  [StorableClass]
35  [Item("GaussianProcessModel", "Represents a Gaussian process posterior.")]
36  public sealed class GaussianProcessModel : NamedItem, IGaussianProcessModel {
37    [Storable]
38    private double negativeLogLikelihood;
39    public double NegativeLogLikelihood {
40      get { return negativeLogLikelihood; }
41    }
42
43    [Storable]
44    private double[] hyperparameterGradients;
45    public double[] HyperparameterGradients {
46      get {
47        var copy = new double[hyperparameterGradients.Length];
48        Array.Copy(hyperparameterGradients, copy, copy.Length);
49        return copy;
50      }
51    }
52
53    [Storable]
54    private ICovarianceFunction covarianceFunction;
55    public ICovarianceFunction CovarianceFunction {
56      get { return covarianceFunction; }
57    }
58    [Storable]
59    private IMeanFunction meanFunction;
60    public IMeanFunction MeanFunction {
61      get { return meanFunction; }
62    }
63    [Storable]
64    private string targetVariable;
65    public string TargetVariable {
66      get { return targetVariable; }
67    }
68    [Storable]
69    private string[] allowedInputVariables;
70    public string[] AllowedInputVariables {
71      get { return allowedInputVariables; }
72    }
73
74    [Storable]
75    private double[] alpha;
76    [Storable]
77    private double sqrSigmaNoise;
78    public double SigmaNoise {
79      get { return Math.Sqrt(sqrSigmaNoise); }
80    }
81
82    [Storable]
83    private double[] meanParameter;
84    [Storable]
85    private double[] covarianceParameter;
86
87    private double[,] l; // used to be storable in previous versions (is calculated lazily now)
88    private double[,] x; // scaled training dataset, used to be storable in previous versions (is calculated lazily now)
89
90    // BackwardsCompatibility3.4
91    #region Backwards compatible code, remove with 3.5
92    [Storable(Name = "l")] // restore if available but don't store anymore
93    private double[,] l_storable {
94      set { this.l = value; }
95      get {
96        if (trainingDataset == null) return l; // this model has been created with an old version
97        else return null; // if the training dataset is available l should not be serialized
98      }
99    }
100    [Storable(Name = "x")] // restore if available but don't store anymore
101    private double[,] x_storable {
102      set { this.x = value; }
103      get {
104        if (trainingDataset == null) return x; // this model has been created with an old version
105        else return null; // if the training dataset is available x should not be serialized
106      }
107    }
108    #endregion
109
110
111    [Storable]
112    private IDataset trainingDataset; // it is better to store the original training dataset completely because this is more efficient in persistence
113    [Storable]
114    private int[] trainingRows;
115
116    [Storable]
117    private Scaling inputScaling;
118
119
120    [StorableConstructor]
121    private GaussianProcessModel(bool deserializing) : base(deserializing) { }
122    private GaussianProcessModel(GaussianProcessModel original, Cloner cloner)
123      : base(original, cloner) {
124      this.meanFunction = cloner.Clone(original.meanFunction);
125      this.covarianceFunction = cloner.Clone(original.covarianceFunction);
126      if (original.inputScaling != null)
127        this.inputScaling = cloner.Clone(original.inputScaling);
128      this.trainingDataset = cloner.Clone(original.trainingDataset);
129      this.negativeLogLikelihood = original.negativeLogLikelihood;
130      this.targetVariable = original.targetVariable;
131      this.sqrSigmaNoise = original.sqrSigmaNoise;
132      if (original.meanParameter != null) {
133        this.meanParameter = (double[])original.meanParameter.Clone();
134      }
135      if (original.covarianceParameter != null) {
136        this.covarianceParameter = (double[])original.covarianceParameter.Clone();
137      }
138
139      // shallow copies of arrays because they cannot be modified
140      this.trainingRows = original.trainingRows;
141      this.allowedInputVariables = original.allowedInputVariables;
142      this.alpha = original.alpha;
143      this.l = original.l;
144      this.x = original.x;
145    }
146    public GaussianProcessModel(IDataset ds, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows,
147      IEnumerable<double> hyp, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction,
148      bool scaleInputs = true)
149      : base() {
150      this.name = ItemName;
151      this.description = ItemDescription;
152      this.meanFunction = (IMeanFunction)meanFunction.Clone();
153      this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone();
154      this.targetVariable = targetVariable;
155      this.allowedInputVariables = allowedInputVariables.ToArray();
156
157
158      int nVariables = this.allowedInputVariables.Length;
159      meanParameter = hyp
160        .Take(this.meanFunction.GetNumberOfParameters(nVariables))
161        .ToArray();
162
163      covarianceParameter = hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables))
164                                             .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
165                                             .ToArray();
166      sqrSigmaNoise = Math.Exp(2.0 * hyp.Last());
167      CalculateModel(ds, rows, scaleInputs);
168    }
169
170    private void CalculateModel(IDataset ds, IEnumerable<int> rows, bool scaleInputs = true) {
171      this.trainingDataset = (IDataset)ds.Clone();
172      this.trainingRows = rows.ToArray();
173      this.inputScaling = scaleInputs ? new Scaling(ds, allowedInputVariables, rows) : null;
174
175      x = GetData(ds, this.allowedInputVariables, this.trainingRows, this.inputScaling);
176
177      IEnumerable<double> y;
178      y = ds.GetDoubleValues(targetVariable, rows);
179
180      int n = x.GetLength(0);
181
182      // calculate cholesky decomposed (lower triangular) covariance matrix
183      var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));
184      this.l = CalculateL(x, cov, sqrSigmaNoise);
185
186      // calculate mean
187      var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, x.GetLength(1)));
188      double[] m = Enumerable.Range(0, x.GetLength(0))
189        .Select(r => mean.Mean(x, r))
190        .ToArray();
191
192      // calculate sum of diagonal elements for likelihood
193      double diagSum = Enumerable.Range(0, n).Select(i => Math.Log(l[i, i])).Sum();
194
195      // solve for alpha
196      double[] ym = y.Zip(m, (a, b) => a - b).ToArray();
197
198      int info;
199      alglib.densesolverreport denseSolveRep;
200
201      alglib.spdmatrixcholeskysolve(l, n, false, ym, out info, out denseSolveRep, out alpha);
202      for (int i = 0; i < alpha.Length; i++)
203        alpha[i] = alpha[i] / sqrSigmaNoise;
204      negativeLogLikelihood = 0.5 * Util.ScalarProd(ym, alpha) + diagSum + (n / 2.0) * Math.Log(2.0 * Math.PI * sqrSigmaNoise);
205
206      // derivatives
207      int nAllowedVariables = x.GetLength(1);
208
209      alglib.matinvreport matInvRep;
210      double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)];
211      Array.Copy(l, lCopy, lCopy.Length);
212
213      alglib.spdmatrixcholeskyinverse(ref lCopy, n, false, out info, out matInvRep);
214      if (info != 1) throw new ArgumentException("Can't invert matrix to calculate gradients.");
215      for (int i = 0; i < n; i++) {
216        for (int j = 0; j <= i; j++)
217          lCopy[i, j] = lCopy[i, j] / sqrSigmaNoise - alpha[i] * alpha[j];
218      }
219
220      double noiseGradient = sqrSigmaNoise * Enumerable.Range(0, n).Select(i => lCopy[i, i]).Sum();
221
222      double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)];
223      for (int k = 0; k < meanGradients.Length; k++) {
224        var meanGrad = Enumerable.Range(0, alpha.Length)
225        .Select(r => mean.Gradient(x, r, k));
226        meanGradients[k] = -Util.ScalarProd(meanGrad, alpha);
227      }
228
229      double[] covGradients = new double[covarianceFunction.GetNumberOfParameters(nAllowedVariables)];
230      if (covGradients.Length > 0) {
231        for (int i = 0; i < n; i++) {
232          for (int j = 0; j < i; j++) {
233            var g = cov.CovarianceGradient(x, i, j).ToArray();
234            for (int k = 0; k < covGradients.Length; k++) {
235              covGradients[k] += lCopy[i, j] * g[k];
236            }
237          }
238
239          var gDiag = cov.CovarianceGradient(x, i, i).ToArray();
240          for (int k = 0; k < covGradients.Length; k++) {
241            // diag
242            covGradients[k] += 0.5 * lCopy[i, i] * gDiag[k];
243          }
244        }
245      }
246
247      hyperparameterGradients =
248        meanGradients
249        .Concat(covGradients)
250        .Concat(new double[] { noiseGradient }).ToArray();
251
252    }
253
254    private static double[,] GetData(IDataset ds, IEnumerable<string> allowedInputs, IEnumerable<int> rows, Scaling scaling) {
255      if (scaling != null) {
256        return AlglibUtil.PrepareAndScaleInputMatrix(ds, allowedInputs, rows, scaling);
257      } else {
258        return AlglibUtil.PrepareInputMatrix(ds, allowedInputs, rows);
259      }
260    }
261
262    private static double[,] CalculateL(double[,] x, ParameterizedCovarianceFunction cov, double sqrSigmaNoise) {
263      int n = x.GetLength(0);
264      var l = new double[n, n];
265
266      // calculate covariances
267      for (int i = 0; i < n; i++) {
268        for (int j = i; j < n; j++) {
269          l[j, i] = cov.Covariance(x, i, j) / sqrSigmaNoise;
270          if (j == i) l[j, i] += 1.0;
271        }
272      }
273
274      // cholesky decomposition
275      var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);
276      if (!res) throw new ArgumentException("Matrix is not positive semidefinite");
277      return l;
278    }
279
280
281    public override IDeepCloneable Clone(Cloner cloner) {
282      return new GaussianProcessModel(this, cloner);
283    }
284
285    // is called by the solution creator to set all parameter values of the covariance and mean function
286    // to the optimized values (necessary to make the values visible in the GUI)
287    public void FixParameters() {
288      covarianceFunction.SetParameter(covarianceParameter);
289      meanFunction.SetParameter(meanParameter);
290      covarianceParameter = new double[0];
291      meanParameter = new double[0];
292    }
293
294    #region IRegressionModel Members
295    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
296      return GetEstimatedValuesHelper(dataset, rows);
297    }
298    public GaussianProcessRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
299      return new GaussianProcessRegressionSolution(this, new RegressionProblemData(problemData));
300    }
301    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
302      return CreateRegressionSolution(problemData);
303    }
304    #endregion
305
306
307    private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
308      if (x == null) {
309        x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling);
310      }
311      int n = x.GetLength(0);
312
313      double[,] newX = GetData(dataset, allowedInputVariables, rows, inputScaling);
314      int newN = newX.GetLength(0);
315
316      var Ks = new double[newN, n];
317      var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1)));
318      var ms = Enumerable.Range(0, newX.GetLength(0))
319      .Select(r => mean.Mean(newX, r))
320      .ToArray();
321      var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1)));
322      for (int i = 0; i < newN; i++) {
323        for (int j = 0; j < n; j++) {
324          Ks[i, j] = cov.CrossCovariance(x, newX, j, i);
325        }
326      }
327
328      return Enumerable.Range(0, newN)
329        .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha));
330    }
331
332    public IEnumerable<double> GetEstimatedVariance(IDataset dataset, IEnumerable<int> rows) {
333      if (x == null) {
334        x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling);
335      }
336      int n = x.GetLength(0);
337
338      var newX = GetData(dataset, allowedInputVariables, rows, inputScaling);
339      int newN = newX.GetLength(0);
340
341      var kss = new double[newN];
342      double[,] sWKs = new double[n, newN];
343      var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));
344
345      if (l == null) {
346        l = CalculateL(x, cov, sqrSigmaNoise);
347      }
348
349      // for stddev
350      for (int i = 0; i < newN; i++)
351        kss[i] = cov.Covariance(newX, i, i);
352
353      for (int i = 0; i < newN; i++) {
354        for (int j = 0; j < n; j++) {
355          sWKs[j, i] = cov.CrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise);
356        }
357      }
358
359      // for stddev
360      alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0);
361
362      for (int i = 0; i < newN; i++) {
363        var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i));
364        kss[i] -= sumV;
365        if (kss[i] < 0) kss[i] = 0;
366      }
367      return kss;
368    }
369  }
370}
Note: See TracBrowser for help on using the repository browser.