Real-time/Online Bayesian Inference

Hello all,

I started to use Pystan last week for a state-space model.
My model has two stages: First, parameters estimation (offline inference using mcmc). Second, a real-time/online inference in order to forecast/predict the position of objects at each time t.

The offline inference stage is easy to implement (thanks to the community’s efforts). However, I have been looking for real-time inference functions but I didn’t find any existing one.
Could you please tell me if there is a real-time/online inference library (whether in Stan, Pystan or Rstan)

Thanks so much,

1 Like

Sorry, your question fell through a bit.

I don’t think I understand the details of your problem, but assuming you know how to make a prediction if you knew the exact parameter values, you can easily generalize to the Bayesian setting - you take N posterior samples from the offline model fit one by one, make a single prediction for each sample. This gives you N samples of the prediction which you can treat as a distribution of possible outcomes. (choosing N depends on how much precision you need, if you care about tail probabilities, you need more than if you just core about some central tendency).

Note however that this means you don’t use the data in the online phase to improve your future inferences (change your parameter estimates). To do that, you currently need to refit the whole model (I believe there are people working on how to avoid refitting the whole model, but is AFAIK an open research question with no satisfactory answers).

Most packages in R wrapping Stan models (e.g. brms) support predictions for new data out of the box. I know that ctsem supports some state-space models, but I am not sure about their support for predictions. In Python world, I have no idea at all.

Does that answer your question?


Thanks a lot for answering my question, sorry I didn’t detail my question.
Yes ideally, I want the online phase to improve my future inferences and thus change my parameter estimates, but as you stated it’s an open research question.

How can I predict the position of a robot after let’s say X steps? By prediction here I mean forecasting the position after X steps.


Then the only way to do that currently in Stan would be to periodically refit the whole model using the most recent observations.

Stan provides you with samples of the parameters of the model (here I assume it is primarily the transition and noise matrices you are interested in). You need to know how to make a prediction, provided you would know the exact values of all the parameters - that’s domain specific. If you know that, you simply create one prediction for each posterior sample of the parameters - this gives you samples of your predictions taking into account the uncertainties in the parameters…

If I understand your use case correctly, then I don’t think this exists right now. But @s.maskell is this at all similar to what you’re working on with extending Stan for use with streaming data?

Thanks a lot for your answer, I can try this solution temporarily since online inference doesn’t exist yet

Thanks a lot for your answer, yeah I am interested in data streaming, say I have a state-space model to forecast the position of an object from time t to t+k, and I want to do so in a real-time manner without the need to refit the model and slow down the calculations.
Thanks a lot for putting me in touch with people who work on similar approaches, I am highly interested in that

@jonah yes. This sounds like the kind of setting we are interested in.
@zeina: can you explain a little more about your application? It sounds like you want to do online inference using parameters estimated offline. That’s exactly the setting we are working to address (by putting particle filters, “PFs”, (and related algorithms) in Stan): we’ve got a talk at StanCon on this in a few days’ time (Thursday 13 August 2020). In the terminology we would use, you have uncertain parameters that are constant (and can be estimated offline) and a dynamic state that changes over time. Crucially, the current state is a stochastic function of the previous state. In a robotics context, I’m guessing the state might be the true positions of the actuators and the parameters might be things like the measurement noise variance. If you can explain a bit more about your application, I can ensure we use it as a test case and you can then test our alpha version of PF-Stan.


Thanks a lot for your reply, your research activity in this field is exactly what I am interested in. I sent you more details about the model via email.

Regarding the conference, where can we find the detailed program?

1 Like

@s.maskell Cool, thanks chiming in here. Looking forward to seeing your talk!

@zeina Glad this is related to what you’re interested in. The details for the conference are at (there’s a link to the program from there). It’s a 24-hour conference in multiple time zones so @s.maskell’s talk may not be convenient for you depending on where you are, but you can get advanced access to the recordings if you register (after the conference the recordings will also be made public).

1 Like

For completeness, it’s probably worth explaining that, if (as I think might be the case here) the state-space model involves linear Gaussian relationships only, you can integrate over the state-sequence analytically using a Kalman filter. That might be awkward in Stan and/or involve a much bigger Stan file, but you could, in theory at least, do it: you don’t need to sample the state-sequence in Stan in such a setting.

In that linear Gaussian context, you could then use the parameter estimates in a Kalman filter for any online inference or predictions that we wish to calculate. That wouldn’t require any sampling. However, that assumes you want to use parameter estimates and ignore the uncertainty associated with those estimates; it would be better to factor that parameter uncertainty into the online inference, though that would require a particle filter or similar to cater for the resulting non-Gaussian state-space model.