Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs @ 11130

Last change on this file since 11130 was 10489, checked in by gkronber, 11 years ago

#2125 fixed the bug that covariance functions returned the full gradient vector even when parameters are partially fixed.
changed the calculation of NN covariance and gradient to direct calculation (instead of AutoDiff)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
34  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
35
36    public IValueParameter<DoubleValue> ScaleParameter {
37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
38    }
39
40    public IValueParameter<DoubleValue> InverseLengthParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
42    }
43
44    public IValueParameter<DoubleValue> PeriodParameter {
45      get { return (IValueParameter<DoubleValue>)Parameters["Period"]; }
46    }
47
48    private bool HasFixedScaleParameter {
49      get { return ScaleParameter.Value != null; }
50    }
51    private bool HasFixedInverseLengthParameter {
52      get { return InverseLengthParameter.Value != null; }
53    }
54    private bool HasFixedPeriodParameter {
55      get { return PeriodParameter.Value != null; }
56    }
57
58
59    [StorableConstructor]
60    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
61    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
62      : base(original, cloner) {
63    }
64
65    public CovariancePeriodic()
66      : base() {
67      Name = ItemName;
68      Description = ItemDescription;
69
70      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."));
71      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."));
72      Parameters.Add(new OptionalValueParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function."));
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new CovariancePeriodic(this, cloner);
77    }
78
79    public int GetNumberOfParameters(int numberOfVariables) {
80      return (HasFixedScaleParameter ? 0 : 1) +
81       (HasFixedPeriodParameter ? 0 : 1) +
82       (HasFixedInverseLengthParameter ? 0 : 1);
83    }
84
85    public void SetParameter(double[] p) {
86      double scale, inverseLength, period;
87      GetParameterValues(p, out scale, out period, out inverseLength);
88      ScaleParameter.Value = new DoubleValue(scale);
89      PeriodParameter.Value = new DoubleValue(period);
90      InverseLengthParameter.Value = new DoubleValue(inverseLength);
91    }
92
93
94    private void GetParameterValues(double[]
95      p, out double scale, out double period, out double inverseLength) {
96      // gather parameter values
97      int c = 0;
98      if (HasFixedInverseLengthParameter) {
99        inverseLength = InverseLengthParameter.Value.Value;
100      } else {
101        inverseLength = 1.0 / Math.Exp(p[c]);
102        c++;
103      }
104      if (HasFixedPeriodParameter) {
105        period = PeriodParameter.Value.Value;
106      } else {
107        period = Math.Exp(p[c]);
108        c++;
109      }
110      if (HasFixedScaleParameter) {
111        scale = ScaleParameter.Value.Value;
112      } else {
113        scale = Math.Exp(2 * p[c]);
114        c++;
115      }
116      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriodic", "p");
117    }
118
119    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
120      double inverseLength, period, scale;
121      GetParameterValues(p, out scale, out period, out inverseLength);
122      var fixedInverseLength = HasFixedInverseLengthParameter;
123      var fixedPeriod = HasFixedPeriodParameter;
124      var fixedScale = HasFixedScaleParameter;
125      // create functions
126      var cov = new ParameterizedCovarianceFunction();
127      cov.Covariance = (x, i, j) => {
128        double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
129        k = Math.PI * k / period;
130        k = Math.Sin(k) * inverseLength;
131        k = k * k;
132
133        return scale * Math.Exp(-2.0 * k);
134      };
135      cov.CrossCovariance = (x, xt, i, j) => {
136        double k = GetDistance(x, xt, i, j, columnIndices);
137        k = Math.PI * k / period;
138        k = Math.Sin(k) * inverseLength;
139        k = k * k;
140
141        return scale * Math.Exp(-2.0 * k);
142      };
143      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale);
144      return cov;
145    }
146
147
148    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength,
149      bool fixedInverseLength, bool fixedPeriod, bool fixedScale) {
150      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
151      double gradient = Math.Sin(k) * inverseLength;
152      gradient *= gradient;
153      if (!fixedInverseLength) yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
154      if (!fixedPeriod) {
155        double r = Math.Sin(k) * inverseLength;
156        yield return 2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength;
157      }
158      if (!fixedScale)
159        yield return 2.0 * scale * Math.Exp(-2 * gradient);
160
161    }
162
163    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
164      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.