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 | using System.Linq;
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
27 | [StorableClass]
|
---|
28 | [Item(Name = "MeanProd", Description = "Product of mean functions for Gaussian processes.")]
|
---|
29 | public class MeanProd : Item, IMeanFunction {
|
---|
30 | [Storable]
|
---|
31 | private ItemList<IMeanFunction> factors;
|
---|
32 |
|
---|
33 | [Storable]
|
---|
34 | private int numberOfVariables;
|
---|
35 |
|
---|
36 | public ItemList<IMeanFunction> Factors {
|
---|
37 | get { return factors; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
41 | this.numberOfVariables = numberOfVariables;
|
---|
42 | return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected MeanProd(bool deserializing)
|
---|
47 | : base(deserializing) {
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected MeanProd(MeanProd original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | this.factors = cloner.Clone(original.factors);
|
---|
53 | this.numberOfVariables = original.numberOfVariables;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public MeanProd() {
|
---|
57 | this.factors = new ItemList<IMeanFunction>();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void SetParameter(double[] hyp) {
|
---|
61 | int offset = 0;
|
---|
62 | foreach (var t in factors) {
|
---|
63 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
64 | t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
65 | offset += numberOfParameters;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void SetData(double[,] x) {
|
---|
70 | foreach (var t in factors) t.SetData(x);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public double[] GetMean(double[,] x) {
|
---|
74 | var res = factors.First().GetMean(x);
|
---|
75 | foreach (var t in factors.Skip(1)) {
|
---|
76 | var a = t.GetMean(x);
|
---|
77 | for (int i = 0; i < res.Length; i++) res[i] *= a[i];
|
---|
78 | }
|
---|
79 | return res;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public double[] GetGradients(int k, double[,] x) {
|
---|
83 | double[] res = Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
|
---|
84 | // find index of factor for the given k
|
---|
85 | int j = 0;
|
---|
86 | while (k >= factors[j].GetNumberOfParameters(numberOfVariables)) {
|
---|
87 | k -= factors[j].GetNumberOfParameters(numberOfVariables);
|
---|
88 | j++;
|
---|
89 | }
|
---|
90 | for (int i = 0; i < factors.Count; i++) {
|
---|
91 | var f = factors[i];
|
---|
92 | if (i == j) {
|
---|
93 | // multiply gradient
|
---|
94 | var g = f.GetGradients(k, x);
|
---|
95 | for (int ii = 0; ii < res.Length; ii++) res[ii] *= g[ii];
|
---|
96 | } else {
|
---|
97 | // multiply mean
|
---|
98 | var m = f.GetMean(x);
|
---|
99 | for (int ii = 0; ii < res.Length; ii++) res[ii] *= m[ii];
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return res;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
106 | return new MeanProd(this, cloner);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|