MUQ  0.4.3
Density.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
5 DensityBase::DensityBase(Eigen::VectorXi const& inputSizes) : Distribution(inputSizes(0), inputSizes.tail(inputSizes.size()-1)),
6  ModPiece(inputSizes, Eigen::VectorXi::Ones(1))
7 {};
8 
10 {
11  outputs.resize(1);
12  outputs.at(0) = LogDensity(inputs) * Eigen::VectorXd::Ones(1);
13 }
14 
15 void DensityBase::GradientImpl(unsigned int const outputDimWrt,
16  unsigned int const inputDimWrt,
17  ref_vector<Eigen::VectorXd> const& input,
18  Eigen::VectorXd const& sensitivity)
19 {
20  gradient = GradLogDensity(inputDimWrt,input)*sensitivity(0);
21 }
22 
23 void DensityBase::JacobianImpl(unsigned int const outputDimWrt,
24  unsigned int const inputDimWrt,
25  ref_vector<Eigen::VectorXd> const& input)
26 {
27  jacobian = GradLogDensity(inputDimWrt,input).transpose();
28 }
29 
30 
31 void DensityBase::ApplyJacobianImpl(unsigned int const outputDimWrt,
32  unsigned int const inputDimWrt,
33  ref_vector<Eigen::VectorXd> const& input,
34  Eigen::VectorXd const& vec)
35 {
36  jacobianAction = GradLogDensity(inputDimWrt, input).transpose()*vec;
37 }
38 
39 void DensityBase::ApplyHessianImpl(unsigned int const outWrt,
40  unsigned int const inWrt1,
41  unsigned int const inWrt2,
42  ref_vector<Eigen::VectorXd> const& input,
43  Eigen::VectorXd const& sens,
44  Eigen::VectorXd const& vec)
45 {
46  if(inWrt2<inputSizes.size()){
47  hessAction = sens(0)*ApplyLogDensityHessian(inWrt1,inWrt2,input,vec);
48  }else{
49  hessAction = GradLogDensity(inWrt1,input);
50  }
51 }
52 
53 
54 Density::Density(std::shared_ptr<Distribution> distIn) : DensityBase(GetInputSizes(distIn)), dist(distIn) {}
55 
57 {
58  return dist->LogDensity(inputs);
59 }
60 
61 Eigen::VectorXd Density::GradLogDensityImpl(unsigned int wrt, ref_vector<Eigen::VectorXd> const& inputs)
62 {
63  return dist->GradLogDensityImpl(wrt, inputs);
64 }
65 
66 Eigen::VectorXd Density::SampleImpl(ref_vector<Eigen::VectorXd> const& inputs)
67 {
68  return dist->SampleImpl(inputs);
69 }
70 
71 Eigen::VectorXd Density::ApplyLogDensityHessianImpl(unsigned int const inWrt1,
72  unsigned int const inWrt2,
73  ref_vector<Eigen::VectorXd> const& input,
74  Eigen::VectorXd const& vec)
75 {
76  return dist->ApplyLogDensityHessianImpl(inWrt1,inWrt2,input,vec);
77 }
78 
79 Eigen::VectorXi Density::GetInputSizes(std::shared_ptr<Distribution> distIn)
80 {
81  Eigen::VectorXi output(1+distIn->hyperSizes.size());
82  output(0) = distIn->varSize;
83  output.tail(distIn->hyperSizes.size()) = distIn->hyperSizes;
84  return output;
85 }
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input) override
Definition: Density.cpp:23
virtual void ApplyJacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec) override
Definition: Density.cpp:31
virtual void ApplyHessianImpl(unsigned int const outWrt, unsigned int const inWrt1, unsigned int const inWrt2, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec) override
Definition: Density.cpp:39
virtual void GradientImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity) override
Definition: Density.cpp:15
DensityBase(Eigen::VectorXi const &inputSizes)
Definition: Density.cpp:5
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Definition: Density.cpp:9
static Eigen::VectorXi GetInputSizes(std::shared_ptr< Distribution > distIn)
Definition: Density.cpp:79
virtual Eigen::VectorXd GradLogDensityImpl(unsigned int wrt, ref_vector< Eigen::VectorXd > const &inputs) override
Definition: Density.cpp:61
virtual Eigen::VectorXd SampleImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Sample the distribution.
Definition: Density.cpp:66
std::shared_ptr< Distribution > dist
Definition: Density.h:52
virtual double LogDensityImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Implement the log-density.
Definition: Density.cpp:56
virtual Eigen::VectorXd ApplyLogDensityHessianImpl(unsigned int const inWrt1, unsigned int const inWrt2, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec) override
Definition: Density.cpp:71
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 Eigen::VectorXd GradLogDensity(unsigned int wrt, std::vector< Eigen::VectorXd > const &inputs)
Definition: Distribution.h:40
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
Eigen::MatrixXd jacobian
Definition: ModPiece.h:506
const Eigen::VectorXi inputSizes
Definition: ModPiece.h:469
std::vector< Eigen::VectorXd > outputs
Definition: ModPiece.h:503
Eigen::VectorXd gradient
Definition: ModPiece.h:504
Eigen::VectorXd hessAction
Definition: ModPiece.h:507
Eigen::VectorXd jacobianAction
Definition: ModPiece.h:505
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37