18 std::shared_ptr<AbstractSamplingProblem> prob,
19 std::shared_ptr<GaussianBase> priorIn) :
MCMCProposal(pt,prob),
20 beta(pt.
get(
"Beta",0.5)),
25 CrankNicolsonProposal::CrankNicolsonProposal(boost::property_tree::ptree
const& pt,
26 std::shared_ptr<AbstractSamplingProblem> prob) :
MCMCProposal(pt,prob),
27 beta(pt.
get(
"Beta",0.5))
36 std::vector<Eigen::VectorXd> props = currentState->state;
38 std::vector<Eigen::VectorXd> hypers =
GetPriorInputs(currentState->state);
40 Eigen::VectorXd priorSamp =
priorDist->Sample(hypers);
45 return std::make_shared<SamplingState>(props, 1.0);
49 std::shared_ptr<SamplingState>
const& propState)
51 std::vector<Eigen::VectorXd> hypers =
GetPriorInputs(currState->state);
53 priorDist->ResetHyperparameters(WorkPiece::ToRefVector(hypers));
57 hypers.insert(hypers.begin(), (diff +
priorDist->GetMean()).eval());
63 std::vector<Eigen::VectorXd> hyperParams;
68 meanIns.push_back( std::cref( currState.at(
priorMeanInds.at(i)) ) );
76 covIns.push_back( std::cref( currState.at(
priorCovInds.at(i)) ) );
89 std::shared_ptr<SamplingProblem> prob2 = std::dynamic_pointer_cast<SamplingProblem>(
prob);
93 std::shared_ptr<ModPiece> targetDens = prob2->GetDistribution();
94 std::shared_ptr<ModGraphPiece> targetDens2 = std::dynamic_pointer_cast<ModGraphPiece>(targetDens);
98 auto graph = targetDens2->GetGraph();
101 auto priorPiece = graph->GetPiece(nodeName);
105 std::shared_ptr<Density> priorDens = std::dynamic_pointer_cast<Density>(priorPiece);
108 priorDist = std::dynamic_pointer_cast<GaussianBase>(priorDens->GetDistribution());
112 auto gaussPrior = std::dynamic_pointer_cast<Gaussian>(
priorDist);
113 if(gaussPrior==
nullptr)
118 if(inputTypes == Gaussian::None)
121 auto newGraph = graph->Clone();
122 auto constPieces = targetDens2->GetConstantPieces();
123 for(
int i=0; i<constPieces.size(); ++i)
124 newGraph->RemoveNode( newGraph->GetName(constPieces.at(i)) );
127 if(inputTypes & Gaussian::Mean){
128 std::string meanInput = graph->GetParent(nodeName, 1);
129 if(newGraph->HasNode(meanInput)){
133 priorMeanModel = std::make_shared<IdentityOperator>(priorDens->inputSizes(1));
134 auto iter = std::find(constPieces.begin(), constPieces.end(), graph->GetPiece(meanInput));
135 priorMeanInds.push_back( std::distance(constPieces.begin(),iter));
140 if((inputTypes & Gaussian::DiagCovariance) || (inputTypes & Gaussian::FullCovariance) || (inputTypes & Gaussian::DiagPrecision)||(inputTypes & Gaussian::FullPrecision)){
141 int covInd = (inputTypes & Gaussian::Mean) ? 2 : 1;
142 priorUsesCov = (inputTypes & Gaussian::DiagCovariance) || (inputTypes & Gaussian::FullCovariance);
144 std::string covInput = graph->GetParent(nodeName, covInd);
145 if(newGraph->HasNode(covInput)){
149 priorCovModel = std::make_shared<IdentityOperator>(priorDens->inputSizes(covInd));
150 auto iter = std::find(constPieces.begin(), constPieces.end(), graph->GetPiece(covInput));
151 priorCovInds.push_back( std::distance(constPieces.begin(),iter));
REGISTER_MCMC_PROPOSAL(CrankNicolsonProposal) CrankNicolsonProposal
An implement of the dimension-independent pCN proposal.
std::vector< int > priorCovInds
std::shared_ptr< muq::Modeling::ModPiece > priorMeanModel
std::shared_ptr< muq::Modeling::GaussianBase > priorDist
The proposal distribution.
void ExtractPrior(std::shared_ptr< AbstractSamplingProblem > const &prob, std::string nodeName)
std::vector< Eigen::VectorXd > GetPriorInputs(std::vector< Eigen::VectorXd > const &currState)
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
std::shared_ptr< muq::Modeling::ModPiece > priorCovModel
CrankNicolsonProposal(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > prob, std::shared_ptr< muq::Modeling::GaussianBase > prior)
virtual std::shared_ptr< SamplingState > Sample(std::shared_ptr< SamplingState > const ¤tState) override
std::vector< int > priorMeanInds
std::shared_ptr< AbstractSamplingProblem > prob
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())