Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3043-Regression-Instances-For-Scaling/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceScale.cs @ 17578

Last change on this file since 17578 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableType("4871900E-8B7A-4D74-969A-773D63198733")]
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(StorableConstructorFlag _) : base(_) {
49    }
50
51    private CovarianceScale(CovarianceScale original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public CovarianceScale()
56      : base() {
57      Name = ItemName;
58      Description = ItemDescription;
59
60      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
61      Parameters.Add(new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso()));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new CovarianceScale(this, cloner);
66    }
67
68    public int GetNumberOfParameters(int numberOfVariables) {
69      return (HasFixedScaleParameter ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
70    }
71
72    public void SetParameter(double[] p) {
73      double scale;
74      GetParameterValues(p, out scale);
75      ScaleParameter.Value = new DoubleValue(scale);
76      CovarianceFunctionParameter.Value.SetParameter(p.Skip(1).ToArray());
77    }
78
79    private void GetParameterValues(double[] p, out double scale) {
80      // gather parameter values
81      if (HasFixedScaleParameter) {
82        scale = ScaleParameter.Value.Value;
83      } else {
84        scale = Math.Exp(2 * p[0]);
85      }
86    }
87
88    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
89      double scale;
90      GetParameterValues(p, out scale);
91      var fixedScale = HasFixedScaleParameter;
92      var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
93      // create functions
94      var cov = new ParameterizedCovarianceFunction();
95      cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j);
96      cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j);
97      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
98      return cov;
99    }
100
101    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov,
102      bool fixedScale) {
103      var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count);
104      if (!fixedScale) {
105        gr.Add(2 * scale * cov.Covariance(x, i, j));
106      }
107      foreach (var g in cov.CovarianceGradient(x, i, j))
108        gr.Add(scale * g);
109      return gr;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.