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 |
|
---|
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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | [StorableClass]
|
---|
33 | [Item(Name = "CovarianceSquaredExponentialArd", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
34 | public sealed class CovarianceSquaredExponentialArd : ParameterizedNamedItem, ICovarianceFunction {
|
---|
35 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
36 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public IValueParameter<DoubleArray> InverseLengthParameter {
|
---|
40 | get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | private CovarianceSquaredExponentialArd(bool deserializing) : base(deserializing) { }
|
---|
45 | private CovarianceSquaredExponentialArd(CovarianceSquaredExponentialArd original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | }
|
---|
48 | public CovarianceSquaredExponentialArd()
|
---|
49 | : base() {
|
---|
50 | Name = ItemName;
|
---|
51 | Description = ItemDescription;
|
---|
52 |
|
---|
53 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD."));
|
---|
54 | Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."));
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new CovarianceSquaredExponentialArd(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
62 | return
|
---|
63 | (ScaleParameter.Value != null ? 0 : 1) +
|
---|
64 | (InverseLengthParameter.Value != null ? 0 : numberOfVariables);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void SetParameter(double[] p) {
|
---|
68 | double scale;
|
---|
69 | double[] inverseLength;
|
---|
70 | GetParameterValues(p, out scale, out inverseLength);
|
---|
71 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
72 | InverseLengthParameter.Value = new DoubleArray(inverseLength);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void GetParameterValues(double[] p, out double scale, out double[] inverseLength) {
|
---|
76 | int c = 0;
|
---|
77 | // gather parameter values
|
---|
78 | if (ScaleParameter.Value != null) {
|
---|
79 | scale = ScaleParameter.Value.Value;
|
---|
80 | } else {
|
---|
81 | scale = Math.Exp(2 * p[c]);
|
---|
82 | c++;
|
---|
83 | }
|
---|
84 | if (InverseLengthParameter.Value != null) {
|
---|
85 | inverseLength = InverseLengthParameter.Value.ToArray();
|
---|
86 | } else {
|
---|
87 | inverseLength = p.Skip(1).Select(e => 1.0 / Math.Exp(e)).ToArray();
|
---|
88 | c += inverseLength.Length;
|
---|
89 | }
|
---|
90 | if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "p");
|
---|
91 | }
|
---|
92 |
|
---|
93 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
|
---|
94 | double scale;
|
---|
95 | double[] inverseLength;
|
---|
96 | GetParameterValues(p, out scale, out inverseLength);
|
---|
97 | // create functions
|
---|
98 | var cov = new ParameterizedCovarianceFunction();
|
---|
99 | cov.Covariance = (x, i, j) => {
|
---|
100 | double d = i == j
|
---|
101 | ? 0.0
|
---|
102 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
103 | return scale * Math.Exp(-d / 2.0);
|
---|
104 | };
|
---|
105 | cov.CrossCovariance = (x, xt, i, j) => {
|
---|
106 | double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
|
---|
107 | return scale * Math.Exp(-d / 2.0);
|
---|
108 | };
|
---|
109 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, inverseLength);
|
---|
110 | return cov;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double[] inverseLength) {
|
---|
115 | if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
|
---|
116 | double d = i == j
|
---|
117 | ? 0.0
|
---|
118 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
119 | int k = 0;
|
---|
120 | foreach (var columnIndex in columnIndices) {
|
---|
121 | double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]);
|
---|
122 | yield return scale * Math.Exp(-d / 2.0) * sqrDist;
|
---|
123 | k++;
|
---|
124 | }
|
---|
125 |
|
---|
126 | yield return 2.0 * scale * Math.Exp(-d / 2.0);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|