Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/12 19:02:47 (12 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/CovarianceSquaredExponentialIso.cs

    r8929 r8982  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq.Expressions;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
     
    3234    Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
    3335  public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
    34     [Storable]
    35     private double sf2;
    36     [Storable]
    37     private readonly HyperParameter<DoubleValue> scaleParameter;
    38     public IValueParameter<DoubleValue> ScaleParameter { get { return scaleParameter; } }
     36    public IValueParameter<DoubleValue> ScaleParameter {
     37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
     38    }
    3939
    40     [Storable]
    41     private double inverseLength;
    42     [Storable]
    43     private readonly HyperParameter<DoubleValue> inverseLengthParameter;
    44     public IValueParameter<DoubleValue> InverseLengthParameter { get { return inverseLengthParameter; } }
     40    public IValueParameter<DoubleValue> InverseLengthParameter {
     41      get { return (IValueParameter<DoubleValue>)Parameters["InverseLength"]; }
     42    }
    4543
    4644    [StorableConstructor]
     
    5149    private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
    5250      : base(original, cloner) {
    53       this.sf2 = original.sf2;
    54       this.scaleParameter = cloner.Clone(original.scaleParameter);
    55 
    56       this.inverseLength = original.inverseLength;
    57       this.inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
    58 
    59       RegisterEvents();
    6051    }
    6152
     
    6556      Description = ItemDescription;
    6657
    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();
     58      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale parameter of the isometric squared exponential covariance function."));
     59      Parameters.Add(new OptionalValueParameter<DoubleValue>("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function."));
    7460    }
    7561
     
    7864    }
    7965
    80     [StorableHook(HookType.AfterDeserialization)]
    81     private void AfterDeserialization() {
    82       RegisterEvents();
     66    public int GetNumberOfParameters(int numberOfVariables) {
     67      return
     68        (ScaleParameter.Value != null ? 0 : 1) +
     69        (InverseLengthParameter.Value != null ? 0 : 1);
    8370    }
    8471
    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 
    90     public int GetNumberOfParameters(int numberOfVariables) {
    91       return
    92         (scaleParameter.Fixed ? 0 : 1) +
    93         (inverseLengthParameter.Fixed ? 0 : 1);
    94     }
    95 
    96     public void SetParameter(double[] hyp) {
    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 CovarianceSquaredExponentialIso", "hyp");
     72    public void SetParameter(double[] p) {
     73      double scale, inverseLength;
     74      GetParameterValues(p, out scale, out inverseLength);
     75      ScaleParameter.Value = new DoubleValue(scale);
     76      InverseLengthParameter.Value = new DoubleValue(inverseLength);
    10777    }
    10878
    10979
    110     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    111       double d = i == j
    112                    ? 0.0
    113                    : Util.SqrDist(x, i, j, inverseLength, columnIndices);
    114       return sf2 * Math.Exp(-d / 2.0);
     80    private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
     81      // gather parameter values
     82      int c = 0;
     83      if (InverseLengthParameter.Value != null) {
     84        inverseLength = InverseLengthParameter.Value.Value;
     85      } else {
     86        inverseLength = 1.0 / Math.Exp(p[c]);
     87        c++;
     88      }
     89
     90      if (ScaleParameter.Value != null) {
     91        scale = ScaleParameter.Value.Value;
     92      } else {
     93        scale = Math.Exp(2 * p[c]);
     94        c++;
     95      }
     96      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "p");
    11597    }
    11698
    117     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
     99    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     100      double inverseLength, scale;
     101      GetParameterValues(p, out scale, out inverseLength);
     102      // create functions
     103      var cov = new ParameterizedCovarianceFunction();
     104      cov.Covariance = (x, i, j) => {
     105        double d = i == j
     106                ? 0.0
     107                : Util.SqrDist(x, i, j, inverseLength, columnIndices);
     108        return scale * Math.Exp(-d / 2.0);
     109      };
     110      cov.CrossCovariance = (x, xt, i, j) => {
     111        double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
     112        return scale * Math.Exp(-d / 2.0);
     113      };
     114      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices);
     115      return cov;
     116    }
     117
     118    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, IEnumerable<int> columnIndices) {
    118119      double d = i == j
    119120                   ? 0.0
     
    123124      yield return 2.0 * sf2 * g;
    124125    }
    125 
    126     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    127       double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
    128       return sf2 * Math.Exp(-d / 2.0);
    129     }
    130126  }
    131127}
Note: See TracChangeset for help on using the changeset viewer.