1 #ifndef SAMPLECOLLECTION_H
2 #define SAMPLECOLLECTION_H
17 #include <boost/property_tree/ptree.hpp>
20 namespace SamplingAlgorithms{
45 std::shared_ptr<muq::Modeling::ModPiece>
f;
62 const Eigen::VectorXd&
mu;
77 virtual void Add(std::shared_ptr<SamplingState> newSamp);
79 virtual std::shared_ptr<SamplingState>
at(
unsigned i);
80 virtual const std::shared_ptr<SamplingState>
at(
unsigned i)
const;
83 virtual unsigned int size()
const;
88 virtual std::shared_ptr<SampleCollection>
head(
unsigned int N)
const{
return segment(0,N,1);}
93 virtual std::shared_ptr<SampleCollection>
tail(
unsigned int N)
const{
return segment(
size()-N,N,1);}
100 virtual std::shared_ptr<SampleCollection>
segment(
unsigned int startInd,
unsigned int length,
unsigned int skipBy=1)
const;
102 virtual const std::shared_ptr<SamplingState>
back()
const;
106 Eigen::VectorXd
const& mean,
107 int blockNum=-1)
const override;
109 int blockNum=-1)
const override{
return CentralMoment(order,
Mean(blockNum), blockNum);};
111 virtual Eigen::VectorXd
Mean(
int blockInd=-1)
const override;
113 virtual Eigen::MatrixXd
Covariance(Eigen::VectorXd
const& mean,
int blockInd=-1)
const override;
122 virtual std::vector<Eigen::MatrixXd>
RunningCovariance(Eigen::VectorXd
const& mean,
int blockInd=-1)
const;
153 virtual Eigen::VectorXd
ESS(std::string
const& method=
"Batch")
const override{
return ESS(-1,method);};
159 virtual Eigen::VectorXd
ESS(
int blockDim)
const override{
return ESS(blockDim,
"Batch");};
166 virtual Eigen::VectorXd
ESS(
int blockDim, std::string
const& method)
const override;
177 Eigen::VectorXd
BatchESS(
int blockInd=-1,
int batchSize=-1,
int overlap=-1)
const;
210 double MultiBatchESS(
int blockInd=-1,
int batchSize=-1,
int overlap=-1)
const;
221 std::string
const& method)
const override;
233 virtual Eigen::VectorXd
BatchError(
int blockInd=-1,
int batchSize=-1,
int overlap=-1)
const;
243 virtual Eigen::VectorXd
MultiBatchError(
int blockInd=-1,
int batchSize=-1,
int overlap=-1)
const;
248 virtual Eigen::MatrixXd
AsMatrix(
int blockDim=-1)
const;
252 static std::shared_ptr<SampleCollection>
FromMatrix(Eigen::MatrixXd
const& samps);
259 virtual Eigen::VectorXd
Weights()
const;
269 virtual void WriteToFile(std::string
const& filename, std::string
const& group =
"/")
const;
275 Eigen::MatrixXd
GetMeta(std::string
const& name)
const;
283 std::set<std::string> ListMeta(
bool requireAll=
true)
const;
291 virtual Eigen::VectorXd
ExpectedValue(std::shared_ptr<muq::Modeling::ModPiece>
const& f,
292 std::vector<std::string>
const& metains = std::vector<std::string>())
const override;
300 std::vector<Eigen::VectorXd> RunningExpectedValue(std::shared_ptr<muq::Modeling::ModPiece>
const& f, std::vector<std::string>
const& metains = std::vector<std::string>())
const;
306 virtual unsigned int BlockSize(
int blockInd)
const override;
309 virtual unsigned int NumBlocks()
const override;
333 virtual Eigen::VectorXd
Rhat(
int blockDim,
334 unsigned int numSegments=4,
335 boost::property_tree::ptree options = boost::property_tree::ptree())
const;
338 virtual Eigen::VectorXd
Rhat(
unsigned int numSegments=4,
339 boost::property_tree::ptree options = boost::property_tree::ptree())
const{
return Rhat(-1,numSegments,options);};
343 std::vector<std::shared_ptr<SamplingState>>
samples;
346 static std::pair<double,double>
RecursiveWeightSum(std::vector<std::shared_ptr<SamplingState>>::const_iterator start,
347 std::vector<std::shared_ptr<SamplingState>>::const_iterator end);
350 template<
typename FuncType>
351 static std::pair<double,Eigen::VectorXd>
RecursiveSum(std::vector<std::shared_ptr<SamplingState>>::const_iterator start,
352 std::vector<std::shared_ptr<SamplingState>>::const_iterator end,
355 int numSamps = std::distance(start,end);
356 const int maxSamps = 20;
359 if(numSamps<maxSamps){
361 Eigen::VectorXd sum = (*start)->weight * f(**start);
362 double weightSum = (*start)->weight;
364 for(
auto it=start+1; it!=end; ++it){
365 sum += (*it)->weight * f(**it);
366 weightSum += (*it)->weight;
368 return std::make_pair(weightSum, sum);
372 int halfDist = std::floor(0.5*numSamps);
373 double weight1, weight2;
374 Eigen::VectorXd sum1, sum2;
375 std::tie(weight1,sum1) =
RecursiveSum(start, start+halfDist, f);
376 std::tie(weight2,sum2) =
RecursiveSum(start+halfDist, end, f);
378 return std::make_pair(weight1+weight2, (sum1+sum2).eval());
385 std::unordered_map<std::string, Eigen::MatrixXd>
GetMeta()
const;
396 bool CreateDataset(std::shared_ptr<muq::Utilities::HDF5File> hdf5file, std::string
const& dataname,
int const dataSize,
int const totSamps)
const;
Eigen::VectorXd const & operator()(SamplingState const &a)
ExpectedModPieceValue(std::shared_ptr< muq::Modeling::ModPiece > const &f, std::vector< std::string > const &metains)
const std::vector< std::string > metains
std::shared_ptr< muq::Modeling::ModPiece > f
virtual ~ExpectedModPieceValue()=default
A class to hold and analyze a collection of SamplingState objects.
virtual std::shared_ptr< SamplingState > at(unsigned i)
std::unordered_map< std::string, Eigen::MatrixXd > GetMeta() const
SampleCollection()=default
double MultiBatchESS(int blockInd=-1, int batchSize=-1, int overlap=-1) const
virtual Eigen::VectorXd ESS(std::string const &method="Batch") const override
Returns the effective sample size of the samples.
virtual Eigen::VectorXd MultiBatchError(int blockInd=-1, int batchSize=-1, int overlap=-1) const
virtual std::vector< Eigen::MatrixXd > RunningCovariance(int blockInd=-1) const
std::vector< std::shared_ptr< SamplingState > > samples
virtual Eigen::VectorXd Rhat(unsigned int numSegments=4, boost::property_tree::ptree options=boost::property_tree::ptree()) const
virtual std::shared_ptr< SampleCollection > tail(unsigned int N) const
virtual Eigen::VectorXd CentralMoment(unsigned order, Eigen::VectorXd const &mean, int blockNum=-1) const override
Computes the componentwise central moments (e.g., variance, skewness, kurtosis, etc....
virtual Eigen::VectorXd CentralMoment(unsigned int order, int blockNum=-1) const override
virtual Eigen::VectorXd StandardError(std::string const &method="Batch") const override
virtual Eigen::VectorXd ESS(int blockDim) const override
virtual ~SampleCollection()=default
virtual std::shared_ptr< SampleCollection > segment(unsigned int startInd, unsigned int length, unsigned int skipBy=1) const
virtual void Add(std::shared_ptr< SamplingState > newSamp)
virtual Eigen::MatrixXd AsMatrix(int blockDim=-1) const
static std::pair< double, double > RecursiveWeightSum(std::vector< std::shared_ptr< SamplingState >>::const_iterator start, std::vector< std::shared_ptr< SamplingState >>::const_iterator end)
virtual std::shared_ptr< SampleCollection > head(unsigned int N) const
bool CreateDataset(std::shared_ptr< muq::Utilities::HDF5File > hdf5file, std::string const &dataname, int const dataSize, int const totSamps) const
virtual Eigen::VectorXd Rhat(int blockDim, unsigned int numSegments=4, boost::property_tree::ptree options=boost::property_tree::ptree()) const
Eigen::VectorXd BatchESS(int blockInd=-1, int batchSize=-1, int overlap=-1) const
virtual Eigen::VectorXd Mean(int blockInd=-1) const override
virtual Eigen::VectorXd Weights() const
virtual Eigen::VectorXd StandardError(int blockInd, std::string const &method) const override
virtual Eigen::MatrixXd Covariance(int blockInd=-1) const override
static std::shared_ptr< SampleCollection > FromMatrix(Eigen::MatrixXd const &samps)
static std::pair< double, Eigen::VectorXd > RecursiveSum(std::vector< std::shared_ptr< SamplingState >>::const_iterator start, std::vector< std::shared_ptr< SamplingState >>::const_iterator end, FuncType &f)
virtual const std::shared_ptr< SamplingState > back() const
virtual Eigen::VectorXd StandardError(int blockInd) const override
virtual unsigned int size() const
virtual Eigen::VectorXd BatchError(int blockInd=-1, int batchSize=-1, int overlap=-1) const
Abstract base class for computing sample-based approximations of expectations.
virtual Eigen::MatrixXd Covariance(int blockInd=-1) const
virtual unsigned int BlockSize(int blockInd) const =0
virtual Eigen::VectorXd ExpectedValue(std::shared_ptr< muq::Modeling::ModPiece > const &f, std::vector< std::string > const &metains=std::vector< std::string >()) const =0
virtual unsigned int NumBlocks() const =0
virtual ~SamplingStateIdentity()=default
Eigen::VectorXd const & operator()(SamplingState const &a)
SamplingStateIdentity(int blockIndIn)
Eigen::VectorXd const & operator()(SamplingState const &a)
SamplingStatePartialMoment(int blockIndIn, int momentPowerIn, Eigen::VectorXd const &muIn)
virtual ~SamplingStatePartialMoment()=default
const Eigen::VectorXd & mu
Each state is one sample generated by a sampling algorithm.