Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePolynomial.cs @ 12062

Last change on this file since 12062 was 9535, checked in by gkronber, 12 years ago

#2032: corrected typo

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