Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17317 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.3 KB
RevLine 
[8401]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[8323]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8612]26using HeuristicLab.Data;
[8982]27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[8323]29
[8371]30namespace HeuristicLab.Algorithms.DataAnalysis {
[16565]31  [StorableType("1335C8EF-73CA-40F4-9124-EC6D7E3C68E0")]
[8615]32  [Item(Name = "CovarianceSquaredExponentialIso",
[8323]33    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
[8615]34  public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
[8982]35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
[8612]38
[8982]39    public IValueParameter<DoubleValue> InverseLengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
41    }
[8323]42
[10489]43    private bool HasFixedInverseLengthParameter {
44      get { return InverseLengthParameter.Value != null; }
45    }
46    private bool HasFixedScaleParameter {
47      get { return ScaleParameter.Value != null; }
48    }
49
[8323]50    [StorableConstructor]
[16565]51    private CovarianceSquaredExponentialIso(StorableConstructorFlag _) : base(_) {
[8323]52    }
53
[8615]54    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
[8323]55      : base(original, cloner) {
56    }
57
[8615]58    public CovarianceSquaredExponentialIso()
[8323]59      : base() {
[8612]60      Name = ItemName;
61      Description = ItemDescription;
62
[8982]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."));
[8323]65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
[8615]68      return new CovarianceSquaredExponentialIso(this, cloner);
[8323]69    }
70
[8982]71    public int GetNumberOfParameters(int numberOfVariables) {
72      return
[10489]73        (HasFixedScaleParameter ? 0 : 1) +
74        (HasFixedInverseLengthParameter ? 0 : 1);
[8612]75    }
76
[8982]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);
[8612]82    }
83
[8323]84
[8982]85    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
86      // gather parameter values
87      int c = 0;
[10489]88      if (HasFixedInverseLengthParameter) {
[8982]89        inverseLength = InverseLengthParameter.Value.Value;
90      } else {
91        inverseLength = 1.0 / Math.Exp(p[c]);
92        c++;
[8612]93      }
[8982]94
[10489]95      if (HasFixedScaleParameter) {
[8982]96        scale = ScaleParameter.Value.Value;
97      } else {
98        scale = Math.Exp(2 * p[c]);
99        c++;
[8612]100      }
[8982]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");
[8416]102    }
[8323]103
[13721]104    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]105      double inverseLength, scale;
106      GetParameterValues(p, out scale, out inverseLength);
[10489]107      var fixedInverseLength = HasFixedInverseLengthParameter;
108      var fixedScale = HasFixedScaleParameter;
[8982]109      // create functions
110      var cov = new ParameterizedCovarianceFunction();
111      cov.Covariance = (x, i, j) => {
112        double d = i == j
113                ? 0.0
[13721]114                : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[8982]115        return scale * Math.Exp(-d / 2.0);
116      };
117      cov.CrossCovariance = (x, xt, i, j) => {
[13721]118        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
[8982]119        return scale * Math.Exp(-d / 2.0);
120      };
[10489]121      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
122        fixedInverseLength, fixedScale);
[8982]123      return cov;
[8323]124    }
125
[9108]126    // order of returned gradients must match the order in GetParameterValues!
[13784]127    private static IList<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices,
[10489]128      bool fixedInverseLength, bool fixedScale) {
[8484]129      double d = i == j
130                   ? 0.0
[13721]131                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[8484]132      double g = Math.Exp(-d / 2.0);
[13784]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;
[8323]137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.