Changeset 8484 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs
- Timestamp:
- 08/14/12 13:25:17 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs
r8473 r8484 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 29 30 [Item(Name = "CovariancePeriodic", Description = "Periodic covariance function for Gaussian processes.")] 30 31 public class CovariancePeriodic : Item, ICovarianceFunction { 31 [Storable]32 private double[,] x;33 [Storable]34 private double[,] xt;35 32 [Storable] 36 33 private double sf2; … … 43 40 public double Period { get { return p; } } 44 41 45 private bool symmetric;46 47 private double[,] sd;48 42 public int GetNumberOfParameters(int numberOfVariables) { 49 43 return 3; … … 53 47 protected CovariancePeriodic(CovariancePeriodic original, Cloner cloner) 54 48 : base(original, cloner) { 55 if (original.x != null) {56 x = new double[original.x.GetLength(0), original.x.GetLength(1)];57 Array.Copy(original.x, x, x.Length);58 xt = new double[original.xt.GetLength(0), original.xt.GetLength(1)];59 Array.Copy(original.xt, xt, xt.Length);60 }61 49 sf2 = original.sf2; 62 50 l = original.l; 63 51 p = original.p; 64 symmetric = original.symmetric;65 52 } 66 53 public CovariancePeriodic() … … 77 64 this.p = Math.Exp(hyp[1]); 78 65 this.sf2 = Math.Exp(2 * hyp[2]); 79 // sf2 = Math.Min(10E6, sf2); // upper limit for the scale80 81 sd = null;82 }83 public void SetData(double[,] x) {84 SetData(x, x);85 this.symmetric = true;86 66 } 87 67 88 public void SetData(double[,] x, double[,] xt) { 89 this.x = x; 90 this.xt = xt; 91 this.symmetric = false; 92 93 sd = null; 94 } 95 96 public double GetCovariance(int i, int j) { 97 if (sd == null) CalculateSquaredDistances(); 98 double k = sd[i, j]; 68 public double GetCovariance(double[,] x, int i, int j) { 69 double k = i == j ? 0.0 : GetDistance(x, x, i, j); 99 70 k = Math.PI * k / p; 100 71 k = Math.Sin(k) / l; … … 104 75 } 105 76 106 public double GetGradient(int i, int j, int k) { 107 double v = Math.PI * sd[i, j] / p; 108 switch (k) { 109 case 0: { 110 double newK = Math.Sin(v) / l; 111 newK = newK * newK; 112 return 4 * sf2 * Math.Exp(-2 * newK) * newK; 113 } 114 case 1: { 115 double r = Math.Sin(v) / l; 116 return 4 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v; 117 } 118 case 2: { 119 double newK = Math.Sin(v) / l; 120 newK = newK * newK; 121 return 2 * sf2 * Math.Exp(-2 * newK); 122 123 } 124 default: { 125 throw new ArgumentException("CovariancePeriodic only has three hyperparameters.", "k"); 126 } 127 } 77 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 78 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p; 79 double gradient = Math.Sin(v) / l; 80 gradient *= gradient; 81 yield return 4.0 * sf2 * Math.Exp(-2.0 * gradient) * gradient; 82 double r = Math.Sin(v) / l; 83 yield return 4.0 * sf2 / l * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v; 84 yield return 2.0 * sf2 * Math.Exp(-2 * gradient); 128 85 } 129 86 130 p rivate void CalculateSquaredDistances() {131 if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException();132 int rows = x.GetLength(0);133 int cols = xt.GetLength(0);134 sd = new double[rows, cols];87 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 88 double k = GetDistance(x, xt, i, j); 89 k = Math.PI * k / p; 90 k = Math.Sin(k) / l; 91 k = k * k; 135 92 136 if (symmetric) { 137 for (int i = 0; i < rows; i++) { 138 for (int j = i; j < cols; j++) { 139 sd[i, j] = Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(x, j))); 140 sd[j, i] = sd[i, j]; 141 } 142 } 143 } else { 144 for (int i = 0; i < rows; i++) { 145 for (int j = 0; j < cols; j++) { 146 sd[i, j] = Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(xt, j))); 147 } 148 } 149 } 93 return sf2 * Math.Exp(-2.0 * k); 94 } 95 96 private double GetDistance(double[,] x, double[,] xt, int i, int j) { 97 return Math.Sqrt(Util.SqrDist(Util.GetRow(x, i), Util.GetRow(xt, j))); 150 98 } 151 99 }
Note: See TracChangeset
for help on using the changeset viewer.