Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 initial import of Gaussian process regression algorithm

File size: 3.3 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess {
8  [StorableClass]
9  [Item(Name = "CovarianceSEiso",
10    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
11  public class CovarianceSEiso : Item, ICovarianceFunction {
12    [Storable]
13    private double[,] x;
14    [Storable]
15    private double[,] xt;
16    [Storable]
17    private double sf2;
18    [Storable]
19    private double l;
20    [Storable]
21    private bool symmetric;
22    private double[,] sd;
23
24    [StorableConstructor]
25    protected CovarianceSEiso(bool deserializing)
26      : base(deserializing) {
27    }
28
29    protected CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
30      : base(original, cloner) {
31      // note: using shallow copies here
32      this.x = original.x;
33      this.xt = original.xt;
34      this.sf2 = original.sf2;
35      this.l = original.l;
36      this.symmetric = original.symmetric;
37    }
38
39    public CovarianceSEiso()
40      : base() {
41    }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new CovarianceSEiso(this, cloner);
45    }
46
47    public int GetNumberOfParameters(int numberOfVariables) {
48      return 2;
49    }
50
51    public void SetParameter(double[] hyp, double[,] x) {
52      SetParameter(hyp, x, x);
53      this.symmetric = true;
54    }
55
56
57    public void SetParameter(double[] hyp, double[,] x, double[,] xt) {
58      this.l = Math.Exp(hyp[0]);
59      this.sf2 = Math.Exp(2 * hyp[1]);
60
61      this.symmetric = false;
62      this.x = x;
63      this.xt = xt;
64      sd = null;
65    }
66
67    public double GetCovariance(int i, int j) {
68      if (sd == null) CalculateSquaredDistances();
69      return sf2 * Math.Exp(-sd[i, j] / 2.0);
70    }
71
72
73    public double[] GetDiagonalCovariances() {
74      if (x != xt) throw new InvalidOperationException();
75      int rows = x.GetLength(0);
76      var sd = new double[rows];
77      for (int i = 0; i < rows; i++) {
78        sd[i] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, i).Select(e => e / l));
79      }
80      return sd.Select(d => sf2 * Math.Exp(-d / 2.0)).ToArray();
81    }
82
83
84    public double[] GetGradient(int i, int j) {
85      var res = new double[2];
86      res[0] = sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j];
87      res[1] = 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
88      return res;
89    }
90
91    private void CalculateSquaredDistances() {
92      if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
93      int rows = x.GetLength(0);
94      int cols = xt.GetLength(0);
95      sd = new double[rows, cols];
96      if (symmetric) {
97        for (int i = 0; i < rows; i++) {
98          for (int j = i; j < rows; j++) {
99            sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
100            sd[j, i] = sd[i, j];
101          }
102        }
103      } else {
104        for (int i = 0; i < rows; i++) {
105          for (int j = 0; j < cols; j++) {
106            sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
107          }
108        }
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.