ABI recommendations for simple (linear) models

Hi everyone,

I am new to amortized Bayesian inference (ABI) and, in fact, I have so far not tried to fit any model, e.g. using your software, yet. So far, I have “only” read a few papers on ABI/SBI to get an initial understanding of the topic, including the paper on Amortized Bayesian Multilevel Models.
I am interested in ABI and BayesFlow solely because of the amortization aspect, because I need to fit my models repeatedly to many data sets. Other reasons for considering ABI (e.g. intractable likelihood in complicated/mechanistic models) do not matter to me at all (at least for now). Hence, the models I am interested in generally come from the class of generalized linear (mixed) models. Concrete examples could include a negative binomial regression model for count data with only few (say 1-3) covariates or a linear mixed model for longitudinal observations such as a hierarchical intercept-slope model. Because most examples I have seen so far (one exception) focus on more complicated models I was hoping to get some answers to a couple of questions regarding the use of ABI for such simpler models and maybe some experiences here.

Summary network
I was wondering whether for (hierarchical) GLMs would it make more sense to handcraft summary statistics instead of specifying and training a summary network? E.g. for the simplest case of a linear model with Gaussian error term, the sufficient statistics are available analytically, right? Perhaps to broaden the question: When would you recommend to handcraft summary statistics and when would you recommend to use a summary network instead?

Inference network
Taking into account the relative simplicity of my models of interest, are there specific inference network architectures that you would recommend? I.e. are there certain networks for which I can expect the training to be notably faster without sacrificing inferential accuracy compared to other alternatives?

Inferential accuracy
Do you have experience with the inferential accuracy of ABI in these settings compared to gold-standards like Stan/NUTS? Importantly, I do not only/mainly care about means/medians but very much about tail probabilities (e.g. 98% quantile).

Simulation gap
I have read about the potential problem of having a simulation gap and ABI not being able to estimate the posterior reliably in such scenarios. I would like to know “how large” this gap would need to be to cause problems in practical applications. I am asking this because I may want to deliberately specify a prior for a parameter that is not well aligned with the observed data. The prior would still have some probability mass in the relevant regions and be able to sample parameters that are aligned with the observed data, so there would only be misalignment but not an actual complete gap.

Thank you very much and best wishes
David

Hi David, welcome to the forum! I’ll try to answer point by point:

Summary networks
Nowadays I would say that it is rarely necessary to use hand-crafted summary statistics. DeepSets for example do mean-pooling, so the mean of the data can be easily recovered. I’ve sometimes encountered the issue that summary networks fail to learn to higher-order moments, but these were usually solved by adjusting network training.

So I would say for your models, a good default is to start without any handcrafted summary statistics and only add them if necessary (including them alongside wouldn’t hurt of course). If you encounter the case that the approximator fails to learn the correct posterior, one strategy to differentiate between an issue in summary network and the inference network is to plot the summary dimensions against the parameters to see if they are informative, or train a small MLP to predict the parameters from the summary statistics, or any other similar method.

Inference network
For low dimensional parameter spaces, good old coupling flows should work just fine. All case studies I have been doing for the multilevel stuff were using plain coupling flows, sometimes with spline transform, and I would only change to something else if I notice that they fail to learn the correct posterior. This is just my personal preference though, @KLDivergence for example prefers the newer architectures.

Inferential accuracy
Yes, this is definitely an issue I’m afraid. To give you some perspective: I would say that about 20% of the models I set up with ABI just work, 50% need some minor tweaks and the other 30% some serious debugging to get it to work reliably. ABI is still a relatively young field, and it’s popularity is mostly grounded in the fact that it allows estimating models that are otherwise impossible to fit. However, I believe that ABI is very promising for models that are traditionally inferred with MCMC. Many things already work, it’s just not as user-friendly yet. This is not to discourage you from trying it out of course, I’m just trying to not set any wrong expectations. We are always happy to help if you encounter any issues :slight_smile:
For the tail probabilities, they are less accurate than the posterior mean because extreme samples are less seen during training. We do have PointNetworks to target quantiles directly, although I have little experience with that. I would definitely run some model checks that target the quantiles directly, e.g. coverage at the 98% level.

Simulation gap
This is true, but really hard to quantify. The issue is again that the networks see very little samples that actually look like the observed data. We do have some diagnostics in Bayesflow for that and you can compute something like a prior predictive p-value to get a better feel and ensure that you have a decent enough simulation budget to cover this region.

One really nice way to deal with this prior-data conflict are self-consistency losses ( [2501.13483] Robust Amortized Bayesian Inference with Self-Consistency Losses on Unlabeled Data ) that allow network training for data in any region (for example the actually observed data) and we do have experimental support for that in Bayesflow. In our experiments, this allows accurate posterior inference even in situations where the true parameter lies several standard deviations away from the prior.

