Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12341 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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