Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQArd.cs @ 8612

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

#1902 implemented all mean and covariance functions with parameters as ParameterizedNamedItems

File size: 6.1 KB
RevLine 
[8565]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;
[8612]27using HeuristicLab.Data;
[8565]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovarianceRQArd",
33    Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")]
[8612]34  public sealed class CovarianceRQArd : ParameterizedNamedItem, ICovarianceFunction {
[8565]35    [Storable]
36    private double sf2;
37    [Storable]
[8612]38    private readonly HyperParameter<DoubleValue> scaleParameter;
39    public IValueParameter<DoubleValue> ScaleParameter {
40      get { return scaleParameter; }
41    }
42
43    [Storable]
[8565]44    private double[] inverseLength;
[8612]45    [Storable]
46    private readonly HyperParameter<DoubleArray> inverseLengthParameter;
47    public IValueParameter<DoubleArray> InverseLengthParameter {
48      get { return inverseLengthParameter; }
[8565]49    }
[8612]50
[8565]51    [Storable]
[8612]52    private double shape;
53    [Storable]
54    private readonly HyperParameter<DoubleValue> shapeParameter;
55    public IValueParameter<DoubleValue> ShapeParameter {
56      get { return shapeParameter; }
57    }
[8565]58
59    [StorableConstructor]
[8612]60    private CovarianceRQArd(bool deserializing)
[8565]61      : base(deserializing) {
62    }
63
[8612]64    private CovarianceRQArd(CovarianceRQArd original, Cloner cloner)
[8565]65      : base(original, cloner) {
[8612]66      this.scaleParameter = cloner.Clone(original.scaleParameter);
[8565]67      this.sf2 = original.sf2;
[8612]68
69      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
70      if (original.inverseLength != null) {
71        this.inverseLength = new double[original.inverseLength.Length];
72        Array.Copy(original.inverseLength, inverseLength, inverseLength.Length);
73      }
74
75      this.shapeParameter = cloner.Clone(original.shapeParameter);
76      this.shape = original.shape;
77
78      RegisterEvents();
[8565]79    }
80
81    public CovarianceRQArd()
82      : base() {
[8612]83      Name = ItemName;
84      Description = ItemDescription;
85
86      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the rational quadratic covariance function with ARD.");
87      this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
88      this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the rational quadratic covariance function with ARD.");
89
90      Parameters.Add(scaleParameter);
91      Parameters.Add(inverseLengthParameter);
92      Parameters.Add(shapeParameter);
93
94      RegisterEvents();
[8565]95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new CovarianceRQArd(this, cloner);
99    }
100
[8612]101    [StorableHook(HookType.AfterDeserialization)]
102    private void AfterDeserialization() {
103      RegisterEvents();
104    }
105
106    private void RegisterEvents() {
107      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
108      Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; });
109      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
110    }
111
[8565]112    public int GetNumberOfParameters(int numberOfVariables) {
[8612]113      return
114        (scaleParameter.Fixed ? 0 : 1) +
115        (shapeParameter.Fixed ? 0 : 1) +
116        (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
[8565]117    }
118
119    public void SetParameter(double[] hyp) {
[8612]120      int i = 0;
121      if (!scaleParameter.Fixed) {
122        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
123        i++;
124      }
125      if (!shapeParameter.Fixed) {
126        shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
127        i++;
128      }
129      if (!inverseLengthParameter.Fixed) {
130        inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
131        i += hyp.Skip(i).Count();
132      }
133      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQArd", "hyp");
[8565]134    }
135
136
137    public double GetCovariance(double[,] x, int i, int j) {
138      double d = i == j
139                   ? 0.0
140                   : Util.SqrDist(x, i, j, inverseLength);
[8612]141      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
[8565]142    }
143
144    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
145      double d = i == j
146                   ? 0.0
147                   : Util.SqrDist(x, i, j, inverseLength);
[8612]148      double b = 1 + 0.5 * d / shape;
[8565]149      for (int k = 0; k < inverseLength.Length; k++) {
[8612]150        yield return sf2 * Math.Pow(b, -shape - 1) * Util.SqrDist(x[i, k] * inverseLength[k], x[j, k] * inverseLength[k]);
[8565]151      }
[8612]152      yield return 2 * sf2 * Math.Pow(b, -shape);
153      yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
[8565]154    }
155
156    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
157      double d = Util.SqrDist(x, i, xt, j, inverseLength);
[8612]158      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
[8565]159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.