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

Is there any way to compute the Bethe Free Energy of a given model? #407

Closed
Flawless1202 opened this issue Jan 17, 2025 · 2 comments
Closed

Comments

@Flawless1202
Copy link

I want to compute the Bethe Free Energy like follows:

@model function coin_model(y, a, b)
    θ ~ Beta(a, b)
    for i in eachindex(y)
        y[i] ~ Bernoulli(θ)
    end
end

result = infer(
    model = coin_model(a = 4.0, b = 8.0), 
    data  = (y = dataset,)
)

free_energy = compute_free_energy(result.model)

How to implement the compute_free_energy function? @ThijsvdLaar @bvdmitri @alstat @wmkouw @kobus78 Thanks a lot!

@Nimrais
Copy link
Member

Nimrais commented Jan 18, 2025

Hi, I may not fully understand your question, but in most cases, the local free energy is defined per node. RxInfer will produce this as an output - you just need to specify it through a keyword argument in the infer function.

If you're asking about implementing free energy for a model with a custom node, you can look at how it's implemented in the ReactiveMP source code, specifically at the implementation of the Beta distribution here https://github.com/ReactiveBayes/ReactiveMP.jl/blob/main/src/nodes/predefined/beta.jl#L6.

result = infer(
           model = coin_model(a = 4.0, b = 8.0),
           data  = (y = dataset,),
           free_energy=true,
       )
Inference results:
  Posteriors       | available for (θ)
  Free Energy:     | Real[696.125]

@bvdmitri
Copy link
Member

As @Nimrais pointed out you simply need to set free_energy flag to true and it will setup the inference function to also compute Bethe Free Energy with every iteration. See an example here.
Note, however, that it is not always possible to compute BFE for all models because sometimes its way too challenging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants