ReactiveBayes / RxInfer.jl

Julia package for automated Bayesian inference on a factor graph with reactive message passing
MIT License
254 stars 24 forks source link

implement inference() callbacks that allow conditional termination of iterations #84

Closed John-Boik closed 1 year ago

John-Boik commented 1 year ago

This is a followup to a discourse post where I questioned if there is a way to conditionally terminate inference() iterations within callback functions. @bvdmitri requested I create an issue.

One approach would be to blend the current methods with how it's done in DifferentialEquations.jl, which has three types: ContinuousCallback (applied when a given continuous condition function hits zero), DiscreteCallback (applied when its condition function is true, evaluated at the end of every integration step), and VectorContinuousCallback (a vector of continuous callbacks, each with simultaneous root-finding equations, where the effect applied is the first condition that is satisfied).

That page contains an example of using a discrete callback to terminate integration.

condition(u,t,integrator) = u[2]>0  # condition function
affect!(integrator) = terminate!(integrator)  # affect function
cb = DiscreteCallback(condition,affect!)  # callback
sol = solve(prob,Tsit5(),callback=cb)  # use of callback, in our case, this would occur in the inference function

A simpler approach is to have every callback (e.g., before_data_update) return either true or false, where a false terminates iterations.