Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13721 was 13721, checked in by mkommend, 8 years ago

#2591: Changed all GP covariance and mean functions to use int[] for column indices instead of IEnumerable<int>. Changed GP utils, GPModel and StudentTProcessModell as well to use fewer iterators and adapted unit tests to new interface.

File size: 4.1 KB
RevLine 
[8416]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8416]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
[8463]22using System;
23using System.Collections.Generic;
[8323]24using System.Linq;
[8982]25using System.Linq.Expressions;
[8416]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8323]29
[8416]30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
[8620]32  [Item(Name = "CovarianceProduct",
[8416]33    Description = "Product covariance function for Gaussian processes.")]
[8620]34  public sealed class CovarianceProduct : Item, ICovarianceFunction {
[8416]35    [Storable]
36    private ItemList<ICovarianceFunction> factors;
[8323]37
[8416]38    [Storable]
39    private int numberOfVariables;
40    public ItemList<ICovarianceFunction> Factors {
41      get { return factors; }
[8323]42    }
43
[8416]44    [StorableConstructor]
[8620]45    private CovarianceProduct(bool deserializing)
[8416]46      : base(deserializing) {
[8323]47    }
48
[8620]49    private CovarianceProduct(CovarianceProduct original, Cloner cloner)
[8416]50      : base(original, cloner) {
51      this.factors = cloner.Clone(original.factors);
52      this.numberOfVariables = original.numberOfVariables;
[8323]53    }
54
[8620]55    public CovarianceProduct()
[8416]56      : base() {
57      this.factors = new ItemList<ICovarianceFunction>();
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
[8620]61      return new CovarianceProduct(this, cloner);
[8416]62    }
63
64    public int GetNumberOfParameters(int numberOfVariables) {
65      this.numberOfVariables = numberOfVariables;
[8484]66      return factors.Select(f => f.GetNumberOfParameters(numberOfVariables)).Sum();
[8416]67    }
68
[8982]69    public void SetParameter(double[] p) {
[8416]70      int offset = 0;
[8982]71      foreach (var f in factors) {
72        var numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
73        f.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
[8416]74        offset += numberOfParameters;
[8323]75      }
76    }
[8484]77
[13721]78    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]79      if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function.");
80      var functions = new List<ParameterizedCovarianceFunction>();
81      foreach (var f in factors) {
82        int numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
83        functions.Add(f.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
84        p = p.Skip(numberOfParameters).ToArray();
85      }
86
87
88      var product = new ParameterizedCovarianceFunction();
89      product.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Aggregate((a, b) => a * b);
90      product.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Aggregate((a, b) => a * b);
91      product.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, functions);
92      return product;
[8416]93    }
[8323]94
[8982]95    public static IEnumerable<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) {
96      var covariances = factorFunctions.Select(f => f.Covariance(x, i, j)).ToArray();
97      for (int ii = 0; ii < factorFunctions.Count; ii++) {
98        foreach (var g in factorFunctions[ii].CovarianceGradient(x, i, j)) {
[8484]99          double res = g;
100          for (int jj = 0; jj < covariances.Length; jj++)
101            if (ii != jj) res *= covariances[jj];
102          yield return res;
103        }
[8323]104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.