Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceScale.cs @ 8742

Last change on this file since 8742 was 8742, checked in by mkommend, 12 years ago

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File size: 4.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 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 = "CovarianceScale",
34    Description = "Scale covariance function for Gaussian processes.")]
35  public sealed class CovarianceScale : ParameterizedNamedItem, ICovarianceFunction {
36    [Storable]
37    private double sf2;
38    [Storable]
39    private readonly HyperParameter<DoubleValue> scaleParameter;
40    public IValueParameter<DoubleValue> ScaleParameter {
41      get { return scaleParameter; }
42    }
43
44    [Storable]
45    private ICovarianceFunction cov;
46    [Storable]
47    private readonly ValueParameter<ICovarianceFunction> covParameter;
48    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
49      get { return covParameter; }
50    }
51
52    [StorableConstructor]
53    private CovarianceScale(bool deserializing)
54      : base(deserializing) {
55    }
56
57    private CovarianceScale(CovarianceScale original, Cloner cloner)
58      : base(original, cloner) {
59      this.scaleParameter = cloner.Clone(original.scaleParameter);
60      this.sf2 = original.sf2;
61
62      this.covParameter = cloner.Clone(original.covParameter);
63      this.cov = cloner.Clone(original.cov);
64      RegisterEvents();
65    }
66
67    public CovarianceScale()
68      : base() {
69      Name = ItemName;
70      Description = ItemDescription;
71
72      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter.");
73      this.covParameter = new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso());
74      cov = covParameter.Value;
75
76      Parameters.Add(this.scaleParameter);
77      Parameters.Add(covParameter);
78
79      RegisterEvents();
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new CovarianceScale(this, cloner);
84    }
85
86    [StorableHook(HookType.AfterDeserialization)]
87    private void AfterDeserialization() {
88      RegisterEvents();
89    }
90
91    private void RegisterEvents() {
92      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
93      covParameter.ValueChanged += (sender, args) => { cov = covParameter.Value; };
94    }
95
96    public int GetNumberOfParameters(int numberOfVariables) {
97      return (scaleParameter.Fixed ? 0 : 1) + cov.GetNumberOfParameters(numberOfVariables);
98    }
99
100    public void SetParameter(double[] hyp) {
101      int i = 0;
102      if (!scaleParameter.Fixed) {
103        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
104        i++;
105      }
106      cov.SetParameter(hyp.Skip(i).ToArray());
107    }
108
109    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
110      return sf2 * cov.GetCovariance(x, i, j, columnIndices);
111    }
112
113    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
114      yield return 2 * sf2 * cov.GetCovariance(x, i, j, columnIndices);
115      foreach (var g in cov.GetGradient(x, i, j, columnIndices))
116        yield return sf2 * g;
117    }
118
119    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
120      return sf2 * cov.GetCrossCovariance(x, xt, i, j, columnIndices);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.