Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15612 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.2 KB
RevLine 
[9515]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9515]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 {
[9516]35    public IValueParameter<DoubleValue> ConstParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Const"]; }
[9515]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    }
[10489]46    private bool HasFixedConstParameter {
47      get { return ConstParameter.Value != null; }
48    }
49    private bool HasFixedScaleParameter {
50      get { return ScaleParameter.Value != null; }
51    }
[9515]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
[9516]67      Parameters.Add(new OptionalValueParameter<DoubleValue>("Const", "Additive constant in the polymomial."));
[9535]68      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the polynomial covariance function."));
[9515]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
[10489]78        (HasFixedConstParameter ? 0 : 1) +
79        (HasFixedScaleParameter ? 0 : 1);
[9515]80    }
81
82    public void SetParameter(double[] p) {
[9516]83      double @const, scale;
84      GetParameterValues(p, out @const, out scale);
85      ConstParameter.Value = new DoubleValue(@const);
[9515]86      ScaleParameter.Value = new DoubleValue(scale);
87    }
88
[9516]89    private void GetParameterValues(double[] p, out double @const, out double scale) {
[9515]90      // gather parameter values
91      int n = 0;
[10489]92      if (HasFixedConstParameter) {
[9516]93        @const = ConstParameter.Value.Value;
[9515]94      } else {
[9516]95        @const = Math.Exp(p[n]);
[9515]96        n++;
97      }
98
[10489]99      if (HasFixedScaleParameter) {
[9515]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
[13721]108    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[9516]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);
[10489]113      var fixedConst = HasFixedConstParameter;
114      var fixedScale = HasFixedScaleParameter;
[9515]115      // create functions
116      var cov = new ParameterizedCovarianceFunction();
[13721]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);
[10489]119      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale);
[9515]120      return cov;
121    }
122
[13784]123    private static IList<double> GetGradient(double[,] x, int i, int j, double c, double scale, int degree, int[] columnIndices,
[10489]124      bool fixedConst, bool fixedScale) {
[13721]125      double s = Util.ScalarProd(x, i, j, columnIndices, 1.0);
[13784]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;
[9515]130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.