Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs @ 18079

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

#2875: Removed years in copyrights

File size: 6.2 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableType("358BE57A-13C8-40BB-B344-217984D4EB0F")]
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(StorableConstructorFlag _) : base(_) {
60    }
61
62    private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
63      : base(original, cloner) {
64    }
65
66    public CovarianceRationalQuadraticIso()
67      : base() {
68      Name = ItemName;
69      Description = ItemDescription;
70
71      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function."));
72      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function."));
73      Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function."));
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new CovarianceRationalQuadraticIso(this, cloner);
78    }
79
80    public int GetNumberOfParameters(int numberOfVariables) {
81      return (HasFixedScaleParameter ? 0 : 1) +
82        (HasFixedShapeParameter ? 0 : 1) +
83        (HasFixedInverseLengthParameter ? 0 : 1);
84    }
85
86    public void SetParameter(double[] p) {
87      double scale, shape, inverseLength;
88      GetParameterValues(p, out scale, out shape, out inverseLength);
89      ScaleParameter.Value = new DoubleValue(scale);
90      ShapeParameter.Value = new DoubleValue(shape);
91      InverseLengthParameter.Value = new DoubleValue(inverseLength);
92    }
93
94    private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
95      int c = 0;
96      // gather parameter values
97      if (HasFixedInverseLengthParameter) {
98        inverseLength = InverseLengthParameter.Value.Value;
99      } else {
100        inverseLength = 1.0 / Math.Exp(p[c]);
101        c++;
102      }
103      if (HasFixedScaleParameter) {
104        scale = ScaleParameter.Value.Value;
105      } else {
106        scale = Math.Exp(2 * p[c]);
107        c++;
108      }
109      if (HasFixedShapeParameter) {
110        shape = ShapeParameter.Value.Value;
111      } else {
112        shape = Math.Exp(p[c]);
113        c++;
114      }
115      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticIso", "p");
116    }
117
118    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
119      double scale, shape, inverseLength;
120      GetParameterValues(p, out scale, out shape, out inverseLength);
121      var fixedInverseLength = HasFixedInverseLengthParameter;
122      var fixedScale = HasFixedScaleParameter;
123      var fixedShape = HasFixedShapeParameter;
124      // create functions
125      var cov = new ParameterizedCovarianceFunction();
126      cov.Covariance = (x, i, j) => {
127        double d = i == j
128                    ? 0.0
129                    : Util.SqrDist(x, i, j, columnIndices, inverseLength);
130        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
131      };
132      cov.CrossCovariance = (x, xt, i, j) => {
133        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
134        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
135      };
136      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
137      return cov;
138    }
139
140    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength,
141      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
142      double d = i == j
143                   ? 0.0
144                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
145
146      double b = 1 + 0.5 * d / shape;
147      var g = new List<double>(3);
148      if (!fixedInverseLength) g.Add(scale * Math.Pow(b, -shape - 1) * d);
149      if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape));
150      if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)));
151      return g;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.