Changeset 8491 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 08/14/12 17:10:08 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovariancePeriodic.cs
r8484 r8491 34 34 public double Scale { get { return sf2; } } 35 35 [Storable] 36 private double l;37 public double Length { get { return l; } }36 private double inverseLength; 37 public double InverseLength { get { return inverseLength; } } 38 38 [Storable] 39 39 private double p; … … 48 48 : base(original, cloner) { 49 49 sf2 = original.sf2; 50 l = original.l;50 inverseLength = original.inverseLength; 51 51 p = original.p; 52 52 } … … 61 61 public void SetParameter(double[] hyp) { 62 62 if (hyp.Length != 3) throw new ArgumentException(); 63 this. l =Math.Exp(hyp[0]);63 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 64 64 this.p = Math.Exp(hyp[1]); 65 65 this.sf2 = Math.Exp(2 * hyp[2]); … … 69 69 double k = i == j ? 0.0 : GetDistance(x, x, i, j); 70 70 k = Math.PI * k / p; 71 k = Math.Sin(k) / l;71 k = Math.Sin(k) * inverseLength; 72 72 k = k * k; 73 73 … … 77 77 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 78 78 double v = i == j ? 0.0 : Math.PI * GetDistance(x, x, i, j) / p; 79 double gradient = Math.Sin(v) / l;79 double gradient = Math.Sin(v) * inverseLength; 80 80 gradient *= gradient; 81 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;82 double r = Math.Sin(v) * inverseLength; 83 yield return 4.0 * sf2 * inverseLength * Math.Exp(-2 * r * r) * r * Math.Cos(v) * v; 84 84 yield return 2.0 * sf2 * Math.Exp(-2 * gradient); 85 85 } … … 88 88 double k = GetDistance(x, xt, i, j); 89 89 k = Math.PI * k / p; 90 k = Math.Sin(k) / l;90 k = Math.Sin(k) * inverseLength; 91 91 k = k * k; 92 92 … … 95 95 96 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)));97 return Math.Sqrt(Util.SqrDist(x, i, xt, j)); 98 98 } 99 99 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceRQiso.cs
r8484 r8491 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 36 35 public double Scale { get { return sf2; } } 37 36 [Storable] 38 private double l;39 public double Length { get { return l; } }37 private double inverseLength; 38 public double InverseLength { get { return inverseLength; } } 40 39 [Storable] 41 40 private double alpha; … … 50 49 : base(original, cloner) { 51 50 this.sf2 = original.sf2; 52 this. l = original.l;51 this.inverseLength = original.inverseLength; 53 52 this.alpha = original.alpha; 54 53 } … … 68 67 public void SetParameter(double[] hyp) { 69 68 if (hyp.Length != 3) throw new ArgumentException("CovarianceRQiso has three hyperparameters", "k"); 70 this. l =Math.Exp(hyp[0]);69 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 71 70 this.sf2 = Math.Exp(2 * hyp[1]); 72 71 this.alpha = Math.Exp(hyp[2]); … … 75 74 76 75 public double GetCovariance(double[,] x, int i, int j) { 77 double lInv = 1.0 / l;78 76 double d = i == j 79 77 ? 0.0 80 : Util.SqrDist( Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));78 : Util.SqrDist(x, i, j, inverseLength); 81 79 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha); 82 80 } 83 81 84 82 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 85 double lInv = 1.0 / l;86 83 double d = i == j 87 84 ? 0.0 88 : Util.SqrDist( Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));85 : Util.SqrDist(x, i, j, inverseLength); 89 86 90 87 double b = 1 + 0.5 * d / alpha; … … 95 92 96 93 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 97 double lInv = 1.0 / l; 98 double d = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv)); 94 double d = Util.SqrDist(x, i, xt, j, inverseLength); 99 95 return sf2 * Math.Pow(1 + 0.5 * d / alpha, -alpha); 100 96 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEard.cs
r8489 r8491 36 36 37 37 [Storable] 38 private double[] l;38 private double[] inverseLength; 39 39 public double[] Length { 40 40 get { 41 if ( l== null) return new double[0];42 var copy = new double[ l.Length];43 Array.Copy( l, copy, copy.Length);41 if (inverseLength == null) return new double[0]; 42 var copy = new double[inverseLength.Length]; 43 Array.Copy(inverseLength, copy, copy.Length); 44 44 return copy; 45 45 } … … 53 53 protected CovarianceSEard(CovarianceSEard original, Cloner cloner) 54 54 : base(original, cloner) { 55 if (original. l!= null) {56 this. l = new double[original.l.Length];57 Array.Copy(original. l, this.l, l.Length);55 if (original.inverseLength != null) { 56 this.inverseLength = new double[original.inverseLength.Length]; 57 Array.Copy(original.inverseLength, this.inverseLength, inverseLength.Length); 58 58 } 59 59 this.sf2 = original.sf2; … … 68 68 69 69 public void SetParameter(double[] hyp) { 70 this. l = hyp.Take(hyp.Length - 1).Select(Math.Exp).ToArray();70 this.inverseLength = hyp.Take(hyp.Length - 1).Select(p => 1.0 / Math.Exp(p)).ToArray(); 71 71 this.sf2 = Math.Exp(2 * hyp[hyp.Length - 1]); 72 72 } … … 75 75 double d = i == j 76 76 ? 0.0 77 : Util.SqrDist(Util.GetRow(x, i).Select((e, k) => e / l[k]), 78 Util.GetRow(x, j).Select((e, k) => e / l[k])); 77 : Util.SqrDist(x, i, j, inverseLength); 79 78 return sf2 * Math.Exp(-d / 2.0); 80 79 } … … 83 82 double d = i == j 84 83 ? 0.0 85 : Util.SqrDist(Util.GetRow(x, i).Select((e, ii) => e / l[ii]), 86 Util.GetRow(x, j).Select((e, ii) => e / l[ii])); 84 : Util.SqrDist(x, i, j, inverseLength); 87 85 88 for (int ii = 0; ii < l.Length; ii++) {89 double sqrDist = Util.SqrDist(x[i, ii] / l[ii], x[j, ii] / l[ii]);86 for (int ii = 0; ii < inverseLength.Length; ii++) { 87 double sqrDist = Util.SqrDist(x[i, ii] * inverseLength[ii], x[j, ii] * inverseLength[ii]); 90 88 yield return sf2 * Math.Exp(-d / 2.0) * sqrDist; 91 89 } … … 94 92 95 93 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 96 double d = Util.SqrDist( Util.GetRow(x, i).Select((e, k) => e / l[k]), Util.GetRow(xt, j).Select((e, k) => e / l[k]));94 double d = Util.SqrDist(x, i, xt, j, inverseLength); 97 95 return sf2 * Math.Exp(-d / 2.0); 98 96 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSEiso.cs
r8484 r8491 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 36 35 public double Scale { get { return sf2; } } 37 36 [Storable] 38 private double l;39 public double Length { get { return l; } }37 private double inverseLength; 38 public double InverseLength { get { return inverseLength; } } 40 39 41 40 [StorableConstructor] … … 47 46 : base(original, cloner) { 48 47 this.sf2 = original.sf2; 49 this. l = original.l;48 this.inverseLength = original.inverseLength; 50 49 } 51 50 … … 64 63 public void SetParameter(double[] hyp) { 65 64 if (hyp.Length != 2) throw new ArgumentException("CovarianceSEiso has two hyperparameters", "k"); 66 this. l =Math.Exp(hyp[0]);65 this.inverseLength = 1.0 / Math.Exp(hyp[0]); 67 66 this.sf2 = Math.Exp(2 * hyp[1]); 68 67 } … … 70 69 71 70 public double GetCovariance(double[,] x, int i, int j) { 72 double lInv = 1.0 / l;73 71 double d = i == j 74 72 ? 0.0 75 : Util.SqrDist( Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));73 : Util.SqrDist(x, i, j, inverseLength); 76 74 return sf2 * Math.Exp(-d / 2.0); 77 75 } 78 76 79 77 public IEnumerable<double> GetGradient(double[,] x, int i, int j) { 80 double lInv = 1.0 / l;81 78 double d = i == j 82 79 ? 0.0 83 : Util.SqrDist( Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(x, j).Select(e => e * lInv));80 : Util.SqrDist(x, i, j, inverseLength); 84 81 double g = Math.Exp(-d / 2.0); 85 82 yield return sf2 * g * d; … … 88 85 89 86 public double GetCrossCovariance(double[,] x, double[,] xt, int i, int j) { 90 double lInv = 1.0 / l; 91 double d = Util.SqrDist(Util.GetRow(x, i).Select(e => e * lInv), Util.GetRow(xt, j).Select(e => e * lInv)); 87 double d = Util.SqrDist(x, i, xt, j, inverseLength); 92 88 return sf2 * Math.Exp(-d / 2.0); 93 89 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r8463 r8491 38 38 } 39 39 40 public static double SqrDist(double[,] x, int i, int j, double scale = 1.0) { 41 return SqrDist(x, i, x, j, scale); 42 } 43 44 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double scale = 1.0) { 45 double ss = 0.0; 46 for (int k = 0; k < x.GetLength(1); k++) { 47 double d = x[i, k] - xt[j, k]; 48 ss += d * d; 49 } 50 return scale * scale * ss; 51 } 52 public static double SqrDist(double[,] x, int i, int j, double[] scale) { 53 return SqrDist(x, i, x, j, scale); 54 } 55 56 public static double SqrDist(double[,] x, int i, double[,] xt, int j, double[] scale) { 57 double ss = 0.0; 58 for (int k = 0; k < x.GetLength(1); k++) { 59 double d = x[i, k] - xt[j, k]; 60 ss += d * d * scale[k] * scale[k]; 61 } 62 return ss; 63 } 64 40 65 public static IEnumerable<double> GetRow(double[,] x, int r) { 41 66 int cols = x.GetLength(1);
Note: See TracChangeset
for help on using the changeset viewer.