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
RevLine 
[8417]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8417]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
[8323]21
[8417]22using System;
[8484]23using System.Collections.Generic;
[8582]24using System.Linq;
[8417]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[8582]27using HeuristicLab.Data;
[8982]28using HeuristicLab.Parameters;
[8417]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
[8612]34  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
35
[8582]36    public IValueParameter<DoubleValue> ScaleParameter {
[8982]37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
[8582]38    }
[8612]39
[8582]40    public IValueParameter<DoubleValue> InverseLengthParameter {
[8982]41      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
[8582]42    }
[8612]43
[8582]44    public IValueParameter<DoubleValue> PeriodParameter {
[8982]45      get { return (IValueParameter<DoubleValue>)Parameters["Period"]; }
[8582]46    }
47
48
[8417]49    [StorableConstructor]
[8612]50    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
51    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
[8417]52      : base(original, cloner) {
53    }
[8582]54
[8417]55    public CovariancePeriodic()
56      : base() {
[8612]57      Name = ItemName;
58      Description = ItemDescription;
[8678]59
[8982]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."));
[8417]63    }
[8323]64
[8417]65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new CovariancePeriodic(this, cloner);
[8323]67    }
68
[8982]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);
[8323]73    }
74
[8982]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);
[8582]81    }
82
[8982]83
[9108]84    private void GetParameterValues(double[]
85      p, out double scale, out double period, out double inverseLength) {
[8982]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++;
[8582]93      }
[8982]94      if (PeriodParameter.Value != null) {
95        period = PeriodParameter.Value.Value;
96      } else {
97        period = Math.Exp(p[c]);
98        c++;
[8582]99      }
[8982]100      if (ScaleParameter.Value != null) {
101        scale = ScaleParameter.Value.Value;
102      } else {
103        scale = Math.Exp(2 * p[c]);
104        c++;
[8582]105      }
[8982]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");
[8582]107    }
108
[8982]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;
[8323]119
[8982]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;
[8323]132    }
133
[8982]134
135    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) {
[9211]136      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
137      double gradient = Math.Sin(k) * inverseLength;
[8484]138      gradient *= gradient;
[8582]139      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
[9211]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
[8484]144    }
145
[8982]146    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
[8678]147      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
[8323]148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.