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

    r8929 r8982  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
     
    3234    Description = "Noise covariance function for Gaussian processes.")]
    3335  public sealed class CovarianceNoise : ParameterizedNamedItem, ICovarianceFunction {
    34 
    35 
    36     [Storable]
    37     private double sf2;
    38     [Storable]
    39     private readonly HyperParameter<DoubleValue> scaleParameter;
    4036    public IValueParameter<DoubleValue> ScaleParameter {
    41       get { return scaleParameter; }
     37      get { return (IValueParameter<DoubleValue>)Parameters["Scale"]; }
    4238    }
    4339
     
    4945    private CovarianceNoise(CovarianceNoise original, Cloner cloner)
    5046      : base(original, cloner) {
    51       this.scaleParameter = cloner.Clone(original.scaleParameter);
    52       this.sf2 = original.sf2;
    53       RegisterEvents();
    5447    }
    5548
     
    5952      Description = ItemDescription;
    6053
    61       this.scaleParameter = new HyperParameter<DoubleValue>("Scale", "The scale of noise.");
    62       Parameters.Add(this.scaleParameter);
    63 
    64       RegisterEvents();
     54      Parameters.Add(new OptionalValueParameter<DoubleValue>("Scale", "The scale of noise."));
    6555    }
    6656
     
    6959    }
    7060
    71     [StorableHook(HookType.AfterDeserialization)]
    72     private void AfterDeserialization() {
    73       RegisterEvents();
     61    public int GetNumberOfParameters(int numberOfVariables) {
     62      return ScaleParameter.Value != null ? 0 : 1;
    7463    }
    7564
    76     private void RegisterEvents() {
    77       Util.AttachValueChangeHandler<DoubleValue, double>(scaleParameter, () => { sf2 = scaleParameter.Value.Value; });
     65    public void SetParameter(double[] p) {
     66      double scale;
     67      GetParameterValues(p, out scale);
     68      ScaleParameter.Value = new DoubleValue(scale);
    7869    }
    7970
    80     public int GetNumberOfParameters(int numberOfVariables) {
    81       return scaleParameter.Fixed ? 0 : 1;
     71    private void GetParameterValues(double[] p, out double scale) {
     72      int c = 0;
     73      // gather parameter values
     74      if (ScaleParameter.Value != null) {
     75        scale = ScaleParameter.Value.Value;
     76      } else {
     77        scale = Math.Exp(2 * p[c]);
     78        c++;
     79      }
     80      if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNoise", "p");
    8281    }
    8382
    84     public void SetParameter(double[] hyp) {
    85       if (!scaleParameter.Fixed) {
    86         scaleParameter.SetValue(new DoubleValue(Math.Exp(2 * hyp[0])));
    87       } else {
    88         if (hyp.Length > 0) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceNoise", "hyp");
    89       }
    90     }
    91 
    92     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    93       return i == j ? sf2 : 0.0;
    94     }
    95 
    96     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    97       yield return i == j ? 2 * sf2 : 0.0;
    98     }
    99 
    100     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    101       return 0.0;
     83    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     84      double scale;
     85      GetParameterValues(p, out scale);
     86      // create functions
     87      var cov = new ParameterizedCovarianceFunction();
     88      cov.Covariance = (x, i, j) => i == j ? scale : 0.0;
     89      cov.CrossCovariance = (x, xt, i, j) => 0.0;
     90      cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1);
     91      return cov;
    10292    }
    10393  }
Note: See TracChangeset for help on using the changeset viewer.