Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceMaternIso.cs @ 8562

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

#1902 implemented LinearARD and MaternIso covariance functions.

File size: 3.9 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  [StorableClass]
30  [Item(Name = "CovarianceMaternIso",
31    Description = "Matern covariance function for Gaussian processes.")]
32  public class CovarianceMaternIso : Item, ICovarianceFunction {
33    [Storable]
34    private double sf2;
35    public double Scale { get { return sf2; } }
36    [Storable]
37    private double inverseLength;
38    public double InverseLength { get { return inverseLength; } }
39    [Storable]
40    private int d;
41    public int D {
42      get { return d; }
43      set {
44        if (value == 1 || value == 3 || value == 5) d = value;
45        else throw new ArgumentException("D can only take the values 1, 3, or 5");
46      }
47    }
48
49    [StorableConstructor]
50    protected CovarianceMaternIso(bool deserializing)
51      : base(deserializing) {
52    }
53
54    protected CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
55      : base(original, cloner) {
56      this.sf2 = original.sf2;
57      this.inverseLength = original.inverseLength;
58      this.d = original.d;
59    }
60
61    public CovarianceMaternIso()
62      : base() {
63      d = 1;
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new CovarianceMaternIso(this, cloner);
68    }
69
70    public int GetNumberOfParameters(int numberOfVariables) {
71      return 2;
72    }
73
74    public void SetParameter(double[] hyp) {
75      if (hyp.Length != 2) throw new ArgumentException("CovarianceMaternIso has two hyperparameters", "hyp");
76      this.inverseLength = 1.0 / Math.Exp(hyp[0]);
77      this.sf2 = Math.Exp(2 * hyp[1]);
78    }
79
80
81    private double m(double t) {
82      double f;
83      switch (d) {
84        case 1: { f = 1; break; }
85        case 3: { f = 1 + t; break; }
86        case 5: { f = 1 + t * (1 + t / 3.0); break; }
87        default: throw new InvalidOperationException();
88      }
89      return f * Math.Exp(-t);
90    }
91
92    private double dm(double t) {
93      double df;
94      switch (d) {
95        case 1: { df = 1; break; }
96        case 3: { df = t; break; }
97        case 5: { df = t * (1 + t) / 3.0; break; }
98        default: throw new InvalidOperationException();
99      }
100      return df * t * Math.Exp(-t);
101    }
102
103    public double GetCovariance(double[,] x, int i, int j) {
104      double dist = i == j
105                   ? 0.0
106                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
107      return sf2 * m(dist);
108    }
109
110    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
111      double dist = i == j
112                   ? 0.0
113                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
114
115      yield return sf2 * dm(dist);
116      yield return 2 * sf2 * m(dist);
117    }
118
119    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
120      double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength));
121      return sf2 * m(dist);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.