Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceRationalQuadraticIso.cs @ 10225

Last change on this file since 10225 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.6 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 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    [StorableConstructor]
47    private CovarianceRationalQuadraticIso(bool deserializing)
48      : base(deserializing) {
49    }
50
51    private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public CovarianceRationalQuadraticIso()
56      : base() {
57      Name = ItemName;
58      Description = ItemDescription;
59
60      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function."));
61      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function."));
62      Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function."));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new CovarianceRationalQuadraticIso(this, cloner);
67    }
68
69    public int GetNumberOfParameters(int numberOfVariables) {
70      return (ScaleParameter.Value != null ? 0 : 1) +
71        (ShapeParameter.Value != null ? 0 : 1) +
72        (InverseLengthParameter.Value != null ? 0 : 1);
73    }
74
75    public void SetParameter(double[] p) {
76      double scale, shape, inverseLength;
77      GetParameterValues(p, out scale, out shape, out inverseLength);
78      ScaleParameter.Value = new DoubleValue(scale);
79      ShapeParameter.Value = new DoubleValue(shape);
80      InverseLengthParameter.Value = new DoubleValue(inverseLength);
81    }
82
83    private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
84      int c = 0;
85      // gather parameter values
86      if (InverseLengthParameter.Value != null) {
87        inverseLength = InverseLengthParameter.Value.Value;
88      } else {
89        inverseLength = 1.0 / Math.Exp(p[c]);
90        c++;
91      }
92      if (ScaleParameter.Value != null) {
93        scale = ScaleParameter.Value.Value;
94      } else {
95        scale = Math.Exp(2 * p[c]);
96        c++;
97      }
98      if (ShapeParameter.Value != null) {
99        shape = ShapeParameter.Value.Value;
100      } else {
101        shape = Math.Exp(p[c]);
102        c++;
103      }
104      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticIso", "p");
105    }
106
107    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
108      double scale, shape, inverseLength;
109      GetParameterValues(p, out scale, out shape, out inverseLength);
110      // create functions
111      var cov = new ParameterizedCovarianceFunction();
112      cov.Covariance = (x, i, j) => {
113        double d = i == j
114                    ? 0.0
115                    : Util.SqrDist(x, i, j, inverseLength, columnIndices);
116        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
117      };
118      cov.CrossCovariance = (x, xt, i, j) => {
119        double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
120        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
121      };
122      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength);
123      return cov;
124    }
125
126    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double inverseLength) {
127      double d = i == j
128                   ? 0.0
129                   : Util.SqrDist(x, i, j, inverseLength, columnIndices);
130
131      double b = 1 + 0.5 * d / shape;
132      yield return scale * Math.Pow(b, -shape - 1) * d;
133      yield return 2 * scale * Math.Pow(b, -shape);
134      yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.