[8473] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8473] | 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;
|
---|
[8473] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[8612] | 26 | using HeuristicLab.Data;
|
---|
[8982] | 27 | using HeuristicLab.Parameters;
|
---|
[8473] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | [StorableClass]
|
---|
[8615] | 32 | [Item(Name = "CovarianceRationalQuadraticIso",
|
---|
[8473] | 33 | Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
|
---|
[8615] | 34 | public sealed class CovarianceRationalQuadraticIso : ParameterizedNamedItem, ICovarianceFunction {
|
---|
[8982] | 35 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
| 36 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
| 37 | }
|
---|
[8612] | 38 |
|
---|
[8982] | 39 | public IValueParameter<DoubleValue> InverseLengthParameter {
|
---|
| 40 | get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
|
---|
| 41 | }
|
---|
[8473] | 42 |
|
---|
[8982] | 43 | public IValueParameter<DoubleValue> ShapeParameter {
|
---|
| 44 | get { return (IValueParameter<DoubleValue>)Parameters["Shape"]; }
|
---|
| 45 | }
|
---|
[8473] | 46 | [StorableConstructor]
|
---|
[8615] | 47 | private CovarianceRationalQuadraticIso(bool deserializing)
|
---|
[8473] | 48 | : base(deserializing) {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[8615] | 51 | private CovarianceRationalQuadraticIso(CovarianceRationalQuadraticIso original, Cloner cloner)
|
---|
[8473] | 52 | : base(original, cloner) {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[8615] | 55 | public CovarianceRationalQuadraticIso()
|
---|
[8473] | 56 | : base() {
|
---|
[8612] | 57 | Name = ItemName;
|
---|
| 58 | Description = ItemDescription;
|
---|
| 59 |
|
---|
[8982] | 60 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function."));
|
---|
| 61 | Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function."));
|
---|
| 62 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function."));
|
---|
[8473] | 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8615] | 66 | return new CovarianceRationalQuadraticIso(this, cloner);
|
---|
[8473] | 67 | }
|
---|
| 68 |
|
---|
[8982] | 69 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 70 | return (ScaleParameter.Value != null ? 0 : 1) +
|
---|
| 71 | (ShapeParameter.Value != null ? 0 : 1) +
|
---|
| 72 | (InverseLengthParameter.Value != null ? 0 : 1);
|
---|
[8612] | 73 | }
|
---|
| 74 |
|
---|
[8982] | 75 | public void SetParameter(double[] p) {
|
---|
| 76 | double scale, shape, inverseLength;
|
---|
| 77 | GetParameterValues(p, out scale, out shape, out inverseLength);
|
---|
| 78 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
| 79 | ShapeParameter.Value = new DoubleValue(shape);
|
---|
| 80 | InverseLengthParameter.Value = new DoubleValue(inverseLength);
|
---|
[8612] | 81 | }
|
---|
| 82 |
|
---|
[8982] | 83 | private void GetParameterValues(double[] p, out double scale, out double shape, out double inverseLength) {
|
---|
| 84 | int c = 0;
|
---|
| 85 | // gather parameter values
|
---|
[9108] | 86 | if (InverseLengthParameter.Value != null) {
|
---|
| 87 | inverseLength = InverseLengthParameter.Value.Value;
|
---|
| 88 | } else {
|
---|
| 89 | inverseLength = 1.0 / Math.Exp(p[c]);
|
---|
| 90 | c++;
|
---|
| 91 | }
|
---|
[8982] | 92 | if (ScaleParameter.Value != null) {
|
---|
| 93 | scale = ScaleParameter.Value.Value;
|
---|
| 94 | } else {
|
---|
| 95 | scale = Math.Exp(2 * p[c]);
|
---|
| 96 | c++;
|
---|
[8612] | 97 | }
|
---|
[8982] | 98 | if (ShapeParameter.Value != null) {
|
---|
| 99 | shape = ShapeParameter.Value.Value;
|
---|
| 100 | } else {
|
---|
| 101 | shape = Math.Exp(p[c]);
|
---|
| 102 | c++;
|
---|
[8612] | 103 | }
|
---|
[8982] | 104 | if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRationalQuadraticIso", "p");
|
---|
[8473] | 105 | }
|
---|
| 106 |
|
---|
[8982] | 107 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
|
---|
| 108 | double scale, shape, inverseLength;
|
---|
| 109 | GetParameterValues(p, out scale, out shape, out inverseLength);
|
---|
| 110 | // create functions
|
---|
| 111 | var cov = new ParameterizedCovarianceFunction();
|
---|
| 112 | cov.Covariance = (x, i, j) => {
|
---|
| 113 | double d = i == j
|
---|
| 114 | ? 0.0
|
---|
| 115 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[9111] | 116 | return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
|
---|
[8982] | 117 | };
|
---|
| 118 | cov.CrossCovariance = (x, xt, i, j) => {
|
---|
| 119 | double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
|
---|
| 120 | return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
|
---|
| 121 | };
|
---|
| 122 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength);
|
---|
| 123 | return cov;
|
---|
[8473] | 124 | }
|
---|
| 125 |
|
---|
[8982] | 126 | private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double shape, double inverseLength) {
|
---|
[8484] | 127 | double d = i == j
|
---|
| 128 | ? 0.0
|
---|
[8678] | 129 | : Util.SqrDist(x, i, j, inverseLength, columnIndices);
|
---|
[8473] | 130 |
|
---|
[8612] | 131 | double b = 1 + 0.5 * d / shape;
|
---|
[8982] | 132 | yield return scale * Math.Pow(b, -shape - 1) * d;
|
---|
| 133 | yield return 2 * scale * Math.Pow(b, -shape);
|
---|
| 134 | yield return scale * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
|
---|
[8473] | 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|