Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanProduct.cs @ 17607

Last change on this file since 17607 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HEAL.Attic;
27
28namespace HeuristicLab.Algorithms.DataAnalysis {
29  [StorableType("27D33B4E-6100-419E-B4EA-6D5EFDBFF823")]
30  [Item(Name = "MeanProduct", Description = "Product of mean functions for Gaussian processes.")]
31  public sealed class MeanProduct : Item, IMeanFunction {
32    [Storable]
33    private ItemList<IMeanFunction> factors;
34
35    [Storable]
36    private int numberOfVariables;
37
38    public ItemList<IMeanFunction> Factors {
39      get { return factors; }
40    }
41
42    [StorableConstructor]
43    private MeanProduct(StorableConstructorFlag _) : base(_) {
44    }
45
46    private MeanProduct(MeanProduct original, Cloner cloner)
47      : base(original, cloner) {
48      this.factors = cloner.Clone(original.factors);
49      this.numberOfVariables = original.numberOfVariables;
50    }
51
52    public MeanProduct() {
53      this.factors = new ItemList<IMeanFunction>();
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new MeanProduct(this, cloner);
57    }
58
59    public int GetNumberOfParameters(int numberOfVariables) {
60      this.numberOfVariables = numberOfVariables;
61      return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
62    }
63
64    public void SetParameter(double[] p) {
65      int offset = 0;
66      foreach (var t in factors) {
67        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
68        t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
69        offset += numberOfParameters;
70      }
71    }
72
73
74    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
75      var factorMf = new List<ParameterizedMeanFunction>();
76      int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
77      int[] factorIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the correct mean-term
78      int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the l-th hyperparameter of the correct mean-term
79      int c = 0;
80      // get the parameterized mean function for each term
81      for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
82        var numberOfParameters = factors[factorIndex].GetNumberOfParameters(numberOfVariables);
83        factorMf.Add(factors[factorIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
84        p = p.Skip(numberOfParameters).ToArray();
85
86        for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
87          factorIndexMap[c] = factorIndex;
88          hyperParameterIndexMap[c] = hyperParameterIndex;
89          c++;
90        }
91      }
92
93      var mf = new ParameterizedMeanFunction();
94      mf.Mean = (x, i) => factorMf.Select(t => t.Mean(x, i)).Aggregate((a, b) => a * b);
95      mf.Gradient = (x, i, k) => {
96        double result = 1.0;
97        int hyperParameterFactorIndex = factorIndexMap[k];
98        for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
99          if (factorIndex == hyperParameterFactorIndex) {
100            // multiply gradient
101            result *= factorMf[factorIndex].Gradient(x, i, hyperParameterIndexMap[k]);
102          } else {
103            // multiply mean
104            result *= factorMf[factorIndex].Mean(x, i);
105          }
106        }
107        return result;
108      };
109      return mf;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.