Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2526: Updated year of copyrights in license headers

File size: 6.2 KB
RevLine 
[8473]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8473]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;
[8484]23using System.Collections.Generic;
[8473]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8612]26using HeuristicLab.Data;
[8982]27using HeuristicLab.Parameters;
[8473]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
[8615]32  [Item(Name = "CovarianceRationalQuadraticIso",
[8473]33    Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
[8615]34  public sealed class CovarianceRationalQuadraticIso : ParameterizedNamedItem, ICovarianceFunction {
[8982]35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
37    }
[8612]38
[8982]39    public IValueParameter<DoubleValue> InverseLengthParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
41    }
[8473]42
[8982]43    public IValueParameter<DoubleValue> ShapeParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
45    }
[10489]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
[8473]58    [StorableConstructor]
[8615]59    private CovarianceRationalQuadraticIso(bool deserializing)
[8473]60      : base(deserializing) {
61    }
62
[8615]63    private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
[8473]64      : base(original, cloner) {
65    }
66
[8615]67    public CovarianceRationalQuadraticIso()
[8473]68      : base() {
[8612]69      Name = ItemName;
70      Description = ItemDescription;
71
[8982]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."));
[8473]75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
[8615]78      return new CovarianceRationalQuadraticIso(this, cloner);
[8473]79    }
80
[8982]81    public int GetNumberOfParameters(int numberOfVariables) {
[10489]82      return (HasFixedScaleParameter ? 0 : 1) +
83        (HasFixedShapeParameter ? 0 : 1) +
84        (HasFixedInverseLengthParameter ? 0 : 1);
[8612]85    }
86
[8982]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);
[8612]93    }
94
[8982]95    private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
96      int c = 0;
97      // gather parameter values
[10489]98      if (HasFixedInverseLengthParameter) {
[9108]99        inverseLength = InverseLengthParameter.Value.Value;
100      } else {
101        inverseLength = 1.0 / Math.Exp(p[c]);
102        c++;
103      }
[10489]104      if (HasFixedScaleParameter) {
[8982]105        scale = ScaleParameter.Value.Value;
106      } else {
107        scale = Math.Exp(2 * p[c]);
108        c++;
[8612]109      }
[10489]110      if (HasFixedShapeParameter) {
[8982]111        shape = ShapeParameter.Value.Value;
112      } else {
113        shape = Math.Exp(p[c]);
114        c++;
[8612]115      }
[8982]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");
[8473]117    }
118
[13721]119    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]120      double scale, shape, inverseLength;
121      GetParameterValues(p, out scale, out shape, out inverseLength);
[10489]122      var fixedInverseLength = HasFixedInverseLengthParameter;
123      var fixedScale = HasFixedScaleParameter;
124      var fixedShape = HasFixedShapeParameter;
[8982]125      // create functions
126      var cov = new ParameterizedCovarianceFunction();
127      cov.Covariance = (x, i, j) => {
128        double d = i == j
129                    ? 0.0
[13721]130                    : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[9111]131        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
[8982]132      };
133      cov.CrossCovariance = (x, xt, i, j) => {
[13721]134        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
[8982]135        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
136      };
[10489]137      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
[8982]138      return cov;
[8473]139    }
140
[13784]141    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength,
[10489]142      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
[8484]143      double d = i == j
144                   ? 0.0
[13721]145                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[8473]146
[8612]147      double b = 1 + 0.5 * d / shape;
[13784]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;
[8473]153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.