Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticArd.cs @ 17607

Last change on this file since 17607 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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("25439318-762C-4ED6-BAE7-E53860598A01")]
33  [Item(Name = "CovarianceRationalQuadraticArd",
34    Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")]
35  public sealed class CovarianceRationalQuadraticArd : ParameterizedNamedItem, ICovarianceFunction {
36    public IValueParameter<DoubleValue> ScaleParameter {
37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
38    }
39
40    public IValueParameter<DoubleArray> InverseLengthParameter {
41      get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
42    }
43
44    public IValueParameter<DoubleValue> ShapeParameter {
45      get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
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    [StorableConstructor]
58    private CovarianceRationalQuadraticArd(StorableConstructorFlag _) : base(_) {
59    }
60
61    private CovarianceRationalQuadraticArd(CovarianceRationalQuadraticArd original, Cloner cloner)
62      : base(original, cloner) {
63    }
64
65    public CovarianceRationalQuadraticArd()
66      : base() {
67      Name = ItemName;
68      Description = ItemDescription;
69
70      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the rational quadratic covariance function with ARD."));
71      Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."));
72      Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the rational quadratic covariance function with ARD."));
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new CovarianceRationalQuadraticArd(this, cloner);
77    }
78
79    public int GetNumberOfParameters(int numberOfVariables) {
80      return
81        (HasFixedScaleParameter ? 0 : 1) +
82        (HasFixedShapeParameter ? 0 : 1) +
83        (HasFixedInverseLengthParameter ? 0 : numberOfVariables);
84    }
85
86    public void SetParameter(double[] p) {
87      double scale, shape;
88      double[] 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 DoubleArray(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.ToArray();
100      } else {
101        int length = p.Length;
102        if (!HasFixedScaleParameter) length--;
103        if (!HasFixedShapeParameter) length--;
104        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
105        c += inverseLength.Length;
106      }
107      if (HasFixedScaleParameter) {
108        scale = ScaleParameter.Value.Value;
109      } else {
110        scale = Math.Exp(2 * p[c]);
111        c++;
112      }
113      if (HasFixedShapeParameter) {
114        shape = ShapeParameter.Value.Value;
115      } else {
116        shape = Math.Exp(p[c]);
117        c++;
118      }
119      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticArd", "p");
120    }
121
122    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
123      double scale, shape;
124      double[] inverseLength;
125      GetParameterValues(p, out scale, out shape, out inverseLength);
126      var fixedInverseLength = HasFixedInverseLengthParameter;
127      var fixedScale = HasFixedScaleParameter;
128      var fixedShape = HasFixedShapeParameter;
129      // create functions
130      var cov = new ParameterizedCovarianceFunction();
131      cov.Covariance = (x, i, j) => {
132        double d = i == j
133                    ? 0.0
134                    : Util.SqrDist(x, i, j, inverseLength, columnIndices);
135        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
136      };
137      cov.CrossCovariance = (x, xt, i, j) => {
138        double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
139        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
140      };
141      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
142      return cov;
143    }
144
145    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double[] inverseLength,
146      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
147      double d = i == j
148                   ? 0.0
149                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
150      double b = 1 + 0.5 * d / shape;
151      int k = 0;
152      var g = new List<double>(columnIndices.Length + 2);
153      if (!fixedInverseLength) {
154        foreach (var columnIndex in columnIndices) {
155          g.Add(
156            scale * Math.Pow(b, -shape - 1) *
157            Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]));
158          k++;
159        }
160      }
161      if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape));
162      if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)));
163      return g;
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.