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

    r8491 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  [Item(Name = "CovarianceRQiso",
    3132    Description = "Isotropic rational quadratic covariance function for Gaussian processes.")]
    32   public class CovarianceRQiso : Item, ICovarianceFunction {
     33  public sealed class CovarianceRQiso : 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; } }
    3942    [Storable]
    40     private double alpha;
    41     public double Shape { get { return alpha; } }
     43    private readonly HyperParameter<DoubleValue> inverseLengthParameter;
     44    public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
     45
     46    [Storable]
     47    private double shape;
     48    [Storable]
     49    private readonly HyperParameter<DoubleValue> shapeParameter;
     50    public IValueParameter<DoubleValue> ShapeParameter { get { return shapeParameter; } }
    4251
    4352    [StorableConstructor]
    44     protected CovarianceRQiso(bool deserializing)
     53    private CovarianceRQiso(bool deserializing)
    4554      : base(deserializing) {
    4655    }
    4756
    48     protected CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
     57    private CovarianceRQiso(CovarianceRQiso original, Cloner cloner)
    4958      : base(original, cloner) {
    5059      this.sf2 = original.sf2;
     60      this.scaleParameter = cloner.Clone(original.scaleParameter);
     61
    5162      this.inverseLength = original.inverseLength;
    52       this.alpha = original.alpha;
     63      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     64
     65      this.shape = original.shape;
     66      this.shapeParameter = cloner.Clone(original.shapeParameter);
     67
     68      RegisterEvents();
    5369    }
    5470
    5571    public CovarianceRQiso()
    5672      : base() {
     73      Name = ItemName;
     74      Description = ItemDescription;
     75
     76      this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale parameter of the isometric rational quadratic covariance function.");
     77      this.inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric rational quadratic covariance function.");
     78      this.shapeParameter = new HyperParameter<DoubleValue>("Shape", "The shape parameter (alpha) of the isometric rational quadratic covariance function.");
     79
     80      Parameters.Add(scaleParameter);
     81      Parameters.Add(inverseLengthParameter);
     82      Parameters.Add(shapeParameter);
     83
     84      RegisterEvents();
    5785    }
    5886
     
    6189    }
    6290
     91    [StorableHook(HookType.AfterDeserialization)]
     92    private void AfterDeserialization() {
     93      RegisterEvents();
     94    }
     95
     96    private void RegisterEvents() {
     97      Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     98      Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     99      Util.AttachValueChangeHandler<DoubleValue, double>(shapeParameter, () => { shape = shapeParameter.Value.Value; });
     100    }
     101
    63102    public int GetNumberOfParameters(int numberOfVariables) {
    64       return 3;
     103      return
     104        (scaleParameter.Fixed ? 0 : 1) +
     105        (inverseLengthParameter.Fixed ? 0 : 1) +
     106        (shapeParameter.Fixed ? 0 : 1);
    65107    }
    66108
    67109    public void SetParameter(double[] hyp) {
    68       if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k");
    69       this.inverseLength = 1.0 / Math.Exp(hyp[0]);
    70       this.sf2 = Math.Exp(2 * hyp[1]);
    71       this.alpha = Math.Exp(hyp[2]);
     110      int i = 0;
     111      if (!scaleParameter.Fixed) {
     112        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     113        i++;
     114      }
     115      if (!shapeParameter.Fixed) {
     116        shapeParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
     117        i++;
     118      }
     119      if (!inverseLengthParameter.Fixed) {
     120        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
     121        i++;
     122      }
     123      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceRQiso", "hyp");
    72124    }
    73125
     
    77129                   ? 0.0
    78130                   : Util.SqrDist(x, i, j, inverseLength);
    79       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     131      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    80132    }
    81133
     
    85137                   : Util.SqrDist(x, i, j, inverseLength);
    86138
    87       double b = 1 + 0.5 * d / alpha;
    88       yield return sf2 * Math.Pow(b, -alpha - 1) * d;
    89       yield return 2 * sf2 * Math.Pow(b, -alpha);
    90       yield return sf2 * Math.Pow(b, -alpha) * (0.5 * d / b - alpha * Math.Log(b));
     139      double b = 1 + 0.5 * d / shape;
     140      yield return sf2 * Math.Pow(b, -shape - 1) * d;
     141      yield return 2 * sf2 * Math.Pow(b, -shape);
     142      yield return sf2 * Math.Pow(b, -shape) * (0.5 * d / b - shape * Math.Log(b));
    91143    }
    92144
    93145    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    94146      double d = Util.SqrDist(x, i, xt, j, inverseLength);
    95       return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha);
     147      return sf2 * Math.Pow(1 + 0.5 * d / shape, -shape);
    96148    }
    97149  }
Note: See TracChangeset for help on using the changeset viewer.