[8565] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8565] | 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;
|
---|
[8612] | 27 | using HeuristicLab.Data;
|
---|
[8982] | 28 | using HeuristicLab.Parameters;
|
---|
[8565] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 32 | [StorableClass]
|
---|
[8615] | 33 | [Item(Name = "CovarianceRationalQuadraticArd",
|
---|
[8565] | 34 | Description = "Rational quadratic covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
[8615] | 35 | public sealed class CovarianceRationalQuadraticArd : ParameterizedNamedItem, ICovarianceFunction {
|
---|
[8612] | 36 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
[8982] | 37 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
[8612] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public IValueParameter<DoubleArray> InverseLengthParameter {
|
---|
[8982] | 41 | get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
|
---|
[8565] | 42 | }
|
---|
[8612] | 43 |
|
---|
| 44 | public IValueParameter<DoubleValue> ShapeParameter {
|
---|
[8982] | 45 | get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
|
---|
[8612] | 46 | }
|
---|
[10489] | 47 | private bool HasFixedScaleParameter {
|
---|
| 48 | get { return ScaleParameter.Value != null; }
|
---|
| 49 | }
|
---|
| 50 | private bool HasFixedInverseLengthParameter {
|
---|
| 51 | get { return InverseLengthParameter.Value != null; }
|
---|
| 52 | }
|
---|
| 53 | private bool HasFixedShapeParameter {
|
---|
| 54 | get { return ShapeParameter.Value != null; }
|
---|
| 55 | }
|
---|
[8565] | 56 |
|
---|
| 57 | [StorableConstructor]
|
---|
[8615] | 58 | private CovarianceRationalQuadraticArd(bool deserializing)
|
---|
[8565] | 59 | : base(deserializing) {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[8615] | 62 | private CovarianceRationalQuadraticArd(CovarianceRationalQuadraticArd original, Cloner cloner)
|
---|
[8565] | 63 | : base(original, cloner) {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8615] | 66 | public CovarianceRationalQuadraticArd()
|
---|
[8565] | 67 | : base() {
|
---|
[8612] | 68 | Name = ItemName;
|
---|
| 69 | Description = ItemDescription;
|
---|
| 70 |
|
---|
[8982] | 71 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the rational quadratic covariance function with ARD."));
|
---|
| 72 | Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination."));
|
---|
| 73 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the rational quadratic covariance function with ARD."));
|
---|
[8565] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8615] | 77 | return new CovarianceRationalQuadraticArd(this, cloner);
|
---|
[8565] | 78 | }
|
---|
| 79 |
|
---|
[8982] | 80 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 81 | return
|
---|
[10489] | 82 | (HasFixedScaleParameter ? 0 : 1) +
|
---|
| 83 | (HasFixedShapeParameter ? 0 : 1) +
|
---|
| 84 | (HasFixedInverseLengthParameter ? 0 : numberOfVariables);
|
---|
[8612] | 85 | }
|
---|
| 86 |
|
---|
[8982] | 87 | public void SetParameter(double[] p) {
|
---|
| 88 | double scale, shape;
|
---|
| 89 | double[] inverseLength;
|
---|
| 90 | GetParameterValues(p, out scale, out shape, out inverseLength);
|
---|
| 91 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
| 92 | ShapeParameter.Value = new DoubleValue(shape);
|
---|
| 93 | InverseLengthParameter.Value = new DoubleArray(inverseLength);
|
---|
[8612] | 94 | }
|
---|
| 95 |
|
---|
[8982] | 96 | private void GetParameterValues(double[] p, out double scale, out double shape, out double[] inverseLength) {
|
---|
| 97 | int c = 0;
|
---|
| 98 | // gather parameter values
|
---|
[10489] | 99 | if (HasFixedInverseLengthParameter) {
|
---|
[9108] | 100 | inverseLength = InverseLengthParameter.Value.ToArray();
|
---|
| 101 | } else {
|
---|
| 102 | int length = p.Length;
|
---|
[10489] | 103 | if (!HasFixedScaleParameter) length--;
|
---|
| 104 | if (!HasFixedShapeParameter) length--;
|
---|
[9108] | 105 | inverseLength = p.Select(e => 1.0 / Math.Exp(e)).Take(length).ToArray();
|
---|
| 106 | c += inverseLength.Length;
|
---|
| 107 | }
|
---|
[10489] | 108 | if (HasFixedScaleParameter) {
|
---|
[8982] | 109 | scale = ScaleParameter.Value.Value;
|
---|
| 110 | } else {
|
---|
| 111 | scale = Math.Exp(2 * p[c]);
|
---|
| 112 | c++;
|
---|
[8612] | 113 | }
|
---|
[10489] | 114 | if (HasFixedShapeParameter) {
|
---|
[8982] | 115 | shape = ShapeParameter.Value.Value;
|
---|
| 116 | } else {
|
---|
| 117 | shape = Math.Exp(p[c]);
|
---|
| 118 | c++;
|
---|
[8612] | 119 | }
|
---|
[8982] | 120 | if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticArd", "p");
|
---|
[8565] | 121 | }
|
---|
| 122 |
|
---|
[13721] | 123 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
|
---|
[8982] | 124 | double scale, shape;
|
---|
| 125 | double[] inverseLength;
|
---|
| 126 | GetParameterValues(p, out scale, out shape, out inverseLength);
|
---|
[10489] | 127 | var fixedInverseLength = HasFixedInverseLengthParameter;
|
---|
| 128 | var fixedScale = HasFixedScaleParameter;
|
---|
| 129 | var fixedShape = HasFixedShapeParameter;
|
---|
[8982] | 130 | // create functions
|
---|
| 131 | var cov = new ParameterizedCovarianceFunction();
|
---|
| 132 | cov.Covariance = (x, i, j) => {
|
---|
| 133 | double d = i == j
|
---|
| 134 | ? 0.0
|
---|
| 135 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
| 136 | return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
|
---|
| 137 | };
|
---|
| 138 | cov.CrossCovariance = (x, xt, i, j) => {
|
---|
| 139 | double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
|
---|
| 140 | return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
|
---|
| 141 | };
|
---|
[10489] | 142 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
|
---|
[8982] | 143 | return cov;
|
---|
[8565] | 144 | }
|
---|
| 145 |
|
---|
[13784] | 146 | private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, double shape, double[] inverseLength,
|
---|
[10489] | 147 | bool fixedInverseLength, bool fixedScale, bool fixedShape) {
|
---|
[8565] | 148 | double d = i == j
|
---|
| 149 | ? 0.0
|
---|
[8678] | 150 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[8612] | 151 | double b = 1 + 0.5 * d / shape;
|
---|
[8933] | 152 | int k = 0;
|
---|
[13784] | 153 | var g = new List<double>(columnIndices.Length + 2);
|
---|
[10489] | 154 | if (!fixedInverseLength) {
|
---|
| 155 | foreach (var columnIndex in columnIndices) {
|
---|
[13784] | 156 | g.Add(
|
---|
[10489] | 157 | scale * Math.Pow(b, -shape - 1) *
|
---|
[13784] | 158 | Util.SqrDist(x[i, columnIndex] * inverseLength[k], x[j, columnIndex] * inverseLength[k]));
|
---|
[10489] | 159 | k++;
|
---|
| 160 | }
|
---|
[8565] | 161 | }
|
---|
[13784] | 162 | if (!fixedScale) g.Add(2 * scale * Math.Pow(b, -shape));
|
---|
| 163 | if (!fixedShape) g.Add(scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b)));
|
---|
| 164 | return g;
|
---|
[8565] | 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|