pyabc.transition

Transitions kernels

Transition or perturbation strategies to propose new parameters based on the current population. Usually this translates to randomly selecting a parameter in the current generation and then perturbing it, but in general arbitrary transition kernels are possible.

These can be passed to pyabc.smc.ABCSMC via the transitions keyword argument.

class pyabc.transition.AggregatedTransition(mapping: Dict[str | Tuple[str, ...], Transition])[source]

Bases: Transition

Different transitions for different subsets of the parameters.

The transitions are applied independently of each other, i.e. the transition density factorizes. Correlations betweeen parameters must be handled inside a single transition, if needed.

Parameters:

mapping – The mapping of parameters (as tuples of str or single str) to the transition kernel to be used for those parameters.

__init__(mapping: Dict[str | Tuple[str, ...], Transition])[source]
fit(X: DataFrame, w: ndarray) None[source]

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

pdf(x: Parameter | Series | DataFrame) float | ndarray[source]

Evaluate the probability density function (PDF) at x.

Parameters:

x – Parameter. If x is a Parameter or Series, then x should have the columns from X passed to the fit method as indices. If x is a DataFrame, then x should have the same columns as X passed before to the fit method. The order of the columns is not important

Returns:

density – Probability density at x.

Return type:

float

rvs(size: int = None) Parameter | DataFrame

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

rvs_single() Parameter[source]

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') AggregatedTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') AggregatedTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.DiscreteJumpTransition(domain: ndarray, p_stay: float = 0.7)[source]

Bases: DiscreteTransition

Transition with positive random jump probability for discrete parameters. Adapts base draw probabilities to the last generation’s histogram and then jumps to an arbitrary other parameter with a positive jump probability to ensure that the prior is absolutely continuous w.r.t. the proposal.

Parameters:
  • domain – See the PerturbationKernel.

  • p_stay – See the PerturbationKernel.

  • note:: (..) – This transition can only deal with a single parameter. Use an AggregatedTransition to combine multiple parameters.

__init__(domain: ndarray, p_stay: float = 0.7)[source]
fit(X: DataFrame, w: ndarray) None[source]

Fit starting weights to the distribution of samples.

pdf(x: Parameter | Series | DataFrame) float | ndarray[source]

Compute the probability mass function at x.

rvs(size: int = None) Parameter | DataFrame

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

rvs_single() Parameter[source]

Generate a single random variable.

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteJumpTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteJumpTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.DiscreteRandomWalkTransition(n_steps: int = 1, p_l: float = 0.3333333333333333, p_r: float = 0.3333333333333333, p_c: float = 0.3333333333333333)[source]

Bases: DiscreteTransition

This transition is based on a discrete random walk. This may be useful for discrete ordinal parameter distributions that can be described as lying on the grid of integers.

Note

This transition does not adapt to the problem structure and thus has potentially slow convergence. Further, the transition does not satisfy proposal >> prior, so that it is indeed not valid as an importance sampling distribution. This can be overcome by selecting the number of steps as a random variable.

Parameters:

n_steps (int, optional (default = 1)) – Number of random walk steps to take.

__init__(n_steps: int = 1, p_l: float = 0.3333333333333333, p_r: float = 0.3333333333333333, p_c: float = 0.3333333333333333)[source]
fit(X: DataFrame, w: ndarray)[source]

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

pdf(x: Parameter | Series | DataFrame) float | ndarray[source]

Evaluate the probability mass function (PMF) at x.

rvs(size: int = None) Parameter | DataFrame

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

rvs_single() Parameter[source]

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteRandomWalkTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteRandomWalkTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.DiscreteTransition[source]

Bases: Transition

This is a base class for discrete transition kernels.

abstract fit(X: DataFrame, w: ndarray) None

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

abstract pdf(x: Parameter | Series | DataFrame) float | ndarray

Evaluate the probability density function (PDF) at x.

Parameters:

x – Parameter. If x is a Parameter or Series, then x should have the columns from X passed to the fit method as indices. If x is a DataFrame, then x should have the same columns as X passed before to the fit method. The order of the columns is not important

Returns:

density – Probability density at x.

Return type:

float

rvs(size: int = None) Parameter | DataFrame

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

