|
| 1 | +# API |
| 2 | + |
| 3 | +Part of the API of DynamicPPL is defined in the more lightweight interface package [AbstractPPL.jl](https://github.com/TuringLang/AbstractPPL.jl) and reexported here. |
| 4 | + |
| 5 | +## Model |
| 6 | + |
| 7 | +### Macros |
| 8 | + |
| 9 | +A core component of DynamicPPL is the [`@model`](@ref) macro. |
| 10 | +It can be used to define probabilistic models in an intuitive way by specifying random variables and their distributions with `~` statements. |
| 11 | +These statements are rewritten by `@model` as calls of [internal functions](@ref model_internal) for sampling the variables and computing their log densities. |
| 12 | + |
| 13 | +```@docs |
| 14 | +@model |
| 15 | +``` |
| 16 | + |
| 17 | +One can nest models and call another model inside the model function with [`@submodel`](@ref). |
| 18 | + |
| 19 | +```@docs |
| 20 | +@submodel |
| 21 | +``` |
| 22 | + |
| 23 | +### Type |
| 24 | + |
| 25 | +A [`Model`](@ref) can be created by calling the model function, as defined by [`@model`](@ref). |
| 26 | + |
| 27 | +```@docs |
| 28 | +Model |
| 29 | +``` |
| 30 | + |
| 31 | +[`Model`](@ref)s are callable structs. |
| 32 | + |
| 33 | +```@docs |
| 34 | +Model() |
| 35 | +``` |
| 36 | + |
| 37 | +Basic properties of a model can be accessed with [`getargnames`](@ref), [`getmissings`](@ref), and [`nameof`](@ref). |
| 38 | + |
| 39 | +```@docs |
| 40 | +nameof(::Model) |
| 41 | +getargnames |
| 42 | +getmissings |
| 43 | +``` |
| 44 | + |
| 45 | +## Evaluation |
| 46 | + |
| 47 | +With [`rand`](@ref) one can draw samples from the prior distribution of a [`Model`](@ref). |
| 48 | + |
| 49 | +```@docs |
| 50 | +rand |
| 51 | +``` |
| 52 | + |
| 53 | +One can also evaluate the log prior, log likelihood, and log joint probability. |
| 54 | + |
| 55 | +```@docs |
| 56 | +logprior |
| 57 | +loglikelihood |
| 58 | +logjoint |
| 59 | +``` |
| 60 | + |
| 61 | +## Condition and decondition |
| 62 | + |
| 63 | +A [`Model`](@ref) can be conditioned on a set of observations with [`AbstractPPL.condition`](@ref) or its alias [`|`](@ref). |
| 64 | + |
| 65 | +```@docs |
| 66 | +|(::Model, ::Any) |
| 67 | +condition |
| 68 | +DynamicPPL.conditioned |
| 69 | +``` |
| 70 | + |
| 71 | +Similarly, one can specify with [`AbstractPPL.decondition`](@ref) that certain, or all, random variables are not observed. |
| 72 | + |
| 73 | +```@docs |
| 74 | +decondition |
| 75 | +``` |
| 76 | + |
| 77 | +## Utilities |
| 78 | + |
| 79 | +It is possible to manually increase (or decrease) the accumulated log density from within a model function. |
| 80 | + |
| 81 | +```@docs |
| 82 | +@addlogprob! |
| 83 | +``` |
| 84 | + |
| 85 | +Return values of the model function for a collection of samples can be obtained with [`generated_quantities`](@ref). |
| 86 | + |
| 87 | +```@docs |
| 88 | +generated_quantities |
| 89 | +``` |
| 90 | + |
| 91 | +For a chain of samples, one can compute the pointwise log-likelihoods of each observed random variable with [`pointwise_loglikelihoods`](@ref). |
| 92 | + |
| 93 | +```@docs |
| 94 | +pointwise_loglikelihoods |
| 95 | +``` |
| 96 | + |
| 97 | +```@docs |
| 98 | +NamedDist |
| 99 | +``` |
| 100 | + |
| 101 | +## Testing Utilities |
| 102 | + |
| 103 | +DynamicPPL provides several demo models and helpers for testing samplers in the `DynamicPPL.TestUtils` submodule. |
| 104 | + |
| 105 | +```@docs |
| 106 | +DynamicPPL.TestUtils.test_sampler_demo_models |
| 107 | +DynamicPPL.TestUtils.test_sampler_continuous |
| 108 | +``` |
| 109 | + |
| 110 | +For every demo model, one can define the true log prior, log likelihood, and log joint probabilities. |
| 111 | + |
| 112 | +```@docs |
| 113 | +DynamicPPL.TestUtils.logprior_true |
| 114 | +DynamicPPL.TestUtils.loglikelihood_true |
| 115 | +DynamicPPL.TestUtils.logjoint_true |
| 116 | +``` |
| 117 | + |
| 118 | +## Advanced |
| 119 | + |
| 120 | +### Variable names |
| 121 | + |
| 122 | +Names and possibly nested indices of variables are described with `AbstractPPL.VarName`. |
| 123 | +They can be defined with `AbstractPPL.@varname`. |
| 124 | +Please see the documentation of [AbstractPPL.jl](https://github.com/TuringLang/AbstractPPL.jl) for further information. |
| 125 | + |
| 126 | +### Data Structures of Variables |
| 127 | + |
| 128 | +DynamicPPL provides different data structures for samples from the model and their log density. |
| 129 | +All of them are subtypes of [`AbstractVarInfo`](@ref). |
| 130 | + |
| 131 | +```@docs |
| 132 | +AbstractVarInfo |
| 133 | +``` |
| 134 | + |
| 135 | +### Common API |
| 136 | + |
| 137 | +```@docs |
| 138 | +getlogp |
| 139 | +setlogp!! |
| 140 | +acclogp!! |
| 141 | +resetlogp!! |
| 142 | +``` |
| 143 | + |
| 144 | +```@docs |
| 145 | +getindex |
| 146 | +push!! |
| 147 | +empty!! |
| 148 | +``` |
| 149 | + |
| 150 | +#### `SimpleVarInfo` |
| 151 | + |
| 152 | +```@docs |
| 153 | +SimpleVarInfo |
| 154 | +``` |
| 155 | + |
| 156 | +#### `VarInfo` |
| 157 | + |
| 158 | +Another data structure is [`VarInfo`](@ref). |
| 159 | + |
| 160 | +```@docs |
| 161 | +VarInfo |
| 162 | +TypedVarInfo |
| 163 | +``` |
| 164 | + |
| 165 | +One main characteristic of [`VarInfo`](@ref) is that samples are stored in a linearized form. |
| 166 | + |
| 167 | +```@docs |
| 168 | +tonamedtuple |
| 169 | +link! |
| 170 | +invlink! |
| 171 | +istrans |
| 172 | +``` |
| 173 | + |
| 174 | +```@docs |
| 175 | +set_flag! |
| 176 | +unset_flag! |
| 177 | +is_flagged |
| 178 | +``` |
| 179 | + |
| 180 | +For Gibbs sampling the following functions were added. |
| 181 | + |
| 182 | +```@docs |
| 183 | +setgid! |
| 184 | +updategid! |
| 185 | +``` |
| 186 | + |
| 187 | +The following functions were used for sequential Monte Carlo methods. |
| 188 | + |
| 189 | +```@docs |
| 190 | +get_num_produce |
| 191 | +set_num_produce! |
| 192 | +increment_num_produce! |
| 193 | +reset_num_produce! |
| 194 | +setorder! |
| 195 | +set_retained_vns_del_by_spl! |
| 196 | +``` |
| 197 | + |
| 198 | +```@docs |
| 199 | +Base.empty! |
| 200 | +``` |
| 201 | + |
| 202 | +### Evaluation Contexts |
| 203 | + |
| 204 | +Internally, both sampling and evaluation of log densities are performed with [`AbstractPPL.evaluate!!`](@ref). |
| 205 | + |
| 206 | +```@docs |
| 207 | +AbstractPPL.evaluate!! |
| 208 | +``` |
| 209 | + |
| 210 | +The behaviour of a model execution can be changed with evaluation contexts that are passed as additional argument to the model function. |
| 211 | +Contexts are subtypes of `AbstractPPL.AbstractContext`. |
| 212 | + |
| 213 | +```@docs |
| 214 | +SamplingContext |
| 215 | +DefaultContext |
| 216 | +LikelihoodContext |
| 217 | +PriorContext |
| 218 | +MiniBatchContext |
| 219 | +PrefixContext |
| 220 | +``` |
| 221 | + |
| 222 | +### Samplers |
| 223 | + |
| 224 | +In DynamicPPL two samplers are defined that are used to initialize unobserved random variables: |
| 225 | +[`SampleFromPrior`](@ref) which samples from the prior distribution, and [`SampleFromUniform`](@ref) which samples from a uniform distribution. |
| 226 | + |
| 227 | +```@docs |
| 228 | +SampleFromPrior |
| 229 | +SampleFromUniform |
| 230 | +``` |
| 231 | + |
| 232 | +Additionally, a generic sampler for inference is implemented. |
| 233 | + |
| 234 | +```@docs |
| 235 | +Sampler |
| 236 | +``` |
| 237 | + |
| 238 | +The default implementation of [`Sampler`](@ref) uses the following unexported functions. |
| 239 | + |
| 240 | +```@docs |
| 241 | +DynamicPPL.initialstep |
| 242 | +DynamicPPL.loadstate |
| 243 | +DynamicPPL.initialsampler |
| 244 | +``` |
| 245 | + |
| 246 | +### [Model-Internal Functions](@id model_internal) |
| 247 | + |
| 248 | +```@docs |
| 249 | +tilde_assume |
| 250 | +dot_tilde_assume |
| 251 | +``` |
| 252 | + |
| 253 | +```@docs |
| 254 | +tilde_observe |
| 255 | +dot_tilde_observe |
| 256 | +``` |
| 257 | + |
0 commit comments