Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9651 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.8 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
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[]
85      p, out double scale, out double period, out double inverseLength) {
86      // gather parameter values
87      int c = 0;
88      if (InverseLengthParameter.Value != null) {
89        inverseLength = InverseLengthParameter.Value.Value;
90      } else {
91        inverseLength = 1.0 / Math.Exp(p[c]);
92        c++;
93      }
94      if (PeriodParameter.Value != null) {
95        period = PeriodParameter.Value.Value;
96      } else {
97        period = Math.Exp(p[c]);
98        c++;
99      }
100      if (ScaleParameter.Value != null) {
101        scale = ScaleParameter.Value.Value;
102      } else {
103        scale = Math.Exp(2 * p[c]);
104        c++;
105      }
106      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriodic", "p");
107    }
108
109    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
110      double inverseLength, period, scale;
111      GetParameterValues(p, out scale, out period, out inverseLength);
112      // create functions
113      var cov = new ParameterizedCovarianceFunction();
114      cov.Covariance = (x, i, j) => {
115        double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
116        k = Math.PI * k / period;
117        k = Math.Sin(k) * inverseLength;
118        k = k * k;
119
120        return scale * Math.Exp(-2.0 * k);
121      };
122      cov.CrossCovariance = (x, xt, i, j) => {
123        double k = GetDistance(x, xt, i, j, columnIndices);
124        k = Math.PI * k / period;
125        k = Math.Sin(k) * inverseLength;
126        k = k * k;
127
128        return scale * Math.Exp(-2.0 * k);
129      };
130      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength);
131      return cov;
132    }
133
134
135    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) {
136      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
137      double gradient = Math.Sin(k) * inverseLength;
138      gradient *= gradient;
139      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
140      double r = Math.Sin(k) * inverseLength;
141      yield return 2.0 * k * scale * Math.Exp(-2 * r * r) *Math.Sin(2*k) * inverseLength * inverseLength;
142      yield return 2.0 * scale * Math.Exp(-2 * gradient);
143
144    }
145
146    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
147      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.