Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs @ 8455

Last change on this file since 8455 was 8455, checked in by gkronber, 12 years ago

#1902 changed calculation of gradients for covariance functions to reduce allocations of arrays

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  [StorableClass]
30  [Item(Name = "CovarianceSEiso",
31    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
32  public class CovarianceSEiso : Item, ICovarianceFunction {
33    [Storable]
34    private double[,] x;
35    [Storable]
36    private double[,] xt;
37    [Storable]
38    private double sf2;
39    [Storable]
40    private double l;
41    [Storable]
42    private bool symmetric;
43    private double[,] sd;
44
45    [StorableConstructor]
46    protected CovarianceSEiso(bool deserializing)
47      : base(deserializing) {
48    }
49
50    protected CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
51      : base(original, cloner) {
52      if (original.x != null) {
53        this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
54        Array.Copy(original.x, this.x, x.Length);
55
56        this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
57        Array.Copy(original.xt, this.xt, xt.Length);
58
59        this.sd = new double[original.sd.GetLength(0), original.sd.GetLength(1)];
60        Array.Copy(original.sd, this.sd, sd.Length);
61        this.sf2 = original.sf2;
62      }
63      this.sf2 = original.sf2;
64      this.l = original.l;
65      this.symmetric = original.symmetric;
66    }
67
68    public CovarianceSEiso()
69      : base() {
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new CovarianceSEiso(this, cloner);
74    }
75
76    public int GetNumberOfParameters(int numberOfVariables) {
77      return 2;
78    }
79
80    public void SetParameter(double[] hyp) {
81      this.l = Math.Exp(hyp[0]);
82      this.sf2 = Math.Min(1E6, Math.Exp(2 * hyp[1])); // upper limit for scale
83      sd = null;
84    }
85    public void SetData(double[,] x) {
86      SetData(x, x);
87      this.symmetric = true;
88    }
89
90
91    public void SetData(double[,] x, double[,] xt) {
92      this.symmetric = false;
93      this.x = x;
94      this.xt = xt;
95      sd = null;
96    }
97
98    public double GetCovariance(int i, int j) {
99      if (sd == null) CalculateSquaredDistances();
100      return sf2 * Math.Exp(-sd[i, j] / 2.0);
101    }
102
103    public double GetGradient(int i, int j, int k) {
104      switch (k) {
105        case 0: return sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j];
106        case 1: return 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
107        default: throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k");
108      }
109    }
110
111    private void CalculateSquaredDistances() {
112      if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
113      int rows = x.GetLength(0);
114      int cols = xt.GetLength(0);
115      sd = new double[rows, cols];
116      if (symmetric) {
117        for (int i = 0; i < rows; i++) {
118          for (int j = i; j < rows; j++) {
119            sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
120            sd[j, i] = sd[i, j];
121          }
122        }
123      } else {
124        for (int i = 0; i < rows; i++) {
125          for (int j = 0; j < cols; j++) {
126            sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
127          }
128        }
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.