MultilevelSimulator#
- class bayesflow.experimental.MultilevelSimulator(meta_fn: Callable[[], dict[str, Any]] | None = None, is_batched: bool = False)[source]#
Bases:
objectA graph-based simulator for multilevel models.
The model is a directed acyclic graph whose nodes hold sampling functions. Every node creates samples once for each combination of its parent’s samples. The data are stored in long format: one row per sample, with integer indices of the dataset, all ancestors, and the node itself.
- Parameters:
- meta_fn: Callable([], dict[str, Any]], optional
Function returning a dict of meta data, called once per dataset in
sample, so that every dataset has its own meta data. Meta data can set the number of repetitions of a node viareps, and sampling functions can accept all keys of the return meta data.- is_batched: bool, optional
Default value for
is_batchedforadd_node. If True, all added nodes are batched by default, if False, all added nodes are unbatched. Default is False.
- add_node(node: str, sample_fn: Callable[[...], dict[str, Any]], reps: int | str | Callable = 1, is_batched: bool | None = None)[source]#
Adds a node with its sampling function and number of repetitions.
- Parameters:
- nodestr
Name of the node.
- sample_fn: Callable[…, dict[str, Any]]
Function returning a dict that maps variable names to sampled values. Each
sample_fnof a node can accept arguments that are named by either the variable names of the node’s parents or the meta data.- repsint, str or Callable, optional
Number of repetitions of the node per combination of its parents’ values. Used to vary the number of groups for each simulated dataset. Either a fixed integer, a key of the meta data, or a function that takes a size, and optionally a combinations argument, or keys of the meta data, and returns the number of repetitions for each of the parent combinations. Default is 1. If
repsis a function, it must be defined at the top-level, otherwise the MultilevelEstimator cannot be saved.- is_batchedbool, optional
If False,
sample_fnis called once per sample with the values of its parents. For much faster batched sampling, set it to True. In this case,sample_fnmust take an addition argument calledbatch_sizeand return a dictionary of sampled values with shape (batch_size, dim). Theis_batchedargument in MultilevelSimulator can be used to set the default for all nodes.
- add_edge(from_node: str, to_node: str)[source]#
Adds a directed edge indicating a dependency between two nodes.
- Parameters:
- from_nodestr
Parent node, whose variables are passed to the sampling function of the child.
- to_nodestr
Child node, which is repeated for every sample of the parent.
- sample(num_datasets: int, meta: dict[str, Any] | None = None) dict[str, NodeSamples][source]#
Samples datasets by traversing the graph in topological order.
- Parameters:
- num_datasetsint
The number of datasets to sample.
- metadict[str, Any], optional
Meta data shared by all datasets, overriding the output of
meta_fn.
- Returns:
- simulationGraphSamples
The samples of every node of type
NodeSamples.NodeSamplecontains two fields:indicescontains a dictionary mapping each sample to its dataset, every ancestor and the node index itself.valuesmaps every variable to an array of shape(num_samples, dim). The output can be used as input toMultilevelEstimator.fit.