Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs @ 9074

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

#1902: removed class HyperParameter and changed implementations of covariance and mean functions to remove the parameter value caching and event handlers for parameter caching. Instead it is now possible to create the actual covariance and mean functions as Func from templates and specified parameter values. The instances of mean and covariance functions configured in the GUI are actually templates where the structure and fixed parameters can be specified.

File size: 4.1 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    public IValueParameter<DoubleValue> ScaleParameter {
37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
38    }
39
40    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
41      get { return (IValueParameter<ICovarianceFunction>)Parameters["CovarianceFunction"]; }
42    }
43
44    [StorableConstructor]
45    private CovarianceScale(bool deserializing)
46      : base(deserializing) {
47    }
48
49    private CovarianceScale(CovarianceScale original, Cloner cloner)
50      : base(original, cloner) {
51    }
52
53    public CovarianceScale()
54      : base() {
55      Name = ItemName;
56      Description = ItemDescription;
57
58      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
59      Parameters.Add(new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso()));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new CovarianceScale(this, cloner);
64    }
65
66    public int GetNumberOfParameters(int numberOfVariables) {
67      return (ScaleParameter.Value != null ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
68    }
69
70    public void SetParameter(double[] p) {
71      double scale;
72      GetParameterValues(p, out scale);
73      ScaleParameter.Value = new DoubleValue(scale);
74      CovarianceFunctionParameter.Value.SetParameter(p.Skip(1).ToArray());
75    }
76
77    private void GetParameterValues(double[] p, out double scale) {
78      // gather parameter values
79      if (ScaleParameter.Value != null) {
80        scale = ScaleParameter.Value.Value;
81      } else {
82        scale = Math.Exp(2 * p[0]);
83      }
84    }
85
86    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
87      double scale;
88      GetParameterValues(p, out scale);
89      var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
90      // create functions
91      var cov = new ParameterizedCovarianceFunction();
92      cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j);
93      cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j);
94      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov);
95      return cov;
96    }
97
98    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov) {
99      yield return 2 * scale * cov.Covariance(x, i, j);
100      foreach (var g in cov.CovarianceGradient(x, i, j))
101        yield return scale * g;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.