@@ -18,8 +18,86 @@ const LogProbType = float(Real)
1818"""
1919 @addlogprob!(ex)
2020
21+ Add a term to the log joint.
22+
23+ If `ex` evaluates to a `NamedTuple` with keys `:loglikelihood` and/or `:logprior`, the
24+ values are added to the log likelihood and log prior respectively.
25+
26+ If `ex` evaluates to a number it is added to the log likelihood. This use is deprecated
27+ and should be replaced with either the `NamedTuple` version or calls to
28+ [`@addloglikelihood!`](@ref).
29+
30+ See also [`@addloglikelihood!`](@ref), [`@addlogprior!`](@ref).
31+
32+ # Examples
33+
34+ ```jldoctest; setup = :(using Distributions)
35+ julia> mylogjoint(x, μ) = (; loglikelihood=loglikelihood(Normal(μ, 1), x), logprior=1.0);
36+
37+ julia> @model function demo(x)
38+ μ ~ Normal()
39+ @addlogprob! mylogjoint(x, μ)
40+ end;
41+
42+ julia> x = [1.3, -2.1];
43+
44+ julia> loglikelihood(demo(x), (μ=0.2,)) ≈ mylogjoint(x, 0.2).loglikelihood
45+ true
46+
47+ julia> logprior(demo(x), (μ=0.2,)) ≈ logpdf(Normal(), 0.2) + mylogjoint(x, 0.2).logprior
48+ true
49+ ```
50+
51+ and to [reject samples](https://github.com/TuringLang/Turing.jl/issues/1328):
52+
53+ ```jldoctest; setup = :(using Distributions, LinearAlgebra)
54+ julia> @model function demo(x)
55+ m ~ MvNormal(zero(x), I)
56+ if dot(m, x) < 0
57+ @addlogprob! (; loglikelihood=-Inf)
58+ # Exit the model evaluation early
59+ return
60+ end
61+ x ~ MvNormal(m, I)
62+ return
63+ end;
64+
65+ julia> logjoint(demo([-2.1]), (m=[0.2],)) == -Inf
66+ true
67+ ```
68+ """
69+ macro addlogprob! (ex)
70+ return quote
71+ val = $ (esc (ex))
72+ vi = $ (esc (:(__varinfo__)))
73+ if val isa Number
74+ Base. depwarn (
75+ """
76+ @addlogprob! with a single number argument is deprecated. Please use
77+ @addlogprob! (; loglikelihood=x) or @addloglikelihood! instead.
78+ """ ,
79+ :addlogprob! ,
80+ )
81+ if hasacc (vi, Val (:LogLikelihood ))
82+ $ (esc (:(__varinfo__))) = accloglikelihood!! ($ (esc (:(__varinfo__))), val)
83+ end
84+ elseif ! isa (val, NamedTuple)
85+ error (" logp must be a NamedTuple." )
86+ else
87+ $ (esc (:(__varinfo__))) = acclogp!! (
88+ $ (esc (:(__varinfo__))), val; ignore_missing_accumulator= true
89+ )
90+ end
91+ end
92+ end
93+
94+ """
95+ @addloglikelihood!(ex)
96+
2197Add the result of the evaluation of `ex` to the log likelihood.
2298
99+ See also [`@addlogprob!`](@ref), [`@addlogprior!`](@ref).
100+
23101# Examples
24102
25103This macro allows you to [include arbitrary terms in the likelihood](https://github.com/TuringLang/Turing.jl/issues/1332)
@@ -29,7 +107,7 @@ julia> myloglikelihood(x, μ) = loglikelihood(Normal(μ, 1), x);
29107
30108julia> @model function demo(x)
31109 μ ~ Normal()
32- @addlogprob ! myloglikelihood(x, μ)
110+ @addloglikelihood ! myloglikelihood(x, μ)
33111 end;
34112
35113julia> x = [1.3, -2.1];
@@ -44,7 +122,7 @@ and to [reject samples](https://github.com/TuringLang/Turing.jl/issues/1328):
44122julia> @model function demo(x)
45123 m ~ MvNormal(zero(x), I)
46124 if dot(m, x) < 0
47- @addlogprob ! -Inf
125+ @addloglikelihood ! -Inf
48126 # Exit the model evaluation early
49127 return
50128 end
@@ -56,10 +134,43 @@ julia> logjoint(demo([-2.1]), (m=[0.2],)) == -Inf
56134true
57135```
58136"""
59- macro addlogprob! (ex)
137+ macro addloglikelihood! (ex)
138+ return quote
139+ if hasacc ($ (esc (:(__varinfo__))), Val (:LogLikelihood ))
140+ $ (esc (:(__varinfo__))) = accloglikelihood!! ($ (esc (:(__varinfo__))), $ (esc (ex)))
141+ end
142+ end
143+ end
144+
145+ """
146+ @addlogprior!(ex)
147+
148+ Add the result of the evaluation of `ex` to the log prior.
149+
150+ See also [`@addloglikelihood!`](@ref), [`@addlogprob!`](@ref).
151+
152+ # Examples
153+
154+ This macro allows you to include arbitrary terms in the prior.
155+
156+ ```jldoctest; setup = :(using Distributions)
157+ julia> mylogpriorextraterm(μ) = μ > 0 ? -1.0 : 0.0;
158+
159+ julia> @model function demo(x)
160+ μ ~ Normal()
161+ @addlogprior! mylogpriorextraterm(μ)
162+ end;
163+
164+ julia> x = [1.3, -2.1];
165+
166+ julia> logprior(demo(x), (μ=0.2,)) ≈ logpdf(Normal(), 0.2) + mylogpriorextraterm(0.2)
167+ true
168+ ```
169+ """
170+ macro addlogprior! (ex)
60171 return quote
61- if $ hasacc ($ (esc (:(__varinfo__))), Val (:LogLikelihood ))
62- $ (esc (:(__varinfo__))) = $ accloglikelihood !! ($ (esc (:(__varinfo__))), $ (esc (ex)))
172+ if hasacc ($ (esc (:(__varinfo__))), Val (:LogPrior ))
173+ $ (esc (:(__varinfo__))) = acclogprior !! ($ (esc (:(__varinfo__))), $ (esc (ex)))
63174 end
64175 end
65176end
0 commit comments