Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceConst.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: 3.3 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  [Item(Name = "CovarianceConst",
32    Description = "Constant covariance function for Gaussian processes.")]
33  public class CovarianceConst : CovarianceFunction {
34
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return scaleParameter; }
37    }
38
39    [Storable]
40    private readonly HyperParameter<DoubleValue> scaleParameter;
41
42    [Storable]
43    private double scale;
44
45    [StorableConstructor]
46    protected CovarianceConst(bool deserializing)
47      : base(deserializing) {
48    }
49
50    protected CovarianceConst(CovarianceConst original, Cloner cloner)
51      : base(original, cloner) {
52      this.scaleParameter = cloner.Clone(original.scaleParameter);
53      this.scale = original.scale;
54
55      RegisterEvents();
56    }
57
58    public CovarianceConst()
59      : base() {
60      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function.");
61      Parameters.Add(scaleParameter);
62      RegisterEvents();
63    }
64
65    [StorableHook(HookType.AfterDeserialization)]
66    private void AfterDeserialization() {
67      RegisterEvents();
68    }
69
70    // caching
71    private void RegisterEvents() {
72      AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
73    }
74
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new CovarianceConst(this, cloner);
78    }
79
80    public override int GetNumberOfParameters(int numberOfVariables) {
81      return scaleParameter.Fixed ? 0 : 1;
82    }
83
84    public override void SetParameter(double[] hyp) {
85      if (!scaleParameter.Fixed && hyp.Length == 1) {
86        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
87      } else {
88        throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "hyp");
89      }
90    }
91
92    public override double GetCovariance(double[,] x, int i, int j) {
93      return scale;
94    }
95
96    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
97      yield return 2.0 * scale;
98    }
99
100    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
101      return scale;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.