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