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