source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePolynomial.cs @ 13784

Last change on this file since 13784 was 13784, checked in by pfleck, 5 years ago

#2591 Made the creation of a GaussianProcessModel faster by avoiding additional iterators during calculation of the hyperparameter gradients.
The gradients of the hyperparameters are now calculated in one sweep and returned as IList, instead of returning an iterator (with yield return).
This avoids a large amount of Move-calls of the iterator, especially for covariance functions with a lot of hyperparameters.
Besides, the signature of the CovarianceGradientFunctionDelegate is changed, to return an IList instead of an IEnumerable to avoid unnececary ToList or ToArray calls.

File size: 5.2 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovariancePolynomial",
33    Description = "Polynomial covariance function for Gaussian processes.")]
34  public sealed class CovariancePolynomial : ParameterizedNamedItem, ICovarianceFunction {
35    public IValueParameter<DoubleValue> ConstParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Const"]; }
37    }
38
39    public IValueParameter<DoubleValue> ScaleParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
41    }
42
43    public IValueParameter<IntValue> DegreeParameter {
44      get { return (IValueParameter<IntValue>)Parameters["Degree"]; }
45    }
46    private bool HasFixedConstParameter {
47      get { return ConstParameter.Value != null; }
48    }
49    private bool HasFixedScaleParameter {
50      get { return ScaleParameter.Value != null; }
51    }
52
53    [StorableConstructor]
54    private CovariancePolynomial(bool deserializing)
55      : base(deserializing) {
56    }
57
58    private CovariancePolynomial(CovariancePolynomial original, Cloner cloner)
59      : base(original, cloner) {
60    }
61
62    public CovariancePolynomial()
63      : base() {
64      Name = ItemName;
65      Description = ItemDescription;
66
67      Parameters.Add(new OptionalValueParameter<DoubleValue>("Const", "Additive constant in the polymomial."));
68      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the polynomial covariance function."));
69      Parameters.Add(new ValueParameter<IntValue>("Degree", "The degree of the polynomial (only non-zero positive values allowed).", new IntValue(2)));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new CovariancePolynomial(this, cloner);
74    }
75
76    public int GetNumberOfParameters(int numberOfVariables) {
77      return
78        (HasFixedConstParameter ? 0 : 1) +
79        (HasFixedScaleParameter ? 0 : 1);
80    }
81
82    public void SetParameter(double[] p) {
83      double @const, scale;
84      GetParameterValues(p, out @const, out scale);
85      ConstParameter.Value = new DoubleValue(@const);
86      ScaleParameter.Value = new DoubleValue(scale);
87    }
88
89    private void GetParameterValues(double[] p, out double @const, out double scale) {
90      // gather parameter values
91      int n = 0;
92      if (HasFixedConstParameter) {
93        @const = ConstParameter.Value.Value;
94      } else {
95        @const = Math.Exp(p[n]);
96        n++;
97      }
98
99      if (HasFixedScaleParameter) {
100        scale = ScaleParameter.Value.Value;
101      } else {
102        scale = Math.Exp(2 * p[n]);
103        n++;
104      }
105      if (p.Length != n) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePolynomial", "p");
106    }
107
108    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
109      double @const, scale;
110      int degree = DegreeParameter.Value.Value;
111      if (degree <= 0) throw new ArgumentException("The degree parameter for CovariancePolynomial must be greater than zero.");
112      GetParameterValues(p, out @const, out scale);
113      var fixedConst = HasFixedConstParameter;
114      var fixedScale = HasFixedScaleParameter;
115      // create functions
116      var cov = new ParameterizedCovarianceFunction();
117      cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, columnIndices, 1.0), degree);
118      cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, columnIndices, 1.0), degree);
119      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale);
120      return cov;
121    }
122
123    private static IList<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, int[] columnIndices,
124      bool fixedConst, bool fixedScale) {
125      double s = Util.ScalarProd(x, i, j, columnIndices, 1.0);
126      var g = new List<double>(2);
127      if (!fixedConst) g.Add(c * degree * scale * Math.Pow(c + s, degree - 1));
128      if (!fixedScale) g.Add(2 * scale * Math.Pow(c + s, degree));
129      return g;
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.