Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs @ 8430

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

#1902 worked on sum and product covariance functions and fixed a few bugs.

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