Free cookie consent management tool by TermsFeed Policy Generator

source: branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePeriodic.cs @ 15428

Last change on this file since 15428 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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