Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs @ 12803

Last change on this file since 12803 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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