Parameter-dependent prior

Hello! I’m working with BayesFlow on a birth-death process experiment, but I’ve hit a technical problem I can’t quite figure out.

My model uses a modified Gaussian prior that is not entirely independent. Specifically, in order to sample from it, this prior relies on certain external factors which vary by culture and requires one specific data point from the observation set itself.

I am trying to figure out how to structure this dependency within the BayesFlow framework:

  • Is it possible to implement a prior that is conditional on a subset of the observed data? Accessing that data point is trivially easy, but I’m unsure on how to do this, specially for the “generating simulated data” part.

  • If so, what is the best practice for passing this data-dependent information into the prior during the inference process?

I’m struggling to find documentation on this specific type of “self-referential” prior. Any pointers, code patterns, or high-level advice on how to handle this architecture would be incredibly helpful.

Thanks in advance for your time and insights!

Hi Oscar, if by “observed data” you mean the data you will later do inference on (post-training), why can’t you simply define your prior to use the data loaded in memory like so:

my_data = ...

def my_prior():
    # do stuff with my_data and RNG
    ...

The prior is NOT used during the inference process. Why would you want to pass the data to the prior then? Or maybe I am misunderstanding something?

Hi Stefan,

Thank you for the quick response! Let me clarify the setup.

If I hardcode the observed data x\_{t_1} globally during training, the neural network will only learn to infer \\theta for that one specific starting value. I want the neural network to generalize. It should be able to estimate \\theta for any observed starting value at t_1.

Some more context on the experiment:

I have an observational gap. My data consists of:

  • A single early observation at t_1 (let’s call this value B(t_1)).

  • A gap with no data between t_1 and t_3.

  • Continuous observations from t_3 onwards B(t>t_3).

I want to infer a parameter \\theta (which represents the state of the system at t_2, where t_1 < t_2 < t_3, and which I could aswell name B(t_2)). Because t_2 is close to t_1, my prior for \\theta is physically constrained by the value at t_1.

Here’s my “theoretical setup”, so to speak:

  • Prior: Gaussian centered at B(t_1).
  • Simulator: Gillespie algorithm which simulates from t_1 onwards.

The workflow would be something like:

  1. Load dataset B_i(t).
  2. Sample from the prior with a gaussian centered at B_i(t_1)
  3. Simulate training data starting from sampled B_i(t_1).

To train this amortized network, the generative model needs to sample different values of the context B(t_1) during training so the network learns the relationship across a wide range of starting conditions.

I am trying to figure out the best way to structure this within BayesFlow, Like, how do I cleanly wrap this in BayesFlow’s GenerativeModel so that the context generated in the prior is correctly passed as an argument to the simulator?

Hope this makes it clearer. Thanks again!

Ok, thanks for the clarification!

GenerativeModel gives me a hint: it is no longer a supported module (seems that you are using legacy bayesflow). I highly recommend switching to BayesFlow 2:

In the new lib, bf.make_simulator can chain arbitrary functions that will match input-output arguments automatically by names. You can bypass the simulator altogether by passing your own class that exposes a .sample(batch_size) method.