abstract rvs_single() Parameter

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') DiscreteTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.GridSearchCV(estimator=None, param_grid=None, scoring=None, n_jobs=1, refit=True, cv=5, verbose=0, pre_dispatch='2*n_jobs', error_score='raise', return_train_score=True)[source]

Bases: GridSearchCV

Do a grid search to automatically select the best parameters for transition classes such as the pyabc.transition.MultivariateNormalTransition.

This is essentially a thin wrapper around ‘sklearn.model_selection.GridSearchCV’. It translates the scikit-learn interface to the interface used in pyABC. It implements hence a thin adapter pattern.

The parameters are just as for sklearn.model_selection.GridSearchCV. Major default values:

  • estimator = MultivariateNormalTransition()

  • param_grid = {‘scaling’: np.linspace(0.05, 1.0, 5)}

  • cv = 5

__init__(estimator=None, param_grid=None, scoring=None, n_jobs=1, refit=True, cv=5, verbose=0, pre_dispatch='2*n_jobs', error_score='raise', return_train_score=True)[source]
fit(X, y=None, groups=None)[source]

Fit the density estimator (perturber) to the sampled data.

set_fit_request(*, groups: bool | None | str = '$UNCHANGED$') GridSearchCV

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

groups (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for groups parameter in fit.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.LocalTransition(k=None, k_fraction=0.25, scaling=1)[source]

Bases: Transition

Local KDE fit. Takes into account only the k nearest neighbors, similar to [Filippi].

Parameters:
  • k (int) – Number of nearest neighbors for local covariance calculation.

  • scaling (float) – Scaling factor for the local covariance matrices.

  • k_fraction (float, optional) – Calculate number of nearest neighbors to use according to k = k_fraction * population_size (and rounds it).

EPS

Scaling of the identity matrix to be added to the covariance in case the covariances are not invertible.

Type:

float

[Filippi]

Filippi, Sarah, Chris P. Barnes, Julien Cornebise, and Michael P.H. Stumpf. “On Optimality of Kernels for Approximate Bayesian Computation Using Sequential Monte Carlo.” Statistical Applications in Genetics and Molecular Biology 12, no. 1 (2013): 87–107. doi:10.1515/sagmb-2012-0069.

__init__(k=None, k_fraction=0.25, scaling=1)[source]
fit(X, w)[source]

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

pdf(x: Parameter | Series | DataFrame)[source]

Evaluate the probability density function (PDF) at x.

Parameters:

x – Parameter. If x is a Parameter or Series, then x should have the columns from X passed to the fit method as indices. If x is a DataFrame, then x should have the same columns as X passed before to the fit method. The order of the columns is not important

Returns:

density – Probability density at x.

Return type:

float

rvs(size: int = None) Parameter | DataFrame

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

rvs_single() Parameter[source]

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') LocalTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') LocalTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

class pyabc.transition.ModelPerturbationKernel(nr_of_models: int, probability_to_stay: float | None = None)[source]

Bases: object

Model perturbation kernel.

nr_of_models

Number of models

probability_to_stay

If None, probability to stay is set to 1/nr_of_models. Otherwise, the supplied value is used.

__init__(nr_of_models: int, probability_to_stay: float | None = None)[source]
pmf(n: int, m: int) float[source]

Probability mass function for a jump to target n from source m.

Parameters:
  • n (int) – Model target nr.

  • m (int) – Model source nr.

Returns:

probability – Probability with which to jump from m to n.

Return type:

float

rvs(m: int) int[source]

Sample a Kernel jump from model m to another model.

Parameters:

m (int) – Model source nr.

Returns:

target – Target model nr.

Return type:

int

class pyabc.transition.MultivariateNormalTransition(scaling: float = 1, bandwidth_selector: ~typing.Callable[[int, int], float] = <function silverman_rule_of_thumb>)[source]

Bases: Transition

Transition via a multivariate Gaussian KDE estimate.

Parameters:
  • scaling (float) – Scaling is a factor which additionally multiplies the covariance with. Since Silverman and Scott usually have too large bandwidths, it should make most sense to have 0 < scaling <= 1

  • bandwidth_selector (optional) – Defaults to silverman_rule_of_thumb. The bandwidth selector is a function of the form f(n_samples: float, dimension: int), where n_samples denotes the (effective) samples size (and is therefore) a float and dimension is the parameter dimension.

