Changeset 8562 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.cs
- Timestamp:
- 09/04/12 09:50:52 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceLinearArd.cs
r8484 r8562 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; … … 32 33 public class CovarianceLinearArd : Item, ICovarianceFunction { 33 34 [Storable] 34 private double[,] x; 35 [Storable] 36 private double[,] xt; 37 38 [Storable] 39 private double[] l; 40 public double[] Length { 35 private double[] inverseLength; 36 public double[] InverseLength { 41 37 get { 42 double[] res = new double[l.Length]; 43 Array.Copy(l, res, res.Length); 38 if (inverseLength == null) return null; 39 double[] res = new double[inverseLength.Length]; 40 Array.Copy(inverseLength, res, res.Length); 44 41 return res; 45 42 } 46 43 } 47 44 48 private double[,] k;49 private bool symmetric;50 51 52 45 public int GetNumberOfParameters(int numberOfVariables) { 53 46 return numberOfVariables; 54 47 } 48 55 49 [StorableConstructor] 56 50 protected CovarianceLinearArd(bool deserializing) : base(deserializing) { } 57 51 protected CovarianceLinearArd(CovarianceLinearArd original, Cloner cloner) 58 52 : base(original, cloner) { 59 if (original.x != null) { 60 this.x = new double[original.x.GetLength(0), original.x.GetLength(1)]; 61 Array.Copy(original.x, this.x, x.Length); 62 63 this.xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)]; 64 Array.Copy(original.xt, this.xt, xt.Length); 65 66 this.k = new double[original.k.GetLength(0), original.k.GetLength(1)]; 67 Array.Copy(original.k, this.k, k.Length); 68 this.l = new double[original.l.GetLength(0)]; 69 Array.Copy(original.l, this.l, l.Length); 70 } 71 this.symmetric = original.symmetric; 53 this.inverseLength = original.InverseLength; // array is copied in the getter 72 54 } 73 55 public CovarianceLinearArd() … … 80 62 81 63 public void SetParameter(double[] hyp) { 82 l = hyp.Select(Math.Exp).ToArray();64 inverseLength = hyp.Select(e => 1.0 / Math.Exp(e)).ToArray(); 83 65 } 84 66 85 public void SetData(double[,] x) { 86 SetData(x, x); 87 this.symmetric = true; 67 public double GetCovariance(double[,] x, int i, int j) { 68 return Util.ScalarProd(x, i, j, inverseLength); 88 69 } 89 70 90 public void SetData(double[,] x, double[,] xt) { 91 this.x = x; 92 this.xt = xt; 93 this.symmetric = false; 94 95 k = null; 71 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 72 for (int k = 0; k < inverseLength.Length; k++) { 73 yield return -2.0 * x[i, k] * x[j, k] * inverseLength[k] * inverseLength[k]; 74 } 96 75 } 97 76 98 public double GetCovariance(int i, int j) { 99 if (k == null) CalculateInnerProduct(); 100 return k[i, j]; 101 } 102 103 public double GetGradient(int i, int j, int k) { 104 105 } 106 107 108 private void CalculateInnerProduct() { 109 if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException(); 110 int rows = x.GetLength(0); 111 int cols = xt.GetLength(0); 112 k = new double[rows, cols]; 113 if (symmetric) { 114 for (int i = 0; i < rows; i++) { 115 for (int j = i; j < cols; j++) { 116 117 k[i, j] = Util.ScalarProd(Util.GetRow(x, i).Select((e, k) => e / l[k]), 118 Util.GetRow(x, j).Select((e, k) => e / l[k])); 119 k[j, i] = k[i, j]; 120 } 121 } 122 } else { 123 for (int i = 0; i < rows; i++) { 124 for (int j = 0; j < cols; j++) { 125 k[i, j] = Util.ScalarProd(Util.GetRow(x, i).Select((e, k) => e / l[k]), 126 Util.GetRow(xt, j).Select((e, k) => e / l[k])); 127 } 128 } 129 } 77 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 78 return Util.ScalarProd(x, i, xt, j, inverseLength); 130 79 } 131 80 }
Note: See TracChangeset
for help on using the changeset viewer.