Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceProduct.cs @ 17529

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

#2875: Removed years in copyrights

File size: 4.1 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 HEAL.Attic;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableType("C65B207C-2E95-4764-8698-D0B1D1EECCF1")]
31  [Item(Name = "CovarianceProduct",
32    Description = "Product covariance function for Gaussian processes.")]
33  public sealed class CovarianceProduct : Item, ICovarianceFunction {
34    [Storable]
35    private ItemList<ICovarianceFunction> factors;
36
37    [Storable]
38    private int numberOfVariables;
39    public ItemList<ICovarianceFunction> Factors {
40      get { return factors; }
41    }
42
43    [StorableConstructor]
44    private CovarianceProduct(StorableConstructorFlag _) : base(_) {
45    }
46
47    private CovarianceProduct(CovarianceProduct original, Cloner cloner)
48      : base(original, cloner) {
49      this.factors = cloner.Clone(original.factors);
50      this.numberOfVariables = original.numberOfVariables;
51    }
52
53    public CovarianceProduct()
54      : base() {
55      this.factors = new ItemList<ICovarianceFunction>();
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new CovarianceProduct(this, cloner);
60    }
61
62    public int GetNumberOfParameters(int numberOfVariables) {
63      this.numberOfVariables = numberOfVariables;
64      return factors.Select(f => f.GetNumberOfParameters(numberOfVariables)).Sum();
65    }
66
67    public void SetParameter(double[] p) {
68      int offset = 0;
69      foreach (var f in factors) {
70        var numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
71        f.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
72        offset += numberOfParameters;
73      }
74    }
75
76    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
77      if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function.");
78      var functions = new List<ParameterizedCovarianceFunction>();
79      foreach (var f in factors) {
80        int numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
81        functions.Add(f.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
82        p = p.Skip(numberOfParameters).ToArray();
83      }
84
85
86      var product = new ParameterizedCovarianceFunction();
87      product.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Aggregate((a, b) => a * b);
88      product.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Aggregate((a, b) => a * b);
89      product.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, functions);
90      return product;
91    }
92
93    public static IList<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) {
94      var covariances = factorFunctions.Select(f => f.Covariance(x, i, j)).ToArray();
95      var gr = new List<double>();
96      for (int ii = 0; ii < factorFunctions.Count; ii++) {
97        foreach (var g in factorFunctions[ii].CovarianceGradient(x, i, j)) {
98          double res = g;
99          for (int jj = 0; jj < covariances.Length; jj++)
100            if (ii != jj) res *= covariances[jj];
101          gr.Add(res);
102        }
103      }
104      return gr;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.