9 namespace pt = boost::property_tree;
15 ExpensiveSamplingProblem::ExpensiveSamplingProblem(std::shared_ptr<muq::Modeling::ModPiece>
const& target, std::shared_ptr<muq::Modeling::ModPiece>
const& qoi, Eigen::VectorXd
const& centroid, pt::ptree pt) :
SamplingProblem(target, qoi), centroid(centroid) {
17 const std::string regOptions = pt.get<std::string>(
"RegressionOptions");
18 reg = std::make_shared<LocalRegression>(target, pt.get_child(regOptions));
20 regQoI = std::make_shared<LocalRegression>(qoi, pt.get_child(pt.get<std::string>(
"QoIRegressionOptions", regOptions)));
27 ExpensiveSamplingProblem::ExpensiveSamplingProblem(std::shared_ptr<muq::Modeling::ModPiece>
const& target, std::shared_ptr<muq::Modeling::ModPiece>
const& qoi, pt::ptree pt) :
SamplingProblem(target, qoi), centroid(Eigen::VectorXd::Zero(target->inputSizes(0))) {
29 const std::string regOptions = pt.get<std::string>(
"RegressionOptions");
30 reg = std::make_shared<LocalRegression>(target, pt.get_child(regOptions));
32 regQoI = std::make_shared<LocalRegression>(qoi, pt.get_child(pt.get<std::string>(
"QoIRegressionOptions", regOptions)));
38 ExpensiveSamplingProblem::ExpensiveSamplingProblem(std::shared_ptr<muq::Modeling::ModPiece>
const& target, pt::ptree pt) :
SamplingProblem(target), centroid(Eigen::VectorXd::Zero(target->inputSizes(0))) {
40 reg = std::make_shared<LocalRegression>(target, pt.get_child(pt.get<std::string>(
"RegressionOptions")));
46 ExpensiveSamplingProblem::ExpensiveSamplingProblem(std::shared_ptr<muq::Modeling::ModPiece>
const& target, Eigen::VectorXd
const& centroid, pt::ptree pt) :
SamplingProblem(target), centroid(centroid) {
48 reg = std::make_shared<LocalRegression>(target, pt.get_child(pt.get<std::string>(
"RegressionOptions")));
55 ExpensiveSamplingProblem::ExpensiveSamplingProblem(std::shared_ptr<muq::Modeling::ModPiece>
const& target, boost::property_tree::ptree pt, std::shared_ptr<parcer::Communicator> comm) :
SamplingProblem(target), centroid(Eigen::VectorXd::Zero(target->inputSizes(0))) {
57 reg = std::make_shared<LocalRegression>(
target, pt.get_child(pt.get<std::string>(
"RegressionOptions")), comm);
65 reg = std::make_shared<LocalRegression>(
target, pt.get_child(pt.get<std::string>(
"RegressionOptions")), comm);
76 assert(
target->numInputs==1);
78 beta = std::pair<double, double>(pt.get<
double>(
"BetaScale", 0.0), -pt.get<
double>(
"BetaExponent", RAND_MAX));
79 assert(
beta.second<0.0);
81 tau0 = pt.get<
double>(
"FirstLevelLength", 1.0);
83 gamma = std::pair<double, double>(std::log(pt.get<
double>(
"GammaScale", 1.0)), pt.get<
double>(
"GammaExponent", 1.0));
84 assert(
gamma.second>0.0);
86 lyapunovPara.first = pt.get<
double>(
"LyapunovScale", 1.0);
87 lyapunovPara.second = pt.get<
double>(
"LyapunovExponent", 1.0);
88 eta = pt.get<
double>(
"TailCorrection", 0.0);
95 std::vector<Eigen::VectorXd> neighbors, results;
97 assert(state->HasMeta(
"iteration"));
98 assert(state->HasMeta(
"IsProposal"));
101 bool addThreshold =
AnyCast(state->meta[
"IsProposal"]);
105 if( !addThreshold ) {
111 assert(neighbors.size()==results.size());
112 assert(neighbors.size()>=
reg->kn);
118 if(
beta.first>0.0 ) { state->meta[
"cumulative random refinement"] =
cumbeta; }
119 state->meta[
"cumulative structural refinement"] =
cumgamma;
121 const double logtarg =
reg->EvaluateRegressor(state->state[0],
122 std::vector<Eigen::VectorXd>(neighbors.begin(), neighbors.begin()+
reg->kn),
123 std::vector<Eigen::VectorXd>(results.begin(), results.begin()+
reg->kn)) (0);
125 return logtarg + (addThreshold ?
eta*(
lastThreshold+threshold)*state->state[0].norm()*std::fabs(logtarg) : 0.0);
133 while(
reg->CacheSize()<
reg->kn ) {
134 auto gauss = std::make_shared<muq::Modeling::Gaussian>(state->state[0], std::pow(std::exp(
gamma.first), 1.0/(1.0+
reg->Order()))*Eigen::VectorXd::Ones(state->state[0].size()));
135 const Eigen::VectorXd& x = gauss->Sample();
143 std::shared_ptr<SamplingState>
const& state,
144 std::vector<Eigen::VectorXd>& neighbors,
145 std::vector<Eigen::VectorXd>& results)
const {
146 reg->NearestNeighbors(state->state[0], neighbors, results);
147 assert(neighbors.size()==results.size());
155 std::shared_ptr<SamplingState>
const& state,
157 std::vector<Eigen::VectorXd>& neighbors,
158 std::vector<Eigen::VectorXd>& results) {
160 const std::tuple<Eigen::VectorXd, double, unsigned int>& lambda =
reg->PoisednessConstant(state->state[0], neighbors);
163 if(
reg->InCache(std::get<0>(lambda)) ) {
165 Eigen::VectorXd point = Eigen::VectorXd::Random(state->state[0].size());
166 point *= RandomGenerator::GetUniform()*radius/point.norm();
167 point += state->state[0];
171 double dist = RAND_MAX;
172 for(
unsigned int i=0; i<
reg->kn; ++i ) {
173 double newdist = (point-state->state[0]).norm();
174 if( newdist<dist ) { dist = newdist; index = i; }
178 assert(dist<=radius);
182 RefineSurrogate(std::get<0>(lambda), std::get<2>(lambda), neighbors, results);
185 return std::get<1>(lambda);
196 double error, radius;
197 std::tie(error, radius) =
reg->ErrorIndicator(state->state[0], neighbors);
200 if( RandomGenerator::GetUniform()<
beta.first*std::pow((
double)
step,
beta.second) ) {
205 std::tie(error, radius) =
reg->ErrorIndicator(state->state[0], std::vector<Eigen::VectorXd>(neighbors.begin(), neighbors.begin()+
reg->kn));
213 state->meta[
"error threshold"] = std::exp(threshold);
214 state->meta[
"error indicator"] = std::exp(error);
217 if( error>threshold ) {
222 return std::exp(threshold);
226 assert(!
reg->InCache(point));
227 const Eigen::VectorXd& result =
reg->Add(point);
230 assert(neighbors.size()==results.size());
231 assert(index<neighbors.size());
233 neighbors.push_back(point);
234 results.push_back(result);
235 std::iter_swap(neighbors.end()-1, neighbors.begin()+index);
236 std::iter_swap(results.end()-1, results.begin()+index);
242 pt.put(
"ReevaluateAcceptedDensity",
true);
246 if(
qoi==
nullptr ) {
return nullptr; }
249 return std::make_shared<SamplingState>(
regQoI->Evaluate(
lastState->state));
unsigned int CacheSize() const
std::pair< double, double > beta
Parameters for random refinement.
unsigned int step
The current step in the MCMC chain.
std::pair< double, double > gamma
Parameters for structural refinement.
unsigned int cumkappa
Cumulative kappa refinements.
virtual void AddOptions(boost::property_tree::ptree &pt) const override
virtual std::shared_ptr< SamplingState > QOI() override
double lastLyapunov
The Lyapunov function at the most recently accepted point.
std::shared_ptr< muq::Approximation::LocalRegression > regQoI
The regression for the quantity of interest.
double eta
Tail correction parameter.
std::shared_ptr< muq::Approximation::LocalRegression > reg
The regression for the log-likelihood.
unsigned int cumbeta
Cumulative beta refinements.
double lastThreshold
The error threshold at the most recently accepted point.
const Eigen::VectorXd centroid
The centroid of the distribution (input parameter)
virtual double LogDensity(std::shared_ptr< SamplingState > const &state) override
double RefineSurrogate(unsigned int const step, std::shared_ptr< SamplingState > const &state, std::vector< Eigen::VectorXd > &neighbors, std::vector< Eigen::VectorXd > &results)
void CheckNumNeighbors(std::shared_ptr< SamplingState > const &state)
Check to make sure we have enough model evaluations.
double LogLyapunovFunction(Eigen::VectorXd const &x) const
Estimate the Lyapunov function.
ExpensiveSamplingProblem(std::shared_ptr< muq::Modeling::ModPiece > const &target, std::shared_ptr< muq::Modeling::ModPiece > const &qoi, Eigen::VectorXd const ¢roid, boost::property_tree::ptree pt)
unsigned int level
The current error threshold level.
double ErrorThreshold(Eigen::VectorXd const &x) const
Compute the error threshold.
void SetUp(boost::property_tree::ptree &pt)
Set up the sampling problem.
void CheckNeighbors(std::shared_ptr< SamplingState > const &state, std::vector< Eigen::VectorXd > &neighbors, std::vector< Eigen::VectorXd > &results) const
Check to make sure we have enough model evaluations.
unsigned int cumgamma
Cumulative gamma refinements.
double tau0
The length of the first level.
std::pair< double, double > lyapunovPara
The scaling and exponent for the Lyapunov function.
Class for sampling problems based purely on a density function.
std::shared_ptr< muq::Modeling::ModPiece > qoi
std::shared_ptr< muq::Modeling::ModPiece > target
The target distribution (the prior in the inference case)
std::shared_ptr< SamplingState > lastState
Class for easily casting boost::any's in assignment operations.