@@ -6,6 +6,51 @@ using MarginalLogDensities: MarginalLogDensities
6
6
_to_varname (n:: Symbol ) = VarName {n} ()
7
7
_to_varname (n:: VarName ) = n
8
8
9
+ """
10
+ marginalize(
11
+ model::DynamicPPL.Model,
12
+ varnames::AbstractVector{<:Union{Symbol,<:VarName}},
13
+ getlogprob=DynamicPPL.getlogjoint,
14
+ method::MarginalLogDensities.AbstractMarginalizer=MarginalLogDensities.LaplaceApprox();
15
+ kwargs...,
16
+ )
17
+
18
+ Construct a `MarginalLogDensities.MarginalLogDensity` object that represents the marginal
19
+ log-density of the given `model`, after marginalizing out the variables specified in
20
+ `varnames`.
21
+
22
+ The resulting object can be called with a vector of parameter values to compute the marginal
23
+ log-density.
24
+
25
+ The `getlogprob` argument can be used to specify which kind of marginal log-density to
26
+ compute. Its default value is `DynamicPPL.getlogjoint` which returns the marginal log-joint
27
+ probability.
28
+
29
+ By default the marginalization is performed with a Laplace approximation. Please see [the
30
+ MarginalLogDensities.jl package](https://github.com/ElOceanografo/MarginalLogDensities.jl/)
31
+ for other options.
32
+
33
+ ## Example
34
+
35
+ ```jldoctest
36
+ julia> using DynamicPPL, Distributions, MarginalLogDensities
37
+
38
+ julia> @model function demo()
39
+ x ~ Normal(1.0)
40
+ y ~ Normal(2.0)
41
+ end
42
+ demo (generic function with 2 methods)
43
+
44
+ julia> marginalized = marginalize(demo(), [:x]);
45
+
46
+ julia> # The resulting callable computes the marginal log-density of `y`.
47
+ marginalized([1.0])
48
+ -1.4189385332046727
49
+
50
+ julia> logpdf(Normal(2.0), 1.0)
51
+ -1.4189385332046727
52
+ ```
53
+ """
9
54
function DynamicPPL. marginalize (
10
55
model:: DynamicPPL.Model ,
11
56
varnames:: AbstractVector{<:Union{Symbol,<:VarName}} ,
0 commit comments