Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 changed interface for covariance functions to improve readability, fixed several bugs in the covariance functions and in the line chart for Gaussian process models.

File size: 3.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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  [Item(Name = "CovarianceSEiso",
32    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
33  public class CovarianceSEiso : Item, ICovarianceFunction {
34    [Storable]
35    private double sf2;
36    public double Scale { get { return sf2; } }
37    [Storable]
38    private double l;
39    public double Length { get { return l; } }
40
41    [StorableConstructor]
42    protected CovarianceSEiso(bool deserializing)
43      : base(deserializing) {
44    }
45
46    protected CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
47      : base(original, cloner) {
48      this.sf2 = original.sf2;
49      this.l = original.l;
50    }
51
52    public CovarianceSEiso()
53      : base() {
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new CovarianceSEiso(this, cloner);
58    }
59
60    public int GetNumberOfParameters(int numberOfVariables) {
61      return 2;
62    }
63
64    public void SetParameter(double[] hyp) {
65      if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k");
66      this.l = Math.Exp(hyp[0]);
67      this.sf2 = Math.Exp(2 * hyp[1]);
68    }
69
70
71    public double GetCovariance(double[,] x, int i, int j) {
72      double lInv = 1.0 / l;
73      double d = i == j
74                   ? 0.0
75                   : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));
76      return sf2 * Math.Exp(-d / 2.0);
77    }
78
79    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
80      double lInv = 1.0 / l;
81      double d = i == j
82                   ? 0.0
83                   : Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));
84      double g = Math.Exp(-d / 2.0);
85      yield return sf2 * g * d;
86      yield return 2.0 * sf2 * g;
87    }
88
89    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
90      double lInv = 1.0 / l;
91      double d = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv));
92      return sf2 * Math.Exp(-d / 2.0);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.