Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 renamed covariance functions

File size: 4.9 KB
RevLine 
[8401]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;
[8484]23using System.Collections.Generic;
[8323]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8612]26using HeuristicLab.Data;
[8323]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[8371]29namespace HeuristicLab.Algorithms.DataAnalysis {
[8323]30  [StorableClass]
[8615]31  [Item(Name = "CovarianceSquaredExponentialIso",
[8323]32    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
[8615]33  public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
[8323]34    [Storable]
35    private double sf2;
36    [Storable]
[8612]37    private readonly HyperParameter<DoubleValue> scaleParameter;
38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
39
40    [Storable]
[8491]41    private double inverseLength;
[8612]42    [Storable]
43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
[8323]45
46    [StorableConstructor]
[8615]47    private CovarianceSquaredExponentialIso(bool deserializing)
[8323]48      : base(deserializing) {
49    }
50
[8615]51    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
[8323]52      : base(original, cloner) {
53      this.sf2 = original.sf2;
[8612]54      this.scaleParameter = cloner.Clone(original.scaleParameter);
55
[8491]56      this.inverseLength = original.inverseLength;
[8612]57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
58
59      RegisterEvents();
[8323]60    }
61
[8615]62    public CovarianceSquaredExponentialIso()
[8323]63      : base() {
[8612]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();
[8323]74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
[8615]77      return new CovarianceSquaredExponentialIso(this, cloner);
[8323]78    }
79
[8612]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
[8323]90    public int GetNumberOfParameters(int numberOfVariables) {
[8612]91      return
92        (scaleParameter.Fixed ? 0 : 1) +
93        (inverseLengthParameter.Fixed ? 0 : 1);
[8323]94    }
95
[8416]96    public void SetParameter(double[] hyp) {
[8612]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      }
[8615]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");
[8416]107    }
[8323]108
109
[8484]110    public double GetCovariance(double[,] x, int i, int j) {
111      double d = i == j
112                   ? 0.0
[8491]113                   : Util.SqrDist(x, i, j, inverseLength);
[8484]114      return sf2 * Math.Exp(-d / 2.0);
[8323]115    }
116
[8484]117    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
118      double d = i == j
119                   ? 0.0
[8491]120                   : Util.SqrDist(x, i, j, inverseLength);
[8484]121      double g = Math.Exp(-d / 2.0);
122      yield return sf2 * g * d;
123      yield return 2.0 * sf2 * g;
[8323]124    }
125
[8484]126    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
[8491]127      double d = Util.SqrDist(x, i, xt, j, inverseLength);
[8484]128      return sf2 * Math.Exp(-d / 2.0);
[8323]129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.