[8401] | 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
|
---|
[8366] | 21 | using System;
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
[8371] | 27 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8366] | 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 int GetNumberOfParameters(int numberOfVariables) {
|
---|
| 34 | return numberOfVariables;
|
---|
| 35 | }
|
---|
| 36 | [StorableConstructor]
|
---|
| 37 | protected MeanLinear(bool deserializing) : base(deserializing) { }
|
---|
| 38 | protected MeanLinear(MeanLinear original, Cloner cloner)
|
---|
| 39 | : base(original, cloner) {
|
---|
[8368] | 40 | if (original.alpha != null) {
|
---|
| 41 | this.alpha = new double[original.alpha.Length];
|
---|
| 42 | Array.Copy(original.alpha, alpha, original.alpha.Length);
|
---|
| 43 | }
|
---|
[8366] | 44 | }
|
---|
| 45 | public MeanLinear()
|
---|
| 46 | : base() {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[8416] | 49 | public void SetParameter(double[] hyp) {
|
---|
[8366] | 50 | this.alpha = new double[hyp.Length];
|
---|
| 51 | Array.Copy(hyp, alpha, hyp.Length);
|
---|
| 52 | }
|
---|
[8416] | 53 | public void SetData(double[,] x) {
|
---|
| 54 | // nothing to do
|
---|
| 55 | }
|
---|
[8366] | 56 |
|
---|
| 57 | public double[] GetMean(double[,] x) {
|
---|
[8416] | 58 | // sanity check
|
---|
| 59 | if (alpha.Length != x.GetLength(1)) throw new ArgumentException("The number of hyperparameters must match the number of variables for the linear mean function.");
|
---|
[8366] | 60 | int cols = x.GetLength(1);
|
---|
[8416] | 61 | int n = x.GetLength(0);
|
---|
[8366] | 62 | return (from i in Enumerable.Range(0, n)
|
---|
| 63 | let rowVector = from j in Enumerable.Range(0, cols)
|
---|
| 64 | select x[i, j]
|
---|
| 65 | select Util.ScalarProd(alpha, rowVector))
|
---|
| 66 | .ToArray();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public double[] GetGradients(int k, double[,] x) {
|
---|
| 70 | int cols = x.GetLength(1);
|
---|
[8416] | 71 | int n = x.GetLength(0);
|
---|
[8366] | 72 | if (k > cols) throw new ArgumentException();
|
---|
| 73 | return (from r in Enumerable.Range(0, n)
|
---|
| 74 | select x[r, k]).ToArray();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 78 | return new MeanLinear(this, cloner);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|