Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.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.4 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 = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
31  public class CovariancePeriodic : Item, ICovarianceFunction {
32    [Storable]
33    private double sf2;
34    public double Scale { get { return sf2; } }
35    [Storable]
36    private double l;
37    public double Length { get { return l; } }
38    [Storable]
39    private double p;
40    public double Period { get { return p; } }
41
42    public int GetNumberOfParameters(int numberOfVariables) {
43      return 3;
44    }
45    [StorableConstructor]
46    protected CovariancePeriodic(bool deserializing) : base(deserializing) { }
47    protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
48      : base(original, cloner) {
49      sf2 = original.sf2;
50      l = original.l;
51      p = original.p;
52    }
53    public CovariancePeriodic()
54      : base() {
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new CovariancePeriodic(this, cloner);
59    }
60
61    public void SetParameter(double[] hyp) {
62      if (hyp.Length != 3) throw new ArgumentException();
63      this.l = Math.Exp(hyp[0]);
64      this.p = Math.Exp(hyp[1]);
65      this.sf2 = Math.Exp(2 * hyp[2]);
66    }
67
68    public double GetCovariance(double[,] x, int i, int j) {
69      double k = i == j ? 0.0 : GetDistance(x, x, i, j);
70      k = Math.PI * k / p;
71      k = Math.Sin(k) / l;
72      k = k * k;
73
74      return sf2 * Math.Exp(-2.0 * k);
75    }
76
77    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
78      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p;
79      double gradient = Math.Sin(v) / l;
80      gradient *= gradient;
81      yield return 4.0 * sf2 * Math.Exp(-2.0 * gradient) * gradient;
82      double r = Math.Sin(v) / l;
83      yield return 4.0 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
84      yield return 2.0 * sf2 * Math.Exp(-2 * gradient);
85    }
86
87    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
88      double k = GetDistance(x, xt, i, j);
89      k = Math.PI * k / p;
90      k = Math.Sin(k) / l;
91      k = k * k;
92
93      return sf2 * Math.Exp(-2.0 * k);
94    }
95
96    private double GetDistance(double[,] x, double[,] xt, int i, int j) {
97      return Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(xt, j)));
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.