9 std::shared_ptr<MultiIndex> boxHighestIndex)
11 componentFactory(componentFactory),
12 boxHighestIndex(boxHighestIndex)
15 ptChains.put(
"NumSamples", 0);
16 ptChains.put(
"PrintLevel", 0);
18 ptBlockID.put(
"BlockIndex",0);
20 const auto rootIndex = std::make_shared<MultiIndex>(
boxHighestIndex->GetLength());
24 std::shared_ptr<MultiIndex> coarse_index = rootIndex;
25 auto proposal_coarse =
componentFactory->Proposal(rootIndex, coarse_problem);
27 std::vector<std::shared_ptr<TransitionKernel>> coarse_kernels(1);
29 coarse_kernels[0] = std::make_shared<MHKernel>(ptBlockID,coarse_problem,proposal_coarse);
31 coarse_kernels[0] = std::make_shared<DummyKernel>(ptBlockID, coarse_problem, proposal_coarse);
34 auto coarse_chain = std::make_shared<SingleChainMCMC>(ptChains,coarse_kernels);
35 coarse_chain->SetState(startPtCoarse);
41 for (
int i = rootPath->Size()-2; i >= 0; i--) {
45 std::shared_ptr<MultiIndex> index = (*rootPath)[i];
49 auto coarse_proposal =
componentFactory->CoarseProposal(index, coarse_index, coarse_problem, coarse_chain);
53 std::vector<std::shared_ptr<TransitionKernel>> kernels(1);
55 kernels[0] = std::make_shared<MIKernel>(ptBlockID,problem,coarse_problem,proposal,coarse_proposal,proposalInterpolation,coarse_chain);
57 kernels[0] = std::make_shared<MIDummyKernel>(ptBlockID, problem, proposal, coarse_proposal, proposalInterpolation, coarse_chain);
59 auto chain = std::make_shared<SingleChainMCMC>(ptChains,kernels);
60 chain->SetState(startingPoint);
62 coarse_problem = problem;
72 boxIndices = MultiIndexFactory::CreateFullTensor(boxSize->GetVector());
76 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
78 if (boxIndex->Max() == 0) {
83 std::shared_ptr<MultiIndex> index = std::make_shared<MultiIndex>(*
boxLowestIndex + *boxIndex);
87 auto coarse_proposal =
componentFactory->CoarseProposal(index, coarse_index, coarse_problem, coarse_chain);
91 std::vector<std::shared_ptr<TransitionKernel>> kernels(1);
93 kernels[0] = std::make_shared<MIKernel>(ptBlockID,problem,coarse_problem,proposal,coarse_proposal,proposalInterpolation,coarse_chain);
95 kernels[0] = std::make_shared<MIDummyKernel>(ptBlockID, problem, proposal, coarse_proposal, proposalInterpolation, coarse_chain);
97 auto chain = std::make_shared<SingleChainMCMC>(ptChains,kernels);
98 chain->SetState(startingPoint);
102 if (*boxIndex == *boxSize)
122 std::vector<Eigen::VectorXd> sampDiff(num_components);
123 for (
int component = 0; component < num_components; component++) {
124 sampDiff[component] = Eigen::VectorXd::Zero(
GetFinestProblem()->blockSizesQOI[component]);
128 for (
unsigned int i = 0; i <
boxIndices->Size(); i++) {
129 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
131 chain->AddNumSamps(1);
135 for (
unsigned int i = 0; i <
boxIndices->Size(); i++) {
136 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
140 if (chain->GetQOIs()->size() > 0) {
141 auto new_state = chain->GetQOIs()->at(chain->GetQOIs()->size()-1);
143 std::shared_ptr<MultiIndex> index = std::make_shared<MultiIndex>(*
boxLowestIndex + *boxIndex);
144 auto indexDiffFromTop = std::make_shared<MultiIndex>(*
boxHighestIndex - *index);
146 if (indexDiffFromTop->Sum() % 2 == 0) {
147 for (
int component = 0; component < num_components; component++) {
148 sampDiff[component] += new_state->state[component];
151 for (
int component = 0; component < num_components; component++) {
152 sampDiff[component] -= new_state->state[component];
158 QOIDiff->Add(std::make_shared<SamplingState>(sampDiff));
163 for (
int i = 0; i <
boxIndices->Size(); i++) {
164 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
166 chain->GetSamples()->WriteToFile(filename,
"/model_" +
boxHighestIndex->ToString() +
"_subchain_" + boxIndex->ToString() +
"_samples");
167 chain->GetQOIs()->WriteToFile(filename,
"/model_" +
boxHighestIndex->ToString() +
"_subchain_" + boxIndex->ToString() +
"_qois");
181 Eigen::VectorXd sampMean = Eigen::VectorXd::Zero(
GetFinestProblem()->blockSizes.sum());
183 for (
int i = 0; i <
boxIndices->Size(); i++) {
184 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
186 auto samps = chain->GetSamples();
188 std::shared_ptr<MultiIndex> index = std::make_shared<MultiIndex>(*
boxLowestIndex + *boxIndex);
189 auto indexDiffFromTop = std::make_shared<MultiIndex>(*
boxHighestIndex - *index);
191 if (indexDiffFromTop->Sum() % 2 == 0) {
192 sampMean += samps->Mean();
194 sampMean -= samps->Mean();
201 void MIMCMCBox::DrawChain(std::shared_ptr<SingleChainMCMC> chain, std::string chainid, std::ofstream& graphfile)
const {
202 graphfile <<
"subgraph cluster_" << chainid <<
" {" << std::endl;
203 graphfile <<
"label=\"Chain " << chainid <<
"\"" << std::endl;
204 for (
int s = 0; s < chain->GetSamples()->size(); s++) {
205 std::shared_ptr<SamplingState> sample = chain->GetSamples()->at(s);
206 std::string nodeid =
"\"s" + chainid +
"node" +
std::to_string(s) +
"\"";
207 sample->meta[
"gvizid"] = nodeid;
209 double logTarget = AnyCast(sample->meta[
"LogTarget"]);
210 graphfile << nodeid <<
" [label=\""
211 << s <<
" - " << sample->weight
212 <<
" (L=" << logTarget <<
")";
214 if (sample->HasMeta(
"QOI"))
217 graphfile <<
"\"]" << std::endl;
219 graphfile <<
"}" << std::endl;
220 for (
int s = 0; s < chain->GetSamples()->size(); s++) {
221 std::shared_ptr<SamplingState> sample = chain->GetSamples()->at(s);
222 std::string nodeid =
"\"s" + chainid +
"node" +
std::to_string(s) +
"\"";
224 if (s < chain->GetSamples()->size() - 1)
225 graphfile << nodeid <<
" -> " <<
"\"s" << chainid <<
"node" << s+1 <<
"\"" << std::endl;
227 if (sample->HasMeta(
"coarseSample")) {
228 std::shared_ptr<SamplingState> coarseSample = AnyCast(sample->meta[
"coarseSample"]);
229 if (coarseSample->HasMeta(
"gvizid")) {
230 std::string coarseid = AnyCast(coarseSample->meta[
"gvizid"]);
231 graphfile << nodeid <<
" -> " << coarseid << std::endl;
233 std::cout <<
"no gvizid!" << std::endl;
248 for (
int i = 0; i <
boxIndices->Size(); i++) {
249 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
250 std::shared_ptr<SingleChainMCMC> singleChain =
boxChains[
boxIndices->MultiToIndex(boxIndex)];
252 std::string chainid =
"box" +
boxHighestIndex->ToString() +
"_node" + boxIndex->ToString() +
"";
253 DrawChain (singleChain, chainid, graphfile);
256 std::string previd =
"";
260 graphfile << chainid <<
" -> " << previd << std::endl;
262 graphfile << chainid << std::endl;
266 for (
int i = 0; i <
boxIndices->Size(); i++) {
267 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
268 std::shared_ptr<SingleChainMCMC> singleChain =
boxChains[
boxIndices->MultiToIndex(boxIndex)];
270 std::string chainid =
"\"box" +
boxHighestIndex->ToString() +
"_node" + boxIndex->ToString() +
"\"";
273 graphfile << chainid <<
" -> " << previd << std::endl;
275 graphfile << chainid << std::endl;
292 int index =
boxIndices->MultiToIndex(boxIndex);
301 std::shared_ptr<MultiIndexLimiter> limiter = std::make_shared<NoLimiter>();
302 std::shared_ptr<MultiIndexSet> output = std::make_shared<MultiIndexSet>(index->GetLength(),limiter);
305 std::shared_ptr<MultiIndex> currentIndex = index;
306 output->AddActive(currentIndex);
311 int maxEntry = currentIndex->GetVector().maxCoeff(&maxCoeffId);
315 std::shared_ptr<MultiIndex> nextIndex = MultiIndex::Copy(currentIndex);
316 nextIndex->SetValue(maxCoeffId, maxEntry - 1);
317 output->AddActive(nextIndex);
319 currentIndex = nextIndex;
std::shared_ptr< SingleChainMCMC > FinestChain()
void Draw(std::ofstream &graphfile, bool drawSamples=true) const
void DrawChain(std::shared_ptr< SingleChainMCMC > chain, std::string chainid, std::ofstream &graphfile) const
std::shared_ptr< SampleCollection > QOIDiff
MIMCMCBox(std::shared_ptr< MIComponentFactory > componentFactory, std::shared_ptr< MultiIndex > boxHighestIndex)
std::shared_ptr< AbstractSamplingProblem > finestProblem
std::shared_ptr< MultiIndexSet > boxIndices
std::shared_ptr< MultiIndex > boxHighestIndex
std::shared_ptr< AbstractSamplingProblem > GetFinestProblem()
Eigen::VectorXd MeanQOI()
std::shared_ptr< MultiIndex > GetLowestIndex()
std::shared_ptr< MIComponentFactory > componentFactory
std::vector< std::shared_ptr< SingleChainMCMC > > tailChains
std::shared_ptr< MultiIndexSet > CreateRootPath(std::shared_ptr< MultiIndex > index)
std::shared_ptr< MultiIndexSet > GetBoxIndices()
std::vector< std::shared_ptr< SingleChainMCMC > > boxChains
std::shared_ptr< MultiIndex > GetHighestIndex()
void WriteToFile(std::string filename)
std::shared_ptr< SingleChainMCMC > GetChain(std::shared_ptr< MultiIndex > index)
std::shared_ptr< SampleCollection > GetQOIDiff()
Eigen::VectorXd MeanParam()
std::shared_ptr< MultiIndex > boxLowestIndex
A class for storing and working with the results of Markov chain Monte Carlo algorithms.
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values