[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;
|
---|
[8323] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[8371] | 28 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8323] | 29 | [StorableClass]
|
---|
| 30 | [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
|
---|
| 31 | public class CovarianceSEard : Item, ICovarianceFunction {
|
---|
| 32 | [Storable]
|
---|
| 33 | private double[,] x;
|
---|
| 34 | [Storable]
|
---|
| 35 | private double[,] xt;
|
---|
| 36 | [Storable]
|
---|
| 37 | private double sf2;
|
---|
| 38 | [Storable]
|
---|
| 39 | private double[] l;
|
---|
| 40 |
|
---|
| 41 | private double[,] sd;
|
---|
| 42 | private bool symmetric;
|
---|
| 43 |
|
---|
| 44 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 45 | return numberOfVariables + 1;
|
---|
| 46 | }
|
---|
| 47 | [StorableConstructor]
|
---|
| 48 | protected CovarianceSEard(bool deserializing) : base(deserializing) { }
|
---|
| 49 | protected CovarianceSEard(CovarianceSEard original, Cloner cloner)
|
---|
| 50 | : base(original, cloner) {
|
---|
[8416] | 51 | if (original.x != null) {
|
---|
| 52 | this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
|
---|
| 53 | Array.Copy(original.x, this.x, x.Length);
|
---|
| 54 |
|
---|
| 55 | this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
|
---|
| 56 | Array.Copy(original.xt, this.xt, xt.Length);
|
---|
| 57 |
|
---|
| 58 | this.sd = new double[original.sd.GetLength(0), original.sd.GetLength(1)];
|
---|
| 59 | Array.Copy(original.sd, this.sd, sd.Length);
|
---|
| 60 |
|
---|
| 61 | this.l = new double[original.l.Length];
|
---|
| 62 | Array.Copy(original.l, this.l, l.Length);
|
---|
| 63 | }
|
---|
[8323] | 64 | this.sf2 = original.sf2;
|
---|
[8416] | 65 | this.symmetric = original.symmetric;
|
---|
[8323] | 66 | }
|
---|
| 67 | public CovarianceSEard()
|
---|
| 68 | : base() {
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 72 | return new CovarianceSEard(this, cloner);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[8416] | 75 | public void SetParameter(double[] hyp) {
|
---|
| 76 | this.l = hyp.Take(hyp.Length - 1).Select(Math.Exp).ToArray();
|
---|
| 77 | this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]);
|
---|
| 78 | sf2 = Math.Min(10E6, sf2); // upper limit for the scale
|
---|
| 79 |
|
---|
| 80 | sd = null;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public void SetData(double[,] x) {
|
---|
| 84 | SetData(x, x);
|
---|
[8323] | 85 | this.symmetric = true;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[8416] | 88 | public void SetData(double[,] x, double[,] xt) {
|
---|
[8323] | 89 | this.x = x;
|
---|
| 90 | this.xt = xt;
|
---|
| 91 | this.symmetric = false;
|
---|
| 92 |
|
---|
| 93 | sd = null;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public double GetCovariance(int i, int j) {
|
---|
| 97 | if (sd == null) CalculateSquaredDistances();
|
---|
| 98 | return sf2 * Math.Exp(-sd[i, j] / 2.0);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[8455] | 101 | public double GetGradient(int i, int j, int k) {
|
---|
| 102 | if (k < l.Length) {
|
---|
[8323] | 103 | double sqrDist = Util.SqrDist(x[i, k] / l[k], xt[j, k] / l[k]);
|
---|
[8455] | 104 | return sf2 * Math.Exp(-sd[i, j] / 2.0) * sqrDist;
|
---|
| 105 | } else if (k == l.Length) {
|
---|
| 106 | return 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
|
---|
| 107 | } else {
|
---|
| 108 | throw new ArgumentException("CovarianceSEard has dimension+1 hyperparameters.", "k");
|
---|
[8323] | 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | private void CalculateSquaredDistances() {
|
---|
| 114 | if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
|
---|
| 115 | int rows = x.GetLength(0);
|
---|
| 116 | int cols = xt.GetLength(0);
|
---|
| 117 | sd = new double[rows, cols];
|
---|
| 118 | if (symmetric) {
|
---|
| 119 | for (int i = 0; i < rows; i++) {
|
---|
| 120 | for (int j = i; j < cols; j++) {
|
---|
| 121 | sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select((e, k) => e / l[k]),
|
---|
| 122 | Util.GetRow(xt, j).Select((e, k) => e / l[k]));
|
---|
| 123 | sd[j, i] = sd[i, j];
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | } else {
|
---|
| 127 | for (int i = 0; i < rows; i++) {
|
---|
| 128 | for (int j = 0; j < cols; j++) {
|
---|
| 129 | sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select((e, k) => e / l[k]),
|
---|
| 130 | Util.GetRow(xt, j).Select((e, k) => e / l[k]));
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|