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

    r8929 r8982  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3233    Description = "Constant covariance function for Gaussian processes.")]
    3334  public sealed class CovarianceConst : ParameterizedNamedItem, ICovarianceFunction {
    34 
    35     [Storable]
    36     private double scale;
    37     [Storable]
    38     private readonly HyperParameter<DoubleValue> scaleParameter;
    3935    public IValueParameter<DoubleValue> ScaleParameter {
    40       get { return scaleParameter; }
     36      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
    4137    }
    4238
     
    4844    private CovarianceConst(CovarianceConst original, Cloner cloner)
    4945      : base(original, cloner) {
    50       this.scaleParameter = cloner.Clone(original.scaleParameter);
    51       this.scale = original.scale;
    52 
    53       RegisterEvents();
    5446    }
    5547
     
    5951      Description = ItemDescription;
    6052
    61       scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of the constant covariance function.");
    62       Parameters.Add(scaleParameter);
    63       RegisterEvents();
     53      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of the constant covariance function."));
    6454    }
    65 
    66     [StorableHook(HookType.AfterDeserialization)]
    67     private void AfterDeserialization() {
    68       RegisterEvents();
    69     }
    70 
    71     // caching
    72     private void RegisterEvents() {
    73       Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { scale = scaleParameter.Value.Value; });
    74     }
    75 
    7655
    7756    public override IDeepCloneable Clone(Cloner cloner) {
     
    8059
    8160    public int GetNumberOfParameters(int numberOfVariables) {
    82       return scaleParameter.Fixed ? 0 : 1;
     61      return ScaleParameter.Value != null ? 0 : 1;
    8362    }
    8463
    85     public void SetParameter(double[] hyp) {
    86       if (!scaleParameter.Fixed && hyp.Length == 1) {
    87         scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
    88       } else {
    89         throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "hyp");
    90       }
     64    public void SetParameter(double[] p) {
     65      double scale;
     66      GetParameterValues(p, out scale);
     67      ScaleParameter.Value = new DoubleValue(scale);
    9168    }
    9269
    93     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    94       return scale;
     70    private void GetParameterValues(double[] p, out double scale) {
     71      int c = 0;
     72      // gather parameter values
     73      if (ScaleParameter.Value != null) {
     74        scale = ScaleParameter.Value.Value;
     75      } else {
     76        scale = Math.Exp(2 * p[c]);
     77        c++;
     78      }
     79      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceConst", "p");
    9580    }
    9681
    97     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    98       yield return 2.0 * scale;
     82    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     83      double scale;
     84      GetParameterValues(p, out scale);
     85      // create functions
     86      var cov = new ParameterizedCovarianceFunction();
     87      cov.Covariance = (x, i, j) => scale;
     88      cov.CrossCovariance = (x, xt, i, j) => scale;
     89      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices);
     90      return cov;
    9991    }
    10092
    101     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    102       return scale;
     93    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double scale, IEnumerable<int> columnIndices) {
     94      yield return 2.0 * scale;
    10395    }
    10496  }
Note: See TracChangeset for help on using the changeset viewer.