Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRationalQuadraticIso.cs @ 9844

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

#1902 renamed covariance functions

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