Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 fixed build fail.

File size: 6.3 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.Parameters;
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.")]
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
46    [Storable]
47    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
48    [Storable]
49    private readonly HyperParameter<DoubleValue> scaleParameter;
50    [Storable]
51    private readonly ConstrainedValueParameter<IntValue> dParameter;
52
53    [Storable]
54    private double inverseLength;
55    [Storable]
56    private double sf2;
57    [Storable]
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) {
67      this.scaleParameter = cloner.Clone(original.scaleParameter);
68      this.sf2 = original.sf2;
69      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
70      this.inverseLength = original.inverseLength;
71      this.dParameter = cloner.Clone(original.dParameter);
72      this.d = original.d;
73      RegisterEvents();
74    }
75
76    public CovarianceMaternIso()
77      : base() {
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      d = dParameter.Value.Value;
86
87      Parameters.Add(inverseLengthParameter);
88      Parameters.Add(scaleParameter);
89      Parameters.Add(dParameter);
90
91      RegisterEvents();
92    }
93
94    [StorableHook(HookType.AfterDeserialization)]
95    private void AfterDeserialization() {
96      RegisterEvents();
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new CovarianceMaternIso(this, cloner);
101    }
102
103    // caching
104    private void RegisterEvents() {
105      AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
106      AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
107      AttachValueChangeHandler<IntValue, int>(dParameter, () => { d = dParameter.Value.Value; });
108    }
109
110    public override int GetNumberOfParameters(int numberOfVariables) {
111      return
112        (inverseLengthParameter.Fixed ? 0 : 1) +
113        (scaleParameter.Fixed ? 0 : 1);
114    }
115
116    public override void SetParameter(double[] hyp) {
117      int i = 0;
118      if (!inverseLengthParameter.Fixed) {
119        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
120        i++;
121      }
122      if (!scaleParameter.Fixed) {
123        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
124        i++;
125      }
126      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceMaternIso", "hyp");
127    }
128
129
130    private double m(double t) {
131      double f;
132      switch (d) {
133        case 1: { f = 1; break; }
134        case 3: { f = 1 + t; break; }
135        case 5: { f = 1 + t * (1 + t / 3.0); break; }
136        default: throw new InvalidOperationException();
137      }
138      return f * Math.Exp(-t);
139    }
140
141    private double dm(double t) {
142      double df;
143      switch (d) {
144        case 1: { df = 1; break; }
145        case 3: { df = t; break; }
146        case 5: { df = t * (1 + t) / 3.0; break; }
147        default: throw new InvalidOperationException();
148      }
149      return df * t * Math.Exp(-t);
150    }
151
152    public override double GetCovariance(double[,] x, int i, int j) {
153      double dist = i == j
154                   ? 0.0
155                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
156      return sf2 * m(dist);
157    }
158
159    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
160      double dist = i == j
161                   ? 0.0
162                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength));
163
164      yield return sf2 * dm(dist);
165      yield return 2 * sf2 * m(dist);
166    }
167
168    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
169      double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength));
170      return sf2 * m(dist);
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.