Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1902 implemented a few covariance functions as parameterized named items. Implemented rudimentary view for Gaussian process models.

File size: 4.0 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.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  [Item(Name = "CovarianceLinearArd",
33    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
34  public class CovarianceLinearArd : CovarianceFunction {
35    public IValueParameter<DoubleArray> InverseLengthParameter {
36      get { return inverseLengthParameter; }
37    }
38
39    [Storable]
40    private HyperParameter<DoubleArray> inverseLengthParameter;
41
42    [Storable]
43    private double[] inverseLength;
44
45    [StorableConstructor]
46    protected CovarianceLinearArd(bool deserializing) : base(deserializing) { }
47    protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
48      : base(original, cloner) {
49      inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
50      if (original.inverseLength != null) {
51        this.inverseLength = new double[original.inverseLength.Length];
52        Array.Copy(original.inverseLength, inverseLength, inverseLength.Length);
53      }
54
55      RegisterEvents();
56    }
57    public CovarianceLinearArd()
58      : base() {
59      inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength",
60                                                               "The inverse length parameter for ARD.");
61      Parameters.Add(inverseLengthParameter);
62      RegisterEvents();
63    }
64
65    [StorableHook(HookType.AfterDeserialization)]
66    private void AfterDeserialization() {
67      RegisterEvents();
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new CovarianceLinearArd(this, cloner);
72    }
73
74    // caching
75    private void RegisterEvents() {
76      AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
77    }
78
79
80    public override int GetNumberOfParameters(int numberOfVariables) {
81      if (!inverseLengthParameter.Fixed)
82        return numberOfVariables;
83      else
84        return 0;
85    }
86
87    public override void SetParameter(double[] hyp) {
88      if (!inverseLengthParameter.Fixed && hyp.Length > 0) {
89        inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray()));
90      } else throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceLinearArd", "hyp");
91    }
92
93    public override double GetCovariance(double[,] x, int i, int j) {
94      return Util.ScalarProd(x, i, j, inverseLength);
95    }
96
97    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
98      for (int k = 0; k < inverseLength.Length; k++) {
99        yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k];
100      }
101    }
102
103    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
104      return Util.ScalarProd(x, i, xt, j, inverseLength);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.