1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess {
|
---|
8 | [StorableClass]
|
---|
9 | [Item(Name = "CovarianceSum",
|
---|
10 | Description = "Sum covariance function for Gaussian processes.")]
|
---|
11 | public class CovarianceSum : Item, ICovarianceFunction {
|
---|
12 | [Storable]
|
---|
13 | private ItemList<ICovarianceFunction> terms;
|
---|
14 |
|
---|
15 | [Storable]
|
---|
16 | private int numberOfVariables;
|
---|
17 | public ItemList<ICovarianceFunction> Terms {
|
---|
18 | get { return terms; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | [StorableConstructor]
|
---|
22 | protected CovarianceSum(bool deserializing)
|
---|
23 | : base(deserializing) {
|
---|
24 | }
|
---|
25 |
|
---|
26 | protected CovarianceSum(CovarianceSum original, Cloner cloner)
|
---|
27 | : base(original, cloner) {
|
---|
28 | this.terms = cloner.Clone(terms);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public CovarianceSum()
|
---|
32 | : base() {
|
---|
33 | }
|
---|
34 |
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
36 | return new CovarianceSum(this, cloner);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public int GetNumberOfParameters(int numberOfVariables) {
|
---|
40 | this.numberOfVariables = numberOfVariables;
|
---|
41 | return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void SetParameter(double[] hyp, double[,] x) {
|
---|
45 | int offset = 0;
|
---|
46 | foreach (var t in terms) {
|
---|
47 | t.SetParameter(hyp.Skip(offset).Take(t.GetNumberOfParameters(numberOfVariables)), x);
|
---|
48 | offset += numberOfVariables;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | public void SetParameter(double[] hyp, double[,] x, double[,] xt) {
|
---|
54 | this.l = Math.Exp(hyp[0]);
|
---|
55 | this.sf2 = Math.Exp(2 * hyp[1]);
|
---|
56 |
|
---|
57 | this.symmetric = false;
|
---|
58 | this.x = x;
|
---|
59 | this.xt = xt;
|
---|
60 | sd = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public double GetCovariance(int i, int j) {
|
---|
64 | if (sd == null) CalculateSquaredDistances();
|
---|
65 | return sf2 * Math.Exp(-sd[i, j] / 2.0);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | public double[] GetDiagonalCovariances() {
|
---|
70 | if (x != xt) throw new InvalidOperationException();
|
---|
71 | int rows = x.GetLength(0);
|
---|
72 | var sd = new double[rows];
|
---|
73 | for (int i = 0; i < rows; i++) {
|
---|
74 | sd[i] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, i).Select(e => e / l));
|
---|
75 | }
|
---|
76 | return sd.Select(d => sf2 * Math.Exp(-d / 2.0)).ToArray();
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | public double[] GetGradient(int i, int j) {
|
---|
81 | var res = new double[2];
|
---|
82 | res[0] = sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j];
|
---|
83 | res[1] = 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0);
|
---|
84 | return res;
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void CalculateSquaredDistances() {
|
---|
88 | if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
|
---|
89 | int rows = x.GetLength(0);
|
---|
90 | int cols = xt.GetLength(0);
|
---|
91 | sd = new double[rows, cols];
|
---|
92 | if (symmetric) {
|
---|
93 | for (int i = 0; i < rows; i++) {
|
---|
94 | for (int j = i; j < rows; j++) {
|
---|
95 | sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
|
---|
96 | sd[j, i] = sd[i, j];
|
---|
97 | }
|
---|
98 | }
|
---|
99 | } else {
|
---|
100 | for (int i = 0; i < rows; i++) {
|
---|
101 | for (int j = 0; j < cols; j++) {
|
---|
102 | sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|