Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/05/12 17:04:30 (12 years ago)
Author:
gkronber
Message:

#1902 implemented a few covariance functions as parameterized named items. Implemented rudimentary view for Gaussian process models.

File:
1 edited

Legend:

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

    r8562 r8582  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3132  [Item(Name = "CovarianceLinearArd",
    3233    Description = "Linear covariance function with automatic relevance determination for Gaussian processes.")]
    33   public class CovarianceLinearArd : Item, ICovarianceFunction {
     34  public class CovarianceLinearArd : CovarianceFunction {
     35    public IValueParameter<DoubleArray> InverseLengthParameter {
     36      get { return inverseLengthParameter; }
     37    }
     38
     39    [Storable]
     40    private HyperParameter<DoubleArray> inverseLengthParameter;
     41
    3442    [Storable]
    3543    private double[] inverseLength;
    36     public double[] InverseLength {
    37       get {
    38         if (inverseLength == null) return null;
    39         double[] res = new double[inverseLength.Length];
    40         Array.Copy(inverseLength, res, res.Length);
    41         return res;
    42       }
    43     }
    44 
    45     public int GetNumberOfParameters(int numberOfVariables) {
    46       return numberOfVariables;
    47     }
    4844
    4945    [StorableConstructor]
     
    5147    protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner)
    5248      : base(original, cloner) {
    53       this.inverseLength = original.InverseLength;  // array is copied in the getter
     49      inverseLengthParameter = cloner.Clone(original.inverseLengthParameter);
     50      if (original.inverseLength != null) {
     51        this.inverseLength = new double[original.inverseLength.Length];
     52        Array.Copy(original.inverseLength, inverseLength, inverseLength.Length);
     53      }
     54
     55      RegisterEvents();
    5456    }
    5557    public CovarianceLinearArd()
    5658      : base() {
     59      inverseLengthParameter = new HyperParameter<DoubleArray>("InverseLength",
     60                                                               "The inverse length parameter for ARD.");
     61      Parameters.Add(inverseLengthParameter);
     62      RegisterEvents();
     63    }
     64
     65    [StorableHook(HookType.AfterDeserialization)]
     66    private void AfterDeserialization() {
     67      RegisterEvents();
    5768    }
    5869
     
    6172    }
    6273
    63     public void SetParameter(double[] hyp) {
    64       inverseLength = hyp.Select(e => 1.0 / Math.Exp(e)).ToArray();
     74    // caching
     75    private void RegisterEvents() {
     76      AttachArrayChangeHandler<DoubleArray, double>(inverseLengthParameter, () => { inverseLength = inverseLengthParameter.Value.ToArray(); });
    6577    }
    6678
    67     public double GetCovariance(double[,] x, int i, int j) {
     79
     80    public override int GetNumberOfParameters(int numberOfVariables) {
     81      if (!inverseLengthParameter.Fixed)
     82        return numberOfVariables;
     83      else
     84        return 0;
     85    }
     86
     87    public override void SetParameter(double[] hyp) {
     88      if (!inverseLengthParameter.Fixed && hyp.Length > 0) {
     89        inverseLengthParameter.SetValue(new DoubleArray(hyp.Select(e => 1.0 / Math.Exp(e)).ToArray()));
     90      } else throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceLinearArd", "hyp");
     91    }
     92
     93    public override double GetCovariance(double[,] x, int i, int j) {
    6894      return Util.ScalarProd(x, i, j, inverseLength);
    6995    }
    7096
    71     public IEnumerable<double> GetGradient(double[,] x, int i, int j) {
     97    public override IEnumerable<double> GetGradient(double[,] x, int i, int j) {
    7298      for (int k = 0; k < inverseLength.Length; k++) {
    7399        yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k];
     
    75101    }
    76102
    77     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
     103    public override double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) {
    78104      return Util.ScalarProd(x, i, xt, j, inverseLength);
    79105    }
Note: See TracChangeset for help on using the changeset viewer.