Changeset 13160 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
- Timestamp:
- 11/15/15 13:54:43 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessModel.cs
r13121 r13160 165 165 .ToArray(); 166 166 sqrSigmaNoise = Math.Exp(2.0 * hyp.Last()); 167 CalculateModel(ds, rows, scaleInputs); 167 try { 168 CalculateModel(ds, rows, scaleInputs); 169 } catch (alglib.alglibexception ae) { 170 // wrap exception so that calling code doesn't have to know about alglib implementation 171 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); 172 } 168 173 } 169 174 … … 306 311 307 312 private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) { 308 if (x == null) { 309 x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling); 310 } 311 int n = x.GetLength(0); 312 313 double[,] newX = GetData(dataset, allowedInputVariables, rows, inputScaling); 314 int newN = newX.GetLength(0); 315 316 var Ks = new double[newN, n]; 317 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1))); 318 var ms = Enumerable.Range(0, newX.GetLength(0)) 319 .Select(r => mean.Mean(newX, r)) 320 .ToArray(); 321 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1))); 322 for (int i = 0; i < newN; i++) { 323 for (int j = 0; j < n; j++) { 324 Ks[i, j] = cov.CrossCovariance(x, newX, j, i); 325 } 326 } 327 328 return Enumerable.Range(0, newN) 329 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 313 try { 314 if (x == null) { 315 x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling); 316 } 317 int n = x.GetLength(0); 318 319 double[,] newX = GetData(dataset, allowedInputVariables, rows, inputScaling); 320 int newN = newX.GetLength(0); 321 322 var Ks = new double[newN, n]; 323 var mean = meanFunction.GetParameterizedMeanFunction(meanParameter, Enumerable.Range(0, newX.GetLength(1))); 324 var ms = Enumerable.Range(0, newX.GetLength(0)) 325 .Select(r => mean.Mean(newX, r)) 326 .ToArray(); 327 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, newX.GetLength(1))); 328 for (int i = 0; i < newN; i++) { 329 for (int j = 0; j < n; j++) { 330 Ks[i, j] = cov.CrossCovariance(x, newX, j, i); 331 } 332 } 333 334 return Enumerable.Range(0, newN) 335 .Select(i => ms[i] + Util.ScalarProd(Util.GetRow(Ks, i), alpha)); 336 } catch (alglib.alglibexception ae) { 337 // wrap exception so that calling code doesn't have to know about alglib implementation 338 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); 339 } 330 340 } 331 341 332 342 public IEnumerable<double> GetEstimatedVariance(IDataset dataset, IEnumerable<int> rows) { 333 if (x == null) { 334 x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling); 335 } 336 int n = x.GetLength(0); 337 338 var newX = GetData(dataset, allowedInputVariables, rows, inputScaling); 339 int newN = newX.GetLength(0); 340 341 var kss = new double[newN]; 342 double[,] sWKs = new double[n, newN]; 343 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1))); 344 345 if (l == null) { 346 l = CalculateL(x, cov, sqrSigmaNoise); 347 } 348 349 // for stddev 350 for (int i = 0; i < newN; i++) 351 kss[i] = cov.Covariance(newX, i, i); 352 353 for (int i = 0; i < newN; i++) { 354 for (int j = 0; j < n; j++) { 355 sWKs[j, i] = cov.CrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise); 356 } 357 } 358 359 // for stddev 360 alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0); 361 362 for (int i = 0; i < newN; i++) { 363 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 364 kss[i] += sqrSigmaNoise; // kss is V(f), add noise variance of predictive distibution to get V(y) 365 kss[i] -= sumV; 366 if (kss[i] < 0) kss[i] = 0; 367 } 368 return kss; 343 try { 344 if (x == null) { 345 x = GetData(trainingDataset, allowedInputVariables, trainingRows, inputScaling); 346 } 347 int n = x.GetLength(0); 348 349 var newX = GetData(dataset, allowedInputVariables, rows, inputScaling); 350 int newN = newX.GetLength(0); 351 352 var kss = new double[newN]; 353 double[,] sWKs = new double[n, newN]; 354 var cov = covarianceFunction.GetParameterizedCovarianceFunction(covarianceParameter, Enumerable.Range(0, x.GetLength(1))); 355 356 if (l == null) { 357 l = CalculateL(x, cov, sqrSigmaNoise); 358 } 359 360 // for stddev 361 for (int i = 0; i < newN; i++) 362 kss[i] = cov.Covariance(newX, i, i); 363 364 for (int i = 0; i < newN; i++) { 365 for (int j = 0; j < n; j++) { 366 sWKs[j, i] = cov.CrossCovariance(x, newX, j, i) / Math.Sqrt(sqrSigmaNoise); 367 } 368 } 369 370 // for stddev 371 alglib.ablas.rmatrixlefttrsm(n, newN, l, 0, 0, false, false, 0, ref sWKs, 0, 0); 372 373 for (int i = 0; i < newN; i++) { 374 var sumV = Util.ScalarProd(Util.GetCol(sWKs, i), Util.GetCol(sWKs, i)); 375 kss[i] += sqrSigmaNoise; // kss is V(f), add noise variance of predictive distibution to get V(y) 376 kss[i] -= sumV; 377 if (kss[i] < 0) kss[i] = 0; 378 } 379 return kss; 380 } catch (alglib.alglibexception ae) { 381 // wrap exception so that calling code doesn't have to know about alglib implementation 382 throw new ArgumentException("There was a problem in the calculation of the Gaussian process model", ae); 383 } 369 384 } 370 385 }
Note: See TracChangeset
for help on using the changeset viewer.