Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2526: Updated year of copyrights in license headers

File size: 6.5 KB
RevLine 
[8417]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
[8417]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8582]26using HeuristicLab.Data;
[8982]27using HeuristicLab.Parameters;
[8417]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
[8612]33  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
34
[8582]35    public IValueParameter<DoubleValue> ScaleParameter {
[8982]36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
[8582]37    }
[8612]38
[8582]39    public IValueParameter<DoubleValue> InverseLengthParameter {
[8982]40      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
[8582]41    }
[8612]42
[8582]43    public IValueParameter<DoubleValue> PeriodParameter {
[8982]44      get { return (IValueParameter<DoubleValue>)Parameters["Period"]; }
[8582]45    }
46
[10489]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    }
[8582]56
[10489]57
[8417]58    [StorableConstructor]
[8612]59    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
60    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
[8417]61      : base(original, cloner) {
62    }
[8582]63
[8417]64    public CovariancePeriodic()
65      : base() {
[8612]66      Name = ItemName;
67      Description = ItemDescription;
[8678]68
[8982]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."));
[8417]72    }
[8323]73
[8417]74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new CovariancePeriodic(this, cloner);
[8323]76    }
77
[8982]78    public int GetNumberOfParameters(int numberOfVariables) {
[10489]79      return (HasFixedScaleParameter ? 0 : 1) +
80       (HasFixedPeriodParameter ? 0 : 1) +
81       (HasFixedInverseLengthParameter ? 0 : 1);
[8323]82    }
83
[8982]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);
[8582]90    }
91
[8982]92
[10489]93    private void GetParameterValues(double[]
[9108]94      p, out double scale, out double period, out double inverseLength) {
[8982]95      // gather parameter values
96      int c = 0;
[10489]97      if (HasFixedInverseLengthParameter) {
[8982]98        inverseLength = InverseLengthParameter.Value.Value;
99      } else {
100        inverseLength = 1.0 / Math.Exp(p[c]);
101        c++;
[8582]102      }
[10489]103      if (HasFixedPeriodParameter) {
[8982]104        period = PeriodParameter.Value.Value;
105      } else {
106        period = Math.Exp(p[c]);
107        c++;
[8582]108      }
[10489]109      if (HasFixedScaleParameter) {
[8982]110        scale = ScaleParameter.Value.Value;
111      } else {
112        scale = Math.Exp(2 * p[c]);
113        c++;
[8582]114      }
[8982]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");
[8582]116    }
117
[13784]118    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]119      double inverseLength, period, scale;
120      GetParameterValues(p, out scale, out period, out inverseLength);
[10489]121      var fixedInverseLength = HasFixedInverseLengthParameter;
122      var fixedPeriod = HasFixedPeriodParameter;
123      var fixedScale = HasFixedScaleParameter;
[8982]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;
[8323]131
[8982]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      };
[10489]142      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale);
[8982]143      return cov;
[8323]144    }
145
[13784]146    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double period, double inverseLength,
[10489]147      bool fixedInverseLength, bool fixedPeriod, bool fixedScale) {
[9211]148      double k = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
149      double gradient = Math.Sin(k) * inverseLength;
[8484]150      gradient *= gradient;
[13784]151      var g = new List<double>(3);
152      if (!fixedInverseLength)
153        g.Add(4.0 * scale * Math.Exp(-2.0 * gradient) * gradient);
[10489]154      if (!fixedPeriod) {
155        double r = Math.Sin(k) * inverseLength;
[13784]156        g.Add(2.0 * k * scale * Math.Exp(-2 * r * r) * Math.Sin(2 * k) * inverseLength * inverseLength);
[10489]157      }
158      if (!fixedScale)
[13784]159        g.Add(2.0 * scale * Math.Exp(-2 * gradient));
160      return g;
[8484]161    }
162
[13721]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));
[8323]165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.