SequentialSimulator#

class bayesflow.simulators.SequentialSimulator(simulators: Sequence[Simulator], expand_outputs: bool = True, replace_inputs: bool = True)[source]#

Bases: Simulator

Combines multiple simulators into one, sequentially.

Initialize a SequentialSimulator.

Parameters:
simulatorsSequence[Simulator]

A sequence of simulator instances to be executed sequentially. Each simulator should return dictionary outputs and may depend on outputs from previous simulators.

expand_outputsbool, optional

If True, 1D output arrays are expanded with an additional dimension at the end. Default is True.

replace_inputsbool, optional

If True, **kwargs are auto-batched and replace simulator outputs.

sample(batch_shape: tuple[int, ...], **kwargs) dict[str, ndarray][source]#

Sample sequentially from the internal simulator.

Parameters:
batch_shapeShape

The shape of the batch to sample. Typically, a tuple indicating the number of samples, but it also accepts an int.

**kwargs

Additional keyword arguments passed to each simulator. These may include previously sampled outputs used as inputs for subsequent simulators.

Returns:
datadict of str to np.ndarray

A dictionary containing the combined outputs from all simulators. Keys are output names and values are sampled arrays. If expand_outputs is True, 1D arrays are expanded to have shape (…, 1).

rejection_sample(batch_shape: tuple[int, ...], predicate: Callable[[dict[str, ndarray]], ndarray], *, axis: int = 0, sample_size: int = None, **kwargs) dict[str, ndarray]#
sample_batched(batch_shape: tuple[int, ...], *, sample_size: int, **kwargs)#

Sample the desired number of simulations in smaller batches.

Limited resources, especially memory, can make it necessary to run simulations in smaller batches. The number of samples per simulated batch is specified by sample_size.

Parameters:
batch_shapeShape

The desired output shape, as in sample(). Will be rounded up to the next complete batch.

sample_sizeint

The number of samples in each simulated batch.

kwargs

Additional keyword arguments passed to sample().