Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSquaredExponentialIso.cs @ 10355

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

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

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