Skip to content

Sum of Bernoullis #299

Answered by bvdmitri
arnauqb asked this question in Q&A
Apr 17, 2024 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

One possible solution to your issue could be as follows:

add(x...) = sum(x)

@model function my_model(obs, N, sigma)
    local p
    for i = 1:N
        # I changed the prior to non-flat version
        p[i] ~ Beta(2, 2)
    end
    local x
    for i = 1:N
        x[i] ~ Bernoulli(p[i])
    end
    # So I use the custom `add` function
    C ~ add(in = x)
    # I also added more iterations to get better posteriors
    for i in eachindex(obs)
    	obs[i] ~ NormalMeanVariance(C, sigma^2)
    end
end

# We enable `CVI` as an approximation method for the `add` function
@meta function my_meta(rng, nr_samples, nr_iterations, optimizer)
    add() -> CVI(rng, nr_samples, nr_iterations, optimizer, F…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@bvdmitri
Comment options

Comment options

You must be logged in to vote
1 reply
@arnauqb
Comment options

Answer selected by arnauqb
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants