Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logdensity_def #350

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/primitives/logdensity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,55 @@ end
end
end

###############################################################################


function MeasureBase.logdensity_def(c::ConditionalModel{A,B,M}, x=NamedTuple()) where {A,B,M}
_logdensity_def(M, Model(c), argvals(c), observations(c), x)
end

export sourceLogdensityDef

sourceLogdensityDef(m::AbstractModel) = sourceLogdensityDef()(Model(m))

# function Base.convert(nt::NamedTuple, args...)
# @show nt
# @show args
# for (n, t) in enumerate(stacktrace())
# print("\t",n,". ")
# println(t)
# end

# end

function sourceLogdensityDef()
function(_m::Model)
proc(_m, st :: Assign) = :($(st.x) = $(st.rhs))
proc(_m, st :: Return) = nothing
proc(_m, st :: LineNumber) = nothing
function proc(_m, st :: Sample)
x = st.x
rhs = st.rhs
@q begin
_ℓ += Soss.logdensity_def($rhs, $x)
$x = Soss.predict($rhs, $x)
end
end

wrap(kernel) = @q begin
_ℓ = 0.0
$kernel
return _ℓ
end

buildSource(_m, proc, wrap) |> MacroTools.flatten
end
end

@gg function _logdensity_def(M::Type{<:TypeLevel}, _m::Model, _args, _data, _pars)
body = type2model(_m) |> sourceLogdensityDef() |> loadvals(_args, _data, _pars)
@under_global from_type(_unwrap_type(M)) @q let M
$body
end
end