Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceLinearArd.cs @ 9503

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

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

File size: 3.8 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 System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item(Name = "CovarianceLinearArd",
34    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
35  public sealed class CovarianceLinearArd : ParameterizedNamedItem, ICovarianceFunction {
36    public IValueParameter<DoubleArray> InverseLengthParameter {
37      get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
38    }
39
40    [StorableConstructor]
41    private CovarianceLinearArd(bool deserializing) : base(deserializing) { }
42    private CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
43      : base(original, cloner) {
44    }
45    public CovarianceLinearArd()
46      : base() {
47      Name = ItemName;
48      Description = ItemDescription;
49
50      Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength",
51                                                             "The inverse length parameter for ARD."));
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new CovarianceLinearArd(this, cloner);
56    }
57
58    public int GetNumberOfParameters(int numberOfVariables) {
59      if (InverseLengthParameter.Value == null)
60        return numberOfVariables;
61      else
62        return 0;
63    }
64
65    public void SetParameter(double[] p) {
66      double[] inverseLength;
67      GetParameterValues(p, out inverseLength);
68      InverseLengthParameter.Value = new DoubleArray(inverseLength);
69    }
70
71    private void GetParameterValues(double[] p, out double[] inverseLength) {
72      // gather parameter values
73      if (InverseLengthParameter.Value != null) {
74        inverseLength = InverseLengthParameter.Value.ToArray();
75      } else {
76        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).ToArray();
77      }
78    }
79
80    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
81      double[] inverseLength;
82      GetParameterValues(p, out inverseLength);
83      // create functions
84      var cov = new ParameterizedCovarianceFunction();
85      cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices);
86      cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
87      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices);
88      return cov;
89    }
90
91    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, IEnumerable<int> columnIndices) {
92      int k = 0;
93      foreach (int columnIndex in columnIndices) {
94        yield return -2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k];
95        k++;
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.