MUQ  0.4.3
InferenceProblem.cpp
Go to the documentation of this file.
3 
4 using namespace muq::Modeling;
5 using namespace muq::SamplingAlgorithms;
6 
7 InferenceProblem::InferenceProblem(std::shared_ptr<muq::Modeling::ModPiece> const& likelyIn,
8  std::shared_ptr<muq::Modeling::ModPiece> const& priorIn,
9  double inverseTempIn) : AbstractSamplingProblem(likelyIn->inputSizes),
10  likely(likelyIn),
11  prior(priorIn),
12  inverseTemp(inverseTempIn){}
13 
14 InferenceProblem::InferenceProblem(std::shared_ptr<muq::Modeling::ModPiece> const& likelyIn,
15  std::shared_ptr<muq::Modeling::ModPiece> const& priorIn,
16  std::shared_ptr<muq::Modeling::ModPiece> const& qoiIn,
17  double inverseTempIn) : AbstractSamplingProblem(likelyIn->inputSizes),
18  likely(likelyIn),
19  prior(priorIn),
20  qoi(qoiIn),
21  inverseTemp(inverseTempIn) {}
22 
23 
24 double InferenceProblem::LogDensity(std::shared_ptr<SamplingState> const& state) {
25  assert(likely);
26  assert(prior);
27 
28  lastState = state;
29  double likelyVal = likely->Evaluate(state->state).at(0)(0);
30  double priorVal = prior->Evaluate(state->state).at(0)(0);
31  state->meta["LogLikelihood"] = likelyVal;
32  state->meta["LogPrior"] = priorVal;
33  state->meta["InverseTemp"] = inverseTemp;
34 
35  return inverseTemp * likelyVal + priorVal;
36 }
37 
38 std::shared_ptr<SamplingState> InferenceProblem::QOI() {
39  assert(lastState);
40  if (qoi == nullptr)
41  return nullptr;
42 
43  return std::make_shared<SamplingState>(qoi->Evaluate(lastState->state));
44 }
45 
46 Eigen::VectorXd InferenceProblem::GradLogDensity(std::shared_ptr<SamplingState> const& state,
47  unsigned const blockWrt)
48 {
49  assert(likely);
50  assert(prior);
51 
52  Eigen::VectorXd sens = Eigen::VectorXd::Ones(1);
53  return inverseTemp * likely->Gradient(0, blockWrt, state->state, sens) + prior->Gradient(0, blockWrt, state->state, sens);
54 }
55 
56 std::shared_ptr<AbstractSamplingProblem> InferenceProblem::Clone() const{
57  return std::make_shared<InferenceProblem>(likely, prior, qoi, inverseTemp);
58 }
Abstract base class for MCMC and Importance Sampling problems.
std::shared_ptr< muq::Modeling::ModPiece > likely
The log-likelihood function.
std::shared_ptr< muq::Modeling::ModPiece > qoi
std::shared_ptr< muq::Modeling::ModPiece > prior
The prior log-density.
virtual std::shared_ptr< AbstractSamplingProblem > Clone() const override
InferenceProblem(std::shared_ptr< muq::Modeling::ModPiece > const &likelyIn, std::shared_ptr< muq::Modeling::ModPiece > const &priorIn, double inverseTempIn=1.0)
virtual Eigen::VectorXd GradLogDensity(std::shared_ptr< SamplingState > const &state, unsigned const blockWrt) override
virtual double LogDensity(std::shared_ptr< SamplingState > const &state) override
virtual std::shared_ptr< SamplingState > QOI() override
std::shared_ptr< SamplingState > lastState