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
RevLine 
[8464]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;
[8484]23using System.Collections.Generic;
[8464]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8582]26using HeuristicLab.Data;
[8464]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.")]
[8582]33  public class CovarianceConst : CovarianceFunction {
34
35    public IValueParameter<DoubleValue> ScaleParameter {
36      get { return scaleParameter; }
37    }
38
[8464]39    [Storable]
[8582]40    private readonly HyperParameter<DoubleValue> scaleParameter;
[8464]41
[8582]42    [Storable]
43    private double scale;
44
[8464]45    [StorableConstructor]
46    protected CovarianceConst(bool deserializing)
47      : base(deserializing) {
48    }
49
50    protected CovarianceConst(CovarianceConst original, Cloner cloner)
51      : base(original, cloner) {
[8582]52      this.scaleParameter = cloner.Clone(original.scaleParameter);
53      this.scale = original.scale;
54
55      RegisterEvents();
[8464]56    }
57
58    public CovarianceConst()
59      : base() {
[8582]60      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function.");
61      Parameters.Add(scaleParameter);
62      RegisterEvents();
[8464]63    }
64
[8582]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
[8464]76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new CovarianceConst(this, cloner);
78    }
79
[8582]80    public override int GetNumberOfParameters(int numberOfVariables) {
81      return scaleParameter.Fixed ? 0 : 1;
[8464]82    }
83
[8582]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      }
[8464]90    }
91
[8582]92    public override double GetCovariance(double[,] x, int i, int j) {
93      return scale;
[8464]94    }
95
[8582]96    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
97      yield return 2.0 * scale;
[8464]98    }
99
[8582]100    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
101      return scale;
[8464]102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.