ReactiveBayes / RxInfer.jl

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

An overview of "sharp bits" of RxInfer #60

Open svilupp opened 2 years ago

svilupp commented 2 years ago

This is inspired by JAX Sharp Bits

It would be awesome to have a documentation page describing the most unexpected differences to what a normal Julia user (or a Turing.jl user) would expect, eg,

SebastianCallh commented 2 years ago

Hi! I am looking into ReactiveMP for some state space modelling. It looks like a very cool and useful package, however, not being able to call functions in models is unfortunately kind of a deal breaker for me. My use case is I would like to call a function that returns transition/observation matrices and use those in the inference.

Is this a something you are looking to make possible in the future or is this a hard limitation of ReactiveML?

bvdmitri commented 2 years ago

Hey @SebastianCallh ,

Depends on what exactly you are trying to achieve. If you just want to generate a state-transition matrix that does depend on some "out-of-model" process just make it datavar(Matrix) and simply pass it together with your observation. There is a good example of online filtering in a hierarchical gaussian filter model: https://biaslab.github.io/ReactiveMP.jl/stable/examples/hierarchical_gaussian_filter/. In this demo we set our priors to be datavar's such that we can continuously change/update them during the inference procedure. You can set up something similar for state-transition matrix as well.

If you want to generate those matrices depending on some random variable in your model than it is not supported yet, but we are working hard to make it work. The main difficulty here is that ReactiveMP.jl is aimed to support fast real-time inference in state-space models and making inference fast for any arbitrary function is quite a difficult challenge.

SebastianCallh commented 2 years ago

Thank you for that insight @bvdmitri . It makes sense to keep the scope limited.

I'm actually working on a library for structural time series primitives and it looks like ReactiveMP is a wonderful inference engine that I could pair it with. I have used Turing with Kalman filter to fit models before but it is quite slow.

There are some examples on how I currently construct and use models here https://github.com/SebastianCallh/STSlib.jl#basics. Do you think this would pair well with ReactiveMP? I was thinking I would call the STS model each time step for states and observations and simply pass them to RectiveMP for inference but I couldn't figure out how to call my functions (operating on mean vectors and covariance matrices) with RandomVariable objects.

bartvanerp commented 1 year ago

Hi @SebastianCallh!

Our sincere apologies for the (extremely) late reply to your question. It just escaped our attention. Although your question seems a bit irrelated to the issue, I am happy to answer it here. From your description I get the feeling that you are looking for the following example in our docs: https://biaslab.github.io/RxInfer.jl/stable/examples/Kalman%20filter%20with%20LSTM%20network%20driven%20dynamic/#Generate-data. It basically describes a Kalman Filter whose transition matrices are modeled by a neural network (here powered by Flux.jl). The code is a bit rough there, because the neural network is trained simultaneously. If your network has already been trained, then you could make use of the more convenient rxinference function, which processes data sequentially in an online manner. Especially our @autoupdates macro might describe what you are looking for.

I hope this answers/solves your problem. If you would like to dive in a bit more detail regarding your implementation, feel free to open a separate issue in which we can discuss your implementation as we think this line of research is very interesting!

bartvanerp commented 1 year ago

Further update on the sharp bits section:

Tasks:

If someone encounters certain limitations of our tool, we highly encourage you to open an issue, such that we know what pitfalls people are experiencing and how we can help improving our package :)

SebastianCallh commented 1 year ago

@bartvanerp Thank you so much for your polite response. I was a bit too eager when posting my question here, and completely agree it belongs in a separate issue. I will study the example you linked!

bvdmitri commented 1 year ago

@mhidalgoaraya

albertpod commented 6 months ago

Hi @mhidalgoaraya! I see this one has "in progress" status. Are we working on it?

albertpod commented 6 months ago

ping @mhidalgoaraya

mhidalgoaraya commented 6 months ago

@albertpod, it is the first time that I see this. It seems that it was assigned to me. I can take care of it. Can we discuss it next week and you get me up to speed. Thanks