[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;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[8371] | 29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8323] | 30 | [StorableClass]
|
---|
| 31 | [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
| 32 | public class CovarianceSEard : Item, ICovarianceFunction {
|
---|
| 33 | [Storable]
|
---|
| 34 | private double sf2;
|
---|
[8473] | 35 | public double Scale { get { return sf2; } }
|
---|
| 36 |
|
---|
[8323] | 37 | [Storable]
|
---|
[8491] | 38 | private double[] inverseLength;
|
---|
[8565] | 39 | public double[] InverseLength {
|
---|
[8473] | 40 | get {
|
---|
[8491] | 41 | if (inverseLength == null) return new double[0];
|
---|
| 42 | var copy = new double[inverseLength.Length];
|
---|
| 43 | Array.Copy(inverseLength, copy, copy.Length);
|
---|
[8473] | 44 | return copy;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[8323] | 47 |
|
---|
| 48 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 49 | return numberOfVariables + 1;
|
---|
| 50 | }
|
---|
| 51 | [StorableConstructor]
|
---|
| 52 | protected CovarianceSEard(bool deserializing) : base(deserializing) { }
|
---|
| 53 | protected CovarianceSEard(CovarianceSEard original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
[8565] | 55 | this.inverseLength = original.InverseLength; // array is cloned in the getter
|
---|
[8323] | 56 | this.sf2 = original.sf2;
|
---|
| 57 | }
|
---|
| 58 | public CovarianceSEard()
|
---|
| 59 | : base() {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 63 | return new CovarianceSEard(this, cloner);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8416] | 66 | public void SetParameter(double[] hyp) {
|
---|
[8491] | 67 | this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray();
|
---|
[8416] | 68 | this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[8484] | 71 | public double GetCovariance(double[,] x, int i, int j) {
|
---|
| 72 | double d = i == j
|
---|
| 73 | ? 0.0
|
---|
[8491] | 74 | : Util.SqrDist(x, i, j, inverseLength);
|
---|
[8484] | 75 | return sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 76 | }
|
---|
| 77 |
|
---|
[8484] | 78 | public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
|
---|
| 79 | double d = i == j
|
---|
| 80 | ? 0.0
|
---|
[8491] | 81 | : Util.SqrDist(x, i, j, inverseLength);
|
---|
[8323] | 82 |
|
---|
[8491] | 83 | for (int ii = 0; ii < inverseLength.Length; ii++) {
|
---|
| 84 | double sqrDist = Util.SqrDist(x[i, ii] * inverseLength[ii], x[j, ii] * inverseLength[ii]);
|
---|
[8489] | 85 | yield return sf2 * Math.Exp(-d / 2.0) * sqrDist;
|
---|
[8323] | 86 | }
|
---|
[8489] | 87 | yield return 2.0 * sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 88 | }
|
---|
| 89 |
|
---|
[8484] | 90 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
|
---|
[8491] | 91 | double d = Util.SqrDist(x, i, xt, j, inverseLength);
|
---|
[8484] | 92 | return sf2 * Math.Exp(-d / 2.0);
|
---|
[8323] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|