Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialArd.cs @ 9598

Last change on this file since 9598 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.3 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;
[8323]24using System.Linq;
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 = "CovarianceSquaredExponentialArd", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
34  public sealed class CovarianceSquaredExponentialArd : ParameterizedNamedItem, ICovarianceFunction {
[8982]35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
[8473]38
[8982]39    public IValueParameter<DoubleArray> InverseLengthParameter {
40      get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
41    }
[8323]42
43    [StorableConstructor]
[8615]44    private CovarianceSquaredExponentialArd(bool deserializing) : base(deserializing) { }
45    private CovarianceSquaredExponentialArd(CovarianceSquaredExponentialArd original, Cloner cloner)
[8323]46      : base(original, cloner) {
47    }
[8615]48    public CovarianceSquaredExponentialArd()
[8323]49      : base() {
[8612]50      Name = ItemName;
51      Description = ItemDescription;
52
[8982]53      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD."));
54      Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."));
[8323]55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
[8615]58      return new CovarianceSquaredExponentialArd(this, cloner);
[8323]59    }
60
[8612]61    public int GetNumberOfParameters(int numberOfVariables) {
62      return
[8982]63        (ScaleParameter.Value != null ? 0 : 1) +
64        (InverseLengthParameter.Value != null ? 0 : numberOfVariables);
[8612]65    }
66
[8982]67    public void SetParameter(double[] p) {
68      double scale;
69      double[] inverseLength;
70      GetParameterValues(p, out scale, out inverseLength);
71      ScaleParameter.Value = new DoubleValue(scale);
72      InverseLengthParameter.Value = new DoubleArray(inverseLength);
73    }
[8612]74
[8982]75    private void GetParameterValues(double[] p, out double scale, out double[] inverseLength) {
76      int c = 0;
77      // gather parameter values
[9108]78      if (InverseLengthParameter.Value != null) {
79        inverseLength = InverseLengthParameter.Value.ToArray();
80      } else {
81        int length = p.Length;
82        if (ScaleParameter.Value == null) length--;
83        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
84        c += inverseLength.Length;
85      }
[8982]86      if (ScaleParameter.Value != null) {
87        scale = ScaleParameter.Value.Value;
88      } else {
89        scale = Math.Exp(2 * p[c]);
90        c++;
[8612]91      }
[8982]92      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "p");
[8416]93    }
94
[8982]95    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
96      double scale;
97      double[] inverseLength;
98      GetParameterValues(p, out scale, out inverseLength);
99      // create functions
100      var cov = new ParameterizedCovarianceFunction();
101      cov.Covariance = (x, i, j) => {
102        double d = i == j
103                 ? 0.0
104                 : Util.SqrDist(x, i, j, inverseLength, columnIndices);
105        return scale * Math.Exp(-d / 2.0);
106      };
107      cov.CrossCovariance = (x, xt, i, j) => {
108        double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
109        return scale * Math.Exp(-d / 2.0);
110      };
111      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength);
112      return cov;
[8323]113    }
114
[9108]115    // order of returned gradients must match the order in GetParameterValues!
[8982]116    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength) {
[8484]117      double d = i == j
118                   ? 0.0
[8678]119                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
[9108]120
[8933]121      int k = 0;
[8932]122      foreach (var columnIndex in columnIndices) {
[8933]123        double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
[8982]124        yield return scale * Math.Exp(-d / 2.0) * sqrDist;
[8933]125        k++;
[8323]126      }
[8982]127      yield return 2.0 * scale * Math.Exp(-d / 2.0);
[8323]128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.