Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMaternIso.cs @ 17246

Last change on this file since 17246 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 6.5 KB
RevLine 
[8562]1#region License Information
2/* HeuristicLab
[17246]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8562]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;
[8582]24using System.Linq;
[8562]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[8582]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[16662]29using HEAL.Attic;
[8562]30
31namespace HeuristicLab.Algorithms.DataAnalysis {
[16662]32  [StorableType("D251400A-4DCA-4500-9738-CE3B7BF96B0D")]
[8562]33  [Item(Name = "CovarianceMaternIso",
34    Description = "Matern covariance function for Gaussian processes.")]
[8612]35  public sealed class CovarianceMaternIso : ParameterizedNamedItem, ICovarianceFunction {
[8582]36    public IValueParameter<DoubleValue> InverseLengthParameter {
[8982]37      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
[8582]38    }
39
[8612]40    public IValueParameter<DoubleValue> ScaleParameter {
[8982]41      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
[8612]42    }
[8582]43
[8612]44    public IConstrainedValueParameter<IntValue> DParameter {
[8982]45      get { return (IConstrainedValueParameter<IntValue>)Parameters["D"]; }
[8612]46    }
[10489]47    private bool HasFixedScaleParameter {
48      get { return ScaleParameter.Value != null; }
49    }
50    private bool HasFixedInverseLengthParameter {
51      get { return InverseLengthParameter.Value != null; }
52    }
[8562]53
54    [StorableConstructor]
[16662]55    private CovarianceMaternIso(StorableConstructorFlag _) : base(_) {
[8562]56    }
57
[8612]58    private CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
[8562]59      : base(original, cloner) {
60    }
61
62    public CovarianceMaternIso()
63      : base() {
[8612]64      Name = ItemName;
65      Description = ItemDescription;
66
[8982]67      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric Matern covariance function."));
68      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric Matern covariance function."));
[8582]69      var validDValues = new ItemSet<IntValue>();
70      validDValues.Add((IntValue)new IntValue(1).AsReadOnly());
71      validDValues.Add((IntValue)new IntValue(3).AsReadOnly());
72      validDValues.Add((IntValue)new IntValue(5).AsReadOnly());
[8982]73      Parameters.Add(new ConstrainedValueParameter<IntValue>("D", "The d parameter (allowed values: 1, 3, or 5) of the isometric Matern covariance function.", validDValues, validDValues.First()));
[8562]74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new CovarianceMaternIso(this, cloner);
78    }
79
[8612]80    public int GetNumberOfParameters(int numberOfVariables) {
[8582]81      return
[10489]82        (HasFixedInverseLengthParameter ? 0 : 1) +
83        (HasFixedScaleParameter ? 0 : 1);
[8562]84    }
85
[8982]86    public void SetParameter(double[] p) {
87      double inverseLength, scale;
88      GetParameterValues(p, out scale, out inverseLength);
89      InverseLengthParameter.Value = new DoubleValue(inverseLength);
90      ScaleParameter.Value = new DoubleValue(scale);
91    }
92
93    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
94      // gather parameter values
95      int c = 0;
[10489]96      if (HasFixedInverseLengthParameter) {
[8982]97        inverseLength = InverseLengthParameter.Value.Value;
98      } else {
99        inverseLength = 1.0 / Math.Exp(p[c]);
100        c++;
[8582]101      }
[8982]102
[10489]103      if (HasFixedScaleParameter) {
[8982]104        scale = ScaleParameter.Value.Value;
105      } else {
106        scale = Math.Exp(2 * p[c]);
107        c++;
[8582]108      }
[8982]109      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceMaternIso", "p");
[8582]110    }
[8562]111
[13721]112    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]113      double inverseLength, scale;
114      int d = DParameter.Value.Value;
115      GetParameterValues(p, out scale, out inverseLength);
[10489]116      var fixedInverseLength = HasFixedInverseLengthParameter;
117      var fixedScale = HasFixedScaleParameter;
[8982]118      // create functions
119      var cov = new ParameterizedCovarianceFunction();
120      cov.Covariance = (x, i, j) => {
121        double dist = i == j
122                       ? 0.0
[13721]123                       : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength));
[8982]124        return scale * m(d, dist);
125      };
126      cov.CrossCovariance = (x, xt, i, j) => {
[13721]127        double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, Math.Sqrt(d) * inverseLength));
[8982]128        return scale * m(d, dist);
129      };
[10489]130      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices, fixedInverseLength, fixedScale);
[8982]131      return cov;
132    }
[8582]133
[8982]134    private static double m(int d, double t) {
[8562]135      double f;
136      switch (d) {
137        case 1: { f = 1; break; }
138        case 3: { f = 1 + t; break; }
139        case 5: { f = 1 + t * (1 + t / 3.0); break; }
140        default: throw new InvalidOperationException();
141      }
142      return f * Math.Exp(-t);
143    }
144
[8982]145    private static double dm(int d, double t) {
[8562]146      double df;
147      switch (d) {
148        case 1: { df = 1; break; }
149        case 3: { df = t; break; }
150        case 5: { df = t * (1 + t) / 3.0; break; }
151        default: throw new InvalidOperationException();
152      }
153      return df * t * Math.Exp(-t);
154    }
155
[13784]156    private static IList<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, int[] columnIndices,
[10489]157      bool fixedInverseLength, bool fixedScale) {
[8562]158      double dist = i == j
159                   ? 0.0
[13721]160                   : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength));
[8562]161
[13784]162      var g = new List<double>(2);
163      if (!fixedInverseLength) g.Add(scale * dm(d, dist));
164      if (!fixedScale) g.Add(2 * scale * m(d, dist));
165      return g;
[8562]166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.