Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceMaternIso.cs @ 10202

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

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

File size: 6.1 KB
RevLine 
[8562]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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;
[8562]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
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    }
[8562]47
[8612]48
[8562]49    [StorableConstructor]
[8612]50    private CovarianceMaternIso(bool deserializing)
[8562]51      : base(deserializing) {
52    }
53
[8612]54    private CovarianceMaternIso(CovarianceMaternIso original, Cloner cloner)
[8562]55      : base(original, cloner) {
56    }
57
58    public CovarianceMaternIso()
59      : base() {
[8612]60      Name = ItemName;
61      Description = ItemDescription;
62
[8982]63      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric Matern covariance function."));
64      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric Matern covariance function."));
[8582]65      var validDValues = new ItemSet<IntValue>();
66      validDValues.Add((IntValue)new IntValue(1).AsReadOnly());
67      validDValues.Add((IntValue)new IntValue(3).AsReadOnly());
68      validDValues.Add((IntValue)new IntValue(5).AsReadOnly());
[8982]69      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]70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new CovarianceMaternIso(this, cloner);
74    }
75
[8612]76    public int GetNumberOfParameters(int numberOfVariables) {
[8582]77      return
[8982]78        (InverseLengthParameter.Value != null ? 0 : 1) +
79        (ScaleParameter.Value != null ? 0 : 1);
[8562]80    }
81
[8982]82    public void SetParameter(double[] p) {
83      double inverseLength, scale;
84      GetParameterValues(p, out scale, out inverseLength);
85      InverseLengthParameter.Value = new DoubleValue(inverseLength);
86      ScaleParameter.Value = new DoubleValue(scale);
87    }
88
89    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
90      // gather parameter values
91      int c = 0;
92      if (InverseLengthParameter.Value != null) {
93        inverseLength = InverseLengthParameter.Value.Value;
94      } else {
95        inverseLength = 1.0 / Math.Exp(p[c]);
96        c++;
[8582]97      }
[8982]98
99      if (ScaleParameter.Value != null) {
100        scale = ScaleParameter.Value.Value;
101      } else {
102        scale = Math.Exp(2 * p[c]);
103        c++;
[8582]104      }
[8982]105      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]106    }
[8562]107
[8982]108    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
109      double inverseLength, scale;
110      int d = DParameter.Value.Value;
111      GetParameterValues(p, out scale, out inverseLength);
112      // create functions
113      var cov = new ParameterizedCovarianceFunction();
114      cov.Covariance = (x, i, j) => {
115        double dist = i == j
116                       ? 0.0
117                       : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));
118        return scale * m(d, dist);
119      };
120      cov.CrossCovariance = (x, xt, i, j) => {
121        double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, Math.Sqrt(d) * inverseLength, columnIndices));
122        return scale * m(d, dist);
123      };
124      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices);
125      return cov;
126    }
[8582]127
[8982]128    private static double m(int d, double t) {
[8562]129      double f;
130      switch (d) {
131        case 1: { f = 1; break; }
132        case 3: { f = 1 + t; break; }
133        case 5: { f = 1 + t * (1 + t / 3.0); break; }
134        default: throw new InvalidOperationException();
135      }
136      return f * Math.Exp(-t);
137    }
138
[8982]139    private static double dm(int d, double t) {
[8562]140      double df;
141      switch (d) {
142        case 1: { df = 1; break; }
143        case 3: { df = t; break; }
144        case 5: { df = t * (1 + t) / 3.0; break; }
145        default: throw new InvalidOperationException();
146      }
147      return df * t * Math.Exp(-t);
148    }
149
150
[8982]151    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, int d, double scale, double inverseLength, IEnumerable<int> columnIndices) {
[8562]152      double dist = i == j
153                   ? 0.0
[8678]154                   : Math.Sqrt(Util.SqrDist(x, i, j, Math.Sqrt(d) * inverseLength, columnIndices));
[8562]155
[8982]156      yield return scale * dm(d, dist);
157      yield return 2 * scale * m(d, dist);
[8562]158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.