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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | [StorableType("A704FDF2-0566-4AB5-B752-D713831B016C")]
|
---|
33 | [Item(Name = "CovarianceLinearArd",
|
---|
34 | Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
35 | public sealed class CovarianceLinearArd : ParameterizedNamedItem, ICovarianceFunction {
|
---|
36 | public IValueParameter<DoubleArray> InverseLengthParameter {
|
---|
37 | get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
|
---|
38 | }
|
---|
39 | private bool HasFixedInverseLengthParameter {
|
---|
40 | get { return InverseLengthParameter.Value != null; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | private CovarianceLinearArd(StorableConstructorFlag _) : base(_) { }
|
---|
45 | private CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | }
|
---|
48 | public CovarianceLinearArd()
|
---|
49 | : base() {
|
---|
50 | Name = ItemName;
|
---|
51 | Description = ItemDescription;
|
---|
52 |
|
---|
53 | Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength",
|
---|
54 | "The inverse length parameter for ARD."));
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new CovarianceLinearArd(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
62 | if (HasFixedInverseLengthParameter)
|
---|
63 | return 0;
|
---|
64 | else
|
---|
65 | return numberOfVariables;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void SetParameter(double[] p) {
|
---|
69 | double[] inverseLength;
|
---|
70 | GetParameterValues(p, out inverseLength);
|
---|
71 | InverseLengthParameter.Value = new DoubleArray(inverseLength);
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void GetParameterValues(double[] p, out double[] inverseLength) {
|
---|
75 | // gather parameter values
|
---|
76 | if (HasFixedInverseLengthParameter) {
|
---|
77 | inverseLength = InverseLengthParameter.Value.ToArray();
|
---|
78 | } else {
|
---|
79 | inverseLength = p.Select(e => 1.0 / Math.Exp(e)).ToArray();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
|
---|
84 | double[] inverseLength;
|
---|
85 | GetParameterValues(p, out inverseLength);
|
---|
86 | var fixedInverseLength = HasFixedInverseLengthParameter;
|
---|
87 | // create functions
|
---|
88 | var cov = new ParameterizedCovarianceFunction();
|
---|
89 | cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices);
|
---|
90 | cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
|
---|
91 | if (fixedInverseLength)
|
---|
92 | cov.CovarianceGradient = (x, i, j) => new double[0];
|
---|
93 | else
|
---|
94 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices);
|
---|
95 | return cov;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static IList<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, int[] columnIndices) {
|
---|
99 | int k = 0;
|
---|
100 | var g = new List<double>(columnIndices.Length);
|
---|
101 | for (int c = 0; c < columnIndices.Length; c++) {
|
---|
102 | var columnIndex = columnIndices[c];
|
---|
103 | g.Add(-2.0 * x[i, columnIndex] * x[j, columnIndex] * inverseLength[k] * inverseLength[k]);
|
---|
104 | k++;
|
---|
105 | }
|
---|
106 | return g;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|