[8401] | 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;
|
---|
[8484] | 23 | using System.Collections.Generic;
|
---|
[8323] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[8612] | 27 | using HeuristicLab.Data;
|
---|
[8323] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[8371] | 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8323] | 31 | [StorableClass]
|
---|
[8615] | 32 | [Item(Name = "CovarianceSquaredExponentialArd", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
| 33 | public sealed class CovarianceSquaredExponentialArd : ParameterizedNamedItem, ICovarianceFunction {
|
---|
[8323] | 34 | [Storable]
|
---|
| 35 | private double sf2;
|
---|
[8612] | 36 | [Storable]
|
---|
| 37 | private readonly HyperParameter<DoubleValue> scaleParameter;
|
---|
| 38 | public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
|
---|
[8473] | 39 |
|
---|
[8323] | 40 | [Storable]
|
---|
[8491] | 41 | private double[] inverseLength;
|
---|
[8612] | 42 | [Storable]
|
---|
| 43 | private readonly HyperParameter<DoubleArray> inverseLengthParameter;
|
---|
| 44 | public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } }
|
---|
[8323] | 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
[8615] | 47 | private CovarianceSquaredExponentialArd(bool deserializing) : base(deserializing) { }
|
---|
| 48 | private CovarianceSquaredExponentialArd(CovarianceSquaredExponentialArd original, Cloner cloner)
|
---|
[8323] | 49 | : base(original, cloner) {
|
---|
| 50 | this.sf2 = original.sf2;
|
---|
[8612] | 51 | this.scaleParameter = cloner.Clone(original.scaleParameter);
|
---|
| 52 |
|
---|
| 53 | if (original.inverseLength != null) {
|
---|
| 54 | this.inverseLength = new double[original.inverseLength.Length];
|
---|
| 55 | Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length);
|
---|
| 56 | }
|
---|
| 57 | this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
|
---|
| 58 |
|
---|
| 59 | RegisterEvents();
|
---|
[8323] | 60 | }
|
---|
[8615] | 61 | public CovarianceSquaredExponentialArd()
|
---|
[8323] | 62 | : base() {
|
---|
[8612] | 63 | Name = ItemName;
|
---|
| 64 | Description = ItemDescription;
|
---|
| 65 |
|
---|
| 66 | this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD.");
|
---|
| 67 | this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
|
---|
| 68 |
|
---|
| 69 | Parameters.Add(scaleParameter);
|
---|
| 70 | Parameters.Add(inverseLengthParameter);
|
---|
| 71 |
|
---|
| 72 | RegisterEvents();
|
---|
[8323] | 73 | }
|
---|
| 74 |
|
---|
| 75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8615] | 76 | return new CovarianceSquaredExponentialArd(this, cloner);
|
---|
[8323] | 77 | }
|
---|
| 78 |
|
---|
[8612] | 79 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 80 | private void AfterDeserialization() {
|
---|
| 81 | RegisterEvents();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private void RegisterEvents() {
|
---|
| 85 | Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
|
---|
| 86 | Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => {
|
---|
| 87 | inverseLength =
|
---|
| 88 | inverseLengthParameter.Value.ToArray();
|
---|
| 89 | });
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 93 | return
|
---|
| 94 | (scaleParameter.Fixed ? 0 : 1) +
|
---|
| 95 | (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
[8416] | 99 | public void SetParameter(double[] hyp) {
|
---|
[8612] | 100 | int i = 0;
|
---|
| 101 | if (!scaleParameter.Fixed) {
|
---|
| 102 | scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
|
---|
| 103 | i++;
|
---|
| 104 | }
|
---|
| 105 | if (!inverseLengthParameter.Fixed) {
|
---|
| 106 | inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
|
---|
| 107 | i += hyp.Skip(i).Count();
|
---|
| 108 | }
|
---|
[8615] | 109 | if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialArd", "hyp");
|
---|
[8416] | 110 | }
|
---|
| 111 |
|
---|
[8678] | 112 | public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
|
---|
[8484] | 113 | double d = i == j
|
---|
| 114 | ? 0.0
|
---|
[8678] | 115 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[8484] | 116 | return sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 117 | }
|
---|
| 118 |
|
---|
[8678] | 119 | public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
|
---|
[8932] | 120 | if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
|
---|
[8484] | 121 | double d = i == j
|
---|
| 122 | ? 0.0
|
---|
[8678] | 123 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[8323] | 124 |
|
---|
[8932] | 125 | foreach (var columnIndex in columnIndices) {
|
---|
| 126 | double sqrDist = Util.SqrDist(x[i, columnIndex] * inverseLength[columnIndex], x[j, columnIndex] * inverseLength[columnIndex]);
|
---|
[8489] | 127 | yield return sf2 * Math.Exp(-d / 2.0) * sqrDist;
|
---|
[8323] | 128 | }
|
---|
[8932] | 129 |
|
---|
[8489] | 130 | yield return 2.0 * sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 131 | }
|
---|
| 132 |
|
---|
[8678] | 133 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
|
---|
| 134 | double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
|
---|
[8484] | 135 | return sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|