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

    r8933 r8982  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
     
    3334    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
    3435  public sealed class CovarianceLinearArd : ParameterizedNamedItem, ICovarianceFunction {
    35     [Storable]
    36     private double[] inverseLength;
    37     [Storable]
    38     private readonly HyperParameter<DoubleArray> inverseLengthParameter;
    3936    public IValueParameter<DoubleArray> InverseLengthParameter {
    40       get { return inverseLengthParameter; }
     37      get { return (IValueParameter<DoubleArray>)Parameters["InverseLength"]; }
    4138    }
    4239
     
    4542    private CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
    4643      : base(original, cloner) {
    47       inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
    48       if (original.inverseLength != null) {
    49         this.inverseLength = new double[original.inverseLength.Length];
    50         Array.Copy(original.inverseLength, inverseLength, inverseLength.Length);
    51       }
    52 
    53       RegisterEvents();
    5444    }
    5545    public CovarianceLinearArd()
     
    5848      Description = ItemDescription;
    5949
    60       inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength",
    61                                                                "The inverse length parameter for ARD.");
    62       Parameters.Add(inverseLengthParameter);
    63       RegisterEvents();
    64     }
    65 
    66     [StorableHook(HookType.AfterDeserialization)]
    67     private void AfterDeserialization() {
    68       RegisterEvents();
     50      Parameters.Add(new OptionalValueParameter<DoubleArray>("InverseLength",
     51                                                             "The inverse length parameter for ARD."));
    6952    }
    7053
     
    7356    }
    7457
    75     // caching
    76     private void RegisterEvents() {
    77       Util.AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
    78     }
    79 
    80 
    8158    public int GetNumberOfParameters(int numberOfVariables) {
    82       if (!inverseLengthParameter.Fixed)
     59      if (InverseLengthParameter.Value == null)
    8360        return numberOfVariables;
    8461      else
     
    8663    }
    8764
    88     public void SetParameter(double[] hyp) {
    89       if (!inverseLengthParameter.Fixed && hyp.Length > 0) {
    90         inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray()));
    91       } else throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceLinearArd", "hyp");
     65    public void SetParameter(double[] p) {
     66      double[] inverseLength;
     67      GetParameterValues(p, out inverseLength);
     68      InverseLengthParameter.Value = new DoubleArray(inverseLength);
    9269    }
    9370
    94     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    95       return Util.ScalarProd(x, i, j, inverseLength, columnIndices);
     71    private void GetParameterValues(double[] p, out double[] inverseLength) {
     72      // gather parameter values
     73      if (InverseLengthParameter.Value != null) {
     74        inverseLength = InverseLengthParameter.Value.ToArray();
     75      } else {
     76        inverseLength = p.Select(e => 1.0 / Math.Exp(e)).ToArray();
     77      }
    9678    }
    9779
    98     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
     80    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     81      double[] inverseLength;
     82      GetParameterValues(p, out inverseLength);
     83      // create functions
     84      var cov = new ParameterizedCovarianceFunction();
     85      cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices);
     86      cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
     87      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices);
     88      return cov;
     89    }
     90
     91    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double[] inverseLength, IEnumerable<int> columnIndices) {
    9992      if (columnIndices == null) columnIndices = Enumerable.Range(0, x.GetLength(1));
    10093
     
    10598      }
    10699    }
    107 
    108     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    109       return Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
    110     }
    111100  }
    112101}
Note: See TracChangeset for help on using the changeset viewer.