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

    r8929 r8982  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Linq.Expressions;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    6667    }
    6768
    68     public void SetParameter(double[] hyp) {
    69       if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function.");
     69    public void SetParameter(double[] p) {
    7070      int offset = 0;
    71       foreach (var t in factors) {
    72         var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
    73         t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
     71      foreach (var f in factors) {
     72        var numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
     73        f.SetParameter(p.Skip(offset).Take(numberOfParameters).ToArray());
    7474        offset += numberOfParameters;
    7575      }
    7676    }
    7777
    78     public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    79       return factors.Select(f => f.GetCovariance(x, i, j, columnIndices)).Aggregate((a, b) => a * b);
     78    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
     79      if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function.");
     80      var functions = new List<ParameterizedCovarianceFunction>();
     81      foreach (var f in factors) {
     82        int numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
     83        functions.Add(f.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
     84        p = p.Skip(numberOfParameters).ToArray();
     85      }
     86
     87
     88      var product = new ParameterizedCovarianceFunction();
     89      product.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Aggregate((a, b) => a * b);
     90      product.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Aggregate((a, b) => a * b);
     91      product.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, functions);
     92      return product;
    8093    }
    8194
    82     public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
    83       var covariances = factors.Select(f => f.GetCovariance(x, i, j, columnIndices)).ToArray();
    84       for (int ii = 0; ii < factors.Count; ii++) {
    85         foreach (var g in factors[ii].GetGradient(x, i, j, columnIndices)) {
     95    public static IEnumerable<double> GetGradient(double[,] x, int i, int j, List<ParameterizedCovarianceFunction> factorFunctions) {
     96      var covariances = factorFunctions.Select(f => f.Covariance(x, i, j)).ToArray();
     97      for (int ii = 0; ii < factorFunctions.Count; ii++) {
     98        foreach (var g in factorFunctions[ii].CovarianceGradient(x, i, j)) {
    8699          double res = g;
    87100          for (int jj = 0; jj < covariances.Length; jj++)
     
    91104      }
    92105    }
    93 
    94     public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
    95       return factors.Select(f => f.GetCrossCovariance(x, xt, i, j, columnIndices)).Aggregate((a, b) => a * b);
    96     }
    97106  }
    98107}
Note: See TracChangeset for help on using the changeset viewer.