|
| 1 | +""" |
| 2 | + LogDensityFunction |
| 3 | +
|
| 4 | +A callable representing a log density function of a `model`. |
| 5 | +
|
| 6 | +# Fields |
| 7 | +$(FIELDS) |
| 8 | +
|
| 9 | +# Examples |
| 10 | +```jldoctest |
| 11 | +julia> using Distributions |
| 12 | +
|
| 13 | +julia> using DynamicPPL: LogDensityFunction |
| 14 | +
|
| 15 | +julia> @model function demo(x) |
| 16 | + m ~ Normal() |
| 17 | + x ~ Normal(m, 1) |
| 18 | + end |
| 19 | +demo (generic function with 2 methods) |
| 20 | +
|
| 21 | +julia> model = demo(1.0); |
| 22 | +
|
| 23 | +julia> f = LogDensityFunction(model); |
| 24 | +
|
| 25 | +julia> # It implements the interface of LogDensityProblems.jl. |
| 26 | + using LogDensityProblems |
| 27 | +
|
| 28 | +julia> LogDensityProblems.logdensity(f, [0.0]) |
| 29 | +-2.3378770664093453 |
| 30 | +
|
| 31 | +julia> LogDensityProblems.dimension(f) |
| 32 | +1 |
| 33 | +
|
| 34 | +julia> # By default it uses `VarInfo` under the hood, but this is not necessary. |
| 35 | + f = LogDensityFunction(model, SimpleVarInfo(model)); |
| 36 | +
|
| 37 | +julia> LogDensityProblems.logdensity(f, [0.0]) |
| 38 | +-2.3378770664093453 |
| 39 | +``` |
| 40 | +""" |
| 41 | +struct LogDensityFunction{V,M,C} |
| 42 | + "varinfo used for evaluation" |
| 43 | + varinfo::V |
| 44 | + "model used for evaluation" |
| 45 | + model::M |
| 46 | + "context used for evaluation" |
| 47 | + context::C |
| 48 | +end |
| 49 | + |
| 50 | +# TODO: Deprecate. |
| 51 | +function LogDensityFunction( |
| 52 | + varinfo::AbstractVarInfo, |
| 53 | + model::Model, |
| 54 | + sampler::AbstractSampler, |
| 55 | + context::AbstractContext, |
| 56 | +) |
| 57 | + return LogDensityFunction(varinfo, model, SamplingContext(sampler, context)) |
| 58 | +end |
| 59 | + |
| 60 | +function LogDensityFunction( |
| 61 | + model::Model, |
| 62 | + varinfo::AbstractVarInfo=VarInfo(model), |
| 63 | + context::AbstractContext=DefaultContext(), |
| 64 | +) |
| 65 | + return LogDensityFunction(varinfo, model, context) |
| 66 | +end |
| 67 | + |
| 68 | +# HACK: heavy usage of `AbstractSampler` for, well, _everything_, is being phased out. In the mean time |
| 69 | +# we need to define these annoying methods to ensure that we stay compatible with everything. |
| 70 | +getsampler(f::LogDensityFunction) = getsampler(f.context) |
| 71 | +hassampler(f::LogDensityFunction) = hassampler(f.context) |
| 72 | + |
| 73 | +_get_indexer(ctx::AbstractContext) = _get_indexer(NodeTrait(ctx), ctx) |
| 74 | +_get_indexer(ctx::SamplingContext) = ctx.sampler |
| 75 | +_get_indexer(::IsParent, ctx::AbstractContext) = _get_indexer(childcontext(ctx)) |
| 76 | +_get_indexer(::IsLeaf, ctx::AbstractContext) = Colon() |
| 77 | + |
| 78 | +""" |
| 79 | + getparams(f::LogDensityFunction) |
| 80 | +
|
| 81 | +Return the parameters of the wrapped varinfo as a vector. |
| 82 | +""" |
| 83 | +getparams(f::LogDensityFunction) = f.varinfo[_get_indexer(f.context)] |
| 84 | + |
| 85 | +# LogDensityProblems interface |
| 86 | +function LogDensityProblems.logdensity(f::LogDensityFunction, θ::AbstractVector) |
| 87 | + vi_new = unflatten(f.varinfo, f.context, θ) |
| 88 | + return getlogp(last(evaluate!!(f.model, vi_new, f.context))) |
| 89 | +end |
| 90 | +function LogDensityProblems.capabilities(::Type{<:LogDensityFunction}) |
| 91 | + return LogDensityProblems.LogDensityOrder{0}() |
| 92 | +end |
| 93 | +# TODO: should we instead implement and call on `length(f.varinfo)` (at least in the cases where no sampler is involved)? |
| 94 | +LogDensityProblems.dimension(f::LogDensityFunction) = length(getparams(f)) |
0 commit comments