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/CovarianceSEiso.cs

    r8491 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  [Item(Name = "CovarianceSEiso",
    3132    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
    32   public class CovarianceSEiso : Item, ICovarianceFunction {
     33  public sealed class CovarianceSEiso : 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; } }
     39
    3640    [Storable]
    3741    private double inverseLength;
    38     public double InverseLength { get { return inverseLength; } }
     42    [Storable]
     43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
     44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
    3945
    4046    [StorableConstructor]
    41     protected CovarianceSEiso(bool deserializing)
     47    private CovarianceSEiso(bool deserializing)
    4248      : base(deserializing) {
    4349    }
    4450
    45     protected CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
     51    private CovarianceSEiso(CovarianceSEiso original, Cloner cloner)
    4652      : base(original, cloner) {
    4753      this.sf2 = original.sf2;
     54      this.scaleParameter = cloner.Clone(original.scaleParameter);
     55
    4856      this.inverseLength = original.inverseLength;
     57      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     58
     59      RegisterEvents();
    4960    }
    5061
    5162    public CovarianceSEiso()
    5263      : base() {
     64      Name = ItemName;
     65      Description = ItemDescription;
     66
     67      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function.");
     68      this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function.");
     69
     70      Parameters.Add(scaleParameter);
     71      Parameters.Add(inverseLengthParameter);
     72
     73      RegisterEvents();
    5374    }
    5475
     
    5778    }
    5879
     80    [StorableHook(HookType.AfterDeserialization)]
     81    private void AfterDeserialization() {
     82      RegisterEvents();
     83    }
     84
     85    private void RegisterEvents() {
     86      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     87      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     88    }
     89
    5990    public int GetNumberOfParameters(int numberOfVariables) {
    60       return 2;
     91      return
     92        (scaleParameter.Fixed ? 0 : 1) +
     93        (inverseLengthParameter.Fixed ? 0 : 1);
    6194    }
    6295
    6396    public void SetParameter(double[] hyp) {
    64       if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k");
    65       this.inverseLength = 1.0 / Math.Exp(hyp[0]);
    66       this.sf2 = Math.Exp(2 * hyp[1]);
     97      int i = 0;
     98      if (!inverseLengthParameter.Fixed) {
     99        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
     100        i++;
     101      }
     102      if (!scaleParameter.Fixed) {
     103        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     104        i++;
     105      }
     106      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSEiso", "hyp");
    67107    }
    68108
Note: See TracChangeset for help on using the changeset viewer.