Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePiecewisePolynomial.cs @ 9598

Last change on this file since 9598 was 9543, checked in by gkronber, 12 years ago

#2059: fixed problem in piecewise polynomial covariance function in combination with dimension masking

File size: 6.5 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 = "CovariancePiecewisePolynomial",
34    Description = "Piecewise polynomial covariance function with compact support for Gaussian processes.")]
35  public sealed class CovariancePiecewisePolynomial : ParameterizedNamedItem, ICovarianceFunction {
36    public IValueParameter<DoubleValue> LengthParameter {
37      get { return (IValueParameter<DoubleValue>)Parameters["Length"]; }
38    }
39
40    public IValueParameter<DoubleValue> ScaleParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
42    }
43
44    public IConstrainedValueParameter<IntValue> VParameter {
45      get { return (IConstrainedValueParameter<IntValue>)Parameters["V"]; }
46    }
47
48    [StorableConstructor]
49    private CovariancePiecewisePolynomial(bool deserializing)
50      : base(deserializing) {
51    }
52
53    private CovariancePiecewisePolynomial(CovariancePiecewisePolynomial original, Cloner cloner)
54      : base(original, cloner) {
55    }
56
57    public CovariancePiecewisePolynomial()
58      : base() {
59      Name = ItemName;
60      Description = ItemDescription;
61
62      Parameters.Add(new OptionalValueParameter<DoubleValue>("Length", "The length parameter of the isometric piecewise polynomial covariance function."));
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the piecewise polynomial covariance function."));
64
65      var validValues = new ItemSet<IntValue>(new IntValue[] {
66        (IntValue)(new IntValue().AsReadOnly()),
67        (IntValue)(new IntValue(1).AsReadOnly()),
68        (IntValue)(new IntValue(2).AsReadOnly()),
69        (IntValue)(new IntValue(3).AsReadOnly()) });
70      Parameters.Add(new ConstrainedValueParameter<IntValue>("V", "The v parameter of the piecewise polynomial function (allowed values 0, 1, 2, 3).", validValues, validValues.First()));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new CovariancePiecewisePolynomial(this, cloner);
75    }
76
77    public int GetNumberOfParameters(int numberOfVariables) {
78      return
79        (LengthParameter.Value != null ? 0 : 1) +
80        (ScaleParameter.Value != null ? 0 : 1);
81    }
82
83    public void SetParameter(double[] p) {
84      double @const, scale;
85      GetParameterValues(p, out @const, out scale);
86      LengthParameter.Value = new DoubleValue(@const);
87      ScaleParameter.Value = new DoubleValue(scale);
88    }
89
90    private void GetParameterValues(double[] p, out double length, out double scale) {
91      // gather parameter values
92      int n = 0;
93      if (LengthParameter.Value != null) {
94        length = LengthParameter.Value.Value;
95      } else {
96        length = Math.Exp(p[n]);
97        n++;
98      }
99
100      if (ScaleParameter.Value != null) {
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 CovariancePiecewisePolynomial", "p");
107    }
108
109    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
110      double length, scale;
111      int v = VParameter.Value.Value;
112      GetParameterValues(p, out length, out scale);
113      int exp = (int)Math.Floor(columnIndices.Count() / 2.0) + v + 1;
114
115      Func<double, double> f;
116      Func<double, double> df;
117      switch (v) {
118        case 0:
119          f = (r) => 1.0;
120          df = (r) => 0.0;
121          break;
122        case 1:
123          f = (r) => 1 + (exp + 1) * r;
124          df = (r) => exp + 1;
125          break;
126        case 2:
127          f = (r) => 1 + (exp + 2) * r + (exp * exp + 4.0 * exp + 3) / 3.0 * r * r;
128          df = (r) => (exp + 2) + 2 * (exp * exp + 4.0 * exp + 3) / 3.0 * r;
129          break;
130        case 3:
131          f = (r) => 1 + (exp + 3) * r + (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r * r +
132                     (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 15.0 * r * r * r;
133          df = (r) => (exp + 3) + 2 * (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r +
134                      (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 5.0 * r * r;
135          break;
136        default: throw new ArgumentException();
137      }
138
139      // create functions
140      var cov = new ParameterizedCovarianceFunction();
141      cov.Covariance = (x, i, j) => {
142        double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices));
143        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
144      };
145      cov.CrossCovariance = (x, xt, i, j) => {
146        double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, 1.0 / length, columnIndices));
147        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
148      };
149      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices);
150      return cov;
151    }
152
153    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, IEnumerable<int> columnIndices) {
154      double k = Math.Sqrt(Util.SqrDist(x, i, x, j, 1.0 / length, columnIndices));
155      yield return scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k));
156      yield return 2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.