MUQ  0.4.3
SamplingProblem.cpp
Go to the documentation of this file.
3 
4 using namespace muq::Modeling;
5 using namespace muq::SamplingAlgorithms;
6 
7 SamplingProblem::SamplingProblem(std::shared_ptr<muq::Modeling::ModPiece> const& targetIn) : AbstractSamplingProblem(targetIn->inputSizes),
8  target(targetIn),
9  qoi(nullptr) {}
10 
11 SamplingProblem::SamplingProblem(std::shared_ptr<muq::Modeling::ModPiece> const& targetIn,
12  std::shared_ptr<muq::Modeling::ModPiece> const& qoiIn) : AbstractSamplingProblem(targetIn->inputSizes,qoiIn->outputSizes),
13  target(targetIn),
14  qoi(qoiIn) {}
15 
16 
17 double SamplingProblem::LogDensity(std::shared_ptr<SamplingState> const& state) {
18  assert(target);
19 
20  lastState = state;
21 
22  return target->Evaluate(state->state).at(0)(0);
23 }
24 
25 std::shared_ptr<SamplingState> SamplingProblem::QOI() {
26  assert(target);
27  assert(lastState);
28  if (qoi == nullptr)
29  return nullptr;
30 
31  return std::make_shared<SamplingState>(qoi->Evaluate(lastState->state));
32 }
33 
34 Eigen::VectorXd SamplingProblem::GradLogDensity(std::shared_ptr<SamplingState> const& state,
35  unsigned const blockWrt)
36 {
37  return target->Gradient(0,blockWrt, state->state, Eigen::VectorXd::Ones(1).eval());
38 }
39 
40 std::vector<int> SamplingProblem::GetBlockSizes(std::shared_ptr<ModPiece> const& target)
41 {
42  int numBlocks = target->inputSizes.size();
43 
44  std::vector<int> output(numBlocks);
45  for(int i=0; i<numBlocks; ++i)
46  output.at(i) = target->inputSizes(i);
47 
48  return output;
49 }
Abstract base class for MCMC and Importance Sampling problems.
virtual Eigen::VectorXd GradLogDensity(std::shared_ptr< SamplingState > const &state, unsigned const blockWrt) override
std::shared_ptr< muq::Modeling::ModPiece > qoi
std::shared_ptr< muq::Modeling::ModPiece > target
The target distribution (the prior in the inference case)
virtual double LogDensity(std::shared_ptr< SamplingState > const &state) override
static std::vector< int > GetBlockSizes(std::shared_ptr< muq::Modeling::ModPiece > const &target)
SamplingProblem(std::shared_ptr< muq::Modeling::ModPiece > const &targetIn)
std::shared_ptr< SamplingState > lastState
virtual std::shared_ptr< SamplingState > QOI() override