Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovariancePiecewisePolynomial.cs @ 16662

Last change on this file since 16662 was 16662, checked in by gkronber, 6 years ago

#2925: merged all changes from trunk to branch (up to r16659)

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableType("62C25AD5-F41F-4CC2-B589-A92CCEE7AC88")]
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    private bool HasFixedLengthParameter {
48      get { return LengthParameter.Value != null; }
49    }
50    private bool HasFixedScaleParameter {
51      get { return ScaleParameter.Value != null; }
52    }
53
54    [StorableConstructor]
55    private CovariancePiecewisePolynomial(StorableConstructorFlag _) : base(_) {
56    }
57
58    private CovariancePiecewisePolynomial(CovariancePiecewisePolynomial original, Cloner cloner)
59      : base(original, cloner) {
60    }
61
62    public CovariancePiecewisePolynomial()
63      : base() {
64      Name = ItemName;
65      Description = ItemDescription;
66
67      Parameters.Add(new OptionalValueParameter<DoubleValue>("Length", "The length parameter of the isometric piecewise polynomial covariance function."));
68      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the piecewise polynomial covariance function."));
69
70      var validValues = new ItemSet<IntValue>(new IntValue[] {
71        (IntValue)(new IntValue().AsReadOnly()),
72        (IntValue)(new IntValue(1).AsReadOnly()),
73        (IntValue)(new IntValue(2).AsReadOnly()),
74        (IntValue)(new IntValue(3).AsReadOnly()) });
75      Parameters.Add(new ConstrainedValueParameter<IntValue>("V", "The v parameter of the piecewise polynomial function (allowed values 0, 1, 2, 3).", validValues, validValues.First()));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new CovariancePiecewisePolynomial(this, cloner);
80    }
81
82    public int GetNumberOfParameters(int numberOfVariables) {
83      return
84        (HasFixedLengthParameter ? 0 : 1) +
85        (HasFixedScaleParameter ? 0 : 1);
86    }
87
88    public void SetParameter(double[] p) {
89      double @const, scale;
90      GetParameterValues(p, out @const, out scale);
91      LengthParameter.Value = new DoubleValue(@const);
92      ScaleParameter.Value = new DoubleValue(scale);
93    }
94
95    private void GetParameterValues(double[] p, out double length, out double scale) {
96      // gather parameter values
97      int n = 0;
98      if (HasFixedLengthParameter) {
99        length = LengthParameter.Value.Value;
100      } else {
101        length = Math.Exp(p[n]);
102        n++;
103      }
104
105      if (HasFixedScaleParameter) {
106        scale = ScaleParameter.Value.Value;
107      } else {
108        scale = Math.Exp(2 * p[n]);
109        n++;
110      }
111      if (p.Length != n) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePiecewisePolynomial", "p");
112    }
113
114    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
115      double length, scale;
116      int v = VParameter.Value.Value;
117      GetParameterValues(p, out length, out scale);
118      var fixedLength = HasFixedLengthParameter;
119      var fixedScale = HasFixedScaleParameter;
120      int exp = (int)Math.Floor(columnIndices.Count() / 2.0) + v + 1;
121
122      Func<double, double> f;
123      Func<double, double> df;
124      switch (v) {
125        case 0:
126          f = (r) => 1.0;
127          df = (r) => 0.0;
128          break;
129        case 1:
130          f = (r) => 1 + (exp + 1) * r;
131          df = (r) => exp + 1;
132          break;
133        case 2:
134          f = (r) => 1 + (exp + 2) * r + (exp * exp + 4.0 * exp + 3) / 3.0 * r * r;
135          df = (r) => (exp + 2) + 2 * (exp * exp + 4.0 * exp + 3) / 3.0 * r;
136          break;
137        case 3:
138          f = (r) => 1 + (exp + 3) * r + (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r * r +
139                     (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 15.0 * r * r * r;
140          df = (r) => (exp + 3) + 2 * (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r +
141                      (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 5.0 * r * r;
142          break;
143        default: throw new ArgumentException();
144      }
145
146      // create functions
147      var cov = new ParameterizedCovarianceFunction();
148      cov.Covariance = (x, i, j) => {
149        double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length));
150        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
151      };
152      cov.CrossCovariance = (x, xt, i, j) => {
153        double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1.0 / length));
154        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
155      };
156      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices, fixedLength, fixedScale);
157      return cov;
158    }
159
160    private static IList<double> GetGradient(double[,] x, int i, int j, double length, double scale, int v, double exp, Func<double, double> f, Func<double, double> df, int[] columnIndices,
161      bool fixedLength, bool fixedScale) {
162      double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length));
163      var g = new List<double>(2);
164      if (!fixedLength) g.Add(scale * Math.Pow(Math.Max(1.0 - k, 0), exp + v - 1) * k * ((exp + v) * f(k) - Math.Max(1 - k, 0) * df(k)));
165      if (!fixedScale) g.Add(2.0 * scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k));
166      return g;
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.