sample#
- Normal.sample(shape=(), *, method=None, rng=None)[source]#
Random sample from the distribution.
- Parameters:
- shapetuple 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 method options are available for all distributions. If the selected method is not available, a NotImplementedError` will be raised.
- rng
numpy.random.Generator
, optional Pseudorandom number generator state. When rng is None, a new
numpy.random.Generator
is created using entropy from the operating system. Types other thannumpy.random.Generator
are passed tonumpy.random.default_rng
to instantiate a Generator.
References
[1]Sampling (statistics), Wikipedia, https://en.wikipedia.org/wiki/Sampling_(statistics)
Examples
Instantiate a distribution with the desired parameters:
>>> import numpy as np >>> from scipy import stats >>> X = stats.Uniform(a=0., b=1.)
Generate a pseudorandom sample:
>>> x = X.sample((1000, 1)) >>> octiles = (np.arange(8) + 1) / 8 >>> np.count_nonzero(x <= octiles, axis=0) array([ 148, 263, 387, 516, 636, 751, 865, 1000]) # may vary
>>> 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)