1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
29 | [StorableClass]
|
---|
30 | [Item(Name = "MeanSum", Description = "Sum of mean functions for Gaussian processes.")]
|
---|
31 | public sealed class MeanSum : Item, IMeanFunction {
|
---|
32 | [Storable]
|
---|
33 | private ItemList<IMeanFunction> terms;
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | private int numberOfVariables;
|
---|
37 | public ItemList<IMeanFunction> Terms {
|
---|
38 | get { return terms; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | private MeanSum(bool deserializing) : base(deserializing) { }
|
---|
43 | private MeanSum(MeanSum original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | this.terms = cloner.Clone(original.terms);
|
---|
46 | this.numberOfVariables = original.numberOfVariables;
|
---|
47 | }
|
---|
48 | public MeanSum() {
|
---|
49 | this.terms = new ItemList<IMeanFunction>();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new MeanSum(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
57 | this.numberOfVariables = numberOfVariables;
|
---|
58 | return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void SetParameter(double[] p) {
|
---|
62 | int offset = 0;
|
---|
63 | foreach (var t in terms) {
|
---|
64 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
65 | t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
66 | offset += numberOfParameters;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
|
---|
71 | var termMf = new List<ParameterizedMeanFunction>();
|
---|
72 | int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
|
---|
73 | int[] termIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term
|
---|
74 | int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term
|
---|
75 | int c = 0;
|
---|
76 | // get the parameterized mean function for each term
|
---|
77 | for (int termIndex = 0; termIndex < terms.Count; termIndex++) {
|
---|
78 | var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables);
|
---|
79 | termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
|
---|
80 | p = p.Skip(numberOfParameters).ToArray();
|
---|
81 |
|
---|
82 | for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
|
---|
83 | termIndexMap[c] = termIndex;
|
---|
84 | hyperParameterIndexMap[c] = hyperParameterIndex;
|
---|
85 | c++;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | var mf = new ParameterizedMeanFunction();
|
---|
90 | mf.Mean = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum();
|
---|
91 | mf.Gradient = (x, i, k) => {
|
---|
92 | return termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]);
|
---|
93 | };
|
---|
94 | return mf;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|