Part of the API of DynamicPPL is defined in the more lightweight interface package AbstractPPL.jl and reexported here.
A core component of DynamicPPL is the @model
macro.
It can be used to define probabilistic models in an intuitive way by specifying random variables and their distributions with ~
statements.
These statements are rewritten by @model
as calls of [internal functions](@ref model_internal) for sampling the variables and computing their log densities.
@model
One can nest models and call another model inside the model function with @submodel
.
@submodel
A Model
can be created by calling the model function, as defined by @model
.
Model
Model
s are callable structs.
Model()
Basic properties of a model can be accessed with getargnames
, getmissings
, and nameof
.
nameof(::Model)
getargnames
getmissings
With rand
one can draw samples from the prior distribution of a Model
.
rand
One can also evaluate the log prior, log likelihood, and log joint probability.
logprior
loglikelihood
logjoint
The LogDensityProblems.jl interface is also supported by simply wrapping a Model
in a DynamicPPL.LogDensityFunction
:
DynamicPPL.LogDensityFunction
A Model
can be conditioned on a set of observations with AbstractPPL.condition
or its alias |
.
|(::Model, ::Any)
condition
DynamicPPL.conditioned
Similarly, one can specify with AbstractPPL.decondition
that certain, or all, random variables are not observed.
decondition
It is possible to manually increase (or decrease) the accumulated log density from within a model function.
@addlogprob!
Return values of the model function for a collection of samples can be obtained with generated_quantities
.
generated_quantities
For a chain of samples, one can compute the pointwise log-likelihoods of each observed random variable with pointwise_loglikelihoods
.
pointwise_loglikelihoods
Sometimes it can be useful to extract the priors of a model. This is the possible using extract_priors
.
extract_priors
NamedDist
DynamicPPL provides several demo models and helpers for testing samplers in the DynamicPPL.TestUtils
submodule.
DynamicPPL.TestUtils.test_sampler
DynamicPPL.TestUtils.test_sampler_on_demo_models
DynamicPPL.TestUtils.test_sampler_continuous
DynamicPPL.TestUtils.marginal_mean_of_samples
DynamicPPL.TestUtils.DEMO_MODELS
For every demo model, one can define the true log prior, log likelihood, and log joint probabilities.
DynamicPPL.TestUtils.logprior_true
DynamicPPL.TestUtils.loglikelihood_true
DynamicPPL.TestUtils.logjoint_true
And in the case where the model includes constrained variables, it can also be useful to define
DynamicPPL.TestUtils.logprior_true_with_logabsdet_jacobian
DynamicPPL.TestUtils.logjoint_true_with_logabsdet_jacobian
Finally, the following methods can also be of use:
DynamicPPL.TestUtils.varnames
DynamicPPL.TestUtils.posterior_mean
DynamicPPL.TestUtils.setup_varinfos
DynamicPPL.TestUtils.update_values!!
DynamicPPL.TestUtils.test_values
Names and possibly nested indices of variables are described with AbstractPPL.VarName
.
They can be defined with AbstractPPL.@varname
.
Please see the documentation of AbstractPPL.jl for further information.
DynamicPPL provides different data structures for samples from the model and their log density.
All of them are subtypes of AbstractVarInfo
.
AbstractVarInfo
getlogp
setlogp!!
acclogp!!
resetlogp!!
keys
getindex
DynamicPPL.getindex_raw
push!!
empty!!
isempty
values_as
DynamicPPL.AbstractTransformation
DynamicPPL.NoTransformation
DynamicPPL.DynamicTransformation
DynamicPPL.StaticTransformation
DynamicPPL.istrans
DynamicPPL.settrans!!
DynamicPPL.transformation
DynamicPPL.link!!
DynamicPPL.invlink!!
DynamicPPL.default_transformation
DynamicPPL.maybe_invlink_before_eval!!
DynamicPPL.reconstruct
DynamicPPL.unflatten
DynamicPPL.tonamedtuple
SimpleVarInfo
Another data structure is VarInfo
.
VarInfo
TypedVarInfo
One main characteristic of VarInfo
is that samples are stored in a linearized form.
link!
invlink!
set_flag!
unset_flag!
is_flagged
For Gibbs sampling the following functions were added.
setgid!
updategid!
The following functions were used for sequential Monte Carlo methods.
get_num_produce
set_num_produce!
increment_num_produce!
reset_num_produce!
setorder!
set_retained_vns_del_by_spl!
Base.empty!
Internally, both sampling and evaluation of log densities are performed with AbstractPPL.evaluate!!
.
AbstractPPL.evaluate!!
The behaviour of a model execution can be changed with evaluation contexts that are passed as additional argument to the model function.
Contexts are subtypes of AbstractPPL.AbstractContext
.
SamplingContext
DefaultContext
LikelihoodContext
PriorContext
MiniBatchContext
PrefixContext
In DynamicPPL two samplers are defined that are used to initialize unobserved random variables:
SampleFromPrior
which samples from the prior distribution, and SampleFromUniform
which samples from a uniform distribution.
SampleFromPrior
SampleFromUniform
Additionally, a generic sampler for inference is implemented.
Sampler
The default implementation of Sampler
uses the following unexported functions.
DynamicPPL.initialstep
DynamicPPL.loadstate
DynamicPPL.initialsampler
tilde_assume
dot_tilde_assume
tilde_observe
dot_tilde_observe