Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs @ 8933

Last change on this file since 8933 was 8933, checked in by gkronber, 11 years ago

#1902 corrected handling of length-parameter arrays in ARD functions and prevented stacking of mask covariance functions to make sure that the length-parameter and the enumerable of selected column indexes are equally long.

File size: 5.6 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 = "CovarianceSquaredExponentialArd", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
33  public sealed class CovarianceSquaredExponentialArd : 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<DoubleArray> inverseLengthParameter;
44    public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } }
45
46    [StorableConstructor]
47    private CovarianceSquaredExponentialArd(bool deserializing) : base(deserializing) { }
48    private CovarianceSquaredExponentialArd(CovarianceSquaredExponentialArd original, Cloner cloner)
49      : base(original, cloner) {
50      this.sf2 = original.sf2;
51      this.scaleParameter = cloner.Clone(original.scaleParameter);
52
53      if (original.inverseLength != null) {
54        this.inverseLength = new double[original.inverseLength.Length];
55        Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length);
56      }
57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
58
59      RegisterEvents();
60    }
61    public CovarianceSquaredExponentialArd()
62      : base() {
63      Name = ItemName;
64      Description = ItemDescription;
65
66      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD.");
67      this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
68
69      Parameters.Add(scaleParameter);
70      Parameters.Add(inverseLengthParameter);
71
72      RegisterEvents();
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new CovarianceSquaredExponentialArd(this, cloner);
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      RegisterEvents();
82    }
83
84    private void RegisterEvents() {
85      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
86      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => {
87        inverseLength =
88          inverseLengthParameter.Value.ToArray();
89      });
90    }
91
92    public int GetNumberOfParameters(int numberOfVariables) {
93      return
94        (scaleParameter.Fixed ? 0 : 1) +
95        (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
96    }
97
98
99    public void SetParameter(double[] hyp) {
100      int i = 0;
101      if (!scaleParameter.Fixed) {
102        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
103        i++;
104      }
105      if (!inverseLengthParameter.Fixed) {
106        inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
107        i += hyp.Skip(i).Count();
108      }
109      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "hyp");
110    }
111
112    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
113      double d = i == j
114                   ? 0.0
115                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
116      return sf2 * Math.Exp(-d / 2.0);
117    }
118
119    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
120      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
121      double d = i == j
122                   ? 0.0
123                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
124      int k = 0;
125      foreach (var columnIndex in columnIndices) {
126        double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
127        yield return sf2 * Math.Exp(-d / 2.0) * sqrDist;
128        k++;
129      }
130
131      yield return 2.0 * sf2 * Math.Exp(-d / 2.0);
132    }
133
134    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
135      double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
136      return sf2 * Math.Exp(-d / 2.0);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.