source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanLinear.cs @ 8473

Last change on this file since 8473 was 8473, checked in by gkronber, 8 years ago

#1902 worked on GPR: added line chart, made parameters of mean and covariance functions readable, removed target variable scaling, moved noise hyperparameter for likelihood function to the end of the parameter list, added methods to calculate the predicted variance, removed limits for scale of covariance functions and introduced exception handling to catch non-spd or singular cov matrixes, implemented rational quadratic covariance function, added unit test case from GBML book (however it does not work as the book seemingly uses a noise-less likelihood function)

File size: 3.1 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
21using System;
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  [StorableClass]
29  [Item(Name = "MeanLinear", Description = "Linear mean function for Gaussian processes.")]
30  public class MeanLinear : Item, IMeanFunction {
31    [Storable]
32    private double[] alpha;
33    public double[] Weights {
34      get {
35        if (alpha == null) return new double[0];
36        var copy = new double[alpha.Length];
37        Array.Copy(alpha, copy, copy.Length);
38        return copy;
39      }
40    }
41    public int GetNumberOfParameters(int numberOfVariables) {
42      return numberOfVariables;
43    }
44    [StorableConstructor]
45    protected MeanLinear(bool deserializing) : base(deserializing) { }
46    protected MeanLinear(MeanLinear original, Cloner cloner)
47      : base(original, cloner) {
48      if (original.alpha != null) {
49        this.alpha = new double[original.alpha.Length];
50        Array.Copy(original.alpha, alpha, original.alpha.Length);
51      }
52    }
53    public MeanLinear()
54      : base() {
55    }
56
57    public void SetParameter(double[] hyp) {
58      this.alpha = new double[hyp.Length];
59      Array.Copy(hyp, alpha, hyp.Length);
60    }
61    public void SetData(double[,] x) {
62      // nothing to do
63    }
64
65    public double[] GetMean(double[,] x) {
66      // sanity check
67      if (alpha.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");
68      int cols = x.GetLength(1);
69      int n = x.GetLength(0);
70      return (from i in Enumerable.Range(0, n)
71              let rowVector = from j in Enumerable.Range(0, cols)
72                              select x[i, j]
73              select Util.ScalarProd(alpha, rowVector))
74        .ToArray();
75    }
76
77    public double[] GetGradients(int k, double[,] x) {
78      int cols = x.GetLength(1);
79      int n = x.GetLength(0);
80      if (k > cols) throw new ArgumentException();
81      return (from r in Enumerable.Range(0, n)
82              select x[r, k]).ToArray();
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new MeanLinear(this, cloner);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.