Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/12 13:28:55 (12 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs

    r8565 r8612  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3031  [StorableClass]
    3132  [Item(Name = "CovarianceSEard", Description = "Squared exponential covariance function with automatic relevance determination for Gaussian processes.")]
    32   public class CovarianceSEard : Item, ICovarianceFunction {
     33  public sealed class CovarianceSEard : ParameterizedNamedItem, ICovarianceFunction {
    3334    [Storable]
    3435    private double sf2;
    35     public double Scale { get { return sf2; } }
     36    [Storable]
     37    private readonly HyperParameter<DoubleValue> scaleParameter;
     38    public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
    3639
    3740    [Storable]
    3841    private double[] inverseLength;
    39     public double[] InverseLength {
    40       get {
    41         if (inverseLength == null) return new double[0];
    42         var copy = new double[inverseLength.Length];
    43         Array.Copy(inverseLength, copy, copy.Length);
    44         return copy;
     42    [Storable]
     43    private readonly HyperParameter<DoubleArray> inverseLengthParameter;
     44    public IValueParameter<DoubleArray> InverseLengthParameter { get { return inverseLengthParameter; } }
     45
     46    [StorableConstructor]
     47    private CovarianceSEard(bool deserializing) : base(deserializing) { }
     48    private CovarianceSEard(CovarianceSEard original, Cloner cloner)
     49      : base(original, cloner) {
     50      this.sf2 = original.sf2;
     51      this.scaleParameter = cloner.Clone(original.scaleParameter);
     52
     53      if (original.inverseLength != null) {
     54        this.inverseLength = new double[original.inverseLength.Length];
     55        Array.Copy(original.inverseLength, this.inverseLength, this.inverseLength.Length);
    4556      }
    46     }
     57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
    4758
    48     public int GetNumberOfParameters(int numberOfVariables) {
    49       return numberOfVariables + 1;
    50     }
    51     [StorableConstructor]
    52     protected CovarianceSEard(bool deserializing) : base(deserializing) { }
    53     protected CovarianceSEard(CovarianceSEard original, Cloner cloner)
    54       : base(original, cloner) {
    55       this.inverseLength = original.InverseLength; // array is cloned in the getter
    56       this.sf2 = original.sf2;
     59      RegisterEvents();
    5760    }
    5861    public CovarianceSEard()
    5962      : base() {
     63      Name = ItemName;
     64      Description = ItemDescription;
     65
     66      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the squared exponential covariance function with ARD.");
     67      this.inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength", "The inverse length parameter for automatic relevance determination.");
     68
     69      Parameters.Add(scaleParameter);
     70      Parameters.Add(inverseLengthParameter);
     71
     72      RegisterEvents();
    6073    }
    6174
     
    6477    }
    6578
     79    [StorableHook(HookType.AfterDeserialization)]
     80    private void AfterDeserialization() {
     81      RegisterEvents();
     82    }
     83
     84    private void RegisterEvents() {
     85      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     86      Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => {
     87        inverseLength =
     88          inverseLengthParameter.Value.ToArray();
     89      });
     90    }
     91
     92    public int GetNumberOfParameters(int numberOfVariables) {
     93      return
     94        (scaleParameter.Fixed ? 0 : 1) +
     95        (inverseLengthParameter.Fixed ? 0 : numberOfVariables);
     96    }
     97
     98
    6699    public void SetParameter(double[] hyp) {
    67       this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray();
    68       this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]);
     100      int i = 0;
     101      if (!scaleParameter.Fixed) {
     102        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     103        i++;
     104      }
     105      if (!inverseLengthParameter.Fixed) {
     106        inverseLengthParameter.SetValue(new DoubleArray(hyp.Skip(i).Select(e => 1.0 / Math.Exp(e)).ToArray()));
     107        i += hyp.Skip(i).Count();
     108      }
     109      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancSEard", "hyp");
    69110    }
    70111
Note: See TracChangeset for help on using the changeset viewer.