Skip to content

API Reference

Decorators

pytest_stochastic.stochastic_test(*, expected, atol=0.0, rtol=0.0, failure_prob=1e-08, bounds=None, variance=None, sub_gaussian_param=None, symmetric=False, moment_bound=None, side='two-sided', seed=None)

Decorator that turns a scalar-returning function into a stochastic test.

Parameters:

Name Type Description Default
expected float

Expected value of the test statistic.

required
atol float

Absolute tolerance.

0.0
rtol float

Relative tolerance (fraction of |expected|).

0.0
failure_prob float

Target false-failure probability.

1e-08
bounds tuple[float, float] | None

(a, b) guaranteeing each sample lies in [a, b].

None
variance float | None

Upper bound on Var(X_i).

None
sub_gaussian_param float | None

Sub-Gaussian parameter sigma.

None
symmetric bool

True if the distribution is symmetric about its mean.

False
moment_bound tuple[float, float] | None

(p, M) such that E[|X - μ|^p] ≤ M for some p > 1.

None
side str

"two-sided" (default), "greater", or "less".

'two-sided'
seed int | None

Optional fixed RNG seed for reproducibility.

None

pytest_stochastic.distributional_test(*, reference, test='ks', significance=1e-06, n_samples=10000, seed=None)

Decorator for testing that outputs match a reference distribution.

Parameters:

Name Type Description Default
reference object

A scipy continuous distribution (e.g. scipy.stats.norm(0, 1)).

required
test str

Statistical test to use: "ks" (Kolmogorov-Smirnov), "chi2" (chi-squared goodness-of-fit), or "anderson".

'ks'
significance float

Significance level alpha. The test asserts p-value > significance.

1e-06
n_samples int

Number of samples to draw from the test function.

10000
seed int | None

Optional fixed RNG seed for reproducibility.

None

Types

pytest_stochastic.TestConfig dataclass

Validated configuration for a single stochastic test.

declared_properties property

Return a dict of all non-None declared distributional properties.

pytest_stochastic.BoundStrategy dataclass

A concentration inequality strategy.

Each instance encapsulates one bound from the registry: the properties it requires, its sample-size formula, and which estimator it uses.

pytest_stochastic.EstimatorType

Bases: Enum

Estimator used to aggregate samples.

Exceptions

pytest_stochastic.ConfigurationError

Bases: Exception

Base error for invalid stochastic test configuration.

pytest_stochastic.NoApplicableBoundError

Bases: ConfigurationError

No concentration bound is applicable with the declared properties.

pytest_stochastic.InvalidToleranceError

Bases: ConfigurationError

The tolerance specification is invalid.

pytest_stochastic.InvalidPropertyError

Bases: ConfigurationError

A declared distributional property is invalid.