source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQArd.cs @ 8565

Last change on this file since 8565 was 8565, checked in by gkronber, 8 years ago

#1902 implemented RQard covariance function.

File size: 3.7 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 = "CovarianceRQArd",
32    Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")]
33  public class CovarianceRQArd : Item, ICovarianceFunction {
34    [Storable]
35    private double sf2;
36    public double Scale { get { return sf2; } }
37    [Storable]
38    private double[] inverseLength;
39    public double[] InverseLength {
40      get {
41        if (inverseLength == null) return null;
42        double[] res = new double[inverseLength.Length];
43        Array.Copy(inverseLength, res, res.Length);
44        return res;
45      }
46    }
47    [Storable]
48    private double alpha;
49    public double Shape { get { return alpha; } }
50
51    [StorableConstructor]
52    protected CovarianceRQArd(bool deserializing)
53      : base(deserializing) {
54    }
55
56    protected CovarianceRQArd(CovarianceRQArd original, Cloner cloner)
57      : base(original, cloner) {
58      this.sf2 = original.sf2;
59      this.inverseLength = original.InverseLength; // array is cloned in the getter
60      this.alpha = original.alpha;
61    }
62
63    public CovarianceRQArd()
64      : base() {
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new CovarianceRQArd(this, cloner);
69    }
70
71    public int GetNumberOfParameters(int numberOfVariables) {
72      return numberOfVariables + 2;
73    }
74
75    public void SetParameter(double[] hyp) {
76      this.inverseLength = hyp.Take(hyp.Length - 2).Select(e => 1.0 / Math.Exp(e)).ToArray();
77      this.sf2 = Math.Exp(2 * hyp[hyp.Length - 2]);
78      this.alpha = Math.Exp(hyp[hyp.Length - 1]);
79    }
80
81
82    public double GetCovariance(double[,] x, int i, int j) {
83      double d = i == j
84                   ? 0.0
85                   : Util.SqrDist(x, i, j, inverseLength);
86      return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
87    }
88
89    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
90      double d = i == j
91                   ? 0.0
92                   : Util.SqrDist(x, i, j, inverseLength);
93      double b = 1 + 0.5 * d / alpha;
94      for (int k = 0; k < inverseLength.Length; k++) {
95        yield return sf2 * Math.Pow(b, -alpha - 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]);
96      }
97      yield return 2 * sf2 * Math.Pow(b, -alpha);
98      yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
99    }
100
101    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
102      double d = Util.SqrDist(x, i, xt, j, inverseLength);
103      return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.