Changeset 8451 for branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
- Timestamp:
- 08/08/12 23:59:16 (12 years ago)
- Location:
- branches/HeuristicLab.Mono
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Mono
- Property svn:mergeinfo changed
-
branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis (added) merged: 8366-8368,8371-8372,8375,8396-8397,8399,8401,8403,8416-8417,8419,8421,8439,8448
- Property svn:mergeinfo changed
-
branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.user
-
- Property svn:ignore
-
branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r8323 r8451 28 28 using HeuristicLab.Problems.DataAnalysis; 29 29 30 namespace HeuristicLab.Algorithms.DataAnalysis .GaussianProcess{30 namespace HeuristicLab.Algorithms.DataAnalysis { 31 31 /// <summary> 32 32 /// Represents a Gaussian process model. … … 35 35 [Item("GaussianProcessModel", "Represents a Gaussian process posterior.")] 36 36 public sealed class GaussianProcessModel : NamedItem, IGaussianProcessModel { 37 38 37 [Storable] 39 38 private double negativeLogLikelihood; 40 41 39 public double NegativeLogLikelihood { 42 40 get { return negativeLogLikelihood; } … … 54 52 } 55 53 [Storable] 56 private double[] hyp;57 public IEnumerable<double> Hyperparameters {58 get { return hyp; }59 }60 [Storable]61 54 private string targetVariable; 62 55 public string TargetVariable { … … 73 66 [Storable] 74 67 private double sqrSigmaNoise; 75 [Storable]76 private double[] meanHyp;77 [Storable]78 private double[] covHyp;79 68 80 69 [Storable] … … 91 80 private GaussianProcessModel(GaussianProcessModel original, Cloner cloner) 92 81 : base(original, cloner) { 93 this.hyp = original.hyp;94 82 this.meanFunction = cloner.Clone(original.meanFunction); 95 83 this.covarianceFunction = cloner.Clone(original.covarianceFunction); 84 this.scaling = cloner.Clone(original.scaling); 96 85 this.negativeLogLikelihood = original.negativeLogLikelihood; 97 86 this.targetVariable = original.targetVariable; 87 this.sqrSigmaNoise = original.sqrSigmaNoise; 88 89 // shallow copies of arrays because they cannot be modified 98 90 this.allowedInputVariables = original.allowedInputVariables; 99 91 this.alpha = original.alpha; 100 this.sqrSigmaNoise = original.sqrSigmaNoise;101 this.scaling = cloner.Clone(original.scaling);102 this.meanHyp = original.meanHyp;103 this.covHyp = original.covHyp;104 92 this.l = original.l; 105 93 this.x = original.x; … … 110 98 this.name = ItemName; 111 99 this.description = ItemDescription; 112 this.hyp = hyp.ToArray(); 113 this.meanFunction = meanFunction; 114 this.covarianceFunction = covarianceFunction; 100 this.meanFunction = (IMeanFunction)meanFunction.Clone(); 101 this.covarianceFunction = (ICovarianceFunction)covarianceFunction.Clone(); 115 102 this.targetVariable = targetVariable; 116 103 this.allowedInputVariables = allowedInputVariables.ToArray(); 117 int nAllowedVariables = allowedInputVariables.Count();118 104 119 105 sqrSigmaNoise = Math.Exp(2.0 * hyp.First()); 120 106 sqrSigmaNoise = Math.Max(10E-6, sqrSigmaNoise); // lower limit for the noise level 121 meanHyp = hyp.Skip(1).Take(meanFunction.GetNumberOfParameters(nAllowedVariables)).ToArray(); 122 covHyp = hyp.Skip(1 + meanFunction.GetNumberOfParameters(nAllowedVariables)).Take(covarianceFunction.GetNumberOfParameters(nAllowedVariables)).ToArray(); 123 124 CalculateModel(ds, targetVariable, allowedInputVariables, rows); 125 } 126 127 private void CalculateModel(Dataset ds, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<int> rows) { 107 108 int nVariables = this.allowedInputVariables.Length; 109 this.meanFunction.SetParameter(hyp.Skip(1) 110 .Take(this.meanFunction.GetNumberOfParameters(nVariables)) 111 .ToArray()); 112 this.covarianceFunction.SetParameter(hyp.Skip(1 + this.meanFunction.GetNumberOfParameters(nVariables)) 113 .Take(this.covarianceFunction.GetNumberOfParameters(nVariables)) 114 .ToArray()); 115 116 CalculateModel(ds, rows); 117 } 118 119 private void CalculateModel(Dataset ds, IEnumerable<int> rows) { 128 120 scaling = new Scaling(ds, allowedInputVariables, rows); 129 121 x = AlglibUtil.PrepareAndScaleInputMatrix(ds, allowedInputVariables, rows, scaling); … … 134 126 l = new double[n, n]; 135 127 136 meanFunction.Set Parameter(meanHyp,x);137 covarianceFunction.Set Parameter(covHyp,x);128 meanFunction.SetData(x); 129 covarianceFunction.SetData(x); 138 130 139 131 // calculate means and covariances 140 double[] m = meanFunction.GetMean( );132 double[] m = meanFunction.GetMean(x); 141 133 for (int i = 0; i < n; i++) { 142 134 … … 188 180 double[] meanGradients = new double[meanFunction.GetNumberOfParameters(nAllowedVariables)]; 189 181 for (int i = 0; i < meanGradients.Length; i++) { 190 var meanGrad = meanFunction.GetGradients(i );182 var meanGrad = meanFunction.GetGradients(i, x); 191 183 meanGradients[i] = -Util.ScalarProd(meanGrad, alpha); 192 184 } 193 185 194 186 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]; 187 if (covGradients.Length > 0) { 188 for (int i = 0; i < n; i++) { 189 for (int j = 0; j < n; j++) { 190 var covDeriv = covarianceFunction.GetGradient(i, j); 191 for (int k = 0; k < covGradients.Length; k++) { 192 covGradients[k] += q[i, j] * covDeriv[k]; 193 } 200 194 } 201 195 } 202 }203 covGradients = covGradients.Select(g => g / 2.0).ToArray();196 covGradients = covGradients.Select(g => g / 2.0).ToArray(); 197 } 204 198 205 199 return new double[] { noiseGradient } … … 244 238 //kss = covarianceFunction.GetDiagonalCovariances(); 245 239 246 covarianceFunction.Set Parameter(covHyp,x, newX);247 meanFunction.Set Parameter(meanHyp,newX);248 var ms = meanFunction.GetMean( );240 covarianceFunction.SetData(x, newX); 241 meanFunction.SetData(newX); 242 var ms = meanFunction.GetMean(newX); 249 243 for (int i = 0; i < newN; i++) { 250 244 … … 267 261 268 262 } 269 270 #region events271 public event EventHandler Changed;272 private void OnChanged(EventArgs e) {273 var handlers = Changed;274 if (handlers != null)275 handlers(this, e);276 }277 #endregion278 263 } 279 264 }
Note: See TracChangeset
for help on using the changeset viewer.