Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/05/12 17:04:30 (12 years ago)
Author:
gkronber
Message:

#1902 implemented a few covariance functions as parameterized named items. Implemented rudimentary view for Gaussian process models.

File:
1 edited

Legend:

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

    r8491 r8582  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2729
     
    2931  [StorableClass]
    3032  [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")]
    31   public class CovariancePeriodic : Item, ICovarianceFunction {
     33  public class CovariancePeriodic : CovarianceFunction {
     34    public IValueParameter<DoubleValue> ScaleParameter {
     35      get { return scaleParameter; }
     36    }
     37    public IValueParameter<DoubleValue> InverseLengthParameter {
     38      get { return inverseLengthParameter; }
     39    }
     40    public IValueParameter<DoubleValue> PeriodParameter {
     41      get { return periodParameter; }
     42    }
     43
    3244    [Storable]
    33     private double sf2;
    34     public double Scale { get { return sf2; } }
     45    private HyperParameter<DoubleValue> scaleParameter;
     46    [Storable]
     47    private HyperParameter<DoubleValue> inverseLengthParameter;
     48    [Storable]
     49    private HyperParameter<DoubleValue> periodParameter;
     50
     51    [Storable]
     52    private double scale;
    3553    [Storable]
    3654    private double inverseLength;
    37     public double InverseLength { get { return inverseLength; } }
    3855    [Storable]
    39     private double p;
    40     public double Period { get { return p; } }
     56    private double period;
    4157
    42     public int GetNumberOfParameters(int numberOfVariables) {
    43       return 3;
    44     }
     58
    4559    [StorableConstructor]
    4660    protected CovariancePeriodic(bool deserializing) : base(deserializing) { }
    4761    protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
    4862      : base(original, cloner) {
    49       sf2 = original.sf2;
    50       inverseLength = original.inverseLength;
    51       p = original.p;
     63      this.scaleParameter = cloner.Clone(original.scaleParameter);
     64      this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     65      this.periodParameter = cloner.Clone(original.periodParameter);
     66      this.scale = original.scale;
     67      this.inverseLength = original.inverseLength;
     68      this.period = original.period;
     69
     70      RegisterEvents();
    5271    }
     72
    5373    public CovariancePeriodic()
    5474      : base() {
     75      scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.");
     76      inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.");
     77      periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function.");
     78      Parameters.Add(scaleParameter);
     79      Parameters.Add(inverseLengthParameter);
     80      Parameters.Add(periodParameter);
     81
     82      RegisterEvents();
     83    }
     84
     85    [StorableHook(HookType.AfterDeserialization)]
     86    private void AfterDeserialization() {
     87      RegisterEvents();
    5588    }
    5689
     
    5992    }
    6093
    61     public void SetParameter(double[] hyp) {
    62       if (hyp.Length != 3) throw new ArgumentException();
    63       this.inverseLength = 1.0 / Math.Exp(hyp[0]);
    64       this.p = Math.Exp(hyp[1]);
    65       this.sf2 = Math.Exp(2 * hyp[2]);
     94    // caching
     95    private void RegisterEvents() {
     96      AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
     97      AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
     98      AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
    6699    }
    67100
    68     public double GetCovariance(double[,] x, int i, int j) {
     101    public override int GetNumberOfParameters(int numberOfVariables) {
     102      return
     103        (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed);
     104    }
     105
     106    public override void SetParameter(double[] hyp) {
     107      int i = 0;
     108      if (!inverseLengthParameter.Fixed) {
     109        inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
     110        i++;
     111      }
     112      if (!periodParameter.Fixed) {
     113        periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
     114        i++;
     115      }
     116      if (!scaleParameter.Fixed) {
     117        scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
     118        i++;
     119      }
     120      if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp");
     121    }
     122
     123    public override double GetCovariance(double[,] x, int i, int j) {
    69124      double k = i == j ? 0.0 : GetDistance(x, x, i, j);
    70       k = Math.PI * k / p;
     125      k = Math.PI * k / period;
    71126      k = Math.Sin(k) * inverseLength;
    72127      k = k * k;
    73128
    74       return sf2 * Math.Exp(-2.0 * k);
     129      return scale * Math.Exp(-2.0 * k);
    75130    }
    76131
    77     public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    78       double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p;
     132    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     133      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / period;
    79134      double gradient = Math.Sin(v) * inverseLength;
    80135      gradient *= gradient;
    81       yield return 4.0 * sf2 * Math.Exp(-2.0 * gradient) * gradient;
     136      yield return 4.0 * scale * Math.Exp(-2.0 * gradient) * gradient;
    82137      double r = Math.Sin(v) * inverseLength;
    83       yield return 4.0 * sf2 * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
    84       yield return 2.0 * sf2 * Math.Exp(-2 * gradient);
     138      yield return 4.0 * scale * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v;
     139      yield return 2.0 * scale * Math.Exp(-2 * gradient);
    85140    }
    86141
    87     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     142    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    88143      double k = GetDistance(x, xt, i, j);
    89       k = Math.PI * k / p;
     144      k = Math.PI * k / period;
    90145      k = Math.Sin(k) * inverseLength;
    91146      k = k * k;
    92147
    93       return sf2 * Math.Exp(-2.0 * k);
     148      return scale * Math.Exp(-2.0 * k);
    94149    }
    95150
Note: See TracChangeset for help on using the changeset viewer.