Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 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 = "CovarianceRationalQuadraticIso",
33    Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
34  public sealed class CovarianceRationalQuadraticIso : ParameterizedNamedItem, ICovarianceFunction {
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
38
39    public IValueParameter<DoubleValue> InverseLengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
41    }
42
43    public IValueParameter<DoubleValue> ShapeParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
45    }
46
47    private bool HasFixedScaleParameter {
48      get { return ScaleParameter.Value != null; }
49    }
50    private bool HasFixedInverseLengthParameter {
51      get { return InverseLengthParameter.Value != null; }
52    }
53    private bool HasFixedShapeParameter {
54      get { return ShapeParameter.Value != null; }
55    }
56
57
58    [StorableConstructor]
59    private CovarianceRationalQuadraticIso(bool deserializing)
60      : base(deserializing) {
61    }
62
63    private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
64      : base(original, cloner) {
65    }
66
67    public CovarianceRationalQuadraticIso()
68      : base() {
69      Name = ItemName;
70      Description = ItemDescription;
71
72      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function."));
73      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function."));
74      Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function."));
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new CovarianceRationalQuadraticIso(this, cloner);
79    }
80
81    public int GetNumberOfParameters(int numberOfVariables) {
82      return (HasFixedScaleParameter ? 0 : 1) +
83        (HasFixedShapeParameter ? 0 : 1) +
84        (HasFixedInverseLengthParameter ? 0 : 1);
85    }
86
87    public void SetParameter(double[] p) {
88      double scale, shape, inverseLength;
89      GetParameterValues(p, out scale, out shape, out inverseLength);
90      ScaleParameter.Value = new DoubleValue(scale);
91      ShapeParameter.Value = new DoubleValue(shape);
92      InverseLengthParameter.Value = new DoubleValue(inverseLength);
93    }
94
95    private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
96      int c = 0;
97      // gather parameter values
98      if (HasFixedInverseLengthParameter) {
99        inverseLength = InverseLengthParameter.Value.Value;
100      } else {
101        inverseLength = 1.0 / Math.Exp(p[c]);
102        c++;
103      }
104      if (HasFixedScaleParameter) {
105        scale = ScaleParameter.Value.Value;
106      } else {
107        scale = Math.Exp(2 * p[c]);
108        c++;
109      }
110      if (HasFixedShapeParameter) {
111        shape = ShapeParameter.Value.Value;
112      } else {
113        shape = Math.Exp(p[c]);
114        c++;
115      }
116      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticIso", "p");
117    }
118
119    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
120      double scale, shape, inverseLength;
121      GetParameterValues(p, out scale, out shape, out inverseLength);
122      var fixedInverseLength = HasFixedInverseLengthParameter;
123      var fixedScale = HasFixedScaleParameter;
124      var fixedShape = HasFixedShapeParameter;
125      // create functions
126      var cov = new ParameterizedCovarianceFunction();
127      cov.Covariance = (x, i, j) => {
128        double d = i == j
129                    ? 0.0
130                    : Util.SqrDist(x, i, j, columnIndices, inverseLength);
131        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
132      };
133      cov.CrossCovariance = (x, xt, i, j) => {
134        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
135        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
136      };
137      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
138      return cov;
139    }
140
141    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength,
142      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
143      double d = i == j
144                   ? 0.0
145                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
146
147      double b = 1 + 0.5 * d / shape;
148      var g = new List<double>(3);
149      if (!fixedInverseLength) g.Add(scale * Math.Pow(b, -shape - 1) * d);
150      if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape));
151      if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)));
152      return g;
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.