Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2875: Removed years in copyrights

File size: 6.2 KB
RevLine 
[8473]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[16565]28using HEAL.Attic;
[8473]29
30namespace HeuristicLab.Algorithms.DataAnalysis {
[16565]31  [StorableType("358BE57A-13C8-40BB-B344-217984D4EB0F")]
[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]
[16565]59    private CovarianceRationalQuadraticIso(StorableConstructorFlag _) : base(_) {
[8473]60    }
61
[8615]62    private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
[8473]63      : base(original, cloner) {
64    }
65
[8615]66    public CovarianceRationalQuadraticIso()
[8473]67      : base() {
[8612]68      Name = ItemName;
69      Description = ItemDescription;
70
[8982]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."));
[8473]74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
[8615]77      return new CovarianceRationalQuadraticIso(this, cloner);
[8473]78    }
79
[8982]80    public int GetNumberOfParameters(int numberOfVariables) {
[10489]81      return (HasFixedScaleParameter ? 0 : 1) +
82        (HasFixedShapeParameter ? 0 : 1) +
83        (HasFixedInverseLengthParameter ? 0 : 1);
[8612]84    }
85
[8982]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);
[8612]92    }
93
[8982]94    private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
95      int c = 0;
96      // gather parameter values
[10489]97      if (HasFixedInverseLengthParameter) {
[9108]98        inverseLength = InverseLengthParameter.Value.Value;
99      } else {
100        inverseLength = 1.0 / Math.Exp(p[c]);
101        c++;
102      }
[10489]103      if (HasFixedScaleParameter) {
[8982]104        scale = ScaleParameter.Value.Value;
105      } else {
106        scale = Math.Exp(2 * p[c]);
107        c++;
[8612]108      }
[10489]109      if (HasFixedShapeParameter) {
[8982]110        shape = ShapeParameter.Value.Value;
111      } else {
112        shape = Math.Exp(p[c]);
113        c++;
[8612]114      }
[8982]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");
[8473]116    }
117
[13721]118    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]119      double scale, shape, inverseLength;
120      GetParameterValues(p, out scale, out shape, out inverseLength);
[10489]121      var fixedInverseLength = HasFixedInverseLengthParameter;
122      var fixedScale = HasFixedScaleParameter;
123      var fixedShape = HasFixedShapeParameter;
[8982]124      // create functions
125      var cov = new ParameterizedCovarianceFunction();
126      cov.Covariance = (x, i, j) => {
127        double d = i == j
128                    ? 0.0
[13721]129                    : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[9111]130        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
[8982]131      };
132      cov.CrossCovariance = (x, xt, i, j) => {
[13721]133        double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
[8982]134        return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
135      };
[10489]136      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
[8982]137      return cov;
[8473]138    }
139
[13784]140    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double inverseLength,
[10489]141      bool fixedInverseLength, bool fixedScale, bool fixedShape) {
[8484]142      double d = i == j
143                   ? 0.0
[13721]144                   : Util.SqrDist(x, i, j, columnIndices, inverseLength);
[8473]145
[8612]146      double b = 1 + 0.5 * d / shape;
[13784]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;
[8473]152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.