[8620] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8620] | 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 = "CovarianceScale",
|
---|
| 34 | Description = "Scale covariance function for Gaussian processes.")]
|
---|
| 35 | public sealed class CovarianceScale : ParameterizedNamedItem, ICovarianceFunction {
|
---|
| 36 | public IValueParameter<DoubleValue> ScaleParameter {
|
---|
[8982] | 37 | get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
[8620] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public IValueParameter<ICovarianceFunction> CovarianceFunctionParameter {
|
---|
[8982] | 41 | get { return (IValueParameter<ICovarianceFunction>)Parameters["CovarianceFunction"]; }
|
---|
[8620] | 42 | }
|
---|
| 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
| 45 | private CovarianceScale(bool deserializing)
|
---|
| 46 | : base(deserializing) {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | private CovarianceScale(CovarianceScale original, Cloner cloner)
|
---|
| 50 | : base(original, cloner) {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public CovarianceScale()
|
---|
| 54 | : base() {
|
---|
| 55 | Name = ItemName;
|
---|
| 56 | Description = ItemDescription;
|
---|
| 57 |
|
---|
[8982] | 58 | Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter."));
|
---|
| 59 | Parameters.Add(new ValueParameter<ICovarianceFunction>("CovarianceFunction", "The covariance function that should be scaled.", new CovarianceSquaredExponentialIso()));
|
---|
[8620] | 60 | }
|
---|
| 61 |
|
---|
| 62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 63 | return new CovarianceScale(this, cloner);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8982] | 66 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 67 | return (ScaleParameter.Value != null ? 0 : 1) + CovarianceFunctionParameter.Value.GetNumberOfParameters(numberOfVariables);
|
---|
[8620] | 68 | }
|
---|
| 69 |
|
---|
[8982] | 70 | public void SetParameter(double[] p) {
|
---|
| 71 | double scale;
|
---|
| 72 | GetParameterValues(p, out scale);
|
---|
| 73 | ScaleParameter.Value = new DoubleValue(scale);
|
---|
| 74 | CovarianceFunctionParameter.Value.SetParameter(p.Skip(1).ToArray());
|
---|
[8620] | 75 | }
|
---|
| 76 |
|
---|
[8982] | 77 | private void GetParameterValues(double[] p, out double scale) {
|
---|
| 78 | // gather parameter values
|
---|
| 79 | if (ScaleParameter.Value != null) {
|
---|
| 80 | scale = ScaleParameter.Value.Value;
|
---|
| 81 | } else {
|
---|
| 82 | scale = Math.Exp(2 * p[0]);
|
---|
[8620] | 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[8982] | 86 | public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
|
---|
| 87 | double scale;
|
---|
| 88 | GetParameterValues(p, out scale);
|
---|
| 89 | var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
|
---|
| 90 | // create functions
|
---|
| 91 | var cov = new ParameterizedCovarianceFunction();
|
---|
| 92 | cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j);
|
---|
| 93 | cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j);
|
---|
| 94 | cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov);
|
---|
| 95 | return cov;
|
---|
[8620] | 96 | }
|
---|
| 97 |
|
---|
[8982] | 98 | private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov) {
|
---|
| 99 | yield return 2 * scale * cov.Covariance(x, i, j);
|
---|
| 100 | foreach (var g in cov.CovarianceGradient(x, i, j))
|
---|
| 101 | yield return scale * g;
|
---|
[8620] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|