Some other thoughts
Since you mentioned GLMs, if you happen to give this a try, you also need to amortize over the covariate space. For this you probably want to train on a standardized scale and vary the covariance. The good thing is that in your case, your models are tractable so you can always check against Stan to see if you can actually trust the estimates. For multilevel models, you might also be interested in the follow-up draft of the amortized multi-level models ( [2609.40024] Amortized Bayesian Inference on Multilevel Models of Arbitrary Structure ) for other cases beyond the standard two-level (the draft is still a bit rough though). There is also experimental support in Bayesflow for that.

Sorry for the wall of text, I hope this helps!

Daniel

2 Likes

Hi Daniel, thank you so much for your elaborate response!

Summary networks
OK, I think I’ll start following your recommendations then. I thought that maybe since we know the “ingredients” for estimating the parameters of GLMs very well this could be advantageous and speed up the training process. But I am also not familiar at all with neural networks and all these different modern architectures I need to admit.

Inferential accuracy
That sounds reasonable and thank you very much for offering your help already in advance!
“For the tail probabilities, they are less accurate than the posterior mean because extreme samples are less seen during training.” Would simply increasing the simulation budget (i.e. number of training samples) help here? I’ve read in some papers that running the simulator is often the most expensive part of the training, but I guess for my use cases this would be less of an issue.

Simulation gap
Thank you for your thoughts on that. Intuitively I believe that the prior-data conflicts I am interested in are not as extreme such that I need more advanced solutions for this. But we’ll see once I do some first hands-on experiments :slight_smile:

Some other thoughts
“Since you mentioned GLMs, if you happen to give this a try, you also need to amortize over the covariate space.” Yes, I would have to reflect this in my meta_fn, right?
I will check out your preprint :+1: I think that the hierarchical models I will be interested in in this context only have two levels so your paper in Bayesian Analysis probably already covers most of my needs :slight_smile:

BR
David

Yes, you can just increase the simulation budget until it becomes too cumbersome to train.

Exactly :slight_smile:

1 Like

Dear all,

I have continued looking into Amortized Bayesian Inference and BayesFlow. I am currently trying to implement a first working example, which is a negative binomial regression model. I don’t have first results yet because more questions have come up for me for which I hope to find more answers/guidance here.

How to specify/supply inference_conditions?
In this linear regression example (section 2.3) the sample size (“N”) is taken from the simulation program input (.broadcast("N", to="x")) and supplied to the summary network via .rename("N", "inference_conditions"). Do I understand this correctly?
Maybe my concern is not obvious from this example but what irritates me about this step is that information from the ground-truth simulation program is used for the inference network.
Instead, I would seek to use information only from observed data.
To give a concrete example, suppose a binary treatment should be allocated to 50% of all subjects (simulation input) but for a particular data set we observe say 45% (simulated data).
I have read the BayesFlow 2 software paper and from section 4.5 it appears to me that an alternative approach would be to define custom summary statistics, incorporate them into the simulation pipeline, and add them to the inference conditions in the adapter via .concatenate(["means", "log_vars", "auto_corrs", "cross_corr", "period"], into="inference_conditions")(taken from the example).
Is my thinking correct or am I misunderstanding something?

When and which variable/data transformations to use?
I expect there is no universal answer to this question but maybe there exists some guidance.
Again, coming from the linear regression example (section 2.3) I see that a square root transformation is applied to “N”.
This raises for me the question when and which data (and variable) transformations to use.
I presume it is recommended whenever we expect very large ranges of values.
What about, e.g., proportions in (0, 1)? Would it also make sense to apply a transformation here (e.g., logit)?
In this regard, I have also started wondering whether it matters how I parametrize the simulation program. For instance, would it be preferrable to work with the log of the rates instead of the rates directly? Or does this not matter so much?

meta_fn vs. context()
In the linear regression example, the meta_fn argument of bf.make_simulator() is used for providing a function to also amortize over different sample sizes. In this example, it also says:
“We passed the meta simulator separately to the meta_fn argument to make sure that the number of observations N constant within each batch of simulated datasets. This is required since, within each batch, the generated datasets need to have the same shape for them to be easily transformable to tensors for deep learning.”
However, in this example the sample size is provided by a context() function which is added to (the beginning of) the simulator.
So how do these two approaches practically differ and is there a preferred way to do it?
Do the recommendations change when context() / meta_fn do not have an impact on the shape of the dataset, e.g., different allocation probabilities to a treatment?

How to control/allocate the number of simulations?
As a heads-up I don’t have a background in deep learning.
If I understand correctly, the total number simulated datasets used for network training is governed through the parameters batch_size, epochs, and num_batches_per_epoch.
Let’s say I target to use 100,000 datasets for training, then how should I allocate this number among those three dimensions and why?

(Edit:) Which backend to use?
Following recommendations I was planning to use Jax as a backend.
But I want to amortize also over a (large) range of sample sizes and then using Jax would add a lot of compilation overhead, is that right?
In that case, which other backend would you recommend to me, Tensorflow or Torch?

I’m looking forward to your responses!

Best regards
David