Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1423 moved LM-BFGS implementation from data-analysis into the gradient descent algorithm plugin.

File size: 3.7 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 with for Gaussian processes.")]
30  public class CovarianceLinear : Item, ICovarianceFunction {
31    [Storable]
32    private double[,] x;
33    [Storable]
34    private double[,] xt;
35
36
37    private double[,] k;
38    private bool symmetric;
39
40    public int GetNumberOfParameters(int numberOfVariables) {
41      return 0;
42    }
43    [StorableConstructor]
44    protected CovarianceLinear(bool deserializing) : base(deserializing) { }
45    protected CovarianceLinear(CovarianceLinear original, Cloner cloner)
46      : base(original, cloner) {
47      // note: using shallow copies here!
48      this.x = original.x;
49      this.xt = original.xt;
50
51    }
52    public CovarianceLinear()
53      : base() {
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new CovarianceLinear(this, cloner);
58    }
59
60    public void SetParameter(double[] hyp, double[,] x) {
61      if (hyp.Length > 0) throw new ArgumentException();
62      SetParameter(hyp, x, x);
63      this.symmetric = true;
64    }
65
66    public void SetParameter(double[] hyp, double[,] x, double[,] xt) {
67      this.x = x;
68      this.xt = xt;
69      this.symmetric = false;
70
71      k = null;
72    }
73
74    public double GetCovariance(int i, int j) {
75      if (k == null) CalculateInnerProduct();
76      return k[i, j];
77    }
78
79
80    public double[] GetDiagonalCovariances() {
81      if (x != xt) throw new InvalidOperationException();
82      int rows = x.GetLength(0);
83      int cols = x.GetLength(1);
84      var k = new double[rows];
85      for (int i = 0; i < rows; i++) {
86        k[i] = 0;
87        for (int j = 0; j < cols; j++) {
88          k[i] += x[i, j] * x[i, j];
89        }
90      }
91      return k;
92    }
93
94    public double[] GetGradient(int i, int j) {
95      throw new NotSupportedException();
96    }
97
98
99    private void CalculateInnerProduct() {
100      if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();
101      int rows = x.GetLength(0);
102      int cols = xt.GetLength(0);
103      k = new double[rows, cols];
104      if (symmetric) {
105        for (int i = 0; i < rows; i++) {
106          for (int j = i; j < cols; j++) {
107            k[i, j] = Util.ScalarProd(Util.GetRow(x, i),
108                                      Util.GetRow(x, j));
109            k[j, i] = k[i, j];
110          }
111        }
112      } else {
113        for (int i = 0; i < rows; i++) {
114          for (int j = 0; j < cols; j++) {
115            k[i, j] = Util.ScalarProd(Util.GetRow(x, i),
116                                      Util.GetRow(xt, j));
117          }
118        }
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.