SG++-Doxygen-Documentation
PCE with Combigrids (Python)

This simple example shows how to create a Polynomial Chaos Expansion from an adaptively refined combigrid.

1 import numpy as np
2 import pysgpp
3 import time

First we have to define a model to approximate.

1 def expModel(x):
2  return np.exp(-x[0] * x[1])

Then we can create a refined combigrid

1 def ct_to_pce():
2  start_time = time.time()
3  # initialize model function
4  func = pysgpp.multiFunc(expModel)
5  numDims = 2
6  # regular sparse grid level q
7  q = 6
8  # create polynomial basis
9  config = pysgpp.OrthogonalPolynomialBasis1DConfiguration()
10  config.polyParameters.type_ = pysgpp.OrthogonalPolynomialBasisType_LEGENDRE
11  basisFunction = pysgpp.OrthogonalPolynomialBasis1D(config)
12  # create sprarse grid interpolation operation
13  op = pysgpp.CombigridOperation.createExpClenshawCurtisPolynomialInterpolation(numDims, func)
14  # start with regular level q and add some levels adaptively
15  op.getLevelManager().addRegularLevels(q)
16  op.getLevelManager().addLevelsAdaptiveByNumLevels(5)
17 

and construct a PCE representation to easily calculate statistical features of our model.

1  # create polynomial chaos surrogate from sparse grid
2  surrogateConfig = pysgpp.CombigridSurrogateModelConfiguration()
3  surrogateConfig.type = pysgpp.CombigridSurrogateModelsType_POLYNOMIAL_CHAOS_EXPANSION
4  surrogateConfig.loadFromCombigridOperation(op)
5  surrogateConfig.basisFunction = basisFunction
6  pce = pysgpp.createCombigridSurrogateModel(surrogateConfig)
7  # compute sobol indices
8  sobol_indices = pysgpp.DataVector(1)
9  total_indices = pysgpp.DataVector(1)
10  pce.getComponentSobolIndices(sobol_indices)
11  pce.getTotalSobolIndices(total_indices)
12  # print results
13  print("Mean: {} Variance: {}".format(pce.mean(), pce.variance()))
14  print("Sobol indices {}".format(sobol_indices.toString()))
15  print("Total Sobol indices {}".format(total_indices.toString()))
16  print("Sum {}\n".format(sobol_indices.sum()))
17 
18  print("Elapsed time: {} s".format(time.time() - start_time))

Output:

 Mean: 0.796599599298 Variance: 0.0250607565267
 Sobol indices [4.46123454530588048339e-01, 4.46123454530588825495e-01, 1.07753090938823264944e-01]
 Total Sobol indices [5.53876545469411341038e-01, 5.53876545469412118194e-01]
 Sum 1.0
1 if __name__ == "__main__":
2  ct_to_pce()
3 
4