DynamicPPL.DefaultContext
— Typestruct DefaultContext <: AbstractContext end
The DefaultContext
is used by default to compute log the joint probability of the data and parameters when running the model.
DynamicPPL.LikelihoodContext
— Typestruct LikelihoodContext{Tvars} <: AbstractContext
vars::Tvars
end
The LikelihoodContext
enables the computation of the log likelihood of the parameters when running the model. vars
can be used to evaluate the log likelihood for specific values of the model's parameters. If vars
is nothing
, the parameter values inside the VarInfo
will be used by default.
DynamicPPL.Metadata
— TypeThe Metadata
struct stores some metadata about the parameters of the model. This helps query certain information about a variable, such as its distribution, which samplers sample this variable, its value and whether this value is transformed to real space or not.
Let md
be an instance of Metadata
:
md.vns
is the vector of allVarName
instances.md.idcs
is the dictionary that maps eachVarName
instance to its index in
md.vns
, md.ranges
md.dists
, md.orders
and md.flags
.
md.vns[md.idcs[vn]] == vn
.md.dists[md.idcs[vn]]
is the distribution ofvn
.md.gids[md.idcs[vn]]
is the set of algorithms used to samplevn
. This is used in
the Gibbs sampling process.
md.orders[md.idcs[vn]]
is the number ofobserve
statements beforevn
is sampled.md.ranges[md.idcs[vn]]
is the index range ofvn
inmd.vals
.md.vals[md.ranges[md.idcs[vn]]]
is the vector of values of corresponding tovn
.md.flags
is a dictionary of true/false flags.md.flags[flag][md.idcs[vn]]
is the
value of flag
corresponding to vn
.
To make md::Metadata
type stable, all the md.vns
must have the same symbol and distribution type. However, one can have a Julia variable, say x
, that is a matrix or a hierarchical array sampled in partitions, e.g. x[1][:] ~ MvNormal(zeros(2), 1.0); x[2][:] ~ MvNormal(ones(2), 1.0)
, and is managed by a single md::Metadata
so long as all the distributions on the RHS of ~
are of the same type. Type unstable Metadata
will still work but will have inferior performance. When sampling, the first iteration uses a type unstable Metadata
for all the variables then a specialized Metadata
is used for each symbol along with a function barrier to make the rest of the sampling type stable.
DynamicPPL.Metadata
— MethodMetadata()
Construct an empty type unstable instance of Metadata
.
DynamicPPL.MiniBatchContext
— Typestruct MiniBatchContext{Tctx, T} <: AbstractContext
ctx::Tctx
loglike_scalar::T
end
The MiniBatchContext
enables the computation of log(prior) + s * log(likelihood of a batch)
when running the model, where s
is the loglike_scalar
field, typically equal to the number of data points / batch size
. This is useful in batch-based stochastic gradient descent algorithms to be optimizing log(prior) + log(likelihood of all the data points)
in the expectation.
DynamicPPL.Model
— Typestruct Model{F,argnames,defaultnames,missings,Targs,Tdefaults}
name::Symbol
f::F
args::NamedTuple{argnames,Targs}
defaults::NamedTuple{defaultnames,Tdefaults}
end
A Model
struct with model evaluation function of type F
, arguments of names argnames
types Targs
, default arguments of names defaultnames
with types Tdefaults
, and missing arguments missings
.
Here argnames
, defaultargnames
, and missings
are tuples of symbols, e.g. (:a, :b)
.
An argument with a type of Missing
will be in missings
by default. However, in non-traditional use-cases missings
can be defined differently. All variables in missings
are treated as random variables rather than observations.
The default arguments are used internally when constructing instances of the same model with different arguments.
Examples
julia> Model(f, (x = 1.0, y = 2.0))
Model{typeof(f),(:x, :y),(),(),Tuple{Float64,Float64},Tuple{}}(f, (x = 1.0, y = 2.0), NamedTuple())
julia> Model(f, (x = 1.0, y = 2.0), (x = 42,))
Model{typeof(f),(:x, :y),(:x,),(),Tuple{Float64,Float64},Tuple{Int64}}(f, (x = 1.0, y = 2.0), (x = 42,))
julia> Model{(:y,)}(f, (x = 1.0, y = 2.0), (x = 42,)) # with special definition of missings
Model{typeof(f),(:x, :y),(:x,),(:y,),Tuple{Float64,Float64},Tuple{Int64}}(f, (x = 1.0, y = 2.0), (x = 42,))
DynamicPPL.Model
— Type(model::Model)([rng, varinfo, sampler, context])
Sample from the model
using the sampler
with random number generator rng
and the context
, and store the sample and log joint probability in varinfo
.
The method resets the log joint probability of varinfo
and increases the evaluation number of sampler
.
DynamicPPL.Model
— MethodModel(name::Symbol, f, args::NamedTuple[, defaults::NamedTuple = ()])
Create a model of name name
with evaluation function f
and missing arguments deduced from args
.
Default arguments defaults
are used internally when constructing instances of the same model with different arguments.
DynamicPPL.NamedDist
— TypeA named distribution that carries the name of the random variable with it.
DynamicPPL.PriorContext
— Typestruct PriorContext{Tvars} <: AbstractContext
vars::Tvars
end
The PriorContext
enables the computation of the log prior of the parameters vars
when running the model.
DynamicPPL.SampleFromUniform
— TypeRobust initialization method for model parameters in Hamiltonian samplers.
DynamicPPL.Sampler
— TypeSampler{T}
Generic sampler type for inference algorithms of type T
in DynamicPPL.
Sampler
should implement the AbstractMCMC interface, and in particular AbstractMCMC.step
. A default implementation of the initial sampling step is provided that supports resuming sampling from a previous state and setting initial parameter values. It requires to overload loadstate
and initialstep
for loading previous states and actually performing the initial sampling step, respectively. Additionally, sometimes one might want to implement initialsampler
that specifies how the initial parameter values are sampled if they are not provided. By default, values are sampled from the prior.
DynamicPPL.ThreadSafeVarInfo
— TypeThreadSafeVarInfo
A ThreadSafeVarInfo
object wraps an AbstractVarInfo
object and an array of log probabilities for thread-safe execution of a probabilistic model.
DynamicPPL.TypedVarInfo
— MethodTypedVarInfo(vi::UntypedVarInfo)
This function finds all the unique sym
s from the instances of VarName{sym}
found in vi.metadata.vns
. It then extracts the metadata associated with each symbol from the global vi.metadata
field. Finally, a new VarInfo
is created with a new metadata
as a NamedTuple
mapping from symbols to type-stable Metadata
instances, one for each symbol.
DynamicPPL.VarInfo
— Typestruct VarInfo{Tmeta, Tlogp} <: AbstractVarInfo
metadata::Tmeta
logp::Base.RefValue{Tlogp}
num_produce::Base.RefValue{Int}
end
A light wrapper over one or more instances of Metadata
. Let vi
be an instance of VarInfo
. If vi isa VarInfo{<:Metadata}
, then only one Metadata
instance is used for all the sybmols. VarInfo{<:Metadata}
is aliased UntypedVarInfo
. If vi isa VarInfo{<:NamedTuple}
, then vi.metadata
is a NamedTuple
that maps each symbol used on the LHS of ~
in the model to its Metadata
instance. The latter allows for the type specialization of vi
after the first sampling iteration when all the symbols have been observed. VarInfo{<:NamedTuple}
is aliased TypedVarInfo
.
Note: It is the user's responsibility to ensure that each "symbol" is visited at least once whenever the model is called, regardless of any stochastic branching. Each symbol refers to a Julia variable and can be a hierarchical array of many random variables, e.g. x[1] ~ ...
and x[2] ~ ...
both have the same symbol x
.
Base.empty!
— Methodempty!(meta::Metadata)
Empty the fields of meta
.
This is useful when using a sampling algorithm that assumes an empty meta
, e.g. SMC
.
Base.empty!
— Methodempty!(vi::VarInfo)
Empty the fields of vi.metadata
and reset vi.logp[]
and vi.num_produce[]
to zeros.
This is useful when using a sampling algorithm that assumes an empty vi
, e.g. SMC
.
Base.getindex
— Methodgetindex(vi::VarInfo, spl::Union{SampleFromPrior, Sampler})
Return the current value(s) of the random variables sampled by spl
in vi
.
The value(s) may or may not be transformed to Euclidean space.
Base.getindex
— Methodgetindex(vi::VarInfo, vn::VarName)
getindex(vi::VarInfo, vns::Vector{<:VarName})
Return the current value(s) of vn
(vns
) in vi
in the support of its (their) distribution(s).
If the value(s) is (are) transformed to the Euclidean space, it is (they are) transformed back.
Base.haskey
— Methodhaskey(vi::VarInfo, vn::VarName)
Check whether vn
has been sampled in vi
.
Base.isempty
— Methodisempty(vi::VarInfo)
Return true if vi
is empty and false otherwise.
Base.keys
— Methodkeys(vi::AbstractVarInfo)
Return an iterator over all vns
in vi
.
Base.nameof
— Methodnameof(model::Model)
Get the name of the model
as Symbol
.
Base.push!
— Methodpush!(vi::VarInfo, vn::VarName, r, dist::Distribution, gid::Selector)
Push a new random variable vn
with a sampled value r
sampled with a sampler of selector gid
from a distribution dist
to VarInfo
vi
.
Base.push!
— Methodpush!(vi::VarInfo, vn::VarName, r, dist::Distribution, spl::AbstractSampler)
Push a new random variable vn
with a sampled value r
sampled with a sampler spl
from a distribution dist
to VarInfo
vi
.
The sampler is passed here to invalidate its cache where defined.
Base.push!
— Methodpush!(vi::VarInfo, vn::VarName, r, dist::Distribution)
Push a new random variable vn
with a sampled value r
from a distribution dist
to the VarInfo
vi
.
Base.setindex!
— Methodsetindex!(vi::VarInfo, val, spl::Union{SampleFromPrior, Sampler})
Set the current value(s) of the random variables sampled by spl
in vi
to val
.
The value(s) may or may not be transformed to Euclidean space.
Base.setindex!
— Methodsetindex!(vi::VarInfo, val, vn::VarName)
Set the current value(s) of the random variable vn
in vi
to val
.
The value(s) may or may not be transformed to Euclidean space.
DynamicPPL._apply!
— Method_apply!(kernel!, vi::AbstractVarInfo, values, keys)
Calls kernel!(vi, vn, values, keys)
for every vn
in vi
.
DynamicPPL._evaluate
— Method_evaluate(rng, model::Model, varinfo, sampler, context)
Evaluate the model
with the arguments matching the given sampler
and varinfo
object.
DynamicPPL.acclogp!
— Methodacclogp!(vi::VarInfo, logp)
Add logp
to the value of the log of the joint probability of the observed data and parameters sampled in vi
.
DynamicPPL.build_model_info
— Methodbuild_model_info(input_expr)
Builds the model_info
dictionary from the model's expression.
DynamicPPL.build_output
— Methodbuild_output(modelinfo, linenumbernode)
Builds the output expression.
DynamicPPL.dot_tilde_assume
— Methoddot_tilde_assume(rng, ctx, sampler, right, left, vn, inds, vi)
Handle broadcasted assumed variables, e.g., x .~ MvNormal()
(where x
does not occur in the model inputs), accumulate the log probability, and return the sampled value.
Falls back to dot_tilde(rng, ctx, sampler, right, left, vn, inds, vi)
.
DynamicPPL.dot_tilde_observe
— Methoddot_tilde_observe(ctx, sampler, right, left, vi)
Handle broadcasted observed constants, e.g., [1.0] .~ MvNormal()
, accumulate the log probability, and return the observed value.
Falls back to dot_tilde(ctx, sampler, right, left, vi)
.
DynamicPPL.dot_tilde_observe
— Methoddot_tilde_observe(ctx, sampler, right, left, vname, vinds, vi)
Handle broadcasted observed values, e.g., x .~ MvNormal()
(where x
does occur the model inputs), accumulate the log probability, and return the observed value.
Falls back to dot_tilde(ctx, sampler, right, left, vi)
ignoring the information about variable name and indices; if needed, these can be accessed through this function, though.
DynamicPPL.evaluate_threadsafe
— Methodevaluate_threadsafe(rng, model, varinfo, sampler, context)
Evaluate the model
with varinfo
wrapped inside a ThreadSafeVarInfo
.
With the wrapper, Julia's multithreading can be used for observe statements in the model
but parallel sampling will lead to undefined behaviour. This method is not exposed and supposed to be used only internally in DynamicPPL.
See also: evaluate_threadunsafe
DynamicPPL.evaluate_threadunsafe
— Methodevaluate_threadunsafe(rng, model, varinfo, sampler, context)
Evaluate the model
without wrapping varinfo
inside a ThreadSafeVarInfo
.
If the model
makes use of Julia's multithreading this will lead to undefined behaviour. This method is not exposed and supposed to be used only internally in DynamicPPL.
See also: evaluate_threadsafe
DynamicPPL.generate_dot_tilde
— Methodgenerate_dot_tilde(left, right)
Generate the expression that replaces left .~ right
in the model body.
DynamicPPL.generate_mainbody
— Methodgenerate_mainbody(mod, expr, warn)
Generate the body of the main evaluation function from expression expr
and arguments args
.
If warn
is true, a warning is displayed if internal variables are used in the model definition.
DynamicPPL.generate_tilde
— Methodgenerate_tilde(left, right)
Generate an observe
expression for data variables and assume
expression for parameter variables.
DynamicPPL.generated_quantities
— Methodgenerated_quantities(model::Model, chain::AbstractChains)
Execute model
for each of the samples in chain
and return an array of the values returned by the model
for each sample.
Examples
General
Often you might have additional quantities computed inside the model that you want to inspect, e.g.
@model function demo(x)
# sample and observe
θ ~ Prior()
x ~ Likelihood()
return interesting_quantity(θ, x)
end
m = demo(data)
chain = sample(m, alg, n)
# To inspect the `interesting_quantity(θ, x)` where `θ` is replaced by samples
# from the posterior/`chain`:
generated_quantities(m, chain) # <= results in a `Vector` of returned values
# from `interesting_quantity(θ, x)`
Concrete (and simple)
julia> using DynamicPPL, Turing
julia> @model function demo(xs)
s ~ InverseGamma(2, 3)
m_shifted ~ Normal(10, √s)
m = m_shifted - 10
for i in eachindex(xs)
xs[i] ~ Normal(m, √s)
end
return (m, )
end
demo (generic function with 1 method)
julia> model = demo(randn(10));
julia> chain = sample(model, MH(), 10);
julia> generated_quantities(model, chain)
10×1 Array{Tuple{Float64},2}:
(2.1964758025119338,)
(2.1964758025119338,)
(0.09270081916291417,)
(0.09270081916291417,)
(0.09270081916291417,)
(0.09270081916291417,)
(0.09270081916291417,)
(0.043088571494005024,)
(-0.16489786710222099,)
(-0.16489786710222099,)
DynamicPPL.get_matching_type
— Methodget_matching_type(spl::AbstractSampler, vi, ::Type{T}) where {T}
Get the specialized version of type T
for sampler spl
.
For example, if T === Float64
and spl::Hamiltonian
, the matching type is eltype(vi[spl])
.
DynamicPPL.get_num_produce
— Methodget_num_produce(vi::VarInfo)
Return the num_produce
of vi
.
DynamicPPL.getall
— Methodgetall(vi::VarInfo)
Return the values of all the variables in vi
.
The values may or may not be transformed to Euclidean space.
DynamicPPL.getargnames
— Methodgetargnames(model::Model)
Get a tuple of the argument names of the model
.
DynamicPPL.getargs_dottilde
— Methodgetargs_dottilde(x)
Return the arguments L
and R
, if x
is an expression of the form L .~ R
or (~).(L, R)
, or nothing
otherwise.
DynamicPPL.getargs_tilde
— Methodgetargs_tilde(x)
Return the arguments L
and R
, if x
is an expression of the form L ~ R
, or nothing
otherwise.
DynamicPPL.getdist
— Methodgetdist(vi::VarInfo, vn::VarName)
Return the distribution from which vn
was sampled in vi
.
DynamicPPL.getgid
— Methodgetgid(vi::VarInfo, vn::VarName)
Return the set of sampler selectors associated with vn
in vi
.
DynamicPPL.getidx
— Methodgetidx(vi::VarInfo, vn::VarName)
Return the index of vn
in the metadata of vi
corresponding to vn
.
DynamicPPL.getlogp
— Methodgetlogp(vi::VarInfo)
Return the log of the joint probability of the observed data and parameters sampled in vi
.
DynamicPPL.getmetadata
— Methodgetmetadata(vi::VarInfo, vn::VarName)
Return the metadata in vi
that belongs to vn
.
DynamicPPL.getmissings
— Methodgetmissings(model::Model)
Get a tuple of the names of the missing arguments of the model
.
DynamicPPL.getrange
— Methodgetrange(vi::VarInfo, vn::VarName)
Return the index range of vn
in the metadata of vi
.
DynamicPPL.getranges
— Methodgetranges(vi::AbstractVarInfo, vns::Vector{<:VarName})
Return the indices of vns
in the metadata of vi
corresponding to vn
.
DynamicPPL.getval
— Methodgetval(vi::VarInfo, vns::Vector{<:VarName})
Return the value(s) of vns
.
The values may or may not be transformed to Euclidean space.
DynamicPPL.getval
— Methodgetval(vi::UntypedVarInfo, vview::Union{Int, UnitRange, Vector{Int}})
Return a view vi.vals[vview]
.
DynamicPPL.getval
— Methodgetval(vi::VarInfo, vn::VarName)
Return the value(s) of vn
.
The values may or may not be transformed to Euclidean space.
DynamicPPL.inargnames
— Methodinargnames(varname::VarName, model::Model)
Statically check whether the variable of name varname
is an argument of the model
.
Possibly existing indices of varname
are neglected.
DynamicPPL.increment_num_produce!
— Methodincrement_num_produce!(vi::VarInfo)
Add 1 to num_produce
in vi
.
DynamicPPL.initialsampler
— Methodinitialsampler(sampler::Sampler)
Return the sampler that is used for generating the initial parameters when sampling with sampler
.
By default, it returns an instance of SampleFromPrior
.
DynamicPPL.initialstep
— Functioninitialstep(rng, model, sampler, varinfo; kwargs...)
Perform the initial sampling step of the sampler
for the model
.
The varinfo
contains the initial samples, which can be provided by the user or sampled randomly.
DynamicPPL.inmissings
— Methodinmissings(varname::VarName, model::Model)
Statically check whether the variable of name varname
is a statically declared unobserved variable of the model
.
Possibly existing indices of varname
are neglected.
DynamicPPL.invlink!
— Methodinvlink!(vi::VarInfo, spl::AbstractSampler)
Transform the values of the random variables sampled by spl
in vi
from the Euclidean space back to the support of their distributions and sets their corresponding "trans"
flag values to false
.
DynamicPPL.is_flagged
— Methodis_flagged(vi::VarInfo, vn::VarName, flag::String)
Check whether vn
has a true value for flag
in vi
.
DynamicPPL.isassumption
— Methodisassumption(expr)
Return an expression that can be evaluated to check if expr
is an assumption in the model.
Let expr
be :(x[1])
. It is an assumption in the following cases: 1. x
is not among the input data to the model, 2. x
is among the input data to the model but with a value missing
, or 3. x
is among the input data to the model with a value other than missing, but x[1] === missing
.
When expr
is not an expression or symbol (i.e., a literal), this expands to false
.
DynamicPPL.islinked
— Methodislinked(vi::VarInfo, spl::Union{Sampler, SampleFromPrior})
Check whether vi
is in the transformed space for a particular sampler spl
.
Turing's Hamiltonian samplers use the link
and invlink
functions from Bijectors.jl to map a constrained variable (for example, one bounded to the space [0, 1]
) from its constrained space to the set of real numbers. islinked
checks if the number is in the constrained space or the real space.
DynamicPPL.istrans
— Methodistrans(vi::VarInfo, vn::VarName)
Return true if vn
's values in vi
are transformed to Euclidean space, and false if they are in the support of vn
's distribution.
DynamicPPL.link!
— Methodlink!(vi::VarInfo, spl::Sampler)
Transform the values of the random variables sampled by spl
in vi
from the support of their distributions to the Euclidean space and set their corresponding "trans"
flag values to true
.
DynamicPPL.loadstate
— Functionloadstate(data)
Load sampler state from data
.
DynamicPPL.logjoint
— Methodlogjoint(model::Model, varinfo::AbstractVarInfo)
Return the log joint probability of variables varinfo
for the probabilistic model
.
See logjoint
and loglikelihood
.
DynamicPPL.logprior
— Methodlogprior(model::Model, varinfo::AbstractVarInfo)
Return the log prior probability of variables varinfo
for the probabilistic model
.
See also logjoint
and loglikelihood
.
DynamicPPL.matchingvalue
— Methodmatchingvalue(sampler, vi, value)
Convert the value
to the correct type for the sampler
and the vi
object.
DynamicPPL.pointwise_loglikelihoods
— Methodpointwise_loglikelihoods(model::Model, chain::Chains, keytype = String)
Runs model
on each sample in chain
returning a Dict{String, Matrix{Float64}}
with keys corresponding to symbols of the observations, and values being matrices of shape (num_chains, num_samples)
.
keytype
specifies what the type of the keys used in the returned Dict
are. Currently, only String
and VarName
are supported.
Notes
Say y
is a Vector
of n
i.i.d. Normal(μ, σ)
variables, with μ
and σ
both being <:Real
. Then the observe (i.e. when the left-hand side is an observation) statements can be implemented in two ways:
for i in eachindex(y)
y[i] ~ Normal(μ, σ)
end
or
y ~ MvNormal(fill(μ, n), fill(σ, n))
Unfortunately, just by looking at the latter statement, it's impossible to tell whether or not this is one single observation which is n
dimensional OR if we have multiple 1-dimensional observations. Therefore, loglikelihoods
will only work with the first example.
Examples
julia> using DynamicPPL, Turing
julia> @model function demo(xs, y)
s ~ InverseGamma(2, 3)
m ~ Normal(0, √s)
for i in eachindex(xs)
xs[i] ~ Normal(m, √s)
end
y ~ Normal(m, √s)
end
demo (generic function with 1 method)
julia> model = demo(randn(3), randn());
julia> chain = sample(model, MH(), 10);
julia> pointwise_loglikelihoods(model, chain)
Dict{String,Array{Float64,2}} with 4 entries:
"xs[3]" => [-1.42862; -2.67573; … ; -1.66251; -1.66251]
"xs[1]" => [-1.42932; -2.68123; … ; -1.66333; -1.66333]
"xs[2]" => [-1.6724; -0.861339; … ; -1.62359; -1.62359]
"y" => [-1.51265; -0.914129; … ; -1.5499; -1.5499]
julia> pointwise_loglikelihoods(model, chain, String)
Dict{String,Array{Float64,2}} with 4 entries:
"xs[3]" => [-1.42862; -2.67573; … ; -1.66251; -1.66251]
"xs[1]" => [-1.42932; -2.68123; … ; -1.66333; -1.66333]
"xs[2]" => [-1.6724; -0.861339; … ; -1.62359; -1.62359]
"y" => [-1.51265; -0.914129; … ; -1.5499; -1.5499]
julia> pointwise_loglikelihoods(model, chain, VarName)
Dict{VarName,Array{Float64,2}} with 4 entries:
xs[2] => [-1.6724; -0.861339; … ; -1.62359; -1.62359]
y => [-1.51265; -0.914129; … ; -1.5499; -1.5499]
xs[1] => [-1.42932; -2.68123; … ; -1.66333; -1.66333]
xs[3] => [-1.42862; -2.67573; … ; -1.66251; -1.66251]
DynamicPPL.reset_num_produce!
— Methodreset_num_produce!(vi::AbstractVarInfo)
Reset the value of num_produce
the log of the joint probability of the observed data and parameters sampled in vi
to 0.
DynamicPPL.resetlogp!
— Methodresetlogp!(vi::AbstractVarInfo)
Reset the value of the log of the joint probability of the observed data and parameters sampled in vi
to 0.
DynamicPPL.set_flag!
— Methodset_flag!(vi::VarInfo, vn::VarName, flag::String)
Set vn
's value for flag
to true
in vi
.
DynamicPPL.set_num_produce!
— Methodset_num_produce!(vi::VarInfo, n::Int)
Set the num_produce
field of vi
to n
.
DynamicPPL.set_retained_vns_del_by_spl!
— Methodset_retained_vns_del_by_spl!(vi::VarInfo, spl::Sampler)
Set the "del"
flag of variables in vi
with order > vi.num_produce[]
to true
.
DynamicPPL.setall!
— Methodsetall!(vi::VarInfo, val)
Set the values of all the variables in vi
to val
.
The values may or may not be transformed to Euclidean space.
DynamicPPL.setgid!
— Methodsetgid!(vi::VarInfo, gid::Selector, vn::VarName)
Add gid
to the set of sampler selectors associated with vn
in vi
.
DynamicPPL.setlogp!
— Methodsetlogp!(vi::VarInfo, logp)
Set the log of the joint probability of the observed data and parameters sampled in vi
to logp
.
DynamicPPL.setorder!
— Methodsetorder!(vi::VarInfo, vn::VarName, index::Int)
Set the order
of vn
in vi
to index
, where order
is the number of observe statements run before sampling
vn`.
DynamicPPL.settrans!
— Methodsettrans!(vi::VarInfo, trans::Bool, vn::VarName)
Set the trans
flag value of vn
in vi
.
DynamicPPL.setval!
— Methodsetval!(vi::AbstractVarInfo, x)
setval!(vi::AbstractVarInfo, chains::AbstractChains, sample_idx::Int, chain_idx::Int)
Set the values in vi
to the provided values and leave those which are not present in x
or chains
unchanged.
Notes
This is rather limited for two reasons:
- It uses
subsumes_string(string(vn), map(string, keys))
under the hood, and therefore suffers from the same limitations assubsumes_string
. - It will set every
vn
present inkeys
. It will NOT however set everyk
present inkeys
. This means that ifvn == [m[1], m[2]]
, representing some variablem
, callingsetval!(vi, (m = [1.0, 2.0]))
will be a no-op since it will try to findm[1]
andm[2]
inkeys((m = [1.0, 2.0]))
.
Example
julia> using DynamicPPL, Distributions, StableRNGs
julia> @model function demo(x)
m ~ Normal()
for i in eachindex(x)
x[i] ~ Normal(m, 1)
end
end;
julia> rng = StableRNG(42);
julia> m = demo([missing]);
julia> var_info = DynamicPPL.VarInfo(rng, m);
julia> var_info[@varname(m)]
-0.6702516921145671
julia> var_info[@varname(x[1])]
-0.22312984965118443
julia> DynamicPPL.setval!(var_info, (m = 100.0, )); # set `m` and and keep `x[1]`
julia> var_info[@varname(m)] # [✓] changed
100.0
julia> var_info[@varname(x[1])] # [✓] unchanged
-0.22312984965118443
julia> m(rng, var_info); # rerun model
julia> var_info[@varname(m)] # [✓] unchanged
100.0
julia> var_info[@varname(x[1])] # [✓] unchanged
-0.22312984965118443
DynamicPPL.setval!
— Methodsetval!(vi::UntypedVarInfo, val, vview::Union{Int, UnitRange, Vector{Int}})
Set the value of vi.vals[vview]
to val
.
DynamicPPL.setval!
— Methodsetval!(vi::VarInfo, val, vn::VarName)
Set the value(s) of vn
in the metadata of vi
to val
.
The values may or may not be transformed to Euclidean space.
DynamicPPL.setval_and_resample!
— Methodsetval_and_resample!(vi::AbstractVarInfo, x)
setval_and_resample!(vi::AbstractVarInfo, chains::AbstractChains, sample_idx, chain_idx)
Set the values in vi
to the provided values and those which are not present in x
or chains
to be resampled.
Note that this does not resample the values not provided! It will call setflag!(vi, vn, "del")
for variables vn
for which no values are provided, which means that the next time we call model(vi)
these variables will be resampled.
Note
- This suffers from the same limitations as
setval!
. Seesetval!
for more info.
Example
julia> using DynamicPPL, Distributions, StableRNGs
julia> @model function demo(x)
m ~ Normal()
for i in eachindex(x)
x[i] ~ Normal(m, 1)
end
end;
julia> rng = StableRNG(42);
julia> m = demo([missing]);
julia> var_info = DynamicPPL.VarInfo(rng, m);
julia> var_info[@varname(m)]
-0.6702516921145671
julia> var_info[@varname(x[1])]
-0.22312984965118443
julia> DynamicPPL.setval_and_resample!(var_info, (m = 100.0, )); # set `m` and ready `x[1]` for resampling
julia> var_info[@varname(m)] # [✓] changed
100.0
julia> var_info[@varname(x[1])] # [✓] unchanged
-0.22312984965118443
julia> m(rng, var_info); # sample `x[1]` conditioned on `m = 100.0`
julia> var_info[@varname(m)] # [✓] unchanged
100.0
julia> var_info[@varname(x[1])] # [✓] changed
101.37363069798343
See also
DynamicPPL.subsumes_string
— Functionsubsumes_string(u::String, v::String[, u_indexing])
Check whether stringified variable name v
describes a sub-range of stringified variable u
.
This is a very restricted version subumes(u::VarName, v::VarName)
only really supporting:
- Scalar:
x
subsumesx[1, 2]
,x[1, 2]
subsumesx[1, 2][3]
, etc.
Note
- To get same matching capabilities as
AbstractPPL.subumes(u::VarName, v::VarName)
for strings, one can always doeval(varname(Meta.parse(u))
to getVarName
ofu
, and similarly tov
. But this is slow.
DynamicPPL.syms
— Methodsyms(vi::VarInfo)
Returns a tuple of the unique symbols of random variables sampled in vi
.
DynamicPPL.tilde_assume
— Methodtilde_assume(rng, ctx, sampler, right, vn, inds, vi)
Handle assumed variables, e.g., x ~ Normal()
(where x
does occur in the model inputs), accumulate the log probability, and return the sampled value.
Falls back to tilde(rng, ctx, sampler, right, vn, inds, vi)
.
DynamicPPL.tilde_observe
— Methodtilde_observe(ctx, sampler, right, left, vi)
Handle observed constants, e.g., 1.0 ~ Normal()
, accumulate the log probability, and return the observed value.
Falls back to tilde(ctx, sampler, right, left, vi)
.
DynamicPPL.tilde_observe
— Methodtilde_observe(ctx, sampler, right, left, vname, vinds, vi)
Handle observed variables, e.g., x ~ Normal()
(where x
does occur in the model inputs), accumulate the log probability, and return the observed value.
Falls back to tilde(ctx, sampler, right, left, vi)
ignoring the information about variable name and indices; if needed, these can be accessed through this function, though.
DynamicPPL.tonamedtuple
— Methodtonamedtuple(vi::VarInfo)
Convert a vi
into a NamedTuple
where each variable symbol maps to the values and indexing string of the variable.
For example, a model that had a vector of vector-valued variables x
would return
(x = ([1.5, 2.0], [3.0, 1.0], ["x[1]", "x[2]"]), )
DynamicPPL.unset_flag!
— Methodunset_flag!(vi::VarInfo, vn::VarName, flag::String)
Set vn
's value for flag
to false
in vi
.
DynamicPPL.updategid!
— Methodupdategid!(vi::VarInfo, vn::VarName, spl::Sampler)
Set vn
's gid
to Set([spl.selector])
, if vn
does not have a sampler selector linked and vn
's symbol is in the space of spl
.
StatsBase.loglikelihood
— MethodDynamicPPL.@addlogprob!
— Macro@addlogprob!(ex)
Add the result of the evaluation of ex
to the joint log probability.
DynamicPPL.@model
— Macro@model(expr[, warn = true])
Macro to specify a probabilistic model.
If warn
is true
, a warning is displayed if internal variable names are used in the model definition.
Examples
Model definition:
@model function model(x, y = 42)
...
end
To generate a Model
, call model(xvalue)
or model(xvalue, yvalue)
.