Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceMaternIso.cs @ 8582

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

#1902 implemented a few covariance functions as parameterized named items. Implemented rudimentary view for Gaussian process models.

File size: 6.2 KB
RevLine 
[8562]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;
[8582]24using System.Linq;
[8562]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[8582]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[8562]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item(Name = "CovarianceMaternIso",
34    Description = "Matern covariance function for Gaussian processes.")]
[8582]35  public class CovarianceMaternIso : CovarianceFunction {
36    public IValueParameter<DoubleValue> ScaleParameter {
37      get { return scaleParameter; }
38    }
39    public IValueParameter<DoubleValue> InverseLengthParameter {
40      get { return inverseLengthParameter; }
41    }
42    public IConstrainedValueParameter<IntValue> DParameter {
43      get { return dParameter; }
44    }
45
[8562]46    [Storable]
[8582]47    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
[8562]48    [Storable]
[8582]49    private readonly HyperParameter<DoubleValue> scaleParameter;
50    [Storable]
51    private readonly ConstrainedValueParameter<IntValue> dParameter;
52
53    [Storable]
[8562]54    private double inverseLength;
55    [Storable]
[8582]56    private double sf2;
57    [Storable]
[8562]58    private int d;
59
60    [StorableConstructor]
61    protected CovarianceMaternIso(bool deserializing)
62      : base(deserializing) {
63    }
64
65    protected CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
66      : base(original, cloner) {
[8582]67      this.scaleParameter = cloner.Clone(original.scaleParameter);
[8562]68      this.sf2 = original.sf2;
[8582]69      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
[8562]70      this.inverseLength = original.inverseLength;
[8582]71      this.dParameter = cloner.Clone(original.dParameter);
[8562]72      this.d = original.d;
[8582]73      RegisterEvents();
[8562]74    }
75
76    public CovarianceMaternIso()
77      : base() {
[8582]78      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric Matern covariance function.");
79      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric Matern covariance function.");
80      var validDValues = new ItemSet<IntValue>();
81      validDValues.Add((IntValue)new IntValue(1).AsReadOnly());
82      validDValues.Add((IntValue)new IntValue(3).AsReadOnly());
83      validDValues.Add((IntValue)new IntValue(5).AsReadOnly());
84      dParameter = new ConstrainedValueParameter<IntValue>("D", "The d parameter (allowed values: 1, 3, or 5) of the isometric Matern covariance function.", validDValues, validDValues.First());
85
86      Parameters.Add(inverseLengthParameter);
87      Parameters.Add(scaleParameter);
88      Parameters.Add(dParameter);
89
90      RegisterEvents();
[8562]91    }
92
[8582]93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      RegisterEvents();
96    }
97
[8562]98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new CovarianceMaternIso(this, cloner);
100    }
101
[8582]102    // caching
103    private void RegisterEvents() {
104      AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
105      AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
106      AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; });
[8562]107    }
108
[8582]109    public override int GetNumberOfParameters(int numberOfVariables) {
110      return
111        (inverseLengthParameter.Fixed ? 0 : 1) +
112        (scaleParameter.Fixed ? 0 : 1);
[8562]113    }
114
[8582]115    public override void SetParameter(double[] hyp) {
116      int i = 0;
117      if (!inverseLengthParameter.Fixed) {
118        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
119        i++;
120      }
121      if (!scaleParameter.Fixed) {
122        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
123        i++;
124      }
125      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceMaternIso", "hyp");
126    }
[8562]127
[8582]128
[8562]129    private double m(double t) {
130      double f;
131      switch (d) {
132        case 1: { f = 1; break; }
133        case 3: { f = 1 + t; break; }
134        case 5: { f = 1 + t * (1 + t / 3.0); break; }
135        default: throw new InvalidOperationException();
136      }
137      return f * Math.Exp(-t);
138    }
139
140    private double dm(double t) {
141      double df;
142      switch (d) {
143        case 1: { df = 1; break; }
144        case 3: { df = t; break; }
145        case 5: { df = t * (1 + t) / 3.0; break; }
146        default: throw new InvalidOperationException();
147      }
148      return df * t * Math.Exp(-t);
149    }
150
[8582]151    public override double GetCovariance(double[,] x, int i, int j) {
[8562]152      double dist = i == j
153                   ? 0.0
154                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
155      return sf2 * m(dist);
156    }
157
[8582]158    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
[8562]159      double dist = i == j
160                   ? 0.0
161                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
162
163      yield return sf2 * dm(dist);
164      yield return 2 * sf2 * m(dist);
165    }
166
[8582]167    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
[8562]168      double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength));
169      return sf2 * m(dist);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.