Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.cs @ 8562

Last change on this file since 8562 was 8562, checked in by gkronber, 12 years ago

#1902 implemented LinearARD and MaternIso covariance functions.

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  [Item(Name = "CovarianceLinearArd",
32    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
33  public class CovarianceLinearArd : Item, ICovarianceFunction {
34    [Storable]
35    private double[] inverseLength;
36    public double[] InverseLength {
37      get {
38        if (inverseLength == null) return null;
39        double[] res = new double[inverseLength.Length];
40        Array.Copy(inverseLength, res, res.Length);
41        return res;
42      }
43    }
44
45    public int GetNumberOfParameters(int numberOfVariables) {
46      return numberOfVariables;
47    }
48
49    [StorableConstructor]
50    protected CovarianceLinearArd(bool deserializing) : base(deserializing) { }
51    protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
52      : base(original, cloner) {
53      this.inverseLength = original.InverseLength;  // array is copied in the getter
54    }
55    public CovarianceLinearArd()
56      : base() {
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new CovarianceLinearArd(this, cloner);
61    }
62
63    public void SetParameter(double[] hyp) {
64      inverseLength = hyp.Select(e => 1.0 / Math.Exp(e)).ToArray();
65    }
66
67    public double GetCovariance(double[,] x, int i, int j) {
68      return Util.ScalarProd(x, i, j, inverseLength);
69    }
70
71    public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
72      for (int k = 0; k < inverseLength.Length; k++) {
73        yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k];
74      }
75    }
76
77    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
78      return Util.ScalarProd(x, i, xt, j, inverseLength);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.