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.Collections.Generic;
|
---|
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 = "CovarianceRQiso",
|
---|
31 | Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
|
---|
32 | public class CovarianceRQiso : Item, ICovarianceFunction {
|
---|
33 | [Storable]
|
---|
34 | private double sf2;
|
---|
35 | public double Scale { get { return sf2; } }
|
---|
36 | [Storable]
|
---|
37 | private double inverseLength;
|
---|
38 | public double InverseLength { get { return inverseLength; } }
|
---|
39 | [Storable]
|
---|
40 | private double alpha;
|
---|
41 | public double Shape { get { return alpha; } }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected CovarianceRQiso(bool deserializing)
|
---|
45 | : base(deserializing) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | this.sf2 = original.sf2;
|
---|
51 | this.inverseLength = original.inverseLength;
|
---|
52 | this.alpha = original.alpha;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public CovarianceRQiso()
|
---|
56 | : base() {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new CovarianceRQiso(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
64 | return 3;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void SetParameter(double[] hyp) {
|
---|
68 | if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");
|
---|
69 | this.inverseLength = 1.0 / Math.Exp(hyp[0]);
|
---|
70 | this.sf2 = Math.Exp(2 * hyp[1]);
|
---|
71 | this.alpha = Math.Exp(hyp[2]);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | public double GetCovariance(double[,] x, int i, int j) {
|
---|
76 | double d = i == j
|
---|
77 | ? 0.0
|
---|
78 | : Util.SqrDist(x, i, j, inverseLength);
|
---|
79 | return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
|
---|
83 | double d = i == j
|
---|
84 | ? 0.0
|
---|
85 | : Util.SqrDist(x, i, j, inverseLength);
|
---|
86 |
|
---|
87 | double b = 1 + 0.5 * d / alpha;
|
---|
88 | yield return sf2 * Math.Pow(b, -alpha - 1) * d;
|
---|
89 | yield return 2 * sf2 * Math.Pow(b, -alpha);
|
---|
90 | yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
|
---|
94 | double d = Util.SqrDist(x, i, xt, j, inverseLength);
|
---|
95 | return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|