Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/12 11:58:17 (12 years ago)
Author:
mkommend
Message:

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs

    r8477 r8742  
    3131  [Item(Name = "CovarianceSum",
    3232    Description = "Sum covariance function for Gaussian processes.")]
    33   public class CovarianceSum : Item, ICovarianceFunction {
     33  public sealed class CovarianceSum : Item, ICovarianceFunction {
    3434    [Storable]
    3535    private ItemList<ICovarianceFunction> terms;
     
    4242
    4343    [StorableConstructor]
    44     protected CovarianceSum(bool deserializing)
     44    private CovarianceSum(bool deserializing)
    4545      : base(deserializing) {
    4646    }
    4747
    48     protected CovarianceSum(CovarianceSum original, Cloner cloner)
     48    private CovarianceSum(CovarianceSum original, Cloner cloner)
    4949      : base(original, cloner) {
    5050      this.terms = cloner.Clone(original.terms);
    5151      this.numberOfVariables = original.numberOfVariables;
    52       AttachEventHandlers();
    5352    }
    5453
     
    5655      : base() {
    5756      this.terms = new ItemList<ICovarianceFunction>();
    58       AttachEventHandlers();
    59     }
    60 
    61     private void AttachEventHandlers() {
    62       this.terms.CollectionReset += (sender, args) => ClearCache();
    63       this.terms.ItemsAdded += (sender, args) => ClearCache();
    64       this.terms.ItemsRemoved += (sender, args) => ClearCache();
    65       this.terms.ItemsReplaced += (sender, args) => ClearCache();
    66       this.terms.ItemsMoved += (sender, args) => ClearCache();
    6757    }
    6858
     
    7767
    7868    public void SetParameter(double[] hyp) {
     69      if (terms.Count == 0) throw new ArgumentException("At least one term is needed for sum covariance function.");
    7970      int offset = 0;
    8071      foreach (var t in terms) {
     
    8475      }
    8576    }
    86     public void SetData(double[,] x) {
    87       SetData(x, x);
     77
     78    public double GetCovariance(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
     79      return terms.Select(t => t.GetCovariance(x, i, j, columnIndices)).Sum();
    8880    }
    8981
    90     public void SetData(double[,] x, double[,] xt) {
    91       foreach (var t in terms) {
    92         t.SetData(x, xt);
    93       }
     82    public IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices) {
     83      return terms.Select(t => t.GetGradient(x, i, j, columnIndices)).Aggregate(Enumerable.Concat);
    9484    }
    9585
    96     public double GetCovariance(int i, int j) {
    97       return terms.Select(t => t.GetCovariance(i, j)).Sum();
    98     }
    99 
    100     private Dictionary<int, Tuple<int, int>> cachedParameterMap;
    101     public double GetGradient(int i, int j, int k) {
    102       if (cachedParameterMap == null) {
    103         CalculateParameterMap();
    104       }
    105       int ti = cachedParameterMap[k].Item1;
    106       k = cachedParameterMap[k].Item2;
    107       return terms[ti].GetGradient(i, j, k);
    108     }
    109     private void ClearCache() {
    110       cachedParameterMap = null;
    111     }
    112 
    113     private void CalculateParameterMap() {
    114       cachedParameterMap = new Dictionary<int, Tuple<int, int>>();
    115       int k = 0;
    116       for (int ti = 0; ti < terms.Count; ti++) {
    117         for (int ti_k = 0; ti_k < terms[ti].GetNumberOfParameters(numberOfVariables); ti_k++) {
    118           cachedParameterMap[k++] = Tuple.Create(ti, ti_k);
    119         }
    120       }
     86    public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j, IEnumerable<int> columnIndices) {
     87      return terms.Select(t => t.GetCrossCovariance(x, xt, i, j, columnIndices)).Sum();
    12188    }
    12289  }
Note: See TracChangeset for help on using the changeset viewer.