[8439] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8439] | 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
|
---|
[8982] | 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[8439] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[16565] | 26 | using HEAL.Attic;
|
---|
[8439] | 27 |
|
---|
| 28 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16565] | 29 | [StorableType("27D33B4E-6100-419E-B4EA-6D5EFDBFF823")]
|
---|
[8620] | 30 | [Item(Name = "MeanProduct", Description = "Product of mean functions for Gaussian processes.")]
|
---|
| 31 | public sealed class MeanProduct : Item, IMeanFunction {
|
---|
[8439] | 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]
|
---|
[16565] | 43 | private MeanProduct(StorableConstructorFlag _) : base(_) {
|
---|
[8439] | 44 | }
|
---|
| 45 |
|
---|
[8620] | 46 | private MeanProduct(MeanProduct original, Cloner cloner)
|
---|
[8439] | 47 | : base(original, cloner) {
|
---|
| 48 | this.factors = cloner.Clone(original.factors);
|
---|
| 49 | this.numberOfVariables = original.numberOfVariables;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[8620] | 52 | public MeanProduct() {
|
---|
[8439] | 53 | this.factors = new ItemList<IMeanFunction>();
|
---|
| 54 | }
|
---|
[8612] | 55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8620] | 56 | return new MeanProduct(this, cloner);
|
---|
[8612] | 57 | }
|
---|
[8439] | 58 |
|
---|
[8612] | 59 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 60 | this.numberOfVariables = numberOfVariables;
|
---|
| 61 | return factors.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[8982] | 64 | public void SetParameter(double[] p) {
|
---|
[8439] | 65 | int offset = 0;
|
---|
| 66 | foreach (var t in factors) {
|
---|
| 67 | var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
|
---|
[8982] | 68 | t.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
|
---|
[8439] | 69 | offset += numberOfParameters;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[8982] | 73 |
|
---|
[13721] | 74 | public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
|
---|
[8982] | 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 | }
|
---|
[8439] | 91 | }
|
---|
| 92 |
|
---|
[8982] | 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 | }
|
---|
[8439] | 106 | }
|
---|
[8982] | 107 | return result;
|
---|
| 108 | };
|
---|
| 109 | return mf;
|
---|
[8439] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|