Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs @ 8455

Last change on this file since 8455 was 8455, checked in by gkronber, 12 years ago

#1902 changed calculation of gradients for covariance functions to reduce allocations of arrays

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  [StorableClass]
29  [Item(Name = "CovarianceSum",
30    Description = "Sum covariance function for Gaussian processes.")]
31  public class CovarianceSum : Item, ICovarianceFunction {
32    [Storable]
33    private ItemList<ICovarianceFunction> terms;
34
35    [Storable]
36    private int numberOfVariables;
37    public ItemList<ICovarianceFunction> Terms {
38      get { return terms; }
39    }
40
41    [StorableConstructor]
42    protected CovarianceSum(bool deserializing)
43      : base(deserializing) {
44    }
45
46    protected CovarianceSum(CovarianceSum original, Cloner cloner)
47      : base(original, cloner) {
48      this.terms = cloner.Clone(original.terms);
49      this.numberOfVariables = original.numberOfVariables;
50    }
51
52    public CovarianceSum()
53      : base() {
54      this.terms = new ItemList<ICovarianceFunction>();
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new CovarianceSum(this, cloner);
59    }
60
61    public int GetNumberOfParameters(int numberOfVariables) {
62      this.numberOfVariables = numberOfVariables;
63      return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
64    }
65
66    public void SetParameter(double[] hyp) {
67      int offset = 0;
68      foreach (var t in terms) {
69        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
70        t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
71        offset += numberOfParameters;
72      }
73    }
74    public void SetData(double[,] x) {
75      SetData(x, x);
76    }
77
78    public void SetData(double[,] x, double[,] xt) {
79      foreach (var t in terms) {
80        t.SetData(x, xt);
81      }
82    }
83
84    public double GetCovariance(int i, int j) {
85      return terms.Select(t => t.GetCovariance(i, j)).Sum();
86    }
87
88    public double GetGradient(int i, int j, int k) {
89      int ii = 0;
90      while (k > terms[ii].GetNumberOfParameters(numberOfVariables)) {
91        k -= terms[ii].GetNumberOfParameters(numberOfVariables);
92      }
93      return terms[ii].GetGradient(i, j, k);
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.