Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 implemented a few covariance functions as parameterized named items. Implemented rudimentary view for Gaussian process models.

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