autoppl  v0.8
A C++ template library for probabilistic programming
ppl::mcmc Namespace Reference

Classes

struct  MomentumHandler
 
struct  MomentumHandler< diag_var >
 
struct  MomentumHandler< unit_var >
 
struct  StepAdapter
 
struct  TreeInput
 
struct  TreeOutput
 
struct  VarAdapter
 
struct  VarAdapter< diag_var >
 
struct  VarAdapter< unit_var >
 

Functions

template<class ExprType , class ConfigType , class Sampler >
MCMCResult base_mcmc (const ExprType &expr, const ConfigType &config, Sampler f)
 
double hamiltonian (double potential, double kinetic)
 
template<class ADExprType , class MatType >
double reset_autodiff (ADExprType &ad_expr, Eigen::MatrixBase< MatType > &adjoints, Eigen::MatrixBase< MatType > &tp_adjoints)
 
template<class ADExprType , class MatType , class MomentumHandlerType >
double leapfrog (ADExprType &ad_expr, Eigen::MatrixBase< MatType > &theta, Eigen::MatrixBase< MatType > &theta_adj, Eigen::MatrixBase< MatType > &tp_adj, Eigen::MatrixBase< MatType > &r, const MomentumHandlerType &m_handler, double epsilon, bool reuse_adj)
 
template<class MatType1 , class MatType2 , class MatType3 >
bool check_entropy (const MatType1 &rho, const MatType2 &p_beg_scaled, const MatType3 &p_end_scaled)
 
template<class InputType , class UniformDistType , class GenType , class MomentumHandlerType >
TreeOutput build_tree (size_t n_params, InputType &input, uint8_t depth, UniformDistType &unif_sampler, GenType &gen, MomentumHandlerType &momentum_handler, double *tree_cache)
 
template<class ADExprType , class MatType , class GenType , class MomentumHandlerType >
double find_reasonable_epsilon (double eps, ADExprType &ad_expr, MatType &theta, MatType &theta_adj, MatType &tp_adj, GenType &gen, MomentumHandlerType &momentum_handler)
 
template<class ProgramType , class OffsetPackType , class MCMCResultType , class NUTSConfigType = NUTSConfig<>>
void nuts_ (ProgramType &program, const NUTSConfigType &config, const OffsetPackType &pack, MCMCResultType &res)
 
template<class ProgramType , class OffsetPackType , class MCMCResultType >
void mh_ (const ProgramType &program, const MHConfig &config, const OffsetPackType &pack, MCMCResultType &res)
 
size_t random_seed ()
 
template<class UniformDistType , class GenType >
bool accept_or_reject (double p, UniformDistType &&unif_sampler, GenType &&gen)
 

Function Documentation

◆ accept_or_reject()

template<class UniformDistType , class GenType >
bool ppl::mcmc::accept_or_reject ( double  p,
UniformDistType &&  unif_sampler,
GenType &&  gen 
)
inline

Accepts or rejects with given probability using UniformDistType object that works with GenType. The uniform sampler must sample from [0,1].

◆ base_mcmc()

template<class ExprType , class ConfigType , class Sampler >
MCMCResult ppl::mcmc::base_mcmc ( const ExprType &  expr,
const ConfigType &  config,
Sampler  f 
)
inline

◆ build_tree()

template<class InputType , class UniformDistType , class GenType , class MomentumHandlerType >
TreeOutput ppl::mcmc::build_tree ( size_t  n_params,
InputType &  input,
uint8_t  depth,
UniformDistType &  unif_sampler,
GenType &  gen,
MomentumHandlerType &  momentum_handler,
double *  tree_cache 
)

Building binary tree for sampling candidates. Helper function to obtain the forward/backward-most position and momentum. Accept/reject policy is based on UniformDistType parameter and GenType

Note that the caller, i.e. nuts(), MUST have theta_adj already pre-computed (theta_adj is a member of input and input will be an instance of TreeInput).

Parameters
n_paramsnumber of (continuous) parameters
inputTreeInput-like input object
depthcurrent depth of building tree
unif_sampleran object like std::uniform_distribution(0,1) used for metropolis acceptance
genrng device
momentum_handlerMomentumHandler-like object to compute kinetic energy and momentum
tree_cachepointer to cache memory that will be used by build_tree. The array of doubles must be of size n_params * 7 * max_depth.

◆ check_entropy()

template<class MatType1 , class MatType2 , class MatType3 >
bool ppl::mcmc::check_entropy ( const MatType1 &  rho,
const MatType2 &  p_beg_scaled,
const MatType3 &  p_end_scaled 
)

