Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinear.cs @ 8417

Last change on this file since 8417 was 8417, checked in by gkronber, 12 years ago

#1902 added periodic covariance function

File size: 3.8 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  [StorableClass]
29  [Item(Name = "CovarianceLinear", Description = "Linear covariance function for Gaussian processes.")]
30  public class CovarianceLinear : Item, ICovarianceFunction {
31    private static readonly double[] emptyArray = new double[0];
32
33    [Storable]
34    private double[,] x;
35    [Storable]
36    private double[,] xt;
37
38    private double[,] k;
39    private bool symmetric;
40
41    public int GetNumberOfParameters(int numberOfVariables) {
42      return 0;
43    }
44    [StorableConstructor]
45    protected CovarianceLinear(bool deserializing) : base(deserializing) { }
46    protected CovarianceLinear(CovarianceLinear original, Cloner cloner)
47      : base(original, cloner) {
48      if (original.x != null) {
49        this.x = new double[original.x.GetLength(0), original.x.GetLength(1)];
50        Array.Copy(original.x, this.x, x.Length);
51
52        this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];
53        Array.Copy(original.xt, this.xt, xt.Length);
54
55        this.k = new double[original.k.GetLength(0), original.k.GetLength(1)];
56        Array.Copy(original.k, this.k, k.Length);
57      }
58      this.symmetric = original.symmetric;
59    }
60    public CovarianceLinear()
61      : base() {
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new CovarianceLinear(this, cloner);
66    }
67
68    public void SetParameter(double[] hyp) {
69      if (hyp.Length > 0) throw new ArgumentException("No hyperparameters are allowed for the linear covariance function.");
70      k = null;
71    }
72
73    public void SetData(double[,] x) {
74      SetData(x, x);
75      this.symmetric = true;
76    }
77
78    public void SetData(double[,] x, double[,] xt) {
79      this.x = x;
80      this.xt = xt;
81      this.symmetric = false;
82
83      k = null;
84    }
85
86    public double GetCovariance(int i, int j) {
87      if (k == null) CalculateInnerProduct();
88      return k[i, j];
89    }
90
91    public double[] GetGradient(int i, int j) {
92      return emptyArray;
93    }
94
95
96    private void CalculateInnerProduct() {
97      if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
98      int rows = x.GetLength(0);
99      int cols = xt.GetLength(0);
100      k = new double[rows, cols];
101      if (symmetric) {
102        for (int i = 0; i < rows; i++) {
103          for (int j = i; j < cols; j++) {
104            k[i, j] = Util.ScalarProd(Util.GetRow(x, i),
105                                      Util.GetRow(x, j));
106            k[j, i] = k[i, j];
107          }
108        }
109      } else {
110        for (int i = 0; i < rows; i++) {
111          for (int j = 0; j < cols; j++) {
112            k[i, j] = Util.ScalarProd(Util.GetRow(x, i),
113                                      Util.GetRow(xt, j));
114          }
115        }
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.