Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs @ 13147

Last change on this file since 13147 was 13147, checked in by gkronber, 9 years ago

#2497: merged r13118:13119 from trunk to stable

File size: 14.2 KB
RevLine 
[8323]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8323]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
[8371]30namespace HeuristicLab.Algorithms.DataAnalysis {
[8323]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]
[8484]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]
[8323]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;
[8582]78    public double SigmaNoise {
79      get { return Math.Sqrt(sqrSigmaNoise); }
80    }
[8323]81
82    [Storable]
[8982]83    private double[] meanParameter;
84    [Storable]
85    private double[] covarianceParameter;
86
[13052]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
[8982]111    [Storable]
[13052]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;
[8323]115
116    [Storable]
[8463]117    private Scaling inputScaling;
[8323]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);
[13147]126      if (original.inputScaling != null)
127        this.inputScaling = cloner.Clone(original.inputScaling);
[13052]128      this.trainingDataset = cloner.Clone(original.trainingDataset);
[8323]129      this.negativeLogLikelihood = original.negativeLogLikelihood;
130      this.targetVariable = original.targetVariable;
[8416]131      this.sqrSigmaNoise = original.sqrSigmaNoise;
[8982]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      }
[8416]138
139      // shallow copies of arrays because they cannot be modified
[13052]140      this.trainingRows = original.trainingRows;
[8323]141      this.allowedInputVariables = original.allowedInputVariables;
142      this.alpha = original.alpha;
143      this.l = original.l;
144      this.x = original.x;
145    }
[12702]146    public GaussianProcessModel(IDataset ds, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows,
[13147]147      IEnumerable<double> hyp, IMeanFunction meanFunction, ICovarianceFunction covarianceFunction,
148      bool scaleInputs = true)
[8323]149      : base() {
150      this.name = ItemName;
151      this.description = ItemDescription;
[8416]152      this.meanFunction = (IMeanFunction)meanFunction.Clone();
153      this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone();
[8323]154      this.targetVariable = targetVariable;
155      this.allowedInputVariables = allowedInputVariables.ToArray();
156
157
[8416]158      int nVariables = this.allowedInputVariables.Length;
[8982]159      meanParameter = hyp
[8416]160        .Take(this.meanFunction.GetNumberOfParameters(nVariables))
[8982]161        .ToArray();
162
163      covarianceParameter = hyp.Skip(this.meanFunction.GetNumberOfParameters(nVariables))
164                                             .Take(this.covarianceFunction.GetNumberOfParameters(nVariables))
165                                             .ToArray();
[8473]166      sqrSigmaNoise = Math.Exp(2.0 * hyp.Last());
[13147]167      CalculateModel(ds, rows, scaleInputs);
[8323]168    }
169
[13147]170    private void CalculateModel(IDataset ds, IEnumerable<int> rows, bool scaleInputs = true) {
[13052]171      this.trainingDataset = (IDataset)ds.Clone();
172      this.trainingRows = rows.ToArray();
[13147]173      this.inputScaling = scaleInputs ? new Scaling(ds, allowedInputVariables, rows) : null;
[8323]174
[13147]175      x = GetData(ds, this.allowedInputVariables, this.trainingRows, this.inputScaling);
176
177      IEnumerable<double> y;
178      y = ds.GetDoubleValues(targetVariable, rows);
179
[8323]180      int n = x.GetLength(0);
181
[13052]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
[8982]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
[8323]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
[13052]198      int info;
199      alglib.densesolverreport denseSolveRep;
200
[8323]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
[8463]209      alglib.matinvreport matInvRep;
[8475]210      double[,] lCopy = new double[l.GetLength(0), l.GetLength(1)];
211      Array.Copy(l, lCopy, lCopy.Length);
[8323]212
[8475]213      alglib.spdmatrixcholeskyinverse(ref lCopy, n, false, out info, out matInvRep);
[8463]214      if (info != 1) throw new ArgumentException("Can't invert matrix to calculate gradients.");
[8323]215      for (int i = 0; i < n; i++) {
[8463]216        for (int j = 0; j <= i; j++)
[8475]217          lCopy[i, j] = lCopy[i, j] / sqrSigmaNoise - alpha[i] * alpha[j];
[8323]218      }
219
[8475]220      double noiseGradient = sqrSigmaNoise * Enumerable.Range(0, n).Select(i => lCopy[i, i]).Sum();
[8323]221
222      double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)];
[8982]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);
[8323]227      }
228
229      double[] covGradients = new double[covarianceFunction.GetNumberOfParameters(nAllowedVariables)];
[8366]230      if (covGradients.Length > 0) {
231        for (int i = 0; i < n; i++) {
[8484]232          for (int j = 0; j < i; j++) {
[8982]233            var g = cov.CovarianceGradient(x, i, j).ToArray();
[8484]234            for (int k = 0; k < covGradients.Length; k++) {
235              covGradients[k] += lCopy[i, j] * g[k];
[8366]236            }
[8323]237          }
[8484]238
[8982]239          var gDiag = cov.CovarianceGradient(x, i, i).ToArray();
[8484]240          for (int k = 0; k < covGradients.Length; k++) {
241            // diag
242            covGradients[k] += 0.5 * lCopy[i, i] * gDiag[k];
243          }
[8323]244        }
245      }
246
[8484]247      hyperparameterGradients =
[8473]248        meanGradients
249        .Concat(covGradients)
250        .Concat(new double[] { noiseGradient }).ToArray();
[8484]251
[8323]252    }
253
[13147]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      }
[13052]260    }
[8323]261
[13052]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
[8323]281    public override IDeepCloneable Clone(Cloner cloner) {
282      return new GaussianProcessModel(this, cloner);
283    }
284
[8982]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
[8323]294    #region IRegressionModel Members
[12702]295    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[8323]296      return GetEstimatedValuesHelper(dataset, rows);
297    }
298    public GaussianProcessRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[8528]299      return new GaussianProcessRegressionSolution(this, new RegressionProblemData(problemData));
[8323]300    }
301    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
302      return CreateRegressionSolution(problemData);
303    }
304    #endregion
305
[8623]306
[12702]307    private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
[13052]308      if (x == null) {
[13147]309        x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling);
[13052]310      }
311      int n = x.GetLength(0);
312
[13147]313      double[,] newX = GetData(dataset, allowedInputVariables, rows, inputScaling);
[8323]314      int newN = newX.GetLength(0);
[13052]315
[8323]316      var Ks = new double[newN, n];
[8982]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();
[9358]321      var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1)));
[8323]322      for (int i = 0; i < newN; i++) {
323        for (int j = 0; j < n; j++) {
[8982]324          Ks[i, j] = cov.CrossCovariance(x, newX, j, i);
[8323]325        }
326      }
327
[8463]328      return Enumerable.Range(0, newN)
[8473]329        .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha));
[8323]330    }
[8473]331
[12702]332    public IEnumerable<double> GetEstimatedVariance(IDataset dataset, IEnumerable<int> rows) {
[13052]333      if (x == null) {
[13147]334        x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling);
[13052]335      }
336      int n = x.GetLength(0);
337
[13147]338      var newX = GetData(dataset, allowedInputVariables, rows, inputScaling);
[8473]339      int newN = newX.GetLength(0);
340
341      var kss = new double[newN];
342      double[,] sWKs = new double[n, newN];
[9357]343      var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1)));
[8473]344
[13052]345      if (l == null) {
346        l = CalculateL(x, cov, sqrSigmaNoise);
347      }
348
[8473]349      // for stddev
350      for (int i = 0; i < newN; i++)
[8982]351        kss[i] = cov.Covariance(newX, i, i);
[8473]352
[8475]353      for (int i = 0; i < newN; i++) {
354        for (int j = 0; j < n; j++) {
[8982]355          sWKs[j, i] = cov.CrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise);
[8473]356        }
357      }
358
359      // for stddev
[8484]360      alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0);
[8473]361
362      for (int i = 0; i < newN; i++) {
[8484]363        var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i));
[13145]364        kss[i] += sqrSigmaNoise; // kss is V(f), add noise variance of predictive distibution to get V(y)
[8475]365        kss[i] -= sumV;
366        if (kss[i] < 0) kss[i] = 0;
[8473]367      }
[8475]368      return kss;
[8473]369    }
[8323]370  }
371}
Note: See TracBrowser for help on using the repository browser.