This simple example shows how to create a Stochastic Collocation surrogate from a regular combigrid.
#include <cmath>
#include <iostream>
#include <vector>
First we create an analytical model for comparison
and define the corresponding probability density functions.
auto pdf1 = std::make_shared<sgpp::combigrid::ProbabilityDensityFunction1D>(config);
auto pdf2 = std::make_shared<sgpp::combigrid::ProbabilityDensityFunction1D>(config);
weightFunctions.
push_back(pdf1->getWeightFunction());
weightFunctions.push_back(pdf2->getWeightFunction());
Then we create a combigrid for our model
std::shared_ptr<sgpp::combigrid::LevelManager> levelManager =
std::make_shared<sgpp::combigrid::AveragingLevelManager>();
auto op = std::make_shared<sgpp::combigrid::CombigridOperation>(grids, evaluators, levelManager,
func, false);
auto op_levelManager = op->getLevelManager();
and from that initialize a Stochastic Collocation surrogate.
std::vector<double> bounds{pdf1->lowerBound(), pdf1->upperBound(), pdf2->lowerBound(),
pdf2->upperBound()};
Finally levels are added to the combigrid and subsequently the collocation surrogate is updated and the model's mean and variance error is printed.
for (size_t q = 0; q < 8; ++q) {
op_levelManager->addRegularLevels(q);
std::cout << "---------------------------------------------------------" << std::endl;
std::cout << "compute mean and variance of stochastic collocation" << std::endl;
std::cout << "#gp = " << op_levelManager->numGridPoints() << std::endl;
sc->updateConfig(update_config);
double mean = sc->mean();
double variance = sc->variance();
std::cout <<
"|mu - E(u)| = " << std::abs(model.
mean - mean) << std::endl;
std::cout <<
"|sigma^2 - Var(u)| = " << std::abs(model.
variance - variance) << std::endl;
}
}