autoppl  v0.8
A C++ template library for probabilistic programming
momentum_handler.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <random>
4 
5 namespace ppl {
6 namespace mcmc {
7 
11 template <class AdapterPolicy>
13 
17 template <>
19 {
21 
22  // Constructor takes in size_t for consistent API with other specializations.
23  MomentumHandler(size_t=0)
24  : dist(0., 1.)
25  {}
26 
27  /*
28  * Sample from N(0, I)
29  */
30  template <class MatType
31  , class GenType>
32  void sample(Eigen::MatrixBase<MatType>& rho,
33  GenType& gen)
34  {
35  rho = MatType::NullaryExpr(rho.rows(),
36  [&]() { return dist(gen); });
37  }
38 
42  template <class MatType>
43  double kinetic(const Eigen::MatrixBase<MatType>& rho) const
44  { return 0.5 * rho.squaredNorm(); }
45 
46  template <class MatType>
47  const MatType& dkinetic_dr(const MatType& rho) const
48  { return rho; }
49 
50 private:
51  std::normal_distribution<> dist;
52 };
53 
57 template <>
59 {
61  using variance_t = Eigen::VectorXd;
62 
63  // initialize m inverse to be identity
64  MomentumHandler(size_t n_params)
65  : dist(0., 1.)
66  , m_inverse_(n_params)
67  {
68  m_inverse_.setOnes();
69  }
70 
74  template <class MatType
75  , class GenType>
76  void sample(Eigen::MatrixBase<MatType>& rho,
77  GenType& gen)
78  {
79  rho = MatType::NullaryExpr(rho.rows(),
80  [&]() { return dist(gen); });
81  rho.array() /= m_inverse_.array().sqrt();
82  }
83 
87  template <class MatType>
88  double kinetic(const Eigen::MatrixBase<MatType>& rho) const
89  { return 0.5 * rho.dot(dkinetic_dr(rho)); }
90 
91  template <class MatType>
92  auto dkinetic_dr(const Eigen::MatrixBase<MatType>& rho) const
93  { return (m_inverse_.array() * rho.array()).matrix(); }
94 
95  variance_t& get_m_inverse() { return m_inverse_; }
96  const variance_t& get_m_inverse() const { return m_inverse_; }
97 
98 private:
99  std::normal_distribution<> dist;
100  variance_t m_inverse_;
101 };
102 
103 } // namespace mcmc
104 } // namespace ppl
ppl::mcmc::MomentumHandler< diag_var >::get_m_inverse
const variance_t & get_m_inverse() const
Definition: momentum_handler.hpp:96
ppl::mcmc::MomentumHandler< diag_var >::get_m_inverse
variance_t & get_m_inverse()
Definition: momentum_handler.hpp:95
ppl::mcmc::MomentumHandler< diag_var >::kinetic
double kinetic(const Eigen::MatrixBase< MatType > &rho) const
Definition: momentum_handler.hpp:88
var_adapter.hpp
ppl::unit_var
Definition: var_adapter.hpp:9
ppl::mcmc::MomentumHandler< unit_var >::kinetic
double kinetic(const Eigen::MatrixBase< MatType > &rho) const
Definition: momentum_handler.hpp:43
ppl::mcmc::MomentumHandler< diag_var >::sample
void sample(Eigen::MatrixBase< MatType > &rho, GenType &gen)
Definition: momentum_handler.hpp:76
ppl::mcmc::MomentumHandler< unit_var >::MomentumHandler
MomentumHandler(size_t=0)
Definition: momentum_handler.hpp:23
ppl::mcmc::MomentumHandler< diag_var >::MomentumHandler
MomentumHandler(size_t n_params)
Definition: momentum_handler.hpp:64
ppl::mcmc::MomentumHandler
Definition: momentum_handler.hpp:12
ppl::mcmc::MomentumHandler< unit_var >::sample
void sample(Eigen::MatrixBase< MatType > &rho, GenType &gen)
Definition: momentum_handler.hpp:32
ppl::diag_var
Definition: var_adapter.hpp:10
ppl::mcmc::MomentumHandler< diag_var >::variance_t
Eigen::VectorXd variance_t
Definition: momentum_handler.hpp:61
ppl::mcmc::MomentumHandler< unit_var >::dkinetic_dr
const MatType & dkinetic_dr(const MatType &rho) const
Definition: momentum_handler.hpp:47
ppl
Definition: bounded.hpp:11
ppl::mcmc::MomentumHandler< diag_var >::dkinetic_dr
auto dkinetic_dr(const Eigen::MatrixBase< MatType > &rho) const
Definition: momentum_handler.hpp:92