Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10489 was 10489, checked in by gkronber, 10 years ago

#2125 fixed the bug that covariance functions returned the full gradient vector even when parameters are partially fixed.
changed the calculation of NN covariance and gradient to direct calculation (instead of AutoDiff)

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