Checks if state is entroping based on integrated momentum vector across the path and the scaled momentum at the ends of the path.

Parameters
rhointegrated momentum vector
p_beg_scaledscaled momentum beginning of current path (given the direction)
p_end_scaledscaled momentum end of current path (given the direction).

◆ find_reasonable_epsilon()

template<class ADExprType , class MatType , class GenType , class MomentumHandlerType >
double ppl::mcmc::find_reasonable_epsilon ( double  eps,
ADExprType &  ad_expr,
MatType &  theta,
MatType &  theta_adj,
MatType &  tp_adj,
GenType &  gen,
MomentumHandlerType &  momentum_handler 
)

Finds a reasonable epsilon for NUTS algorithm.

Parameters
epsinitial epsilon (see Gelman's paper)
ad_exprAD expression bound to theta and theta_adj
thetavector of theta values
theta_adjvector of theta adjoints
genrng device
momentum_handlerMomentumHandler-like object

◆ hamiltonian()

double ppl::mcmc::hamiltonian ( double  potential,
double  kinetic 
)
inline

Compute Hamiltonian given potential and kinetic energy. Returns simply the sum of the two.

◆ leapfrog()

template<class ADExprType , class MatType , class MomentumHandlerType >
double ppl::mcmc::leapfrog ( ADExprType &  ad_expr,
Eigen::MatrixBase< MatType > &  theta,
Eigen::MatrixBase< MatType > &  theta_adj,
Eigen::MatrixBase< MatType > &  tp_adj,
Eigen::MatrixBase< MatType > &  r,
const MomentumHandlerType &  m_handler,
double  epsilon,
bool  reuse_adj 
)
inline

Leapfrog algorithm. Expects theta, theta_adj, tp_adj, r to be Eigen column-like objects.

Updates theta, theta_adj, tp_adj, and r to contain the new leaped values and adjoints.

Parameters
ad_exprAD expression representing L(theta) It must be built such that values are read from theta and adjoints are placed into theta_adj.
thetatheta at which we want to start leaping
theta_adjadjoint for theta. If not reusing, resets adjoints first.
tp_adjadjoints for transformed parameters. If not reusing, resets adjoints first.
rmomentum vector to start leaping
m_handlermomentum handler to compute correct dkinetic/dr
epsilonstep size
reuse_adjflag to not compute gradient of L(theta) if user can guarantee that theta_adj currently has it.
Returns
new potential energy (L(theta'))

◆ mh_()

template<class ProgramType , class OffsetPackType , class MCMCResultType >
void ppl::mcmc::mh_ ( const ProgramType &  program,
const MHConfig config,
const OffsetPackType &  pack,
MCMCResultType &  res 
)
inline

Metropolis-Hastings algorithm to sample from posterior distribution. Any variables that model references which are parameters are sampled.

Template Parameters
ProgramTypeprogram expression type
OffsetPackTypeoffset pack type (likely util::OffsetPack)
Parameters
programprogram expression
configconfiguration object
packoffset pack from activating program expression
ressampling result object to populate

◆ nuts_()

template<class ProgramType , class OffsetPackType , class MCMCResultType , class NUTSConfigType = NUTSConfig<>>
void ppl::mcmc::nuts_ ( ProgramType &  program,
const NUTSConfigType &  config,
const OffsetPackType &  pack,
MCMCResultType &  res 
)

No-U-Turn Sampler (NUTS)

User must ensure that the program does not have any discrete parameters. Discrete data is allowed.

Parameters
programprogram expression used to determine log-pdf
configNUTS configuration object
packoffset pack result of activating program. It will likely be util::OffsetPack where each offset value is equivalent to the total number of values needed, i.e. if pack.uc_offset is 10, there is exactly 10 unconstrained values for the program.
resresult object of calling NUTS that will be populated with samples and other information.

◆ random_seed()

size_t ppl::mcmc::random_seed ( )
inline

Get current time in milliseconds for random seeding.

◆ reset_autodiff()

template<class ADExprType , class MatType >
double ppl::mcmc::reset_autodiff ( ADExprType &  ad_expr,
Eigen::MatrixBase< MatType > &  adjoints,
Eigen::MatrixBase< MatType > &  tp_adjoints 
)
inline

Helper function for leapfrog algorithm. Resets adjoints and then differentiates AD expression.

Parameters
ad_exprAD expression to differentiate
adjointsEigen generic matrix type that supports member fn "setZero"
tp_adjointsEigen generic matrix type that supports member fn "setZero"
Returns
result of calling ad::autodiff on ad_expr after resetting adjoints