MUQ  0.4.3
UniformBox.cpp
Go to the documentation of this file.
2 
4 
5 using namespace muq::Utilities;
6 using namespace muq::Modeling;
7 
8 UniformBox::UniformBox(Eigen::MatrixXd const& boundsIn) : Distribution(boundsIn.rows(),
9  Eigen::VectorXi()),
10  bounds(boundsIn),
11  volume(ComputeVolume(boundsIn)) {
12 
13 }
14 
15 double UniformBox::ComputeVolume(Eigen::MatrixXd const& boundsIn)
16 {
17  return (boundsIn.col(1)-boundsIn.col(0)).prod();
18 }
19 
20 Eigen::MatrixXd UniformBox::CreateBoundsPairs(std::vector<std::pair<double,double>> const& boundsVec)
21 {
22  Eigen::MatrixXd bounds(boundsVec.size(),2);
23  for(int i=0; i<boundsVec.size(); ++i){
24  bounds(i,0) = boundsVec.at(i).first;
25  bounds(i,1) = boundsVec.at(i).second;
26  }
27 
28  return bounds;
29 }
30 
31 Eigen::MatrixXd UniformBox::CreateBoundsDouble(std::vector<double> const& boundsVec)
32 {
33  assert(boundsVec.size()%2==0);
34 
35  Eigen::MatrixXd bounds(boundsVec.size()/2,2);
36  for(int i=0; i<boundsVec.size()/2; ++i){
37  bounds(i,0) = boundsVec.at(2*i);
38  bounds(i,1) = boundsVec.at(2*i+1);
39  }
40 
41  return bounds;
42 }
43 
45  // loop though each dimension
46  for( unsigned int i=0; i<bounds.rows(); ++i ) {
47  // get the value of the point for this dimension
48  const double x = inputs.at(0).get()(i);
49 
50  // if this dimension is outside the hypercube
51  if( x<bounds(i,0) || x>bounds(i,1) ) {
52  // return negative infinity
53  return -std::numeric_limits<double>::infinity();
54  }
55  }
56 
57  // the point is inside the domain...
58  return -log(volume);
59 }
60 
61 Eigen::VectorXd UniformBox::SampleImpl(ref_vector<Eigen::VectorXd> const& inputs) {
62  assert(bounds.rows()>0);
63 
64  Eigen::VectorXd sample;
65  sample = bounds.col(0).array() + (bounds.col(1)-bounds.col(0)).array() * RandomGenerator::GetUniform(bounds.rows()).array();
66  return sample;
67 }
virtual Eigen::VectorXd SampleImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Sample the distribution.
Definition: UniformBox.cpp:61
static Eigen::MatrixXd CreateBoundsPairs(std::vector< std::pair< double, double >> const &bounds)
Definition: UniformBox.cpp:20
STATIC_VARIADIC_TO_VECTOR(CreateBoundsPairs,(std::pair< double, double >),(Eigen::MatrixXd)) static Eigen static STATIC_VARIADIC_TO_VECTOR(CreateBoundsDouble,(double),(Eigen::MatrixXd)) const Eigen double ComputeVolume(Eigen::MatrixXd const &boundsIn)
A matrix describing the bounding box.
Definition: UniformBox.cpp:15
virtual double LogDensityImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Evaluate the log-density.
Definition: UniformBox.cpp:44
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37