Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceSum.cs @ 17209

Last change on this file since 17209 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.5 KB
RevLine 
[8416]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) 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;
[8366]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[16565]27using HEAL.Attic;
[8323]28
[8416]29namespace HeuristicLab.Algorithms.DataAnalysis {
[16565]30  [StorableType("8F1A684A-98BE-429A-BDA2-E1FB7DDF09F0")]
[8366]31  [Item(Name = "CovarianceSum",
32    Description = "Sum covariance function for Gaussian processes.")]
[8612]33  public sealed class CovarianceSum : Item, ICovarianceFunction {
[8366]34    [Storable]
35    private ItemList<ICovarianceFunction> terms;
[8323]36
[8366]37    [Storable]
38    private int numberOfVariables;
39    public ItemList<ICovarianceFunction> Terms {
40      get { return terms; }
[8323]41    }
42
[8366]43    [StorableConstructor]
[16565]44    private CovarianceSum(StorableConstructorFlag _) : base(_) {
[8323]45    }
46
[8612]47    private CovarianceSum(CovarianceSum original, Cloner cloner)
[8366]48      : base(original, cloner) {
[8416]49      this.terms = cloner.Clone(original.terms);
50      this.numberOfVariables = original.numberOfVariables;
[8323]51    }
52
[8366]53    public CovarianceSum()
54      : base() {
[8416]55      this.terms = new ItemList<ICovarianceFunction>();
[8323]56    }
57
[8366]58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new CovarianceSum(this, cloner);
60    }
61
62    public int GetNumberOfParameters(int numberOfVariables) {
63      this.numberOfVariables = numberOfVariables;
64      return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
65    }
66
[8982]67    public void SetParameter(double[] p) {
[8366]68      int offset = 0;
69      foreach (var t in terms) {
[8416]70        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
[8982]71        t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
[8416]72        offset += numberOfParameters;
[8323]73      }
74    }
[8484]75
[13721]76    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
[8982]77      if (terms.Count == 0) throw new ArgumentException("at least one term is necessary for the product covariance function.");
78      var functions = new List<ParameterizedCovarianceFunction>();
79      foreach (var t in terms) {
80        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
81        functions.Add(t.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
82        p = p.Skip(numberOfParameters).ToArray();
83      }
[8323]84
[8982]85      var sum = new ParameterizedCovarianceFunction();
86      sum.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Sum();
87      sum.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Sum();
[13784]88      sum.CovarianceGradient = (x, i, j) => {
[13891]89        var g = new List<double>();
[13784]90        foreach (var e in functions)
91          g.AddRange(e.CovarianceGradient(x, i, j));
92        return g;
93      };
[8982]94      return sum;
[8366]95    }
[8323]96  }
97}
Note: See TracBrowser for help on using the repository browser.