Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs @ 8982

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

#1902: removed class HyperParameter and changed implementations of covariance and mean functions to remove the parameter value caching and event handlers for parameter caching. Instead it is now possible to create the actual covariance and mean functions as Func from templates and specified parameter values. The instances of mean and covariance functions configured in the GUI are actually templates where the structure and fixed parameters can be specified.

File size: 5.8 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 = "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
49    [StorableConstructor]
50    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
51    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public CovariancePeriodic()
56      : base() {
57      Name = ItemName;
58      Description = ItemDescription;
59
60      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."));
61      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."));
62      Parameters.Add(new OptionalValueParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function."));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new CovariancePeriodic(this, cloner);
67    }
68
69    public int GetNumberOfParameters(int numberOfVariables) {
70      return (ScaleParameter.Value != null ? 0 : 1) +
71       (PeriodParameter.Value != null ? 0 : 1) +
72       (InverseLengthParameter.Value != null ? 0 : 1);
73    }
74
75    public void SetParameter(double[] p) {
76      double scale, inverseLength, period;
77      GetParameterValues(p, out scale, out period, out inverseLength);
78      ScaleParameter.Value = new DoubleValue(scale);
79      PeriodParameter.Value = new DoubleValue(period);
80      InverseLengthParameter.Value = new DoubleValue(inverseLength);
81    }
82
83
84    private void GetParameterValues(double[] p, out double scale, out double period, out double inverseLength) {
85      // gather parameter values
86      int c = 0;
87      if (InverseLengthParameter.Value != null) {
88        inverseLength = InverseLengthParameter.Value.Value;
89      } else {
90        inverseLength = 1.0 / Math.Exp(p[c]);
91        c++;
92      }
93      if (PeriodParameter.Value != null) {
94        period = PeriodParameter.Value.Value;
95      } else {
96        period = Math.Exp(p[c]);
97        c++;
98      }
99      if (ScaleParameter.Value != null) {
100        scale = ScaleParameter.Value.Value;
101      } else {
102        scale = Math.Exp(2 * p[c]);
103        c++;
104      }
105      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriodic", "p");
106    }
107
108    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
109      double inverseLength, period, scale;
110      GetParameterValues(p, out scale, out period, out inverseLength);
111      // create functions
112      var cov = new ParameterizedCovarianceFunction();
113      cov.Covariance = (x, i, j) => {
114        double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
115        k = Math.PI * k / period;
116        k = Math.Sin(k) * inverseLength;
117        k = k * k;
118
119        return scale * Math.Exp(-2.0 * k);
120      };
121      cov.CrossCovariance = (x, xt, i, j) => {
122        double k = GetDistance(x, xt, i, j, columnIndices);
123        k = Math.PI * k / period;
124        k = Math.Sin(k) * inverseLength;
125        k = k * k;
126
127        return scale * Math.Exp(-2.0 * k);
128      };
129      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength);
130      return cov;
131    }
132
133
134    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) {
135      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
136      double gradient = Math.Sin(v) * inverseLength;
137      gradient *= gradient;
138      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
139      double r = Math.Sin(v) * inverseLength;
140      yield return 4.0 * scale * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
141      yield return 2.0 * scale * Math.Exp(-2 * gradient);
142    }
143
144    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
145      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.