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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
30 | [StorableClass]
|
---|
31 | [Item(Name = "CovarianceSum",
|
---|
32 | Description = "Sum covariance function for Gaussian processes.")]
|
---|
33 | public sealed class CovarianceSum : Item, ICovarianceFunction {
|
---|
34 | [Storable]
|
---|
35 | private ItemList<ICovarianceFunction> terms;
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private int numberOfVariables;
|
---|
39 | public ItemList<ICovarianceFunction> Terms {
|
---|
40 | get { return terms; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | private CovarianceSum(bool deserializing)
|
---|
45 | : base(deserializing) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | private CovarianceSum(CovarianceSum original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | this.terms = cloner.Clone(original.terms);
|
---|
51 | this.numberOfVariables = original.numberOfVariables;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public CovarianceSum()
|
---|
55 | : base() {
|
---|
56 | this.terms = new ItemList<ICovarianceFunction>();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new CovarianceSum(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
64 | this.numberOfVariables = numberOfVariables;
|
---|
65 | return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void SetParameter(double[] hyp) {
|
---|
69 | if (terms.Count == 0) throw new ArgumentException("At least one term is needed for sum covariance function.");
|
---|
70 | int offset = 0;
|
---|
71 | foreach (var t in terms) {
|
---|
72 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
73 | t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
74 | offset += numberOfParameters;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public double GetCovariance(double[,] x, int i, int j) {
|
---|
79 | return terms.Select(t => t.GetCovariance(x, i, j)).Sum();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
|
---|
83 | return terms.Select(t => t.GetGradient(x, i, j)).Aggregate(Enumerable.Concat);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
|
---|
87 | return terms.Select(t => t.GetCrossCovariance(x, xt, i, j)).Sum();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|