HierarchicalSimulator#

class bayesflow.simulators.HierarchicalSimulator(hierarchy: Sequence[Simulator])[source]#

Bases: Simulator

Initialize the hierarchical simulator with a sequence of simulators.

Parameters:
hierarchySequence[Simulator]

A sequence of simulator instances representing each level of the hierarchy. Each level’s output is used as input for the next, with increasing batch dimensions.

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

Sample from a hierarchy of simulators.

Parameters:
batch_shapeShape

A tuple where each element specifies the number of samples at the corresponding level of the hierarchy. The total batch size increases multiplicatively through the levels.

**kwargs

Additional keyword arguments passed to each simulator. These are combined with outputs from previous levels and repeated appropriately.

Returns:
output_datadict of str to np.ndarray

A dictionary containing the outputs from the entire hierarchy. Outputs are reshaped to match the hierarchical batch shape, i.e., with shape equal to batch_shape + original_shape.

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().