__init__(scaling: float = 1, bandwidth_selector: ~typing.Callable[[int, int], float] = <function silverman_rule_of_thumb>)[source]
fit(X: DataFrame, w: ndarray) None[source]

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

pdf(x: Parameter | Series | DataFrame) float | ndarray[source]

Evaluate the probability density function (PDF) at x.

Parameters:

x – Parameter. If x is a Parameter or Series, then x should have the columns from X passed to the fit method as indices. If x is a DataFrame, then x should have the same columns as X passed before to the fit method. The order of the columns is not important

Returns:

density – Probability density at x.

Return type:

float

rvs(size: int = None) Parameter | DataFrame[source]

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

rvs_single() Parameter[source]

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') MultivariateNormalTransition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') MultivariateNormalTransition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

exception pyabc.transition.NotEnoughParticles[source]

Bases: Exception

class pyabc.transition.PerturbationKernel(domain: ndarray, p_stay: float = 0.7)[source]

Bases: object

Parameter perturbation kernel for a discrete set of parameters.

Parameters:
  • domain – All possible parameter values.

  • p_stay – The probability to stay at a given parameter value.

__init__(domain: ndarray, p_stay: float = 0.7)[source]
pdf(b: float, a: float) float[source]

Probability mass function for a jump to target b from source a.

rvs(a: float) float[source]

Sample a kernel jump from parameter a to another parameter.

class pyabc.transition.Transition[source]

Bases: BaseEstimator

Abstract Transition base class. Derive all Transitions from this class

Note

This class does a little bit of meta-programming.

The fit, pdf and rvs methods are automatically wrapped to handle the special case of no parameters.

Hence, you can safely assume that you encounter at least one parameter. All the defined transitions will then automatically generalize to the case of no parameter.

abstract fit(X: DataFrame, w: ndarray) None[source]

Fit the density estimator (perturber) to the sampled data. Concrete implementations might do something like fitting a KDE.

The parameters given as X and w are automatically stored in self.X and self.w.

Parameters:
  • X – The parameters.

  • w – The corresponding weights

mean_cv(n_samples: None | int = None) float[source]

Estimate the uncertainty on the KDE.

Parameters:

n_samples (int, optional) – Estimate the CV for n_samples samples. If this parameter is not given, the sample size of the last fit is used.

Returns:

mean_cv – The estimated average coefficient of variation.

Return type:

float

Note

A call to this method, as a side effect, also sets the attributes test_points_, test_weights_ and variation_at_test_points_. These are the individual points, weights and variations used to calculate the mean.

abstract pdf(x: Parameter | Series | DataFrame) float | ndarray[source]

Evaluate the probability density function (PDF) at x.

Parameters:

x – Parameter. If x is a Parameter or Series, then x should have the columns from X passed to the fit method as indices. If x is a DataFrame, then x should have the same columns as X passed before to the fit method. The order of the columns is not important

Returns:

density – Probability density at x.

Return type:

float

rvs(size: int = None) Parameter | DataFrame[source]

Sample from the density.

Parameters:

size – Number of independent samples to draw. If None, a single Parameter from rvs_single() is returned, if it is an integer >= 1, a pandas.DataFrame with the corresponding number of rows is returned.

Returns:

samples

Return type:

The parameter sample(s).

Note

This method can be overridden for efficient implementations. The default is to call rvs_single repeatedly (which might not be the most efficient way).

abstract rvs_single() Parameter[source]

Random variable sample (rvs).

Sample from the fitted distribution.

Returns:

A sample from the fitted model.

Return type:

sample

set_fit_request(*, w: bool | None | str = '$UNCHANGED$') Transition

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, w: bool | None | str = '$UNCHANGED$') Transition

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

w (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for w parameter in score.

Returns:

self – The updated object.

Return type:

object

pyabc.transition.scott_rule_of_thumb(n_samples, dimension)[source]

Scott’s rule of thumb.

\[\left ( \frac{1}{n} \right ) ^{\frac{1}{d+4}}\]

(see also scipy.stats.kde.gaussian_kde.scotts_factor)

pyabc.transition.silverman_rule_of_thumb(n_samples, dimension)[source]

Silverman’s rule of thumb.

\[\left ( \frac{4}{n (d+2)} \right ) ^ {\frac{1}{d + 4}}\]

(see also scipy.stats.kde.gaussian_kde.silverman_factor)