Skip to content

Commit b55c1e1

Browse files
committed
Document
1 parent 001a05a commit b55c1e1

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

docs/src/api.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,27 @@ SamplingContext
470470
DefaultContext
471471
PrefixContext
472472
ConditionContext
473+
InitContext
474+
```
475+
476+
### VarInfo initialisation
477+
478+
`InitContext` is used to initialise, or overwrite, values in a VarInfo.
479+
480+
To accomplish this, an initialisation _strategy_ is required, which defines how new values are to be obtained.
481+
There are three concrete strategies provided in DynamicPPL:
482+
483+
```@docs
484+
PriorInit
485+
UniformInit
486+
ParamsInit
487+
```
488+
489+
If you wish to write your own, you have to subtype [`DynamicPPL.AbstractInitStrategy`](@ref) and implement the `init` method.
490+
491+
```@docs
492+
DynamicPPL.AbstractInitStrategy
493+
DynamicPPL.init
473494
```
474495

475496
### Samplers

src/contexts/init.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ init(rng::Random.AbstractRNG, ::VarName, dist::Distribution, ::PriorInit) = rand
3232
UniformInit(lower, upper)
3333
3434
Obtain new values by first transforming the distribution of the random variable
35-
to unconstrained space, and then sampling a value uniformly between `lower` and
36-
`upper`.
35+
to unconstrained space, then sampling a value uniformly between `lower` and
36+
`upper`, and transforming that value back to the original space.
3737
38-
If unspecified, defaults to `(lower, upper) = (-2, 2)`, which mimics Stan's
39-
default initialisation strategy.
38+
If `lower` and `upper` are unspecified, they default to `(-2, 2)`, which mimics
39+
Stan's default initialisation strategy.
4040
4141
Requires that `lower <= upper`.
4242
@@ -91,17 +91,17 @@ struct ParamsInit{P,S<:AbstractInitStrategy} <: AbstractInitStrategy
9191
end
9292
end
9393
function init(rng::Random.AbstractRNG, vn::VarName, dist::Distribution, p::ParamsInit)
94-
# TODO(penelopeysm): We should do a check to make sure that all of the
95-
# parameters in `p.params` were actually used, and either warn or error if
96-
# they aren't. This is non-trivial (we need to use something like
97-
# varname_leaves), so I'm going to defer it to a later PR.
94+
# TODO(penelopeysm): It would be nice to do a check to make sure that all
95+
# of the parameters in `p.params` were actually used, and either warn or
96+
# error if they aren't. This is actually quite non-trivial though because
97+
# the structure of Dicts in particular can have arbitrary nesting.
9898
return if hasvalue(p.params, vn, dist)
9999
x = getvalue(p.params, vn, dist)
100100
if x === missing
101101
init(rng, vn, dist, p.default)
102102
else
103-
# TODO(penelopeysm): We could also check that the type of x matches
104-
# the dist?
103+
# TODO(penelopeysm): Since x is user-supplied, maybe we could also
104+
# check here that the type / size of x matches the dist?
105105
x
106106
end
107107
else

0 commit comments

Comments
 (0)