Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs @ 8682

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

#1902

  • fixed bug in cloning constructor of CovarianceMask
  • removed throwing Exceptions in CovariancePeriodic (the behaviour of the periodic covariance function now matches the GPML implementation again.
File size: 6.1 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.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
33  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
34
35    [Storable]
36    private double scale;
37    [Storable]
38    private readonly HyperParameter<DoubleValue> scaleParameter;
39    public IValueParameter<DoubleValue> ScaleParameter {
40      get { return scaleParameter; }
41    }
42
43    [Storable]
44    private double inverseLength;
45    [Storable]
46    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
47    public IValueParameter<DoubleValue> InverseLengthParameter {
48      get { return inverseLengthParameter; }
49    }
50
51    [Storable]
52    private double period;
53    [Storable]
54    private readonly HyperParameter<DoubleValue> periodParameter;
55    public IValueParameter<DoubleValue> PeriodParameter {
56      get { return periodParameter; }
57    }
58
59
60    [StorableConstructor]
61    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
62    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
63      : base(original, cloner) {
64      this.scaleParameter = cloner.Clone(original.scaleParameter);
65      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
66      this.periodParameter = cloner.Clone(original.periodParameter);
67      this.scale = original.scale;
68      this.inverseLength = original.inverseLength;
69      this.period = original.period;
70
71      RegisterEvents();
72    }
73
74    public CovariancePeriodic()
75      : base() {
76      Name = ItemName;
77      Description = ItemDescription;
78
79      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.");
80      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.");
81      periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function.");
82      Parameters.Add(scaleParameter);
83      Parameters.Add(inverseLengthParameter);
84      Parameters.Add(periodParameter);
85
86      RegisterEvents();
87    }
88
89    [StorableHook(HookType.AfterDeserialization)]
90    private void AfterDeserialization() {
91      RegisterEvents();
92    }
93
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new CovariancePeriodic(this, cloner);
96    }
97
98    // caching
99    private void RegisterEvents() {
100      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
101      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
102      Util.AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
103    }
104
105    public int GetNumberOfParameters(int numberOfVariables) {
106      return
107        (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed);
108    }
109
110    public void SetParameter(double[] hyp) {
111      int i = 0;
112      if (!inverseLengthParameter.Fixed) {
113        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
114        i++;
115      }
116      if (!periodParameter.Fixed) {
117        periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
118        i++;
119      }
120      if (!scaleParameter.Fixed) {
121        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
122        i++;
123      }
124      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp");
125    }
126
127    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
128      double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
129      k = Math.PI * k / period;
130      k = Math.Sin(k) * inverseLength;
131      k = k * k;
132
133      return scale * Math.Exp(-2.0 * k);
134    }
135
136    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
137      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
138      double gradient = Math.Sin(v) * inverseLength;
139      gradient *= gradient;
140      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
141      double r = Math.Sin(v) * inverseLength;
142      yield return 4.0 * scale * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
143      yield return 2.0 * scale * Math.Exp(-2 * gradient);
144    }
145
146    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
147      double k = GetDistance(x, xt, i, j, columnIndices);
148      k = Math.PI * k / period;
149      k = Math.Sin(k) * inverseLength;
150      k = k * k;
151
152      return scale * Math.Exp(-2.0 * k);
153    }
154
155    private double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
156      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.