Changeset 8366 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 07/31/12 11:19:24 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs
r8323 r8366 1 using System .Collections.Generic;1 using System; 2 2 using System.Linq; 3 using HeuristicLab.Common; 4 using HeuristicLab.Core; 5 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 3 6 4 7 namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess { 5 public class CovarianceSum : ICovarianceFunction { 6 private IList<ICovarianceFunction> covariances; 8 [StorableClass] 9 [Item(Name = "CovarianceSum", 10 Description = "Sum covariance function for Gaussian processes.")] 11 public class CovarianceSum : Item, ICovarianceFunction { 12 [Storable] 13 private ItemList<ICovarianceFunction> terms; 7 14 8 public int NumberOfParameters { 9 get { return covariances.Sum(c => c.NumberOfParameters); } 15 [Storable] 16 private int numberOfVariables; 17 public ItemList<ICovarianceFunction> Terms { 18 get { return terms; } 10 19 } 11 20 12 public CovarianceSum(IEnumerable<ICovarianceFunction> covariances) { 13 this.covariances = covariances.ToList(); 21 [StorableConstructor] 22 protected CovarianceSum(bool deserializing) 23 : base(deserializing) { 14 24 } 15 25 16 public void SetMatrix(double[,] x) { 17 foreach (var covariance in covariances) { 18 covariance.SetMatrix(x, x); 26 protected CovarianceSum(CovarianceSum original, Cloner cloner) 27 : base(original, cloner) { 28 this.terms = cloner.Clone(terms); 29 } 30 31 public CovarianceSum() 32 : base() { 33 } 34 35 public override IDeepCloneable Clone(Cloner cloner) { 36 return new CovarianceSum(this, cloner); 37 } 38 39 public int GetNumberOfParameters(int numberOfVariables) { 40 this.numberOfVariables = numberOfVariables; 41 return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum(); 42 } 43 44 public void SetParameter(double[] hyp, double[,] x) { 45 int offset = 0; 46 foreach (var t in terms) { 47 t.SetParameter(hyp.Skip(offset).Take(t.GetNumberOfParameters(numberOfVariables)), x); 48 offset += numberOfVariables; 19 49 } 20 50 } 21 51 22 public void SetMatrix(double[,] x, double[,] xt) {23 foreach (var covariance in covariances) {24 covariance.SetMatrix(x, xt);25 }26 }27 52 28 public void SetHyperparamter(double[] hyp) { 29 int i = 0; 30 foreach (var covariance in covariances) { 31 int n = covariance.NumberOfParameters; 32 covariance.SetHyperparamter(hyp.Skip(i).Take(n).ToArray()); 33 i += n; 34 } 53 public void SetParameter(double[] hyp, double[,] x, double[,] xt) { 54 this.l = Math.Exp(hyp[0]); 55 this.sf2 = Math.Exp(2 * hyp[1]); 56 57 this.symmetric = false; 58 this.x = x; 59 this.xt = xt; 60 sd = null; 35 61 } 36 62 37 63 public double GetCovariance(int i, int j) { 38 return covariances.Select(c => c.GetCovariance(i, j)).Sum(); 64 if (sd == null) CalculateSquaredDistances(); 65 return sf2 * Math.Exp(-sd[i, j] / 2.0); 39 66 } 40 67 41 68 42 69 public double[] GetDiagonalCovariances() { 43 return covariances 44 .Select(c => c.GetDiagonalCovariances()) 45 .Aggregate((s, d) => s.Zip(d, (a, b) => a + b).ToArray()) 46 .ToArray(); 70 if (x != xt) throw new InvalidOperationException(); 71 int rows = x.GetLength(0); 72 var sd = new double[rows]; 73 for (int i = 0; i < rows; i++) { 74 sd[i] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, i).Select(e => e / l)); 75 } 76 return sd.Select(d => sf2 * Math.Exp(-d / 2.0)).ToArray(); 47 77 } 48 78 49 public double[] GetDerivatives(int i, int j) { 50 return covariances 51 .Select(c => c.GetDerivatives(i, j)) 52 .Aggregate(Enumerable.Empty<double>(), (h0, h1) => h0.Concat(h1)) 53 .ToArray(); 79 80 public double[] GetGradient(int i, int j) { 81 var res = new double[2]; 82 res[0] = sf2 * Math.Exp(-sd[i, j] / 2.0) * sd[i, j]; 83 res[1] = 2.0 * sf2 * Math.Exp(-sd[i, j] / 2.0); 84 return res; 85 } 86 87 private void CalculateSquaredDistances() { 88 if (x.GetLength(1) != xt.GetLength(1)) throw new InvalidOperationException(); 89 int rows = x.GetLength(0); 90 int cols = xt.GetLength(0); 91 sd = new double[rows, cols]; 92 if (symmetric) { 93 for (int i = 0; i < rows; i++) { 94 for (int j = i; j < rows; j++) { 95 sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l)); 96 sd[j, i] = sd[i, j]; 97 } 98 } 99 } else { 100 for (int i = 0; i < rows; i++) { 101 for (int j = 0; j < cols; j++) { 102 sd[i, j] = Util.SqrDist(Util.GetRow(x, i).Select(e => e / l), Util.GetRow(xt, j).Select(e => e / l)); 103 } 104 } 105 } 54 106 } 55 107 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8323 r8366 138 138 139 139 // calculate means and covariances 140 double[] m = meanFunction.GetMean( );140 double[] m = meanFunction.GetMean(x); 141 141 for (int i = 0; i < n; i++) { 142 142 … … 188 188 double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)]; 189 189 for (int i = 0; i < meanGradients.Length; i++) { 190 var meanGrad = meanFunction.GetGradients(i );190 var meanGrad = meanFunction.GetGradients(i, x); 191 191 meanGradients[i] = -Util.ScalarProd(meanGrad, alpha); 192 192 } 193 193 194 194 double[] covGradients = new double[covarianceFunction.GetNumberOfParameters(nAllowedVariables)]; 195 for (int i = 0; i < n; i++) { 196 for (int j = 0; j < n; j++) { 197 var covDeriv = covarianceFunction.GetGradient(i, j); 198 for (int k = 0; k < covGradients.Length; k++) { 199 covGradients[k] += q[i, j] * covDeriv[k]; 195 if (covGradients.Length > 0) { 196 for (int i = 0; i < n; i++) { 197 for (int j = 0; j < n; j++) { 198 var covDeriv = covarianceFunction.GetGradient(i, j); 199 for (int k = 0; k < covGradients.Length; k++) { 200 covGradients[k] += q[i, j] * covDeriv[k]; 201 } 200 202 } 201 203 } 202 }203 covGradients = covGradients.Select(g => g / 2.0).ToArray();204 covGradients = covGradients.Select(g => g / 2.0).ToArray(); 205 } 204 206 205 207 return new double[] { noiseGradient } … … 246 248 covarianceFunction.SetParameter(covHyp, x, newX); 247 249 meanFunction.SetParameter(meanHyp, newX); 248 var ms = meanFunction.GetMean( );250 var ms = meanFunction.GetMean(newX); 249 251 for (int i = 0; i < newN; i++) { 250 252 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/IMeanFunction.cs
r8323 r8366 6 6 int GetNumberOfParameters(int numberOfVariables); 7 7 void SetParameter(double[] hyp, double[,] x); 8 double[] GetMean( );9 double[] GetGradients(int k );8 double[] GetMean(double[,] x); 9 double[] GetGradients(int k, double[,] x); 10 10 } 11 11 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs
r8323 r8366 34 34 } 35 35 36 public double[] GetMean( ) {36 public double[] GetMean(double[,] x) { 37 37 return Enumerable.Repeat(c, n).ToArray(); 38 38 } 39 39 40 public double[] GetGradients(int k ) {40 public double[] GetGradients(int k, double[,] x) { 41 41 if (k > 0) throw new ArgumentException(); 42 42 return Enumerable.Repeat(1.0, n).ToArray(); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanZero.cs
r8323 r8366 29 29 } 30 30 31 public double[] GetMean( ) {31 public double[] GetMean(double[,] x) { 32 32 return Enumerable.Repeat(0.0, n).ToArray(); 33 33 } 34 34 35 public double[] GetGradients(int k ) {35 public double[] GetGradients(int k, double[,] x) { 36 36 if (k > 0) throw new ArgumentException(); 37 37 return Enumerable.Repeat(0.0, n).ToArray(); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/Util.cs
r8323 r8366 24 24 return Enumerable.Range(0, cols).Select(c => x[r, c]); 25 25 } 26 public static IEnumerable<double> GetCol(double[,] x, int c) { 27 int rows = x.GetLength(0); 28 return Enumerable.Range(0, rows).Select(r => x[r, c]); 29 } 26 30 } 27 31 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
r8324 r8366 122 122 </Compile> 123 123 <Compile Include="FixedDataAnalysisAlgorithm.cs" /> 124 <Compile Include="GaussianProcess\CovarianceLinear.cs" /> 125 <Compile Include="GaussianProcess\MeanLinear.cs" /> 124 126 <Compile Include="GaussianProcess\Util.cs" /> 125 127 <Compile Include="GaussianProcess\MeanZero.cs" />
Note: See TracChangeset
for help on using the changeset viewer.