Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/12 19:02:47 (11 years ago)
Author:
gkronber
Message:

#1902: removed class HyperParameter and changed implementations of covariance and mean functions to remove the parameter value caching and event handlers for parameter caching. Instead it is now possible to create the actual covariance and mean functions as Func from templates and specified parameter values. The instances of mean and covariance functions configured in the GUI are actually templates where the structure and fixed parameters can be specified.

File:
1 edited

Legend:

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

    r8929 r8982  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
     
    3334  public sealed class CovariancePeriodic : ParameterizedNamedItem, ICovarianceFunction {
    3435
    35     [Storable]
    36     private double scale;
    37     [Storable]
    38     private readonly HyperParameter<DoubleValue> scaleParameter;
    3936    public IValueParameter<DoubleValue> ScaleParameter {
    40       get { return scaleParameter; }
     37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
    4138    }
    4239
    43     [Storable]
    44     private double inverseLength;
    45     [Storable]
    46     private readonly HyperParameter<DoubleValue> inverseLengthParameter;
    4740    public IValueParameter<DoubleValue> InverseLengthParameter {
    48       get { return inverseLengthParameter; }
     41      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
    4942    }
    5043
    51     [Storable]
    52     private double period;
    53     [Storable]
    54     private readonly HyperParameter<DoubleValue> periodParameter;
    5544    public IValueParameter<DoubleValue> PeriodParameter {
    56       get { return periodParameter; }
     45      get { return (IValueParameter<DoubleValue>)Parameters["Period"]; }
    5746    }
    5847
     
    6251    private CovariancePeriodic(CovariancePeriodic original, Cloner cloner)
    6352      : base(original, cloner) {
    64       this.scaleParameter = cloner.Clone(original.scaleParameter);
    65       this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
    66       this.periodParameter = cloner.Clone(original.periodParameter);
    67       this.scale = original.scale;
    68       this.inverseLength = original.inverseLength;
    69       this.period = original.period;
    70 
    71       RegisterEvents();
    7253    }
    7354
     
    7758      Description = ItemDescription;
    7859
    79       scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the periodic covariance function.");
    80       inverseLengthParameter = new HyperParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function.");
    81       periodParameter = new HyperParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function.");
    82       Parameters.Add(scaleParameter);
    83       Parameters.Add(inverseLengthParameter);
    84       Parameters.Add(periodParameter);
    85 
    86       RegisterEvents();
    87     }
    88 
    89     [StorableHook(HookType.AfterDeserialization)]
    90     private void AfterDeserialization() {
    91       RegisterEvents();
     60      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the periodic covariance function."));
     61      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter for the periodic covariance function."));
     62      Parameters.Add(new OptionalValueParameter<DoubleValue>("Period", "The period parameter for the periodic covariance function."));
    9263    }
    9364
     
    9667    }
    9768
    98     // caching
    99     private void RegisterEvents() {
    100       Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
    101       Util.AttachValueChangeHandler<DoubleValue, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.Value; });
    102       Util.AttachValueChangeHandler<DoubleValue, double>(periodParameter, () => { period = periodParameter.Value.Value; });
     69    public int GetNumberOfParameters(int numberOfVariables) {
     70      return (ScaleParameter.Value != null ? 0 : 1) +
     71       (PeriodParameter.Value != null ? 0 : 1) +
     72       (InverseLengthParameter.Value != null ? 0 : 1);
    10373    }
    10474
    105     public int GetNumberOfParameters(int numberOfVariables) {
    106       return
    107         (new[] { scaleParameter, inverseLengthParameter, periodParameter }).Count(p => !p.Fixed);
     75    public void SetParameter(double[] p) {
     76      double scale, inverseLength, period;
     77      GetParameterValues(p, out scale, out period, out inverseLength);
     78      ScaleParameter.Value = new DoubleValue(scale);
     79      PeriodParameter.Value = new DoubleValue(period);
     80      InverseLengthParameter.Value = new DoubleValue(inverseLength);
    10881    }
    10982
    110     public void SetParameter(double[] hyp) {
    111       int i = 0;
    112       if (!inverseLengthParameter.Fixed) {
    113         inverseLengthParameter.SetValue(new DoubleValue(1.0 / Math.Exp(hyp[i])));
    114         i++;
     83
     84    private void GetParameterValues(double[] p, out double scale, out double period, out double inverseLength) {
     85      // gather parameter values
     86      int c = 0;
     87      if (InverseLengthParameter.Value != null) {
     88        inverseLength = InverseLengthParameter.Value.Value;
     89      } else {
     90        inverseLength = 1.0 / Math.Exp(p[c]);
     91        c++;
    11592      }
    116       if (!periodParameter.Fixed) {
    117         periodParameter.SetValue(new DoubleValue(Math.Exp(hyp[i])));
    118         i++;
     93      if (PeriodParameter.Value != null) {
     94        period = PeriodParameter.Value.Value;
     95      } else {
     96        period = Math.Exp(p[c]);
     97        c++;
    11998      }
    120       if (!scaleParameter.Fixed) {
    121         scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[i])));
    122         i++;
     99      if (ScaleParameter.Value != null) {
     100        scale = ScaleParameter.Value.Value;
     101      } else {
     102        scale = Math.Exp(2 * p[c]);
     103        c++;
    123104      }
    124       if (hyp.Length != i) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriod", "hyp");
     105      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovariancePeriodic", "p");
    125106    }
    126107
    127     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    128       double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
    129       k = Math.PI * k / period;
    130       k = Math.Sin(k) * inverseLength;
    131       k = k * k;
     108    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     109      double inverseLength, period, scale;
     110      GetParameterValues(p, out scale, out period, out inverseLength);
     111      // create functions
     112      var cov = new ParameterizedCovarianceFunction();
     113      cov.Covariance = (x, i, j) => {
     114        double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
     115        k = Math.PI * k / period;
     116        k = Math.Sin(k) * inverseLength;
     117        k = k * k;
    132118
    133       return scale * Math.Exp(-2.0 * k);
     119        return scale * Math.Exp(-2.0 * k);
     120      };
     121      cov.CrossCovariance = (x, xt, i, j) => {
     122        double k = GetDistance(x, xt, i, j, columnIndices);
     123        k = Math.PI * k / period;
     124        k = Math.Sin(k) * inverseLength;
     125        k = k * k;
     126
     127        return scale * Math.Exp(-2.0 * k);
     128      };
     129      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength);
     130      return cov;
    134131    }
    135132
    136     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
     133
     134    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, double period, double inverseLength) {
    137135      double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j, columnIndices) / period;
    138136      double gradient = Math.Sin(v) * inverseLength;
     
    144142    }
    145143
    146     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    147       double k = GetDistance(x, xt, i, j, columnIndices);
    148       k = Math.PI * k / period;
    149       k = Math.Sin(k) * inverseLength;
    150       k = k * k;
    151 
    152       return scale * Math.Exp(-2.0 * k);
    153     }
    154 
    155     private double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
     144    private static double GetDistance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    156145      return Math.Sqrt(Util.SqrDist(x, i, xt, j, 1, columnIndices));
    157146    }
Note: See TracChangeset for help on using the changeset viewer.