bundles / scipy 1.18.0 / scipy / stats / _probability_distribution / _ProbabilityDistribution / sample
function
scipy.stats._probability_distribution:_ProbabilityDistribution.sample
Signature
def sample(self, shape, *, method, rng)Summary
Random sample from the distribution.
Parameters
shape: tuple of ints, default: ()The shape of the sample to draw. If the parameters of the distribution underlying the random variable are arrays of shape
param_shape, the output array will be of shapeshape + param_shape.method: {None, 'formula', 'inverse_transform'}The strategy used to produce the sample. By default (
None), the infrastructure chooses between the following options, listed in order of precedence.'formula': an implementation specific to the distribution'inverse_transform': generate a uniformly distributed sample and return the inverse CDF at these arguments.
Not all
methodoptions are available for all distributions. If the selectedmethodis not available, aNotImplementedError'will be raised.rng: `numpy.random.Generator` or `scipy.stats.qmc.QMCEngine`, optionalPseudo- or quasi-random number generator state. When
rngis None, a new numpy.random.Generator is created using entropy from the operating system. Types other than numpy.random.Generator and scipy.stats.qmc.QMCEngine are passed to numpy.random.default_rng to instantiate aGenerator.If
rngis an instance of scipy.stats.qmc.QMCEngine configured to use scrambling andshapeis not empty, then each slice along the zeroth axis of the result is a "quasi-independent", low-discrepancy sequence; that is, they are distinct sequences that can be treated as statistically independent for most practical purposes. Separate calls to sample produce new quasi-independent, low-discrepancy sequences. The dimensionality (d) of the providedQMCEngineis ignored; new instances of the same class and configuration options (scramble,optimization, andbits, where applicable) will be created withd=1.
Examples
Instantiate a distribution with the desired parameters:import numpy as np from scipy import stats X = stats.Uniform(a=0., b=1.)✓
x = X.sample((1000, 1)) octiles = (np.arange(8) + 1) / 8✓
np.count_nonzero(x <= octiles, axis=0)
✗array([ 124, 258, 388, 507, 628, 743, 873, 1000])
X = stats.Uniform(a=np.zeros((3, 1)), b=np.ones(2))
✓X.a.shape,
✗((3, 2),)
x = X.sample(shape=(5, 4)) x.shape✓
(5, 4, 3, 2)
Aliases
scipy.stats._distribution_infrastructure._ProbabilityDistribution.sample