DynamicPPL.DefaultContextType
struct 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.LikelihoodContextType
struct 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.MetadataType

The 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 all VarName instances.
  • md.idcs is the dictionary that maps each VarName 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 of vn.
  • md.gids[md.idcs[vn]] is the set of algorithms used to sample vn. This is used in

the Gibbs sampling process.

  • md.orders[md.idcs[vn]] is the number of observe statements before vn is sampled.
  • md.ranges[md.idcs[vn]] is the index range of vn in md.vals.
  • md.vals[md.ranges[md.idcs[vn]]] is the vector of values of corresponding to vn.
  • 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.MiniBatchContextType
struct 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.ModelType
struct 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.ModelType
(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.ModelMethod
Model(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.NamedDistType

A named distribution that carries the name of the random variable with it.

DynamicPPL.PriorContextType
struct PriorContext{Tvars} <: AbstractContext
    vars::Tvars
end

The PriorContext enables the computation of the log prior of the parameters vars when running the model.

DynamicPPL.SamplerType
Sampler{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.TypedVarInfoMethod
TypedVarInfo(vi::UntypedVarInfo)

This function finds all the unique syms 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.VarInfoType
struct 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!Method
empty!(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!Method
empty!(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.getindexMethod
getindex(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.getindexMethod
getindex(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.haskeyMethod
haskey(vi::VarInfo, vn::VarName)

Check whether vn has been sampled in vi.

Base.isemptyMethod
isempty(vi::VarInfo)

Return true if vi is empty and false otherwise.

Base.keysMethod
keys(vi::AbstractVarInfo)

Return an iterator over all vns in vi.

Base.nameofMethod
nameof(model::Model)

Get the name of the model as Symbol.

Base.push!Method
push!(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!Method
push!(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!Method
push!(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!Method
setindex!(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!Method
setindex!(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._evaluateMethod
_evaluate(rng, model::Model, varinfo, sampler, context)

Evaluate the model with the arguments matching the given sampler and varinfo object.

DynamicPPL.acclogp!Method
acclogp!(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.dot_tilde_assumeMethod
dot_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_observeMethod
dot_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_observeMethod
dot_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_threadsafeMethod
evaluate_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_threadunsafeMethod
evaluate_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_mainbodyMethod
generate_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_tildeMethod
generate_tilde(left, right)

Generate an observe expression for data variables and assume expression for parameter variables.

DynamicPPL.generated_quantitiesMethod
generated_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_typeMethod
get_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.getallMethod
getall(vi::VarInfo)

Return the values of all the variables in vi.

The values may or may not be transformed to Euclidean space.

DynamicPPL.getargs_dottildeMethod
getargs_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_tildeMethod
getargs_tilde(x)

Return the arguments L and R, if x is an expression of the form L ~ R, or nothing otherwise.

DynamicPPL.getdistMethod
getdist(vi::VarInfo, vn::VarName)

Return the distribution from which vn was sampled in vi.

DynamicPPL.getgidMethod
getgid(vi::VarInfo, vn::VarName)

Return the set of sampler selectors associated with vn in vi.

DynamicPPL.getidxMethod
getidx(vi::VarInfo, vn::VarName)

Return the index of vn in the metadata of vi corresponding to vn.

DynamicPPL.getlogpMethod
getlogp(vi::VarInfo)

Return the log of the joint probability of the observed data and parameters sampled in vi.

DynamicPPL.getmetadataMethod
getmetadata(vi::VarInfo, vn::VarName)

Return the metadata in vi that belongs to vn.

DynamicPPL.getmissingsMethod
getmissings(model::Model)

Get a tuple of the names of the missing arguments of the model.

DynamicPPL.getrangeMethod
getrange(vi::VarInfo, vn::VarName)

Return the index range of vn in the metadata of vi.

DynamicPPL.getrangesMethod
getranges(vi::AbstractVarInfo, vns::Vector{<:VarName})

Return the indices of vns in the metadata of vi corresponding to vn.

DynamicPPL.getvalMethod
getval(vi::VarInfo, vns::Vector{<:VarName})

Return the value(s) of vns.

The values may or may not be transformed to Euclidean space.

DynamicPPL.getvalMethod
getval(vi::UntypedVarInfo, vview::Union{Int, UnitRange, Vector{Int}})

Return a view vi.vals[vview].

DynamicPPL.getvalMethod
getval(vi::VarInfo, vn::VarName)

Return the value(s) of vn.

The values may or may not be transformed to Euclidean space.

DynamicPPL.inargnamesMethod
inargnames(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.initialsamplerMethod
initialsampler(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.initialstepFunction
initialstep(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.inmissingsMethod
inmissings(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!Method
invlink!(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_flaggedMethod
is_flagged(vi::VarInfo, vn::VarName, flag::String)

Check whether vn has a true value for flag in vi.

DynamicPPL.isassumptionMethod
isassumption(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.islinkedMethod
islinked(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.istransMethod
istrans(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!Method
link!(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.matchingvalueMethod
matchingvalue(sampler, vi, value)

Convert the value to the correct type for the sampler and the vi object.

DynamicPPL.pointwise_loglikelihoodsMethod
pointwise_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!Method
reset_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!Method
resetlogp!(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!Method
set_flag!(vi::VarInfo, vn::VarName, flag::String)

Set vn's value for flag to true in vi.

DynamicPPL.setall!Method
setall!(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!Method
setgid!(vi::VarInfo, gid::Selector, vn::VarName)

Add gid to the set of sampler selectors associated with vn in vi.

DynamicPPL.setlogp!Method
setlogp!(vi::VarInfo, logp)

Set the log of the joint probability of the observed data and parameters sampled in vi to logp.

DynamicPPL.setorder!Method
setorder!(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 samplingvn`.

DynamicPPL.settrans!Method
settrans!(vi::VarInfo, trans::Bool, vn::VarName)

Set the trans flag value of vn in vi.

DynamicPPL.setval!Method
setval!(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:

  1. It uses subsumes_string(string(vn), map(string, keys)) under the hood, and therefore suffers from the same limitations as subsumes_string.
  2. It will set every vn present in keys. It will NOT however set every k present in keys. This means that if vn == [m[1], m[2]], representing some variable m, calling setval!(vi, (m = [1.0, 2.0])) will be a no-op since it will try to find m[1] and m[2] in keys((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!Method
setval!(vi::UntypedVarInfo, val, vview::Union{Int, UnitRange, Vector{Int}})

Set the value of vi.vals[vview] to val.

DynamicPPL.setval!Method
setval!(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!Method
setval_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!. See setval! 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_stringFunction
subsumes_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 subsumes x[1, 2], x[1, 2] subsumes x[1, 2][3], etc.

Note

  • To get same matching capabilities as AbstractPPL.subumes(u::VarName, v::VarName) for strings, one can always do eval(varname(Meta.parse(u)) to get VarName of u, and similarly to v. But this is slow.
DynamicPPL.symsMethod
syms(vi::VarInfo)

Returns a tuple of the unique symbols of random variables sampled in vi.

DynamicPPL.tilde_assumeMethod
tilde_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_observeMethod
tilde_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_observeMethod
tilde_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.tonamedtupleMethod
tonamedtuple(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!Method
unset_flag!(vi::VarInfo, vn::VarName, flag::String)

Set vn's value for flag to false in vi.

DynamicPPL.updategid!Method
updategid!(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.

DynamicPPL.@addlogprob!Macro
@addlogprob!(ex)

Add the result of the evaluation of ex to the joint log probability.

DynamicPPL.@modelMacro
@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).