MUQ  0.4.3
Distribution.cpp
Go to the documentation of this file.
4 
6 
7 using namespace muq::Modeling;
8 
9 std::shared_ptr<Density> Distribution::AsDensity()
10 {
11  return std::make_shared<Density>(shared_from_this());
12 }
13 
14 std::shared_ptr<RandomVariable> Distribution::AsVariable()
15 {
16  return std::make_shared<RandomVariable>(shared_from_this());
17 }
18 
19 ref_vector<const Eigen::VectorXd> Distribution::ToRefVector(std::vector<Eigen::VectorXd> const& vec) const {
20 
22  refs.reserve(vec.size());
23 
24  // populate the input vector
25  for(int i=0; i<vec.size(); ++i)
26  refs.push_back(std::cref(vec.at(i)));
27 
28  return refs;
29 }
30 
32  return LogDensityImpl(inputs);
33 }
34 
35 
36 Eigen::VectorXd Distribution::Sample(ref_vector<Eigen::VectorXd> const& inputs) {
37  return SampleImpl(inputs);
38 }
39 
40 Eigen::VectorXd Distribution::Sample() {
42 }
43 
44 Eigen::VectorXd Distribution::GradLogDensity(unsigned int wrt, ref_vector<Eigen::VectorXd> const& inputs)
45 {
46  assert(wrt<inputs.size());
47  return GradLogDensityImpl(wrt, inputs);
48 }
49 
50 Eigen::VectorXd Distribution::GradLogDensityImpl(unsigned int wrt,
51  ref_vector<Eigen::VectorXd> const& inputs)
52 {
53  // Default to finite difference
54  ref_vector<Eigen::VectorXd> newInputs = inputs;
55  Eigen::VectorXd newIn = newInputs.at(wrt).get();
56  newInputs.at(wrt) = std::cref(newIn);
57 
58  const double f0 = LogDensity(newInputs);
59 
60  const int dim = inputs.at(wrt).get().size();
61 
62  Eigen::VectorXd output(dim);
63  for(int i=0; i<dim; ++i){
64 
65  double eps = std::max(1e-8, 1e-10*std::abs(inputs.at(wrt).get()(i)));
66  newIn(i) += eps;
67 
68  double newF = LogDensity(newInputs);
69  output(i) = (newF-f0)/eps;
70  newIn(i) = inputs.at(wrt).get()(i);
71  }
72 
73  return output;
74 }
75 
76 Eigen::VectorXd Distribution::ApplyLogDensityHessian(unsigned int const inWrt1,
77  unsigned int const inWrt2,
78  ref_vector<Eigen::VectorXd> const& input,
79  Eigen::VectorXd const& vec)
80 {
81  assert(inWrt1<hyperSizes.size()+1);
82  assert(inWrt2<hyperSizes.size()+1);
83  assert(input.size() == hyperSizes.size()+1);
84  if(inWrt2==0){
85  assert(vec.size()==varSize);
86  }else{
87  assert(vec.size()==hyperSizes(inWrt2-1));
88  }
89 
90  return ApplyLogDensityHessianImpl(inWrt1,inWrt2,input,vec);
91 }
92 
93 Eigen::VectorXd Distribution::ApplyLogDensityHessianImpl(unsigned int const inWrt1,
94  unsigned int const inWrt2,
95  ref_vector<Eigen::VectorXd> const& input,
96  Eigen::VectorXd const& vec)
97 {
98  const double stepSize = 1e-8 / vec.norm();
99  Eigen::VectorXd grad1 = GradLogDensity(inWrt1, input);
100  Eigen::VectorXd grad2;
101 
102  ref_vector<Eigen::VectorXd> input2 = input;
103  Eigen::VectorXd x2 = input.at(inWrt2).get() + stepSize * vec;
104  input2.at(inWrt2) = std::cref(x2);
105  grad2 = GradLogDensity(inWrt1, input2);
106 
107  return (grad2 - grad1)/stepSize;
108 }
virtual Eigen::VectorXd GradLogDensityImpl(unsigned int wrt, ref_vector< Eigen::VectorXd > const &inputs)
virtual double LogDensityImpl(ref_vector< Eigen::VectorXd > const &inputs)
Implement the log-density.
Definition: Distribution.h:156
std::shared_ptr< Density > AsDensity()
Returns a density built from this distribution.
Definition: Distribution.cpp:9
virtual Eigen::VectorXd SampleImpl(ref_vector< Eigen::VectorXd > const &inputs)
Sample the distribution.
Definition: Distribution.h:170
virtual Eigen::VectorXd ApplyLogDensityHessian(unsigned int const inWrt1, unsigned int const inWrt2, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec)
virtual double LogDensity(ref_vector< Eigen::VectorXd > const &inputs)
Evaluate the log-density.
Eigen::VectorXd Sample()
Sample the distribution with no inputs.
virtual Eigen::VectorXd GradLogDensity(unsigned int wrt, std::vector< Eigen::VectorXd > const &inputs)
Definition: Distribution.h:40
ref_vector< const Eigen::VectorXd > ToRefVector(std::vector< Eigen::VectorXd > const &anyVec) const
std::shared_ptr< RandomVariable > AsVariable()
Returns a random variable built from this distribution.
const unsigned int varSize
Definition: Distribution.h:145
const Eigen::VectorXi hyperSizes
Definition: Distribution.h:146
virtual Eigen::VectorXd ApplyLogDensityHessianImpl(unsigned int wrt1, unsigned int wrt2, ref_vector< Eigen::VectorXd > const &inputs, Eigen::VectorXd const &vec)
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37