Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12341 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.5 KB
RevLine 
[8417]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
[10489]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    }
[8582]57
[10489]58
[8417]59    [StorableConstructor]
[8612]60    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
61    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
[8417]62      : base(original, cloner) {
63    }
[8582]64
[8417]65    public CovariancePeriodic()
66      : base() {
[8612]67      Name = ItemName;
68      Description = ItemDescription;
[8678]69
[8982]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."));
[8417]73    }
[8323]74
[8417]75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new CovariancePeriodic(this, cloner);
[8323]77    }
78
[8982]79    public int GetNumberOfParameters(int numberOfVariables) {
[10489]80      return (HasFixedScaleParameter ? 0 : 1) +
81       (HasFixedPeriodParameter ? 0 : 1) +
82       (HasFixedInverseLengthParameter ? 0 : 1);
[8323]83    }
84
[8982]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);
[8582]91    }
92
[8982]93
[10489]94    private void GetParameterValues(double[]
[9108]95      p, out double scale, out double period, out double inverseLength) {
[8982]96      // gather parameter values
97      int c = 0;
[10489]98      if (HasFixedInverseLengthParameter) {
[8982]99        inverseLength = InverseLengthParameter.Value.Value;
100      } else {
101        inverseLength = 1.0 / Math.Exp(p[c]);
102        c++;
[8582]103      }
[10489]104      if (HasFixedPeriodParameter) {
[8982]105        period = PeriodParameter.Value.Value;
106      } else {
107        period = Math.Exp(p[c]);
108        c++;
[8582]109      }
[10489]110      if (HasFixedScaleParameter) {
[8982]111        scale = ScaleParameter.Value.Value;
112      } else {
113        scale = Math.Exp(2 * p[c]);
114        c++;
[8582]115      }
[8982]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");
[8582]117    }
118
[8982]119    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
120      double inverseLength, period, scale;
121      GetParameterValues(p, out scale, out period, out inverseLength);
[10489]122      var fixedInverseLength = HasFixedInverseLengthParameter;
123      var fixedPeriod = HasFixedPeriodParameter;
124      var fixedScale = HasFixedScaleParameter;
[8982]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;
[8323]132
[8982]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      };
[10489]143      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale);
[8982]144      return cov;
[8323]145    }
146
[8982]147
[10489]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) {
[9211]150      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
151      double gradient = Math.Sin(k) * inverseLength;
[8484]152      gradient *= gradient;
[10489]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);
[9211]160
[8484]161    }
162
[8982]163    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
[8678]164      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
[8323]165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.