Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs @ 16692

Last change on this file since 16692 was 16692, checked in by abeham, 5 years ago

#2521: merged trunk changes up to r15681 into branch (removal of trunk/sources)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    private bool HasFixedScaleParameter {
40      get { return ScaleParameter.Value != null; }
41    }
42
43    public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
44      get { return (IValueParameter<ICovarianceFunction>)Parameters["CovarianceFunction"]; }
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
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()));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new CovarianceScale(this, cloner);
67    }
68
69    public int GetNumberOfParameters(int numberOfVariables) {
70      return (HasFixedScaleParameter ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
71    }
72
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());
78    }
79
80    private void GetParameterValues(double[] p, out double scale) {
81      // gather parameter values
82      if (HasFixedScaleParameter) {
83        scale = ScaleParameter.Value.Value;
84      } else {
85        scale = Math.Exp(2 * p[0]);
86      }
87    }
88
89    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
90      double scale;
91      GetParameterValues(p, out scale);
92      var fixedScale = HasFixedScaleParameter;
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);
98      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
99      return cov;
100    }
101
102    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov,
103      bool fixedScale) {
104      var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count);
105      if (!fixedScale) {
106        gr.Add(2 * scale * cov.Covariance(x, i, j));
107      }
108      foreach (var g in cov.CovarianceGradient(x, i, j))
109        gr.Add(scale * g);
110      return gr;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.