This simple example shows how to create a Polynomial Chaos Expansion from an adaptively refined combigrid.
import numpy as np
import pysgpp
import time
First we have to define a model to approximate.
def expModel(x):
return np.exp(-x[0] * x[1])
Then we can create a refined combigrid
def ct_to_pce():
start_time = time.time()
func = pysgpp.multiFunc(expModel)
numDims = 2
q = 6
config = pysgpp.OrthogonalPolynomialBasis1DConfiguration()
config.polyParameters.type_ = pysgpp.OrthogonalPolynomialBasisType_LEGENDRE
basisFunction = pysgpp.OrthogonalPolynomialBasis1D(config)
op = pysgpp.CombigridOperation.createExpClenshawCurtisPolynomialInterpolation(numDims, func)
op.getLevelManager().addRegularLevels(q)
op.getLevelManager().addLevelsAdaptiveByNumLevels(5)
and construct a PCE representation to easily calculate statistical features of our model.
surrogateConfig = pysgpp.CombigridSurrogateModelConfiguration()
surrogateConfig.type = pysgpp.CombigridSurrogateModelsType_POLYNOMIAL_CHAOS_EXPANSION
surrogateConfig.loadFromCombigridOperation(op)
surrogateConfig.basisFunction = basisFunction
pce = pysgpp.createCombigridSurrogateModel(surrogateConfig)
sobol_indices = pysgpp.DataVector(1)
total_indices = pysgpp.DataVector(1)
pce.getComponentSobolIndices(sobol_indices)
pce.getTotalSobolIndices(total_indices)
print("Mean: {} Variance: {}".format(pce.mean(), pce.variance()))
print("Sobol indices {}".format(sobol_indices.toString()))
print("Total Sobol indices {}".format(total_indices.toString()))
print("Sum {}\n".format(sobol_indices.sum()))
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
if __name__ == "__main__":
ct_to_pce()