Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceProd.cs @ 8416

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

#1902 worked on sum and product covariance functions and fixed a few bugs.

File size: 3.0 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 = "CovarianceProd",
30    Description = "Product covariance function for Gaussian processes.")]
31  public class CovarianceProd : Item, ICovarianceFunction {
32    [Storable]
33    private ItemList<ICovarianceFunction> factors;
34
35    [Storable]
36    private int numberOfVariables;
37    public ItemList<ICovarianceFunction> Factors {
38      get { return factors; }
39    }
40
41    [StorableConstructor]
42    protected CovarianceProd(bool deserializing)
43      : base(deserializing) {
44    }
45
46    protected CovarianceProd(CovarianceProd original, Cloner cloner)
47      : base(original, cloner) {
48      this.factors = cloner.Clone(original.factors);
49      this.numberOfVariables = original.numberOfVariables;
50    }
51
52    public CovarianceProd()
53      : base() {
54      this.factors = new ItemList<ICovarianceFunction>();
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new CovarianceProd(this, cloner);
59    }
60
61    public int GetNumberOfParameters(int numberOfVariables) {
62      this.numberOfVariables = numberOfVariables;
63      return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
64    }
65
66    public void SetParameter(double[] hyp) {
67      int offset = 0;
68      foreach (var t in factors) {
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 factors) {
80        t.SetData(x, xt);
81      }
82    }
83
84    public double GetCovariance(int i, int j) {
85      return factors.Select(t => t.GetCovariance(i, j)).Aggregate((a, b) => a * b);
86    }
87
88    public double[] GetGradient(int i, int j) {
89      return factors.Select(t => t.GetGradient(i, j)).SelectMany(seq => seq).ToArray();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.