Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSquaredExponentialIso.cs @ 8615

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

#1902 renamed covariance functions

File size: 4.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.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  [Item(Name = "CovarianceSquaredExponentialIso",
32    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
33  public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
34    [Storable]
35    private double sf2;
36    [Storable]
37    private readonly HyperParameter<DoubleValue> scaleParameter;
38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
39
40    [Storable]
41    private double inverseLength;
42    [Storable]
43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
45
46    [StorableConstructor]
47    private CovarianceSquaredExponentialIso(bool deserializing)
48      : base(deserializing) {
49    }
50
51    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
52      : base(original, cloner) {
53      this.sf2 = original.sf2;
54      this.scaleParameter = cloner.Clone(original.scaleParameter);
55
56      this.inverseLength = original.inverseLength;
57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
58
59      RegisterEvents();
60    }
61
62    public CovarianceSquaredExponentialIso()
63      : base() {
64      Name = ItemName;
65      Description = ItemDescription;
66
67      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function.");
68      this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function.");
69
70      Parameters.Add(scaleParameter);
71      Parameters.Add(inverseLengthParameter);
72
73      RegisterEvents();
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new CovarianceSquaredExponentialIso(this, cloner);
78    }
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      RegisterEvents();
83    }
84
85    private void RegisterEvents() {
86      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
87      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
88    }
89
90    public int GetNumberOfParameters(int numberOfVariables) {
91      return
92        (scaleParameter.Fixed ? 0 : 1) +
93        (inverseLengthParameter.Fixed ? 0 : 1);
94    }
95
96    public void SetParameter(double[] hyp) {
97      int i = 0;
98      if (!inverseLengthParameter.Fixed) {
99        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
100        i++;
101      }
102      if (!scaleParameter.Fixed) {
103        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
104        i++;
105      }
106      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "hyp");
107    }
108
109
110    public double GetCovariance(double[,] x, int i, int j) {
111      double d = i == j
112                   ? 0.0
113                   : Util.SqrDist(x, i, j, inverseLength);
114      return sf2 * Math.Exp(-d / 2.0);
115    }
116
117    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
118      double d = i == j
119                   ? 0.0
120                   : Util.SqrDist(x, i, j, inverseLength);
121      double g = Math.Exp(-d / 2.0);
122      yield return sf2 * g * d;
123      yield return 2.0 * sf2 * g;
124    }
125
126    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
127      double d = Util.SqrDist(x, i, xt, j, inverseLength);
128      return sf2 * Math.Exp(-d / 2.0);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.