MUQ  0.4.3
SampleCollection.h
Go to the documentation of this file.
1 #ifndef SAMPLECOLLECTION_H
2 #define SAMPLECOLLECTION_H
3 
4 #include <memory>
5 #include <vector>
6 #include <set>
7 
8 #include <Eigen/Core>
9 
10 #include "MUQ/Modeling/ModPiece.h"
11 
14 
16 
17 #include <boost/property_tree/ptree.hpp>
18 
19 namespace muq{
20  namespace SamplingAlgorithms{
21 
23  public:
24  SamplingStateIdentity(int blockIndIn) : blockInd(blockIndIn){};
25 
26  virtual ~SamplingStateIdentity() = default;
27 
28  Eigen::VectorXd const& operator()(SamplingState const& a);
29 
30  const int blockInd;
31 
32  private:
33  Eigen::VectorXd output;
34  };
35 
37  public:
38  ExpectedModPieceValue(std::shared_ptr<muq::Modeling::ModPiece> const& f, std::vector<std::string> const& metains);
39 
40  virtual ~ExpectedModPieceValue() = default;
41 
42  Eigen::VectorXd const& operator()(SamplingState const& a);
43 
44  private:
45  std::shared_ptr<muq::Modeling::ModPiece> f;
46 
47  const std::vector<std::string> metains;
48  };
49 
51  public:
53  int momentPowerIn,
54  Eigen::VectorXd const& muIn) : blockInd(blockIndIn), momentPower(momentPowerIn), mu(muIn){};
55 
56  virtual ~SamplingStatePartialMoment() = default;
57 
58  Eigen::VectorXd const& operator()(SamplingState const& a);
59 
60  const int blockInd;
61  const int momentPower;
62  const Eigen::VectorXd& mu;
63 
64  private:
65  Eigen::VectorXd output;
66  };
67 
71  class SampleCollection : public SampleEstimator, public std::enable_shared_from_this<SampleCollection>{
72  public:
73  SampleCollection() = default;
74 
75  virtual ~SampleCollection() = default;
76 
77  virtual void Add(std::shared_ptr<SamplingState> newSamp);
78 
79  virtual std::shared_ptr<SamplingState> at(unsigned i);
80  virtual const std::shared_ptr<SamplingState> at(unsigned i) const;
81 
83  virtual unsigned int size() const;
84 
88  virtual std::shared_ptr<SampleCollection> head(unsigned int N) const{return segment(0,N,1);}
89 
93  virtual std::shared_ptr<SampleCollection> tail(unsigned int N) const{return segment(size()-N,N,1);}
94 
95 
100  virtual std::shared_ptr<SampleCollection> segment(unsigned int startInd, unsigned int length, unsigned int skipBy=1) const;
101 
102  virtual const std::shared_ptr<SamplingState> back() const;
103 
105  virtual Eigen::VectorXd CentralMoment(unsigned order,
106  Eigen::VectorXd const& mean,
107  int blockNum=-1) const override;
108  virtual Eigen::VectorXd CentralMoment(unsigned int order,
109  int blockNum=-1) const override{ return CentralMoment(order, Mean(blockNum), blockNum);};
110 
111  virtual Eigen::VectorXd Mean(int blockInd=-1) const override;
112  virtual Eigen::MatrixXd Covariance(int blockInd=-1) const override{return SampleEstimator::Covariance(blockInd);};
113  virtual Eigen::MatrixXd Covariance(Eigen::VectorXd const& mean, int blockInd=-1) const override;
114 
121  virtual std::vector<Eigen::MatrixXd> RunningCovariance(int blockInd=-1) const;
122  virtual std::vector<Eigen::MatrixXd> RunningCovariance(Eigen::VectorXd const& mean, int blockInd=-1) const;
123 
153  virtual Eigen::VectorXd ESS(std::string const& method="Batch") const override{return ESS(-1,method);};
154 
159  virtual Eigen::VectorXd ESS(int blockDim) const override{return ESS(blockDim,"Batch");};
160 
166  virtual Eigen::VectorXd ESS(int blockDim, std::string const& method) const override;
167 
177  Eigen::VectorXd BatchESS(int blockInd=-1, int batchSize=-1, int overlap=-1) const;
178 
210  double MultiBatchESS(int blockInd=-1, int batchSize=-1, int overlap=-1) const;
211 
220  virtual Eigen::VectorXd StandardError(int blockInd,
221  std::string const& method) const override;
222  virtual Eigen::VectorXd StandardError(int blockInd) const override{return StandardError(blockInd,"Batch");};
223  virtual Eigen::VectorXd StandardError(std::string const& method="Batch") const override{return StandardError(-1,method);};
224 
233  virtual Eigen::VectorXd BatchError(int blockInd=-1, int batchSize=-1, int overlap=-1) const;
234 
243  virtual Eigen::VectorXd MultiBatchError(int blockInd=-1, int batchSize=-1, int overlap=-1) const;
244 
248  virtual Eigen::MatrixXd AsMatrix(int blockDim=-1) const;
249 
252  static std::shared_ptr<SampleCollection> FromMatrix(Eigen::MatrixXd const& samps);
253 
259  virtual Eigen::VectorXd Weights() const;
260 
269  virtual void WriteToFile(std::string const& filename, std::string const& group = "/") const;
270 
275  Eigen::MatrixXd GetMeta(std::string const& name) const;
276 
283  std::set<std::string> ListMeta(bool requireAll=true) const;
284 
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;
293 
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;
301 
302 
306  virtual unsigned int BlockSize(int blockInd) const override;
307 
309  virtual unsigned int NumBlocks() const override;
310 
333  virtual Eigen::VectorXd Rhat(int blockDim,
334  unsigned int numSegments=4,
335  boost::property_tree::ptree options = boost::property_tree::ptree()) const;
336 
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);};
340 
341  protected:
342 
343  std::vector<std::shared_ptr<SamplingState>> samples;
344 
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);
348 
349 
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,
353  FuncType& f)
354  {
355  int numSamps = std::distance(start,end);
356  const int maxSamps = 20;
357 
358  // If the number of samples is small enough, we can safely add them up directly
359  if(numSamps<maxSamps){
360 
361  Eigen::VectorXd sum = (*start)->weight * f(**start);
362  double weightSum = (*start)->weight;
363 
364  for(auto it=start+1; it!=end; ++it){
365  sum += (*it)->weight * f(**it);
366  weightSum += (*it)->weight;
367  }
368  return std::make_pair(weightSum, sum);
369 
370  // Otherwise, it's more numerically stable to add things pairwise
371  }else{
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);
377 
378  return std::make_pair(weight1+weight2, (sum1+sum2).eval());
379  }
380  }
381 
385  std::unordered_map<std::string, Eigen::MatrixXd> GetMeta() const;
386 
387  private:
388 
396  bool CreateDataset(std::shared_ptr<muq::Utilities::HDF5File> hdf5file, std::string const& dataname, int const dataSize, int const totSamps) const;
397  };
398  }
399 }
400 
401 #endif // SAMPLECOLLECTION_H
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
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
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 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 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
Eigen::VectorXd const & operator()(SamplingState const &a)
Eigen::VectorXd const & operator()(SamplingState const &a)
SamplingStatePartialMoment(int blockIndIn, int momentPowerIn, Eigen::VectorXd const &muIn)
Each state is one sample generated by a sampling algorithm.
Definition: SamplingState.h:31