source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs @ 13721

Last change on this file since 13721 was 13721, checked in by mkommend, 5 years ago

#2591: Changed all GP covariance and mean functions to use int[] for column indices instead of IEnumerable<int>. Changed GP utils, GPModel and StudentTProcessModell as well to use fewer iterators and adapted unit tests to new interface.

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovarianceSquaredExponentialIso",
33    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
34  public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
38
39    public IValueParameter<DoubleValue> InverseLengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
41    }
42
43    private bool HasFixedInverseLengthParameter {
44      get { return InverseLengthParameter.Value != null; }
45    }
46    private bool HasFixedScaleParameter {
47      get { return ScaleParameter.Value != null; }
48    }
49
50    [StorableConstructor]
51    private CovarianceSquaredExponentialIso(bool deserializing)
52      : base(deserializing) {
53    }
54
55    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
56      : base(original, cloner) {
57    }
58
59    public CovarianceSquaredExponentialIso()
60      : base() {
61      Name = ItemName;
62      Description = ItemDescription;
63
64      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function."));
65      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function."));
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new CovarianceSquaredExponentialIso(this, cloner);
70    }
71
72    public int GetNumberOfParameters(int numberOfVariables) {
73      return
74        (HasFixedScaleParameter ? 0 : 1) +
75        (HasFixedInverseLengthParameter ? 0 : 1);
76    }
77
78    public void SetParameter(double[] p) {
79      double scale, inverseLength;
80      GetParameterValues(p, out scale, out inverseLength);
81      ScaleParameter.Value = new DoubleValue(scale);
82      InverseLengthParameter.Value = new DoubleValue(inverseLength);
83    }
84
85
86    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
87      // gather parameter values
88      int c = 0;
89      if (HasFixedInverseLengthParameter) {
90        inverseLength = InverseLengthParameter.Value.Value;
91      } else {
92        inverseLength = 1.0 / Math.Exp(p[c]);
93        c++;
94      }
95
96      if (HasFixedScaleParameter) {
97        scale = ScaleParameter.Value.Value;
98      } else {
99        scale = Math.Exp(2 * p[c]);
100        c++;
101      }
102      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "p");
103    }
104
105    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
106      double inverseLength, scale;
107      GetParameterValues(p, out scale, out inverseLength);
108      var fixedInverseLength = HasFixedInverseLengthParameter;
109      var fixedScale = HasFixedScaleParameter;
110      // create functions
111      var cov = new ParameterizedCovarianceFunction();
112      cov.Covariance = (x, i, j) => {
113        double d = i == j
114                ? 0.0
115                : Util.SqrDist(x, i, j, columnIndices, inverseLength);
116        return scale * Math.Exp(-d / 2.0);
117      };
118      cov.CrossCovariance = (x, xt, i, j) => {
119        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
120        return scale * Math.Exp(-d / 2.0);
121      };
122      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
123        fixedInverseLength, fixedScale);
124      return cov;
125    }
126
127    // order of returned gradients must match the order in GetParameterValues!
128    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices,
129      bool fixedInverseLength, bool fixedScale) {
130      double d = i == j
131                   ? 0.0
132                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
133      double g = Math.Exp(-d / 2.0);
134      if (!fixedInverseLength) yield return sf2 * g * d;
135      if (!fixedScale) yield return 2.0 * sf2 * g;
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.