bayesflow.benchmarks package
Submodules
bayesflow.benchmarks.bernoulli_glm module
- bayesflow.benchmarks.bernoulli_glm.configurator(forward_dict, mode='posterior')[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.bernoulli_glm.prior(rng=None)[source]
Generates a random draw from the custom prior over the 10 Bernoulli GLM parameters (1 intercept and 9 weights). Uses a global covariance matrix Cov for the multivariate Gaussian prior over the model weights, which is pre-computed for efficiency.
- Parameters
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the prior.
- Return type
np.ndarray of shape (10,)
- bayesflow.benchmarks.bernoulli_glm.simulator(theta, T=100, scale_by_T=True, rng=None)[source]
Simulates data from the custom Bernoulli GLM likelihood, see https://arxiv.org/pdf/2101.04653.pdf, Task T.5
Important: scale_sum should be set to False if the simulator is used with variable T during training, otherwise the information of T will be lost.
- Parameters
theta (np.ndarray of shape (10,)) – The vector of model parameters (theta[0] is intercept, theta[i], i > 0 are weights).
T (int, optional, default: 100) – The simulated duration of the task (eq. the number of Bernoulli draws).
scale_by_T (bool, optional, default: True) – A flag indicating whether to scale the summayr statistics by T.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – The vector of sufficient summary statistics of the data.
- Return type
np.ndarray of shape (10,)
bayesflow.benchmarks.bernoulli_glm_raw module
- bayesflow.benchmarks.bernoulli_glm_raw.configurator(forward_dict, mode='posterior', as_summary_condition=False)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.bernoulli_glm_raw.prior(rng=None)[source]
Generates a random draw from the custom prior over the 10 Bernoulli GLM parameters (1 intercept and 9 weights). Uses a global covariance matrix Cov for the multivariate Gaussian prior over the model weights, which is pre-computed for efficiency.
- Parameters
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the prior.
- Return type
np.ndarray of shape (10,)
- bayesflow.benchmarks.bernoulli_glm_raw.simulator(theta, T=100, rng=None)[source]
Simulates data from the custom Bernoulli GLM likelihood, see: https://arxiv.org/pdf/2101.04653.pdf, Task T.6
Returns the raw Bernoulli data.
- Parameters
theta (np.ndarray of shape (10,)) – The vector of model parameters (theta[0] is intercept, theta[i], i > 0 are weights)
T (int, optional, default: 100) – The simulated duration of the task (eq. the number of Bernoulli draws).
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – The full simulated set of Bernoulli draws. Should be configured with an additional trailing dimension if the data is (properly) to be treated as a set.
- Return type
np.ndarray of shape (T,)
bayesflow.benchmarks.gaussian_linear module
- bayesflow.benchmarks.gaussian_linear.configurator(forward_dict, mode='posterior')[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.gaussian_linear.prior(D=10, scale=0.1, rng=None)[source]
Generates a random draw from a D-dimensional Gaussian prior distribution with a spherical convariance matrix given by sigma * I_D. Represents the location vector of a (conjugate) Gaussian likelihood.
- Parameters
D (int, optional, default : 10) – The dimensionality of the Gaussian prior distribution.
scale (float, optional, default : 0.1) – The scale of the Gaussian prior.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the D-dimensional Gaussian prior.
- Return type
np.ndarray of shape (D, )
- bayesflow.benchmarks.gaussian_linear.simulator(theta, n_obs=None, scale=0.1, rng=None)[source]
Generates batched draws from a D-dimenional Gaussian distributions given a batch of location (mean) parameters of D dimensions. Assumes a spherical convariance matrix given by scale * I_D.
- Parameters
theta (np.ndarray of shape (theta, D)) – The location parameters of the Gaussian likelihood.
n_obs (int or None, optional, default: None) – The number of observations to draw from the likelihood given the location parameter theta. If n obs is None, a single draw is produced.
scale (float, optional, default : 0.1) – The scale of the Gaussian likelihood.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – else np.ndarray of shape (theta.shape[0], n_obs, theta.shape[1]) A single draw or a sample from a batch of Gaussians.
- Return type
np.ndarray of shape (theta.shape[0], theta.shape[1]) if n_obs is None,
bayesflow.benchmarks.gaussian_linear_uniform module
- bayesflow.benchmarks.gaussian_linear_uniform.configurator(forward_dict, mode='posterior')[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.gaussian_linear_uniform.prior(D=10, lower_bound=- 1.0, upper_bound=1.0, rng=None)[source]
Generates a random draw from a D-dimensional uniform prior bounded between lower_bound and upper_bound which represents the location vector of a (conjugate) Gaussian likelihood.
- Parameters
D (int, optional, default : 10) – The dimensionality of the Gaussian prior.
lower_bound (float, optional, default : -1.) – The lower bound of the uniform prior.
upper_bound (float, optional, default : 1.) – The upper bound of the uniform prior.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the D-dimensional uniform prior.
- Return type
np.ndarray of shape (D, )
- bayesflow.benchmarks.gaussian_linear_uniform.simulator(theta, n_obs=None, scale=0.1, rng=None)[source]
Generates batched draws from a D-dimenional Gaussian distributions given a batch of location (mean) parameters of D dimensions. Assumes a spherical convariance matrix given by scale * I_D.
- Parameters
theta (np.ndarray of shape (theta, D)) – The location parameters of the Gaussian likelihood.
n_obs (int or None, optional, default: None) – The number of observations to draw from the likelihood given the location parameter theta. If None, a single draw is produced.
scale (float, optional, default : 0.1) – The scale of the Gaussian likelihood.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – else np.ndarray of shape (theta.shape[0], n_obs, theta.shape[1]) A single draw or a sample from a batch of Gaussians.
- Return type
np.ndarray of shape (theta.shape[0], theta.shape[1]) if n_obs is None,
bayesflow.benchmarks.gaussian_mixture module
- bayesflow.benchmarks.gaussian_mixture.configurator(forward_dict, mode='posterior', scale_data=12)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.gaussian_mixture.prior(lower_bound=- 10.0, upper_bound=10.0, D=2, rng=None)[source]
Generates a random draw from a 2-dimensional uniform prior bounded between lower_bound and upper_bound representing the common mean of a 2D Gaussian mixture model (GMM).
- Parameters
lower_bound (float, optional, default : -10) – The lower bound of the uniform prior.
upper_bound (float, optional, default : 10) – The upper bound of the uniform prior.
D (int, optional, default: 2) – The dimensionality of the mixtrue model
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the D-dimensional uniform prior.
- Return type
np.ndarray of shape (D, )
- bayesflow.benchmarks.gaussian_mixture.simulator(theta, prob=0.5, scale_c1=1.0, scale_c2=0.01, rng=None)[source]
Simulates data from the Gaussian mixture model (GMM) with shared location vector. For more details, see
https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.7
- Parameters
theta (np.ndarray of shape (D,)) – The D-dimensional vector of parameter locations.
prob (float, optional, default: 0.5) – The mixture probability (coefficient).
scale_c1 (float, optional, default: 1.) – The scale of the first component.
scale_c2 (float, optional, default: 0.01) – The scale of the second component.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – The 2D vector generated from the GMM simulator.
- Return type
np.ndarray of shape (2,)
bayesflow.benchmarks.lotka_volterra module
- bayesflow.benchmarks.lotka_volterra.configurator(forward_dict, mode='posterior', scale_data=1000, as_summary_condition=False)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.lotka_volterra.prior(rng=None)[source]
Generates a random draw from a 4-dimensional (independent) lognormal prior which represents the four contact parameters of the Lotka-Volterra model.
- Parameters
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the 4-dimensional prior.
- Return type
np.ndarray of shape (4,)
- bayesflow.benchmarks.lotka_volterra.simulator(theta, X0=30, Y0=1, T=20, subsample=10, flatten=True, rng=None)[source]
Runs a Lotka-Volterra simulation for T time steps and returns subsample evenly spaced points from the simulated trajectory, given contact parameters theta.
See https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.10.
- Parameters
theta (np.ndarray of shape (2,)) – The 2-dimensional vector of disease parameters.
X0 (float, optional, default: 30) – Initial number of prey species.
Y0 (float, optional, default: 1) – Initial number of predator species.
T (T, optional, default: 20) – The duration (time horizon) of the simulation.
subsample (int or None, optional, default: 10) – The number of evenly spaced time points to return. If None, no subsampling will be performed and all T timepoints will be returned.
flatten (bool, optional, default: True) – A flag to indicate whather a 1D (flatten=True) or a 2D (flatten=False) representation of the simulated data is returned.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – otherwise shape (T, 2) or (T*2,) if subsample is None. The time series of simulated predator and pray populations
- Return type
np.ndarray of shape (subsample, 2) or (subsample*2,) if subsample is not None,
bayesflow.benchmarks.sir module
- bayesflow.benchmarks.sir.configurator(forward_dict, mode='posterior', as_summary_condition=False)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.sir.prior(rng=None)[source]
Generates a random draw from a 2-dimensional (independent) lognormal prior which represents the contact and recovery rate parameters of a basic SIR model.
- Parameters
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the 2-dimensional prior.
- Return type
np.ndarray of shape (2,)
- bayesflow.benchmarks.sir.simulator(theta, N=1000000.0, T=160, I0=1.0, R0=0.0, subsample=10, total_count=1000, rng=None)[source]
Runs a basic SIR model simulation for T time steps and returns subsample evenly spaced points from the simulated trajectory, given disease parameters (contact and recovery rate) theta.
See https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.9.
Note, that the simulator will scale the outputs between 0 and 1.
- Parameters
theta (np.ndarray of shape (2,)) – The 2-dimensional vector of disease parameters.
N (float, optional, default: 1e6 = 1 000 000) – The size of the simulated population.
T (T, optional, default: 160) – The duration (time horizon) of the simulation.
I0 (float, optional, default: 1.) – The number of initially infected individuals.
R0 (float, optional, default: 0.) – The number of initially recovered individuals.
subsample (int or None, optional, default: 10) – The number of evenly spaced time points to return. If None, no subsampling will be performed and all T timepoints will be returned.
total_count (int, optional, default: 1000) – The N parameter of the binomial noise distribution. Used just for scaling the data and magnifying the effect of noise, such that max infected = total_count.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – The time series of simulated infected individuals. A trailing dimension of 1 should be added by a BayesFlow configurator if the data is (properly) to be treated as time series.
- Return type
np.ndarray of shape (subsample,) or (T,) if subsample=None
bayesflow.benchmarks.slcp module
- bayesflow.benchmarks.slcp.configurator(forward_dict, mode='posterior', scale_data=30.0, as_summary_condition=False)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.slcp.prior(lower_bound=- 3.0, upper_bound=3.0, rng=None)[source]
Generates a random draw from a 5-dimensional uniform prior bounded between lower_bound and upper_bound which represents the 5 parameters of the SLCP simulator.
- Parameters
lower_bound (float, optional, default : -3) – The lower bound of the uniform prior.
upper_bound (float, optional, default : 3) – The upper bound of the uniform prior.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the 5-dimensional uniform prior.
- Return type
np.ndarray of shape (5, )
- bayesflow.benchmarks.slcp.simulator(theta, n_obs=4, flatten=True, rng=None)[source]
Generates data from the SLCP model designed as a benchmark for a simple likelihood and a complex posterior due to a non-linear pushforward theta -> x.
See https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.3
- Parameters
theta (np.ndarray of shape (theta, D)) – The location parameters of the Gaussian likelihood.
n_obs (int, optional, default: 4) – The number of observations to generate from the slcp likelihood.
flatten (bool, optional, default: True) – A flag to indicate whather a 1D (flatten=True) or a 2D (flatten=False) representation of the simulated data is returned.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – boolean flag. The sample of simulated data from the SLCP model.
- Return type
np.ndarray of shape (n_obs*2, ) or (n_obs, 2), as indictated by the flatten
bayesflow.benchmarks.slcp_distractors module
- bayesflow.benchmarks.slcp_distractors.configurator(forward_dict, mode='posterior', scale_data=50.0, as_summary_condition=False)[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.slcp_distractors.draw_mixture_student_t(num_students, n_draws=46, dim=2, mu_scale=15.0, rng=None)[source]
Helper function to generate n_draws random draws from a mixture of num_students multivariate Student-t distributions.
Uses the function get_random_student_t to create each of the studen-t callable objects.
- Parameters
num_students (int) – The number of multivariate student-t mixture components
n_draws (int, optional, default: 46) – The number of draws to obtain from the mixture distribution.
dim (int, optional, default: 2) – The dimensionality of each student-t distribution in the mixture.
mu_scale (float, optional, default: 15) – The scale of the zero-centered Gaussian prior from which the mean vector of each student-t distribution in the mixture is drawn.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
sample – The random draws from the mixture of students.
- Return type
np.ndarray of shape (n_draws, dim)
- bayesflow.benchmarks.slcp_distractors.get_random_student_t(dim=2, mu_scale=15, shape_scale=0.01, rng=None)[source]
A helper function to create a “frozen” multivariate student-t distribution of dimensions dim.
- Parameters
dim (int, optional, default: 2) – The dimensionality of the student-t distribution.
mu_scale (float, optional, default: 15) – The scale of the zero-centered Gaussian prior from which the mean vector of the student-t distribution is drawn.
shape_scale (float, optional, default: 0.01) – The scale of the assumed np.eye(dim) shape matrix. The default is chosen to keep the scale of the distractors and observations relatively similar.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
student – The student-t generator.
- Return type
callable (scipy.stats._multivariate.multivariate_t_frozen)
- bayesflow.benchmarks.slcp_distractors.prior(lower_bound=- 3.0, upper_bound=3.0, rng=None)[source]
Generates a random draw from a 5-dimensional uniform prior bounded between lower_bound and upper_bound.
- Parameters
lower_bound (float, optional, default : -3) – The lower bound of the uniform prior.
upper_bound (float, optional, default : 3) – The upper bound of the uniform prior.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the 5-dimensional uniform prior.
- Return type
np.ndarray of shape (5, )
- bayesflow.benchmarks.slcp_distractors.simulator(theta, n_obs=4, n_dist=46, dim=2, mu_scale=15.0, flatten=True, rng=None)[source]
Generates data from the SLCP model designed as a benchmark for a simple likelihood and a complex posterior due to a non-linear pushforward theta -> x. In addition, it outputs uninformative distractor data.
See https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.4
- Parameters
theta (np.ndarray of shape (theta, D)) – The location parameters of the Gaussian likelihood.
n_obs (int, optional, default: 4) – The number of observations to generate from the slcp likelihood.
n_dist (int, optional, default: 46) – The number of distractor to draw from the distractor likelihood.
dim (int, optional, default: 2) – The dimensionality of each student-t distribution in the mixture.
mu_scale (float, optional, default: 15) – The scale of the zero-centered Gaussian prior from which the mean vector of each student-t distribution in the mixture is drawn.
flatten (bool, optional, default: True) – A flag to indicate whather a 1D (flatten=True) or a 2D (flatten=False) representation of the simulated data is returned.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – np.ndarray of shape (n_obs + n_dist, 2) if flatten=False
- Return type
np.ndarray of shape (n_obs*2 + n_dist*2,) if flatten=True, otherwise
bayesflow.benchmarks.two_moons module
- bayesflow.benchmarks.two_moons.configurator(forward_dict, mode='posterior')[source]
Configures simulator outputs for use in BayesFlow training.
- bayesflow.benchmarks.two_moons.prior(lower_bound=- 1.0, upper_bound=1.0, rng=None)[source]
Generates a random draw from a 2-dimensional uniform prior bounded between lower_bound and upper_bound which represents the two parameters of the two moons simulator.
- Parameters
lower_bound (float, optional, default : -1) – The lower bound of the uniform prior.
upper_bound (float, optional, default : 1) – The upper bound of the uniform prior.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
theta – A single draw from the 2-dimensional uniform prior.
- Return type
np.ndarray of shape (2,)
- bayesflow.benchmarks.two_moons.simulator(theta, rng=None)[source]
Implements data generation from the two-moons model with a bimodal posterior. See https://arxiv.org/pdf/2101.04653.pdf, Benchmark Task T.8
- Parameters
theta (np.ndarray of shape (2,)) – The vector of two model parameters.
rng (np.random.Generator or None, default: None) – An optional random number generator to use.
- Returns
x – The 2D vector generated from the two moons simulator.
- Return type
np.ndarray of shape (2,)