MUQ  0.4.3
Distribution.h
Go to the documentation of this file.
1 #ifndef DISTRIBUTION_H_
2 #define DISTRIBUTION_H_
3 
6 
8 
9 #include <Eigen/Core>
10 #include <vector>
11 
12 namespace muq {
13  namespace Modeling {
14 
15  class Density;
16  class RandomVariable;
17 
18  class Distribution : public std::enable_shared_from_this<Distribution> {
19  public:
20  friend class Density;
21  friend class RandomVariable;
22 
23  Distribution(unsigned int varSizeIn,
24  Eigen::VectorXi const& hyperSizesIn = Eigen::VectorXi()) : varSize(varSizeIn),
25  hyperSizes(hyperSizesIn){};
26 
27  virtual ~Distribution() = default;
28 
30 
35  virtual double LogDensity(ref_vector<Eigen::VectorXd> const& inputs);
36  virtual double LogDensity(std::vector<Eigen::VectorXd> const& inputs){return LogDensity(ToRefVector(inputs));};;
37  VARIADIC_TO_REFVECTOR(LogDensity, Eigen::VectorXd, double);
38 
39 
40  virtual Eigen::VectorXd GradLogDensity(unsigned int wrt, std::vector<Eigen::VectorXd> const& inputs){return GradLogDensity(wrt, ToRefVector(inputs));};;
41  virtual Eigen::VectorXd GradLogDensity(unsigned int wrt, ref_vector<Eigen::VectorXd> const& inputs);
42 
43  template<typename... Args>
44  Eigen::VectorXd GradLogDensity(unsigned int wrt, Args... args) {
46  return GradLogDensity(wrt, inputs, args...);
47  }
48 
49 
50  virtual Eigen::VectorXd ApplyLogDensityHessian(unsigned int const inWrt1,
51  unsigned int const inWrt2,
52  ref_vector<Eigen::VectorXd> const& input,
53  Eigen::VectorXd const& vec);
54 
55 
56  virtual Eigen::VectorXd ApplyLogDensityHessian(unsigned int const inWrt1,
57  unsigned int const inWrt2,
58  std::vector<Eigen::VectorXd> const& input,
59  Eigen::VectorXd const& vec){return ApplyLogDensityHessian(inWrt1,inWrt2,ToRefVector(input),vec);};
60 
61 
63 
68  Eigen::VectorXd Sample(ref_vector<Eigen::VectorXd> const& inputs);
69  Eigen::VectorXd Sample(std::vector<Eigen::VectorXd> const& inputs){return Sample(ToRefVector(inputs));};
70 
72 
76  Eigen::VectorXd Sample();
77 
78  VARIADIC_TO_REFVECTOR(Sample, Eigen::VectorXd, Eigen::VectorXd);
79 
80 
110  std::shared_ptr<Density> AsDensity();
111 
143  std::shared_ptr<RandomVariable> AsVariable();
144 
145  const unsigned int varSize;
146  const Eigen::VectorXi hyperSizes;
147 
148  protected:
149 
151 
156  virtual double LogDensityImpl(ref_vector<Eigen::VectorXd> const& inputs){
157  throw muq::NotImplementedError(std::string("LogDensityImpl function has not been implemented for class ") + typeid(*this).name());
158  return 0;
159  };
160 
161 
162  virtual Eigen::VectorXd GradLogDensityImpl(unsigned int wrt, ref_vector<Eigen::VectorXd> const& inputs);
163 
164  virtual Eigen::VectorXd ApplyLogDensityHessianImpl(unsigned int wrt1, unsigned int wrt2, ref_vector<Eigen::VectorXd> const& inputs, Eigen::VectorXd const& vec);
165 
167 
170  virtual Eigen::VectorXd SampleImpl(ref_vector<Eigen::VectorXd> const& inputs){
171  throw muq::NotImplementedError(std::string("SampleImpl function has not been implemented for class ") + typeid(*this).name());
172  return Eigen::VectorXd();
173  };
174 
175  ref_vector<const Eigen::VectorXd> ToRefVector(std::vector<Eigen::VectorXd> const& anyVec) const;
176 
177  private:
178 
179  template<typename... Args>
180  Eigen::VectorXd GradLogDensity(unsigned int wrt, ref_vector<Eigen::VectorXd>& inputs, Eigen::VectorXd const& in, Args... args) {
181  inputs.push_back(std::cref(in));
182  return GradLogDensity(wrt, inputs, args...);
183  }
184  };
185  } // namespace Modeling
186 } // namespace muq
187 
188 #endif
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
Eigen::VectorXd Sample(std::vector< Eigen::VectorXd > const &inputs)
Definition: Distribution.h:69
virtual Eigen::VectorXd ApplyLogDensityHessian(unsigned int const inWrt1, unsigned int const inWrt2, std::vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec)
Definition: Distribution.h:56
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.
virtual double LogDensity(std::vector< Eigen::VectorXd > const &inputs)
Definition: Distribution.h:36
Eigen::VectorXd GradLogDensity(unsigned int wrt, Args... args)
Definition: Distribution.h:44
Eigen::VectorXd Sample()
Sample the distribution with no inputs.
VARIADIC_TO_REFVECTOR(Sample, Eigen::VectorXd, Eigen::VectorXd)
virtual Eigen::VectorXd GradLogDensity(unsigned int wrt, std::vector< Eigen::VectorXd > const &inputs)
Definition: Distribution.h:40
virtual ~Distribution()=default
Distribution(unsigned int varSizeIn, Eigen::VectorXi const &hyperSizesIn=Eigen::VectorXi())
Definition: Distribution.h:23
ref_vector< const Eigen::VectorXd > ToRefVector(std::vector< Eigen::VectorXd > const &anyVec) const
VARIADIC_TO_REFVECTOR(LogDensity, Eigen::VectorXd, double)
std::shared_ptr< RandomVariable > AsVariable()
Returns a random variable built from this distribution.
Eigen::VectorXd GradLogDensity(unsigned int wrt, ref_vector< Eigen::VectorXd > &inputs, Eigen::VectorXd const &in, Args... args)
Definition: Distribution.h:180
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)
Class for virtual base functions that are not implemented.
Definition: Exceptions.h:22
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37