Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2875: Removed years in copyrights

File size: 4.3 KB
RevLine 
[8620]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[16565]29using HEAL.Attic;
[8620]30
31namespace HeuristicLab.Algorithms.DataAnalysis {
[16565]32  [StorableType("4871900E-8B7A-4D74-969A-773D63198733")]
[8620]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]
[16565]48    private CovarianceScale(StorableConstructorFlag _) : base(_) {
[8620]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
[8982]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()));
[8620]62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new CovarianceScale(this, cloner);
66    }
67
[8982]68    public int GetNumberOfParameters(int numberOfVariables) {
[10489]69      return (HasFixedScaleParameter ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
[8620]70    }
71
[8982]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());
[8620]77    }
78
[8982]79    private void GetParameterValues(double[] p, out double scale) {
80      // gather parameter values
[10489]81      if (HasFixedScaleParameter) {
[8982]82        scale = ScaleParameter.Value.Value;
83      } else {
84        scale = Math.Exp(2 * p[0]);
[8620]85      }
86    }
87
[13721]88    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]89      double scale;
90      GetParameterValues(p, out scale);
[10489]91      var fixedScale = HasFixedScaleParameter;
[8982]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);
[10489]97      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
[8982]98      return cov;
[8620]99    }
100
[13784]101    private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov,
[10489]102      bool fixedScale) {
[13784]103      var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count);
[10489]104      if (!fixedScale) {
[13784]105        gr.Add(2 * scale * cov.Covariance(x, i, j));
[10489]106      }
[8982]107      foreach (var g in cov.CovarianceGradient(x, i, j))
[13784]108        gr.Add(scale * g);
109      return gr;
[8620]110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.