7 namespace pt = boost::property_tree;
15 AMProposal::AMProposal(pt::ptree pt,
16 std::shared_ptr<AbstractSamplingProblem>
const& prob,
17 Eigen::MatrixXd
const& initialCov) :
MCMCProposal(pt,prob),
19 adaptSteps(pt.
get<unsigned int>(
"AdaptSteps")),
20 adaptStart(pt.
get<unsigned int>(
"AdaptStart")),
21 adaptEnd(pt.
get<unsigned int>(
"AdaptEnd",std::numeric_limits<unsigned int>::max())),
22 adaptScale(pt.
get(
"AdaptScale",2.4*2.4/initialCov.rows()))
24 propChol = propCov.selfadjointView<Eigen::Lower>().llt();
25 newSamps.resize(propCov.rows(), adaptSteps);
29 AMProposal::AMProposal(pt::ptree pt ,
30 std::shared_ptr<AbstractSamplingProblem>
const& prob) :
AMProposal(pt,prob, ConstructCovariance(pt,prob))
34 Eigen::MatrixXd AMProposal::ConstructCovariance(pt::ptree
const& pt ,
35 std::shared_ptr<AbstractSamplingProblem>
const& prob)
37 int dim =
prob->blockSizes(pt.get(
"BlockIndex",0));
38 double propVar = pt.get(
"InitialVariance",1.0);
39 return propVar * Eigen::MatrixXd::Identity(dim,dim);
43 void AMProposal::Adapt(
unsigned int const t, std::vector<std::shared_ptr<SamplingState>>
const& states) {
60 Eigen::VectorXd oldMean;
61 std::swap(
mean,oldMean);
84 std::shared_ptr<SamplingState>
AMProposal::Sample(std::shared_ptr<SamplingState>
const& currentState)
88 Eigen::VectorXd
const& xc = currentState->state.at(
blockInd);
90 std::vector<Eigen::VectorXd> props = currentState->state;
94 return std::make_shared<SamplingState>(props, 1.0);
98 std::shared_ptr<SamplingState>
const& propState)
101 return -0.5*diff.dot(
propChol.solve(diff));
REGISTER_MCMC_PROPOSAL(AMProposal) AMProposal
An implemental of the Adaptive Metropolis algorithm.
virtual std::shared_ptr< SamplingState > Sample(std::shared_ptr< SamplingState > const ¤tState) override
Eigen::LLT< Eigen::MatrixXd, Eigen::Lower > propChol
The Cholesky factorization of the current proposal covariance.
virtual Eigen::MatrixXd ProposalCovariance() const
Eigen::VectorXd mean
The current mean.
Eigen::MatrixXd newSamps
Samples seen between visits.
virtual void Adapt(unsigned int const t, std::vector< std::shared_ptr< SamplingState >> const &states) override
Adapt the proposal after each step.
const unsigned int adaptStart
When should we start adapting?
const unsigned int adaptEnd
After how many iterations should we stop adapting?
unsigned int numAdaptSamps
Total number of samples that have been used so far to compute mean and covariance.
const unsigned int adaptSteps
How frequently should we update the adaption?
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
std::shared_ptr< AbstractSamplingProblem > prob
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())