LambdaSimulator#

class bayesflow.simulators.LambdaSimulator(sample_fn: Callable[[Sequence[int]], dict[str, any]], *, is_batched: bool = False)[source]#

Bases: Simulator

Implements a simulator based on a sampling function.

Initialize a simulator based on a simple callable function

Parameters:
sample_fnCallable[[Sequence[int]], dict[str, any]]

A function that generates samples. It should accept batch_shape as its first argument (if is_batched=True), followed by keyword arguments.

is_batchedbool, optional

Whether the sample_fn is implemented to handle batched sampling directly. If False, sample_fn will be called once per sample and results will be stacked. Default is False.

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

Sample using the wrapped sampling function.

Parameters:
batch_shapeShape

The shape of the batch to sample. Typically, a tuple indicating the number of samples, but an int can also be passed.

**kwargs

Additional keyword arguments passed to the sampling function. Only valid arguments (as determined by the function’s signature) are used.

Returns:
datadict of str to np.ndarray

A dictionary of sampled outputs. Keys are output names and values are numpy arrays. If is_batched is False, individual outputs are stacked along the first axis.

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