Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8678 was 8678, checked in by gkronber, 12 years ago

#1902: added masking covariance function and made necessary changes to interface and utility class.

File size: 6.7 KB
RevLine 
[8417]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
[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;
[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
35    [Storable]
36    private double scale;
37    [Storable]
38    private readonly HyperParameter<DoubleValue> scaleParameter;
[8582]39    public IValueParameter<DoubleValue> ScaleParameter {
40      get { return scaleParameter; }
41    }
[8612]42
43    [Storable]
44    private double inverseLength;
45    [Storable]
46    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
[8582]47    public IValueParameter<DoubleValue> InverseLengthParameter {
48      get { return inverseLengthParameter; }
49    }
[8612]50
51    [Storable]
52    private double period;
53    [Storable]
54    private readonly HyperParameter<DoubleValue> periodParameter;
[8582]55    public IValueParameter<DoubleValue> PeriodParameter {
56      get { return periodParameter; }
57    }
58
59
[8417]60    [StorableConstructor]
[8612]61    private CovariancePeriodic(bool deserializing) : base(deserializing) { }
62    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
[8417]63      : base(original, cloner) {
[8582]64      this.scaleParameter = cloner.Clone(original.scaleParameter);
65      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
66      this.periodParameter = cloner.Clone(original.periodParameter);
67      this.scale = original.scale;
68      this.inverseLength = original.inverseLength;
69      this.period = original.period;
70
71      RegisterEvents();
[8417]72    }
[8582]73
[8417]74    public CovariancePeriodic()
75      : base() {
[8612]76      Name = ItemName;
77      Description = ItemDescription;
[8678]78
[8582]79      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.");
80      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.");
81      periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function.");
82      Parameters.Add(scaleParameter);
83      Parameters.Add(inverseLengthParameter);
84      Parameters.Add(periodParameter);
85
86      RegisterEvents();
[8417]87    }
[8323]88
[8582]89    [StorableHook(HookType.AfterDeserialization)]
90    private void AfterDeserialization() {
91      RegisterEvents();
92    }
93
[8417]94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new CovariancePeriodic(this, cloner);
[8323]96    }
97
[8582]98    // caching
99    private void RegisterEvents() {
[8612]100      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
101      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
102      Util.AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
[8323]103    }
104
[8612]105    public int GetNumberOfParameters(int numberOfVariables) {
[8582]106      return
107        (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed);
108    }
109
[8612]110    public void SetParameter(double[] hyp) {
[8582]111      int i = 0;
112      if (!inverseLengthParameter.Fixed) {
113        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
114        i++;
115      }
116      if (!periodParameter.Fixed) {
117        periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
118        i++;
119      }
120      if (!scaleParameter.Fixed) {
121        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
122        i++;
123      }
124      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp");
125    }
126
[8678]127    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
128      if (columnIndices == null || columnIndices.Count() != 1)
129        throw new ArgumentException("The periodic covariance function can only be used for one dimension.", "columnIndices");
130      double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
[8582]131      k = Math.PI * k / period;
[8491]132      k = Math.Sin(k) * inverseLength;
[8484]133      k = k * k;
[8323]134
[8582]135      return scale * Math.Exp(-2.0 * k);
[8323]136    }
137
[8678]138    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
139      if (columnIndices == null || columnIndices.Count() != 1)
140        throw new ArgumentException("The periodic covariance function can only be used for one dimension.", "columnIndices");
141      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
[8491]142      double gradient = Math.Sin(v) * inverseLength;
[8484]143      gradient *= gradient;
[8582]144      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
[8491]145      double r = Math.Sin(v) * inverseLength;
[8582]146      yield return 4.0 * scale * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
147      yield return 2.0 * scale * Math.Exp(-2 * gradient);
[8484]148    }
149
[8678]150    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
151      if (columnIndices == null || columnIndices.Count() != 1)
152        throw new ArgumentException("The periodic covariance function can only be used for one dimension.", "columnIndices");
153      double k = GetDistance(x, xt, i, j, columnIndices);
[8582]154      k = Math.PI * k / period;
[8491]155      k = Math.Sin(k) * inverseLength;
[8323]156      k = k * k;
157
[8582]158      return scale * Math.Exp(-2.0 * k);
[8323]159    }
160
[8678]161    private double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
162      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
[8323]163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.