Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs @ 8660

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

#1902 implemented all mean and covariance functions with parameters as ParameterizedNamedItems

File size: 3.2 KB
RevLine 
[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
21
[8323]22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
[8612]26using HeuristicLab.Data;
[8323]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[8371]29namespace HeuristicLab.Algorithms.DataAnalysis {
[8323]30  [StorableClass]
31  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
[8612]32  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
[8323]33    [Storable]
34    private double c;
[8612]35    [Storable]
36    private readonly HyperParameter<DoubleValue> valueParameter;
37    public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } }
[8473]38
[8323]39    [StorableConstructor]
[8612]40    private MeanConst(bool deserializing) : base(deserializing) { }
41    private MeanConst(MeanConst original, Cloner cloner)
[8323]42      : base(original, cloner) {
43      this.c = original.c;
[8612]44      this.valueParameter = cloner.Clone(original.valueParameter);
45      RegisterEvents();
[8323]46    }
47    public MeanConst()
48      : base() {
[8612]49      this.name = ItemName;
50      this.description = ItemDescription;
51
52      this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function.");
53      Parameters.Add(valueParameter);
54      RegisterEvents();
[8323]55    }
56
[8612]57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new MeanConst(this, cloner);
[8323]59    }
[8612]60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterEvents();
[8416]64    }
[8323]65
[8612]66    private void RegisterEvents() {
67      Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; });
68    }
69
70    public int GetNumberOfParameters(int numberOfVariables) {
71      return valueParameter.Fixed ? 0 : 1;
72    }
73
74    public void SetParameter(double[] hyp) {
75      if (!valueParameter.Fixed) {
76        valueParameter.SetValue(new DoubleValue(hyp[0]));
77      } else if (hyp.Length > 0)
78        throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp");
79    }
80
[8366]81    public double[] GetMean(double[,] x) {
[8416]82      return Enumerable.Repeat(c, x.GetLength(0)).ToArray();
[8323]83    }
84
[8366]85    public double[] GetGradients(int k, double[,] x) {
[8323]86      if (k > 0) throw new ArgumentException();
[8416]87      return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
[8323]88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.