8 bool useQoisIn) : boxes(boxesIn),
9 blockSizes(useQoisIn ? boxesIn.at(0)->GetFinestProblem()->blockSizesQOI : boxesIn.at(0)->GetFinestProblem()->blockSizes),
33 std::vector<std::shared_ptr<MarkovChain>> chains =
GetDiffChains();
35 Eigen::VectorXd estVar = chains.at(0)->StandardError(blockDim, method).array().square();
36 for(
unsigned int i=1; i<chains.size(); ++i)
37 estVar += chains.at(i)->StandardError(blockDim, method).array().square().matrix();
39 return estVar.array().sqrt();
57 dim =
boxes.at(0)->FinestChain()->GetQOIs()->BlockSize(blockInd);
59 dim =
boxes.at(0)->FinestChain()->GetSamples()->BlockSize(blockInd);
63 for(
unsigned int boxInd = 0; boxInd<
boxes.size(); ++boxInd){
65 diffChains.at(boxInd) = std::make_shared<MarkovChain>();
67 auto& box =
boxes.at(boxInd);
68 auto boxIndices = box->GetBoxIndices();
70 unsigned int numSamps;
72 numSamps = box->FinestChain()->GetQOIs()->size();
74 numSamps = box->FinestChain()->GetSamples()->size();
78 for(
unsigned int sampInd =0; sampInd < numSamps; ++sampInd)
81 diff = Eigen::VectorXd::Zero(dim);
83 for (
int i = 0; i < boxIndices->Size(); i++) {
85 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
86 auto chain = box->GetChain(boxIndex);
87 std::shared_ptr<MarkovChain> samps;
89 samps = chain->GetQOIs();
91 samps = chain->GetSamples();
94 MultiIndex index = *(box->GetLowestIndex()) + *boxIndex;
95 MultiIndex indexDiffFromTop = *(box->GetHighestIndex()) - index;
97 double mult = (indexDiffFromTop.Sum() % 2 == 0) ? 1.0 : -1.0;
100 diff += mult * f->Evaluate(samps->at(sampInd)->state).at(0);
102 diff += mult * samps->at(sampInd)->ToVector(blockInd);
107 diffChains.at(boxInd)->Add(std::make_shared<SamplingState>(diff));
116 std::vector<std::string>
const& metains)
const
118 assert(f->outputSizes.size()==1);
120 Eigen::VectorXd telescopingSum = Eigen::VectorXd::Zero(f->outputSizes(0));
123 for (
auto& box :
boxes) {
126 Eigen::VectorXd diffMean = Eigen::VectorXd::Zero(f->outputSizes(0));
128 auto boxIndices = box->GetBoxIndices();
129 for (
int i = 0; i < boxIndices->Size(); i++) {
131 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
132 auto chain = box->GetChain(boxIndex);
133 std::shared_ptr<MarkovChain> samps;
135 samps = chain->GetQOIs();
137 samps = chain->GetSamples();
140 MultiIndex index = *(box->GetLowestIndex()) + *boxIndex;
141 MultiIndex indexDiffFromTop = *(box->GetHighestIndex()) - index;
143 if (indexDiffFromTop.Sum() % 2 == 0) {
144 diffMean += samps->ExpectedValue(f,metains);
146 diffMean -= samps->ExpectedValue(f,metains);
151 telescopingSum += diffMean;
154 return telescopingSum;
160 Eigen::MatrixXd telescopingSum = Eigen::MatrixXd::Zero(
BlockSize(blockInd),
BlockSize(blockInd));
163 for (
auto& box :
boxes) {
166 Eigen::MatrixXd diffMean = Eigen::MatrixXd::Zero(
BlockSize(blockInd),
BlockSize(blockInd));
168 auto boxIndices = box->GetBoxIndices();
169 for (
int i = 0; i < boxIndices->Size(); i++) {
171 std::shared_ptr<MultiIndex> boxIndex = (*boxIndices)[i];
172 auto chain = box->GetChain(boxIndex);
173 std::shared_ptr<MarkovChain> samps;
175 samps = chain->GetQOIs();
177 samps = chain->GetSamples();
180 MultiIndex index = *(box->GetLowestIndex()) + *boxIndex;
181 MultiIndex indexDiffFromTop = *(box->GetHighestIndex()) - index;
183 if (indexDiffFromTop.Sum() % 2 == 0) {
184 diffMean += samps->Covariance(mean);
186 diffMean -= samps->Covariance(mean);
191 telescopingSum += diffMean;
194 return 0.5*(telescopingSum + telescopingSum.transpose());
virtual unsigned int BlockSize(int blockInd) const override
std::vector< std::shared_ptr< MarkovChain > > GetDiffChains(int blockInd=-1, std::shared_ptr< muq::Modeling::ModPiece > const &f=nullptr) const
MultiIndexEstimator(std::vector< std::shared_ptr< MIMCMCBox >> const &boxesIn, bool useQoisIn=false)
virtual unsigned int NumBlocks() const override
virtual Eigen::MatrixXd Covariance(int blockInd=-1) const override
virtual Eigen::VectorXd ExpectedValue(std::shared_ptr< muq::Modeling::ModPiece > const &f, std::vector< std::string > const &metains=std::vector< std::string >()) const override
virtual Eigen::VectorXd ESS(std::string const &method="Batch") const override
virtual Eigen::VectorXd StandardError(int blockDim, std::string const &method) const override
std::vector< std::shared_ptr< MarkovChain > > diffChains
std::vector< std::shared_ptr< MIMCMCBox > > boxes
const Eigen::VectorXi blockSizes
virtual Eigen::VectorXd Variance(int blockInd=-1) const