probcomp / GenExperimental.jl

Featherweight embedded probabilistic programming language and compositional inference programming library
MIT License
17 stars 2 forks source link

Generic SIR implementaton #67

Closed marcoct closed 6 years ago

marcoct commented 6 years ago

Should be very simple. Iris and I started this earlier in the RANSAC notebook. I'll work off of that.

First, the non-generator implementations:

Re-simulation version:

function sir(target::Generator{T}, args::Tuple, constraints::T, num::Int) where {T}
    scores = Vector{Float64}(num)
    traces = Vector{T}(num)
    for i=1:num
        traces[i] = deepcopy(constraints)
        (scores[i], _) = generate!(target, args, traces[i])
    end
    chosen = categorical_log(scores)
    return traces[chosen]
end

With custom proposal:

function sir(target::Generator{T}, target_args::Tuple, constraints::T,
               proposal::Generator, proposal_args, mapping::Dict) where {T}
    scores = Vector{Float64}(num)
    traces = Vector{T}(num)
    generator = compose(target, proposal, mapping) # TODO will be renamed `nest`
    for i=1:num
        traces[i] = deepcopy(constraints)
        (scores[i], _) = generate!(generator, (target_args, proposal_args), traces[i])
    end
    chosen = categorical_log(scores)
    return traces[chosen]
end

The generator implementations will require a little bit more instrumentation. There will only be one implementation of each (written in terms of the generator implementation).

marcoct commented 6 years ago

https://github.com/probcomp/Gen.jl/blob/20170713-marcoct-generators/src/sir.jl

marcoct commented 6 years ago

For now, if you want to combine resimulation with custom proposal in the generic SIR implementation, you need to explicitly write the resimulated fragments into the proposal program, and if they are not assessable generator calls, then you will be getting unnecessary noise. I think the interleaved execution combinator is the next useful version to target, because it seems conceptually simple and powerful in a way that ad-hoc solutions do not. See https://github.com/probcomp/Gen.jl/issues/1