Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12448 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    private bool HasFixedInverseLengthParameter {
45      get { return InverseLengthParameter.Value != null; }
46    }
47    private bool HasFixedScaleParameter {
48      get { return ScaleParameter.Value != null; }
49    }
50
51    [StorableConstructor]
52    private CovarianceSquaredExponentialIso(bool deserializing)
53      : base(deserializing) {
54    }
55
56    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
57      : base(original, cloner) {
58    }
59
60    public CovarianceSquaredExponentialIso()
61      : base() {
62      Name = ItemName;
63      Description = ItemDescription;
64
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."));
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new CovarianceSquaredExponentialIso(this, cloner);
71    }
72
73    public int GetNumberOfParameters(int numberOfVariables) {
74      return
75        (HasFixedScaleParameter ? 0 : 1) +
76        (HasFixedInverseLengthParameter ? 0 : 1);
77    }
78
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);
84    }
85
86
87    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
88      // gather parameter values
89      int c = 0;
90      if (HasFixedInverseLengthParameter) {
91        inverseLength = InverseLengthParameter.Value.Value;
92      } else {
93        inverseLength = 1.0 / Math.Exp(p[c]);
94        c++;
95      }
96
97      if (HasFixedScaleParameter) {
98        scale = ScaleParameter.Value.Value;
99      } else {
100        scale = Math.Exp(2 * p[c]);
101        c++;
102      }
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");
104    }
105
106    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
107      double inverseLength, scale;
108      GetParameterValues(p, out scale, out inverseLength);
109      var fixedInverseLength = HasFixedInverseLengthParameter;
110      var fixedScale = HasFixedScaleParameter;
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      };
123      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
124        fixedInverseLength, fixedScale);
125      return cov;
126    }
127
128    // order of returned gradients must match the order in GetParameterValues!
129    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices,
130      bool fixedInverseLength, bool fixedScale) {
131      double d = i == j
132                   ? 0.0
133                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
134      double g = Math.Exp(-d / 2.0);
135      if (!fixedInverseLength) yield return sf2 * g * d;
136      if (!fixedScale) yield return 2.0 * sf2 * g;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.