Changeset 8742 for branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs
- Timestamp:
- 10/05/12 11:58:17 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs
r8477 r8742 31 31 [Item(Name = "CovarianceSum", 32 32 Description = "Sum covariance function for Gaussian processes.")] 33 public class CovarianceSum : Item, ICovarianceFunction {33 public sealed class CovarianceSum : Item, ICovarianceFunction { 34 34 [Storable] 35 35 private ItemList<ICovarianceFunction> terms; … … 42 42 43 43 [StorableConstructor] 44 pr otectedCovarianceSum(bool deserializing)44 private CovarianceSum(bool deserializing) 45 45 : base(deserializing) { 46 46 } 47 47 48 pr otectedCovarianceSum(CovarianceSum original, Cloner cloner)48 private CovarianceSum(CovarianceSum original, Cloner cloner) 49 49 : base(original, cloner) { 50 50 this.terms = cloner.Clone(original.terms); 51 51 this.numberOfVariables = original.numberOfVariables; 52 AttachEventHandlers();53 52 } 54 53 … … 56 55 : base() { 57 56 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();67 57 } 68 58 … … 77 67 78 68 public void SetParameter(double[] hyp) { 69 if (terms.Count == 0) throw new ArgumentException("At least one term is needed for sum covariance function."); 79 70 int offset = 0; 80 71 foreach (var t in terms) { … … 84 75 } 85 76 } 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(); 88 80 } 89 81 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); 94 84 } 95 85 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(); 121 88 } 122 89 }
Note: See TracChangeset
for help on using the changeset viewer.