Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16565 was 16565, checked in by gkronber, 5 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableType("1335C8EF-73CA-40F4-9124-EC6D7E3C68E0")]
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(StorableConstructorFlag _) : base(_) {
52    }
53
54    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
55      : base(original, cloner) {
56    }
57
58    public CovarianceSquaredExponentialIso()
59      : base() {
60      Name = ItemName;
61      Description = ItemDescription;
62
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function."));
64      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function."));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new CovarianceSquaredExponentialIso(this, cloner);
69    }
70
71    public int GetNumberOfParameters(int numberOfVariables) {
72      return
73        (HasFixedScaleParameter ? 0 : 1) +
74        (HasFixedInverseLengthParameter ? 0 : 1);
75    }
76
77    public void SetParameter(double[] p) {
78      double scale, inverseLength;
79      GetParameterValues(p, out scale, out inverseLength);
80      ScaleParameter.Value = new DoubleValue(scale);
81      InverseLengthParameter.Value = new DoubleValue(inverseLength);
82    }
83
84
85    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
86      // gather parameter values
87      int c = 0;
88      if (HasFixedInverseLengthParameter) {
89        inverseLength = InverseLengthParameter.Value.Value;
90      } else {
91        inverseLength = 1.0 / Math.Exp(p[c]);
92        c++;
93      }
94
95      if (HasFixedScaleParameter) {
96        scale = ScaleParameter.Value.Value;
97      } else {
98        scale = Math.Exp(2 * p[c]);
99        c++;
100      }
101      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "p");
102    }
103
104    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
105      double inverseLength, scale;
106      GetParameterValues(p, out scale, out inverseLength);
107      var fixedInverseLength = HasFixedInverseLengthParameter;
108      var fixedScale = HasFixedScaleParameter;
109      // create functions
110      var cov = new ParameterizedCovarianceFunction();
111      cov.Covariance = (x, i, j) => {
112        double d = i == j
113                ? 0.0
114                : Util.SqrDist(x, i, j, columnIndices, inverseLength);
115        return scale * Math.Exp(-d / 2.0);
116      };
117      cov.CrossCovariance = (x, xt, i, j) => {
118        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
119        return scale * Math.Exp(-d / 2.0);
120      };
121      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
122        fixedInverseLength, fixedScale);
123      return cov;
124    }
125
126    // order of returned gradients must match the order in GetParameterValues!
127    private static IList<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices,
128      bool fixedInverseLength, bool fixedScale) {
129      double d = i == j
130                   ? 0.0
131                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
132      double g = Math.Exp(-d / 2.0);
133      var gr = new List<double>(2);
134      if (!fixedInverseLength) gr.Add(sf2 * g * d);
135      if (!fixedScale) gr.Add(2.0 * sf2 * g);
136      return gr;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.