From cae06dc8317b6a1740199acab4afdd9ba0353a28 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 09:05:00 +0100 Subject: [PATCH 01/14] REF: Remover generator and randomstate Remvoe promoised deprecations --- doc/source/generator.rst | 86 +- doc/source/legacy.rst | 106 +- doc/source/new-or-different.rst | 75 - randomgen/_seed_sequence.pyx | 2 + randomgen/aes.pyx | 2 + randomgen/chacha.pyx | 2 + randomgen/dsfmt.pyx | 2 + randomgen/efiix64.pyx | 2 + randomgen/entropy.pyx | 2 + randomgen/generator.pyi | 406 +- randomgen/generator.pyx | 4991 +---------------- randomgen/hc128.pyx | 2 + randomgen/jsf.pyx | 3 + randomgen/lxm.pyx | 3 + randomgen/mt19937.pyx | 3 + randomgen/mt64.pyx | 3 + randomgen/mtrand.pyi | 291 +- randomgen/mtrand.pyx | 4274 +------------- randomgen/pcg32.pyx | 3 + randomgen/pcg64.pyx | 3 + randomgen/philox.pyx | 3 + randomgen/rdrand.pyx | 3 + randomgen/romu.pyx | 3 + randomgen/sfc.pyx | 9 +- randomgen/sfmt.pyx | 3 + randomgen/speck128.pyx | 3 + randomgen/tests/test_against_numpy.py | 619 -- randomgen/tests/test_direct.py | 108 +- randomgen/tests/test_extended_generator.py | 6 +- randomgen/tests/test_final_release_changes.py | 42 - randomgen/tests/test_generator_117.py | 325 -- randomgen/tests/test_generator_mt19937.py | 2773 +-------- .../test_generator_mt19937_regressions.py | 156 - randomgen/tests/test_randomstate.py | 2132 ------- .../tests/test_randomstate_regression.py | 182 - randomgen/tests/test_recent_numpy_changes.py | 141 - randomgen/tests/test_sfc.py | 4 +- randomgen/tests/test_smoke.py | 205 +- randomgen/tests/test_wrapper.py | 2 +- randomgen/tests/test_wrapper_numba.py | 2 +- randomgen/threefry.pyx | 3 + randomgen/wrapper.pyx | 3 + randomgen/xoroshiro128.pyx | 3 + randomgen/xorshift1024.pyx | 4 +- randomgen/xoshiro256.pyx | 3 + randomgen/xoshiro512.pyx | 3 + setup.py | 7 +- tools/practrand-driver.py | 8 +- tools/process-prng-tester-results.py | 10 +- 49 files changed, 517 insertions(+), 16509 deletions(-) delete mode 100644 randomgen/tests/test_against_numpy.py delete mode 100644 randomgen/tests/test_generator_117.py delete mode 100644 randomgen/tests/test_generator_mt19937_regressions.py delete mode 100644 randomgen/tests/test_randomstate.py delete mode 100644 randomgen/tests/test_randomstate_regression.py delete mode 100644 randomgen/tests/test_recent_numpy_changes.py diff --git a/doc/source/generator.rst b/doc/source/generator.rst index efac33dc3..fd9346e6f 100644 --- a/doc/source/generator.rst +++ b/doc/source/generator.rst @@ -7,94 +7,10 @@ Random Generator

Deprecated

- :class:`~randomgen.generator.Generator` is **deprecated**. You should be using + :class:`~randomgen.generator.Generator` has been **removed**. You should be using :class:`numpy.random.Generator`. -The :class:`~randomgen.generator.Generator` provides access to -a wide range of distributions, and served as a replacement for -:class:`~numpy.random.RandomState`. The main difference between -the two is that :class:`~randomgen.generator.Generator` relies -on an additional bit generator to manage state and generate the random -bits which are then transformed into random values from useful -distributions. The default bit generator used by -:class:`~randomgen.generator.Generator` is -:class:`~randomgen.xoroshiro128.Xoroshiro128`. The bit generator can be -changed by passing an instantized bit generator to -:class:`~randomgen.generator.Generator`. - .. currentmodule:: randomgen.generator .. autoclass:: Generator - -Seed and State Manipulation -=========================== -.. autosummary:: - :toctree: generated/ - - ~Generator.seed - ~Generator.state - ~Generator.bit_generator - -Simple random data -================== -.. autosummary:: - :toctree: generated/ - - ~Generator.rand - ~Generator.randn - ~Generator.integers - ~Generator.random - ~Generator.choice - ~Generator.bytes - ~Generator.uintegers - -Permutations -============ -.. autosummary:: - :toctree: generated/ - - ~Generator.shuffle - ~Generator.permutation - -Distributions -============= -.. autosummary:: - :toctree: generated/ - - ~Generator.beta - ~Generator.binomial - ~Generator.chisquare - ~Generator.complex_normal - ~Generator.dirichlet - ~Generator.exponential - ~Generator.f - ~Generator.gamma - ~Generator.geometric - ~Generator.gumbel - ~Generator.hypergeometric - ~Generator.laplace - ~Generator.logistic - ~Generator.lognormal - ~Generator.logseries - ~Generator.multinomial - ~Generator.multivariate_normal - ~Generator.negative_binomial - ~Generator.noncentral_chisquare - ~Generator.noncentral_f - ~Generator.normal - ~Generator.pareto - ~Generator.poisson - ~Generator.power - ~Generator.rayleigh - ~Generator.standard_cauchy - ~Generator.standard_exponential - ~Generator.standard_gamma - ~Generator.standard_normal - ~Generator.standard_t - ~Generator.triangular - ~Generator.uniform - ~Generator.vonmises - ~Generator.wald - ~Generator.weibull - ~Generator.zipf \ No newline at end of file diff --git a/doc/source/legacy.rst b/doc/source/legacy.rst index 710049297..8586e65b9 100644 --- a/doc/source/legacy.rst +++ b/doc/source/legacy.rst @@ -7,115 +7,11 @@ Legacy Random Generation

Deprecated

- :class:`~randomgen.mtrand.RandomState` is **deprecated**. You should be using + :class:`~randomgen.mtrand.RandomState` has been **removed**. You should be using :class:`numpy.random.Generator`, or if you must have backward compatibility with NumPy before 1.17, :class:`numpy.random.RandomState`. -The :class:`~randomgen.mtrand.RandomState` provides access to -legacy generators. These all depend on normals produced using a -polar transformation or inverse CDF exponentials or gammas. This -class should only be used if it is essential to have randoms that -are identical to what would have been produced by NumPy. - -:class:`~randomgen.mtrand.RandomState` add additional information -to the state which is required when using Box-Muller normals since these -are produced in pairs. It is important to use -:attr:`~randomgen.mtrand.RandomState.get_state()` -when accessing the state so that these extra values are saved. - -.. code-block:: python - - from randomgen import MT19937 - from randomgen.mtrand import RandomState - from numpy.random import RandomState - # Use same seed - rs = RandomState(12345) - mt19937 = MT19937(12345) - lg = RandomState(mt19937) - - # Identical output - rs.standard_normal() - lg.standard_normal() - - rs.random_sample() - lg.random_sample() - - rs.standard_exponential() - lg.standard_exponential() - - .. currentmodule:: randomgen.mtrand .. autoclass:: RandomState - -Seeding and State -================= - -.. autosummary:: - :toctree: generated/ - - ~RandomState.get_state - ~RandomState.set_state - -Simple random data -================== -.. autosummary:: - :toctree: generated/ - - ~RandomState.rand - ~RandomState.randn - ~RandomState.randint - ~RandomState.random_integers - ~RandomState.random_sample - ~RandomState.choice - ~RandomState.bytes - -Permutations -============ -.. autosummary:: - :toctree: generated/ - - ~RandomState.shuffle - ~RandomState.permutation - -Distributions -============= -.. autosummary:: - :toctree: generated/ - - ~RandomState.beta - ~RandomState.binomial - ~RandomState.chisquare - ~RandomState.dirichlet - ~RandomState.exponential - ~RandomState.f - ~RandomState.gamma - ~RandomState.geometric - ~RandomState.gumbel - ~RandomState.hypergeometric - ~RandomState.laplace - ~RandomState.logistic - ~RandomState.lognormal - ~RandomState.logseries - ~RandomState.multinomial - ~RandomState.multivariate_normal - ~RandomState.negative_binomial - ~RandomState.noncentral_chisquare - ~RandomState.noncentral_f - ~RandomState.normal - ~RandomState.pareto - ~RandomState.poisson - ~RandomState.power - ~RandomState.rayleigh - ~RandomState.standard_cauchy - ~RandomState.standard_exponential - ~RandomState.standard_gamma - ~RandomState.standard_normal - ~RandomState.standard_t - ~RandomState.triangular - ~RandomState.uniform - ~RandomState.vonmises - ~RandomState.wald - ~RandomState.weibull - ~RandomState.zipf \ No newline at end of file diff --git a/doc/source/new-or-different.rst b/doc/source/new-or-different.rst index 45fe9e558..2fadd79b9 100644 --- a/doc/source/new-or-different.rst +++ b/doc/source/new-or-different.rst @@ -109,36 +109,6 @@ Differences from NumPy before 1.17 :meth:`~randomgen.generator.Generator.standard_exponential` or :meth:`~randomgen.generator.Generator.standard_gamma`. -.. ipython:: python - :suppress: - :okwarning: - - import warnings - warnings.filterwarnings("ignore", "RandomState", FutureWarning) - warnings.filterwarnings("ignore", "Generator", FutureWarning) - from randomgen import Generator - Generator() - -.. ipython:: python - :okwarning: - - from randomgen import Generator, Xoroshiro128 - import numpy.random - rg = Generator(Xoroshiro128(mode="sequence")) - %timeit rg.standard_normal(100000) - %timeit numpy.random.standard_normal(100000) - -.. ipython:: python - - %timeit rg.standard_exponential(100000) - %timeit numpy.random.standard_exponential(100000) - -.. ipython:: python - - %timeit rg.standard_gamma(3.0, 100000) - %timeit numpy.random.standard_gamma(3.0, 100000) - - * The Box-Muller used to produce NumPy's normals is no longer available. * All bit generators functions to produce doubles, uint64s and uint32s via CTypes (:meth:`~randomgen.xoroshiro128.Xoroshiro128.ctypes`) @@ -156,13 +126,6 @@ Differences from NumPy before 1.17 * Standard Gammas (:meth:`~randomgen.generator.Generator.standard_gamma`) * Standard Exponentials (:meth:`~randomgen.generator.Generator.standard_exponential`) -.. ipython:: python - - rg.seed(0) - rg.random(3, dtype='d') - rg.seed(0) - rg.random(3, dtype='f') - * Optional ``out`` argument that allows existing arrays to be filled for select core distributions @@ -174,11 +137,6 @@ Differences from NumPy before 1.17 This allows multithreading to fill large arrays in chunks using suitable PRNGs in parallel. -.. ipython:: python - - existing = np.zeros(4) - rg.random(out=existing[:2]) - print(existing) * :meth:`~randomgen.generator.Generator.integers` supports broadcasting inputs. @@ -188,60 +146,27 @@ Differences from NumPy before 1.17 ``endpoint``. Closed intervals are simpler to use when the distribution may include the maximum value of a given integer type. -.. ipython:: python - - rg.seed(1234) - rg.integers(0, np.iinfo(np.int64).max+1) - rg.seed(1234) - rg.integers(0, np.iinfo(np.int64).max, endpoint=True) * The closed interval is particularly helpful when using arrays since it avoids object-dtype arrays when sampling from the full range. -.. ipython:: python - - rg.seed(1234) - lower = np.zeros((2, 1), dtype=np.uint64) - upper = np.array([10, np.iinfo(np.uint64).max+1], dtype=object) - upper - rg.integers(lower, upper, dtype=np.uint64) - rg.seed(1234) - upper = np.array([10, np.iinfo(np.uint64).max], dtype=np.uint64) - upper - rg.integers(lower, upper, endpoint=True, dtype=np.uint64) * Support for Lemire’s method of generating uniform integers on an arbitrary interval by setting ``use_masked=True`` in (:meth:`~randomgen.generator.Generator.integers`). -.. ipython:: python - :okwarning: - - %timeit rg.integers(0, 1535, size=100000, use_masked=False) - %timeit numpy.random.randint(0, 1535, size=100000) * :meth:`~randomgen.generator.Generator.multinomial` supports multidimensional values of ``n`` -.. ipython:: python - - rg.multinomial([10, 100], np.ones(6) / 6.) * :meth:`~randomgen.generator.Generator.choice` is much faster when sampling small amounts from large arrays -.. ipython:: python - - x = np.arange(1000000) - %timeit rg.choice(x, 10) * :meth:`~randomgen.generator.Generator.choice` supports the ``axis`` keyword to work with multidimensional arrays. -.. ipython:: python - - x = np.reshape(np.arange(20), (2, 10)) - rg.choice(x, 2, axis=1) * For changes since the previous release, see the :ref:`change-log` diff --git a/randomgen/_seed_sequence.pyx b/randomgen/_seed_sequence.pyx index e24a45f21..269386075 100644 --- a/randomgen/_seed_sequence.pyx +++ b/randomgen/_seed_sequence.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True """ BitGenerator base class and SeedSequence used to seed the BitGenerators. diff --git a/randomgen/aes.pyx b/randomgen/aes.pyx index caf0ae81f..adf15ead4 100644 --- a/randomgen/aes.pyx +++ b/randomgen/aes.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True import numpy as np from randomgen.common cimport * diff --git a/randomgen/chacha.pyx b/randomgen/chacha.pyx index 244dffa4f..a8dc5a700 100644 --- a/randomgen/chacha.pyx +++ b/randomgen/chacha.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True import numpy as np from randomgen.common cimport * diff --git a/randomgen/dsfmt.pyx b/randomgen/dsfmt.pyx index bc7503dbe..1bf725506 100644 --- a/randomgen/dsfmt.pyx +++ b/randomgen/dsfmt.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True import operator import numpy as np diff --git a/randomgen/efiix64.pyx b/randomgen/efiix64.pyx index 1471dac14..45a02b508 100644 --- a/randomgen/efiix64.pyx +++ b/randomgen/efiix64.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True import numpy as np cimport numpy as np diff --git a/randomgen/entropy.pyx b/randomgen/entropy.pyx index babf762c0..eb770e773 100644 --- a/randomgen/entropy.pyx +++ b/randomgen/entropy.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True cimport numpy as np import numpy as np diff --git a/randomgen/generator.pyi b/randomgen/generator.pyi index 29b38f26e..b254739ba 100644 --- a/randomgen/generator.pyi +++ b/randomgen/generator.pyi @@ -8,362 +8,56 @@ from randomgen.common import BitGenerator from randomgen.typing import RequiredSize, Size class Generator: - _bit_generator: BitGenerator - lock: Lock - _poisson_lam_max: int + ... def __init__(self, bit_generator: Optional[BitGenerator] = ...) -> None: ... - @property - def bit_generator(self) -> BitGenerator: ... - def seed(self, *args: Any, **kwargs: Any) -> None: ... - @property - def state(self) -> Dict[str, Any]: ... - @state.setter - def state(self, value: Dict[str, Any]) -> None: ... - def uintegers( - self, size: Size = ..., bits: Literal[32, 64] = ... - ) -> Union[int, ndarray]: ... - def random_uintegers( - self, size: Size = ..., bits: Literal[32, 64] = ... - ) -> Union[int, ndarray]: ... - def random_sample( - self, *args: Tuple[int, ...], **kwargs: Dict[str, Tuple[int, ...]] - ) -> Union[float, ndarray]: ... - def random( - self, size: Size = ..., dtype: str = ..., out: ndarray = ... - ) -> Union[float, ndarray]: ... - def beta( - self, a: Union[float, ndarray], b: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def exponential( - self, scale: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[float, ndarray]: ... - def standard_exponential( - self, size: Size = ..., dtype: str = ..., method: str = ..., out: ndarray = ... - ) -> Union[float, ndarray]: ... - def tomaxint(self, size: Size = ...) -> Union[int, ndarray]: ... - def randint( - self, - *args: Tuple[Union[int, Tuple[int, ...]], ...], - **kwargs: Dict[str, Union[int, Tuple[int, ...]]] - ) -> Union[int, ndarray]: ... - def integers( - self, - low: Union[int, ndarray], - high: Optional[Union[int, ndarray]] = ..., - size: Size = ..., - dtype: str = ..., - use_masked: Optional[bool] = ..., - endpoint: bool = ..., - closed: bool = ..., - ) -> Union[int, ndarray]: ... - def bytes(self, length: int) -> ndarray: ... - def choice( - self, - a: Union[int, Sequence[Any]], - size: Size = ..., - replace: bool = ..., - p: Optional[ndarray] = ..., - axis: int = ..., - shuffle: bool = ..., - ) -> Sequence[Any]: ... - def uniform( - self, - low: Optional[Union[float, ndarray]] = ..., - high: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def rand( - self, *args: Tuple[int, ...], dtype: str = ... - ) -> Union[float, ndarray]: ... - def randn( - self, *args: Tuple[int, ...], dtype: str = ... - ) -> Union[float, ndarray]: ... - def random_integers( - self, - low: Union[int, ndarray], - high: Optional[Union[int, ndarray]] = ..., - size: Size = ..., - ) -> Union[int, ndarray]: ... - # Complicated, continuous distributions:... - def standard_normal( - self, size: Size = ..., dtype: str = ..., out: ndarray = ... - ) -> Union[float, ndarray]: ... - def normal( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def standard_gamma( - self, - shape: Union[float, ndarray], - size: Size = ..., - dtype: str = ..., - out: ndarray = ..., - ) -> Union[float, ndarray]: ... - def gamma( - self, - shape: Union[float, ndarray], - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def f( - self, - dfnum: Union[float, ndarray], - dfden: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def noncentral_f( - self, - dfnum: Union[float, ndarray], - dfden: Union[float, ndarray], - nonc: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def chisquare( - self, df: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def noncentral_chisquare( - self, df: Union[float, ndarray], nonc: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def standard_cauchy(self, size: Size = ...) -> Union[float, ndarray]: ... - def standard_t( - self, df: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def vonmises( - self, mu: Union[float, ndarray], kappa: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def pareto( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def weibull( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def power( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def laplace( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def gumbel( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def logistic( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def lognormal( - self, - mean: Optional[Union[float, ndarray]] = ..., - sigma: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def rayleigh( - self, scale: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[float, ndarray]: ... - def wald( - self, - mean: Union[float, ndarray], - scale: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def triangular( - self, - left: Union[float, ndarray], - mode: Union[float, ndarray], - right: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - # Complicated, discrete distributions: - def binomial( - self, n: Union[int, ndarray], p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def negative_binomial( - self, n: Union[int, ndarray], p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def poisson( - self, lam: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[int, ndarray]: ... - def zipf( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def geometric( - self, p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def hypergeometric( - self, - ngood: Union[int, ndarray], - nbad: Union[int, ndarray], - nsample: Union[int, ndarray], - size: Size = ..., - ) -> Union[int, ndarray]: ... - def logseries( - self, p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - # Multivariate distributions: - def multivariate_normal( - self, - mean: ndarray, - cov: ndarray, - size: Size = ..., - check_valid: str = ..., - tol: float = ..., - *, - method: str = ... - ) -> ndarray: ... - def multinomial( - self, n: Union[int, ndarray], pvals: Union[float, ndarray], size: Size = ... - ) -> ndarray: ... - def dirichlet(self, alpha: ndarray, size: Size = ...) -> ndarray: ... - # Shuffling and permutations: - def shuffle(self, x: Sequence[Any]) -> None: ... - def permutation(self, x: Sequence[Any]) -> None: ... - def complex_normal( - self, - loc: Optional[Union[float, ndarray]] = ..., - gamma: Optional[Union[float, ndarray]] = ..., - relation: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[complex, ndarray]: ... -class ExtendedGenerator: - _bit_generator: BitGenerator - lock: Lock - _generator: Generator - def __init__(self, bit_generator: Optional[BitGenerator] = ...) -> None: ... - @property - def bit_generator(self) -> BitGenerator: ... - @property - def state(self) -> Dict[str, Any]: ... - @state.setter - def state(self, value: Dict[str, Any]) -> None: ... - @overload - def uintegers(self, size: None, bits: Literal[32, 64] = ...) -> int: ... - @overload - def uintegers(self, size: RequiredSize, bits: Literal[32, 64] = ...) -> ndarray: ... - @overload - def random(self) -> float: ... # type: ignore[misc] - @overload - def random(self, size: None = ...) -> float: ... # type: ignore[misc] - @overload - def random( - self, size: Size = ..., dtype: str = ..., out: Optional[ndarray] = ... - ) -> ndarray: ... - # Multivariate distributions: - def multivariate_normal( - self, - mean: ndarray, - cov: ndarray, - size: Size = ..., - check_valid: Literal["raise", "ignore", "warn"] = ..., - tol: float = ..., - *, - method: Literal["svd", "eigh", "cholesky", "factor"] = ... - ) -> ndarray: ... - @overload - def complex_normal( # type: ignore[misc] - self, - loc: complex = ..., - gamma: complex = ..., - relation: complex = ..., - ) -> complex: ... - @overload - def complex_normal( # type: ignore[misc] - self, - loc: complex = ..., - gamma: complex = ..., - relation: complex = ..., - size: RequiredSize = ..., - ) -> ndarray: ... - @overload - def complex_normal( - self, - loc: Union[complex, ndarray] = ..., - gamma: Union[complex, ndarray] = ..., - relation: Union[complex, ndarray] = ..., - size: Size = ..., - ) -> ndarray: ... - def standard_wishart( - self, df: int, dim: int, size: Size = ..., *, rescale: bool = ... - ) -> ndarray: ... - def wishart( - self, - df: Union[int, ndarray], - scale: ndarray, - size: Size = ..., - *, - check_valid: Literal["raise", "ignore", "warn"] = ..., - tol: float = ..., - rank: Optional[int] = ..., - method: Literal["svd", "eigh", "cholesky", "factor"] = ... - ) -> ndarray: ... - def multivariate_complex_normal( - self, - loc: ndarray, - gamma: Optional[ndarray] = ..., - relation: Optional[ndarray] = ..., - size: Size = ..., - *, - check_valid: Literal["raise", "ignore", "warn"] = ..., - tol: float = ..., - method: Literal["svd", "eigh", "cholesky", "factor"] = ... - ) -> ndarray: ... - -_random_generator: Generator +def _raises_not_implemented(*args: Any, **kwargs: Any) -> None: ... -beta = _random_generator.beta -binomial = _random_generator.binomial -bytes = _random_generator.bytes -chisquare = _random_generator.chisquare -choice = _random_generator.choice -complex_normal = _random_generator.complex_normal -dirichlet = _random_generator.dirichlet -exponential = _random_generator.exponential -f = _random_generator.f -gamma = _random_generator.gamma -geometric = _random_generator.geometric -gumbel = _random_generator.gumbel -hypergeometric = _random_generator.hypergeometric -integers = _random_generator.integers -laplace = _random_generator.laplace -logistic = _random_generator.logistic -lognormal = _random_generator.lognormal -logseries = _random_generator.logseries -multinomial = _random_generator.multinomial -multivariate_normal = _random_generator.multivariate_normal -negative_binomial = _random_generator.negative_binomial -noncentral_chisquare = _random_generator.noncentral_chisquare -noncentral_f = _random_generator.noncentral_f -normal = _random_generator.normal -pareto = _random_generator.pareto -permutation = _random_generator.permutation -poisson = _random_generator.poisson -power = _random_generator.power -rand = _random_generator.rand -randint = _random_generator.randint -randn = _random_generator.randn -random_integers = _random_generator.random_integers -random_sample = _random_generator.random_sample -random = _random_generator.random -rayleigh = _random_generator.rayleigh -shuffle = _random_generator.shuffle -standard_cauchy = _random_generator.standard_cauchy -standard_exponential = _random_generator.standard_exponential -standard_gamma = _random_generator.standard_gamma -standard_normal = _random_generator.standard_normal -standard_t = _random_generator.standard_t -tomaxint = _random_generator.tomaxint -triangular = _random_generator.triangular -uniform = _random_generator.uniform -vonmises = _random_generator.vonmises -wald = _random_generator.wald -weibull = _random_generator.weibull -zipf = _random_generator.zipf +beta = _raises_not_implemented +binomial = _raises_not_implemented +bytes = _raises_not_implemented +chisquare = _raises_not_implemented +choice = _raises_not_implemented +complex_normal = _raises_not_implemented +dirichlet = _raises_not_implemented +exponential = _raises_not_implemented +f = _raises_not_implemented +gamma = _raises_not_implemented +geometric = _raises_not_implemented +gumbel = _raises_not_implemented +hypergeometric = _raises_not_implemented +integers = _raises_not_implemented +laplace = _raises_not_implemented +logistic = _raises_not_implemented +lognormal = _raises_not_implemented +logseries = _raises_not_implemented +multinomial = _raises_not_implemented +multivariate_normal = _raises_not_implemented +negative_binomial = _raises_not_implemented +noncentral_chisquare = _raises_not_implemented +noncentral_f = _raises_not_implemented +normal = _raises_not_implemented +pareto = _raises_not_implemented +permutation = _raises_not_implemented +poisson = _raises_not_implemented +power = _raises_not_implemented +rand = _raises_not_implemented +randint = _raises_not_implemented +randn = _raises_not_implemented +random_integers = _raises_not_implemented +random_sample = _raises_not_implemented +random = _raises_not_implemented +rayleigh = _raises_not_implemented +shuffle = _raises_not_implemented +standard_cauchy = _raises_not_implemented +standard_exponential = _raises_not_implemented +standard_gamma = _raises_not_implemented +standard_normal = _raises_not_implemented +standard_t = _raises_not_implemented +tomaxint = _raises_not_implemented +triangular = _raises_not_implemented +uniform = _raises_not_implemented +vonmises = _raises_not_implemented +wald = _raises_not_implemented +weibull = _raises_not_implemented +zipf = _raises_not_implemented diff --git a/randomgen/generator.pyx b/randomgen/generator.pyx index 0cb655a15..8cfe6166f 100644 --- a/randomgen/generator.pyx +++ b/randomgen/generator.pyx @@ -1,4803 +1,137 @@ #!python -#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3 -import itertools -import operator +#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, binding=True import warnings -from typing import MutableSequence import numpy as np -from randomgen.bounded_integers import _integers_types from randomgen.pcg64 import PCG64 -from randomgen.xoroshiro128 import Xoroshiro128 from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer -from cpython cimport (Py_INCREF, PyComplex_FromDoubles, +from cpython cimport (PyComplex_FromDoubles, PyComplex_ImagAsDouble, PyComplex_RealAsDouble, - PyFloat_AsDouble) -from libc cimport string + ) -cimport cython -cimport numpy as np - -from randomgen.bounded_integers cimport * -from randomgen.common cimport * -from randomgen.distributions cimport * -from randomgen cimport api - -__all__ = ["Generator", "beta", "binomial", "bytes", "chisquare", "choice", - "complex_normal", "dirichlet", "exponential", "f", "gamma", - "geometric", "gumbel", "hypergeometric", "integers", "laplace", - "logistic", "lognormal", "logseries", "multinomial", - "multivariate_normal", "negative_binomial", "noncentral_chisquare", - "noncentral_f", "normal", "pareto", "permutation", - "poisson", "power", "randint", "random", "rayleigh", "shuffle", - "standard_cauchy", "standard_exponential", "standard_gamma", - "standard_normal", "standard_t", "triangular", - "uniform", "vonmises", "wald", "weibull", "zipf", "ExtendedGenerator"] - -np.import_array() - -cdef object broadcast_shape(tuple x, tuple y, bint strict): - cdef bint cond, bcast=True - if x == () or y == (): - if len(x) > len(y): - return True, x - return True, y - lx = len(x) - ly = len(y) - if lx > ly: - shape = list(x[:lx-ly]) - x = x[lx-ly:] - else: - shape = list(y[:ly-lx]) - y = y[ly-lx:] - for xs, ys in zip(x, y): - cond = xs == ys - if not strict: - cond |= min(xs, ys) == 1 - bcast &= cond - if not bcast: - break - shape.append(max(xs, ys)) - return bcast, tuple(shape) - - -cdef _factorize(cov, meth, check_valid, tol, rank): - if meth == "svd": - from numpy.linalg import svd - - (u, s, vh) = svd(cov) - if rank < cov.shape[0]: - locs = np.argsort(s) - s[locs[:s.shape[0]-rank]] = 0.0 - psd = np.allclose(np.dot(vh.T * s, vh), cov, rtol=tol, atol=tol) - _factor = (u * np.sqrt(s)).T - elif meth == "factor": - return cov - elif meth == "eigh": - from numpy.linalg import eigh - - # could call linalg.svd(hermitian=True), but that calculates a - # vh we don't need - (s, u) = eigh(cov) - if rank < cov.shape[0]: - locs = np.argsort(s) - s[locs[:s.shape[0]-rank]] = 0.0 - psd = not np.any(s < -tol) - _factor = (u * np.sqrt(abs(s))).T - else: - if rank == cov.shape[0]: - from numpy.linalg import cholesky - - _factor = cholesky(cov).T - psd = True - else: - try: - from scipy.linalg import get_lapack_funcs - except ImportError: - raise ImportError( - "SciPy is required when using Cholesky factorization with " - "reduced rank covariance." - ) - - func = get_lapack_funcs("pstrf") - _factor, _, rank_c, _ = func(cov) - _factor = np.triu(_factor) - psd = rank_c >= rank - - if not psd and check_valid != "ignore": - if rank < cov.shape[0]: - msg = f"The {rank} is less than the minimum required rank." - else: - msg = "The covariance is not positive-semidefinite." - if check_valid == "warn": - warnings.warn(msg, RuntimeWarning) - else: - raise ValueError(msg) - return _factor - -# TODO: Remove after deprecation -def _rand_dep_message(old, new, args, dtype): - msg = "{old} is deprecated. Use {new}({call}) instead" - dtype = np.dtype(dtype).char - if args: - if len(args) == 1: - size = str(args[0]) - else: - size = "(" + ", ".join(map(str, args)) + ")" - call = "{size}, dtype=\"{dtype}\"".format(size=size, - dtype=str(dtype)) - else: - call = "dtype=\"{dtype}\"".format(dtype=str(dtype)) - return msg.format(old=old, new=new, call=call) - - -cdef class Generator: - """ - Generator(bit_generator=None) - - Random value generator using a bit generator source. - - ``Generator`` exposes methods for generating random numbers drawn - from a variety of probability distributions. In addition to the - distribution-specific arguments, each method takes a keyword argument - `size` that defaults to ``None``. If `size` is ``None``, then a single - value is generated and returned. If `size` is an integer, then a 1-D - array filled with generated values is returned. If `size` is a tuple, - then an array with that shape is filled and returned. - - **No Compatibility Guarantee** - - ``Generator`` is evolving and so it is not possible to provide a - compatibility guarantee like ``RandomState``. In particular, better - algorithms have already been added and bugs that change the stream - have been fixed. This will change once ``Generator`` stabilizes. - - Parameters - ---------- - bit_generator : BitGenerator, optional - Bit generator to use as the core generator. If none is provided, uses - Xoroshiro128. - - Notes - ----- - The Python stdlib module `random` contains pseudo-random number generator - with a number of methods that are similar to the ones available in - ``Generator``. It uses Mersenne Twister, which is available by - using the ``MT19937`` bit generator. ``Generator``, besides being - NumPy-aware, has the advantage that it provides a much larger number - of probability distributions from which to choose. - - Examples - -------- - >>> from randomgen import Generator - >>> rg = Generator() - >>> rg.standard_normal() - -0.203 # random - - Using a specific generator - - >>> from randomgen import MT19937 - >>> rg = Generator(MT19937()) - >>> rg.standard_normal() - -0.203 # random - - """ - cdef public object _bit_generator - cdef bitgen_t _bitgen - cdef binomial_t _binomial - cdef object lock - _poisson_lam_max = POISSON_LAM_MAX - - def __init__(self, bit_generator=None): - warnings.warn("""\ -Generator is deprecated and will be removed sometime after the release of -NumPy 1.21 (or 2 releases after 1.19 if there is a major release). - -Unique features of Generator have been moved to -randomgen.generator.ExtendedGenerator. - -Now is the time to start using numpy.random.Generator. - -In the mean time Generator will only be updated for the most egregious bugs. - -You can silence this warning using - -import warnings -warnings.filterwarnings("ignore", "Generator", FutureWarning) -""", FutureWarning) - - if bit_generator is None: - bit_generator = Xoroshiro128(mode="sequence") - self._bit_generator = bit_generator - - capsule = bit_generator.capsule - cdef const char *name = "BitGenerator" - if not PyCapsule_IsValid(capsule, name): - raise ValueError("Invalid bit generator. The bit generator must " - "be instantized.") - self._bitgen = ( PyCapsule_GetPointer(capsule, name))[0] - self.lock = bit_generator.lock - - def __repr__(self): - out = object.__repr__(self) - return out.replace(type(self).__name__, self.__str__()) - - def __str__(self): - _str = type(self).__name__ - _str += "(" + type(self.bit_generator).__name__ + ")" - return _str - - # Pickling support: - def __getstate__(self): - return self.bit_generator.state - - def __setstate__(self, state): - self.bit_generator.state = state - - def __reduce__(self): - from randomgen._pickle import __generator_ctor - return (__generator_ctor, (self.bit_generator.state["bit_generator"],), - self.bit_generator.state) - - @property - def bit_generator(self): - """ - Gets the bit generator instance used by the generator - - Returns - ------- - bit_generator : BitGenerator - The bit generator instance used by the generator - """ - return self._bit_generator - - def seed(self, *args, **kwargs): - """ - Reseed the bit generator. - - Parameters depend on the bit generator used. - - Notes - ----- - Arguments are directly passed to the bit generator. This is a - convenience function. - - The best method to access seed is to directly use a bit generator - instance. This example demonstrates this best practice. - - >>> from randomgen import Generator, PCG64 - >>> bit_generator = PCG64(1234567891011) - >>> rg = Generator(bit_generator) - >>> bit_generator.seed(1110987654321) - - These best practice examples are equivalent to - - >>> rg = Generator(PCG64(1234567891011)) - >>> rg.seed(1110987654321) - """ - self._bit_generator.seed(*args, **kwargs) - return self - - @property - def state(self): - """ - Get or set the bit generator's state - - Returns - ------- - state : dict - Dictionary containing the information required to describe the - state of the bit generator - - Notes - ----- - This is a trivial pass-through function. Generator does not - directly contain or manipulate the bit generator's state. - - """ - return self._bit_generator.state - - @state.setter - def state(self, value): - self._bit_generator.state = value - - def uintegers(self, size=None, int bits=64): - """ - uintegers(size=None, bits=64) - - Return random unsigned integers - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - bits : int {32, 64} - Size of the unsigned integer to return, either 32 bit or 64 bit. - - Returns - ------- - out : uint or ndarray - Drawn samples. - - Notes - ----- - This method effectively exposes access to the raw underlying - pseudo-random number generator since these all produce unsigned - integers. In practice these are most useful for generating other - random numbers. - These should not be used to produce bounded random numbers by - simple truncation. - """ - cdef np.npy_intp i, n - cdef np.ndarray array - cdef uint32_t* data32 - cdef uint64_t* data64 - if bits == 64: - if size is None: - with self.lock: - return self._bitgen.next_uint64(self._bitgen.state) - array = np.empty(size, np.uint64) - n = np.PyArray_SIZE(array) - data64 = np.PyArray_DATA(array) - with self.lock, nogil: - for i in range(n): - data64[i] = self._bitgen.next_uint64(self._bitgen.state) - elif bits == 32: - if size is None: - with self.lock: - return self._bitgen.next_uint32(self._bitgen.state) - array = np.empty(size, np.uint32) - n = np.PyArray_SIZE(array) - data32 = np.PyArray_DATA(array) - with self.lock, nogil: - for i in range(n): - data32[i] = self._bitgen.next_uint32(self._bitgen.state) - else: - raise ValueError("Unknown value of bits. Must be either 32 or 64.") - - return array - - def random_uintegers(self, size=None, int bits=64): - """ - random_uintegers(size=None, bits=64) - - .. deprecated:: 1.18.0 - - Alias for uintegers. Use uintegers. - """ - warnings.warn("This function is deprecated. Please use uintegers.", - DeprecationWarning) - - return self.uintegers(size=size, bits=bits) - - def random_sample(self, *args, **kwargs): - warnings.warn("random_sample is deprecated in favor of random", - DeprecationWarning) - - return self.random(*args, **kwargs) - - def random(self, size=None, dtype=np.float64, out=None): - """ - random(size=None, dtype='d', out=None) - - Return random floats in the half-open interval [0.0, 1.0). - - Results are from the "continuous uniform" distribution over the - stated interval. To sample :math:`Unif[a, b), b > a` multiply - the output of `random` by `(b-a)` and add `a`:: - - (b - a) * random() + a - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - dtype : {str, dtype}, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - out : ndarray, optional - Alternative output array in which to place the result. If size is not None, - it must have the same shape as the provided size and must match the type of - the output values. - - Returns - ------- - out : float or ndarray of floats - Array of random floats of shape `size` (unless ``size=None``, in which - case a single float is returned). - - Examples - -------- - >>> randomgen.generator.random() - 0.47108547995356098 # random - >>> type(randomgen.generator.random()) - - >>> randomgen.generator.random((5,)) - array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428]) # random - - Three-by-two array of random numbers from [-5, 0): - - >>> 5 * randomgen.generator.random((3, 2)) - 5 - array([[-3.99149989, -0.52338984], # random - [-2.99091858, -0.79479508], - [-1.23204345, -1.75224494]]) - """ - cdef double temp - key = np.dtype(dtype).name - if key == "float64": - return double_fill(&random_double_fill, &self._bitgen, size, self.lock, out) - elif key == "float32": - return float_fill(&random_float, &self._bitgen, size, self.lock, out) - else: - raise TypeError("Unsupported dtype \"{key}\" for random".format(key=key)) - - def beta(self, a, b, size=None): - """ - beta(a, b, size=None) - - Draw samples from a Beta distribution. - - The Beta distribution is a special case of the Dirichlet distribution, - and is related to the Gamma distribution. It has the probability - distribution function - - .. math:: f(x; a,b) = \\frac{1}{B(\\alpha, \\beta)} x^{\\alpha - 1} - (1 - x)^{\\beta - 1}, - - where the normalization, B, is the beta function, - - .. math:: B(\\alpha, \\beta) = \\int_0^1 t^{\\alpha - 1} - (1 - t)^{\\beta - 1} dt. - - It is often seen in Bayesian inference and order statistics. - - Parameters - ---------- - a : float or array_like of floats - Alpha, positive (>0). - b : float or array_like of floats - Beta, positive (>0). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` and ``b`` are both scalars. - Otherwise, ``np.broadcast(a, b).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized beta distribution. - - """ - return cont(&random_beta, &self._bitgen, size, self.lock, 2, - a, "a", CONS_POSITIVE, - b, "b", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def exponential(self, scale=1.0, size=None): - """ - exponential(scale=1.0, size=None) - - Draw samples from an exponential distribution. - - Its probability density function is - - .. math:: f(x; \\frac{1}{\\beta}) = \\frac{1}{\\beta} \\exp(-\\frac{x}{\\beta}), - - for ``x > 0`` and 0 elsewhere. :math:`\\beta` is the scale parameter, - which is the inverse of the rate parameter :math:`\\lambda = 1/\\beta`. - The rate parameter is an alternative, widely used parameterization - of the exponential distribution [3]_. - - The exponential distribution is a continuous analogue of the - geometric distribution. It describes many common situations, such as - the size of raindrops measured over many rainstorms [1]_, or the time - between page requests to Wikipedia [2]_. - - Parameters - ---------- - scale : float or array_like of floats - The scale parameter, :math:`\\beta = 1/\\lambda`. Must be - non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``scale`` is a scalar. Otherwise, - ``np.array(scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized exponential distribution. - - References - ---------- - .. [1] Peyton Z. Peebles Jr., "Probability, Random Variables and - Random Signal Principles", 4th ed, 2001, p. 57. - .. [2] Wikipedia, "Poisson process", - https://en.wikipedia.org/wiki/Poisson_process - .. [3] Wikipedia, "Exponential distribution", - https://en.wikipedia.org/wiki/Exponential_distribution - - """ - return cont(&random_exponential, &self._bitgen, size, self.lock, 1, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, - None) - - def standard_exponential(self, size=None, dtype=np.float64, method="zig", out=None): - """ - standard_exponential(size=None, dtype='d', method='zig', out=None) - - Draw samples from the standard exponential distribution. - - `standard_exponential` is identical to the exponential distribution - with a scale parameter of 1. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - dtype : dtype, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - method : str, optional - Either 'inv' or 'zig'. 'inv' uses the default inverse CDF method. - 'zig' uses the much faster Ziggurat method of Marsaglia and Tsang. - out : ndarray, optional - Alternative output array in which to place the result. If size is not None, - it must have the same shape as the provided size and must match the type of - the output values. - - Returns - ------- - out : float or ndarray - Drawn samples. - - Examples - -------- - Output a 3x8000 array: - - >>> n = randomgen.generator.standard_exponential((3, 8000)) - - """ - key = np.dtype(dtype).name - if key == "float64": - if method == "zig": - return double_fill(&random_standard_exponential_zig_fill, &self._bitgen, size, self.lock, out) - else: - return double_fill(&random_standard_exponential_fill, &self._bitgen, size, self.lock, out) - elif key == "float32": - if method == "zig": - return float_fill(&random_standard_exponential_zig_f, &self._bitgen, size, self.lock, out) - else: - return float_fill(&random_standard_exponential_f, &self._bitgen, size, self.lock, out) - else: - raise TypeError("Unsupported dtype \"{key}\" for standard_exponential".format(key=key)) - - def tomaxint(self, size=None): - """ - tomaxint(size=None) - - Return a sample of uniformly distributed random integers in the interval - [0, ``np.iinfo(int).max``]. The int type translates to the C long - integer type and its precision is platform dependent. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : ndarray - Drawn samples, with shape `size`. - - See Also - -------- - integers : Uniform sampling over a given half-open or closed interval - of integers. - - Examples - -------- - >>> rg = randomgen.Generator() # need a Generator object - >>> rg.tomaxint((2,2,2)) - array([[[1170048599, 1600360186], # random - [ 739731006, 1947757578]], - [[1871712945, 752307660], - [1601631370, 1479324245]]]) - >>> rg.tomaxint((2,2,2)) < np.iinfo(int).max - array([[[ True, True], - [ True, True]], - [[ True, True], - [ True, True]]]) - - """ - warnings.warn("tomaxint is deprecated. Use integers.", - DeprecationWarning) - - return self.integers(0, np.iinfo(int).max + 1, dtype=int, size=size) - - def randint(self, *args, **kwargs): - """ - Deprecated in favor of integers - - See integers docstring for arguments - """ - warnings.warn("randint has been deprecated in favor of integers", - DeprecationWarning) - - return self.integers(*args, **kwargs) - - def integers(self, low, high=None, size=None, dtype=np.int64, - use_masked=None, endpoint=False, closed=None): - """ - integers(low, high=None, size=None, dtype='int64', use_masked=True, endpoint=False) - - Return random integers from `low` (inclusive) to `high` (exclusive), or - if endpoint=True, `low` (inclusive) to `high` (inclusive). - - Return random integers from the "discrete uniform" distribution of - the specified dtype in the "half-open" interval [`low`, `high`). If - `high` is None (the default), then results are from [0, `low`). If - `endpoint` is True, then samples from the closed interval [`low`, `high`] - or [0, `low`] if `high` is None. - - Parameters - ---------- - low : {int, array_like[int]} - Lowest (signed) integers to be drawn from the distribution (unless - ``high=None``, in which case this parameter is one above the - *highest* such integer). - high : {int, array_like[int]}, optional - If provided, one above the largest (signed) integer to be drawn - from the distribution (see above for behavior if ``high=None``). - If array-like, must contain integer values - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - dtype : {str, dtype}, optional - Desired dtype of the result. All dtypes are determined by their - name, i.e., 'int64', 'int', etc, so byteorder is not available - and a specific precision may have different C types depending - on the platform. The default value is 'int'. - - .. versionadded:: 1.11.0 - - use_masked : bool - If True the generator uses rejection sampling with a bit mask to - reject random numbers that are out of bounds. If False the - generator will use Lemire's rejection sampling algorithm. - - .. versionadded:: 1.15.1 - - endpoint : bool - If true, sample from the interval [low, high] instead of the - default [low, high) - - Returns - ------- - out : int or ndarray of ints - `size`-shaped array of random integers from the appropriate - distribution, or a single such random int if `size` not provided. - - Notes - ----- - When using broadcasting with uint64 dtypes, the maximum value (2**64) - cannot be represented as a standard integer type. The high array (or - low if high is None) must have object dtype, e.g., array([2**64]). - - Examples - -------- - >>> randomgen.generator.integers(2, size=10) - array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random - >>> randomgen.generator.integers(1, size=10) - array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) - - Generate a 2 x 4 array of ints between 0 and 4, inclusive: - - >>> randomgen.generator.integers(5, size=(2, 4)) - array([[4, 0, 2, 1], - [3, 2, 2, 0]]) # random - - Generate a 1 x 3 array with 3 different upper bounds - - >>> randomgen.generator.integers(1, [3, 5, 10]) - array([2, 2, 9]) # random - - Generate a 1 by 3 array with 3 different lower bounds - - >>> randomgen.generator.integers([1, 5, 7], 10) - array([9, 8, 7]) # random - - Generate a 2 by 4 array using broadcasting with dtype of uint8 - - >>> randomgen.generator.integers([1, 3, 5, 7], [[10], [20]], dtype=np.uint8) - array([[ 8, 6, 9, 7], - [ 1, 16, 9, 12]], dtype=uint8) # random - - References - ---------- - .. [1] Daniel Lemire., "Fast Random Integer Generation in an Interval", - CoRR, Aug. 13, 2018, https://arxiv.org/abs/1805.10941 - - """ - if use_masked is not None and use_masked: - warnings.warn("use_masked will be removed in the final release and" - " only the Lemire method will be available.", - DeprecationWarning) - if closed is not None: - warnings.warn("closed has been deprecated in favor of endpoint.", - DeprecationWarning) - endpoint = closed - - cdef bint _use_masked = use_masked is None or use_masked - if high is None: - high = low - low = 0 - - dt = np.dtype(dtype) - key = dt.name - if key not in _integers_types: - raise TypeError("Unsupported dtype \"{key}\" for integers".format(key=key)) - if dt.byteorder != "=" and dt.byteorder != "|": - warnings.warn("Byteorder is not supported. If you require " - "platform-independent byteorder, call byteswap when " - "required.\n\nIn future version, specifying " - "byteorder will raise a ValueError", FutureWarning) - - if key == "int32": - ret = _rand_int32(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "int64": - ret = _rand_int64(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "int16": - ret = _rand_int16(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "int8": - ret = _rand_int8(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "uint64": - ret = _rand_uint64(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "uint32": - ret = _rand_uint32(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "uint16": - ret = _rand_uint16(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "uint8": - ret = _rand_uint8(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - elif key == "bool": - ret = _rand_bool(low, high, size, _use_masked, endpoint, &self._bitgen, self.lock) - - if size is None and dtype in (bool, int): - if np.array(ret).shape == (): - return dtype(ret) - return ret - - def bytes(self, np.npy_intp length): - """ - bytes(length) - - Return random bytes. - - Parameters - ---------- - length : int - Number of random bytes. - - Returns - ------- - out : bytes - String of length `length`. - - Examples - -------- - >>> randomgen.generator.bytes(10) - b' eh\\x85\\x022SZ\\xbf\\xa4' # random - - """ - cdef Py_ssize_t n_uint32 = ((length - 1) // 4 + 1) - - return self.integers(0, 4294967296, size=n_uint32, - dtype=np.uint32).astype('>> randomgen.generator.choice(5, 3) - array([0, 3, 4]) # random - >>> #This is equivalent to randomgen.generator.integers(0,5,3) - - Generate a non-uniform random sample from np.arange(5) of size 3: - - >>> randomgen.generator.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) - array([3, 3, 0]) # random - - Generate a uniform random sample from np.arange(5) of size 3 without - replacement: - - >>> randomgen.generator.choice(5, 3, replace=False) - array([3,1,0]) # random - >>> #This is equivalent to randomgen.generator.permutation(np.arange(5))[:3] - - Generate a non-uniform random sample from np.arange(5) of size - 3 without replacement: - - >>> randomgen.generator.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) - array([2, 3, 0]) # random - - Any of the above can be repeated with an arbitrary array-like - instead of just integers. For instance: - - >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher'] - >>> randomgen.generator.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3]) - array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random - dtype='np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED | api.NPY_ARRAY_C_CONTIGUOUS) - pix = np.PyArray_DATA(p) - - if p.ndim != 1: - raise ValueError("`p` must be 1-dimensional") - if p.size != pop_size: - raise ValueError("`a` and `p` must have same size") - p_sum = kahan_sum(pix, d) - if np.isnan(p_sum): - raise ValueError("probabilities contain NaN") - if np.logical_or.reduce(p < 0): - raise ValueError("probabilities are not non-negative") - if abs(p_sum - 1.) > atol: - raise ValueError("probabilities do not sum to 1") - - shape = size - if shape is not None: - size = np.prod(shape, dtype=np.intp) - else: - size = 1 - - # Actual sampling - if replace: - if p is not None: - cdf = p.cumsum() - cdf /= cdf[-1] - uniform_samples = self.random(shape) - idx = cdf.searchsorted(uniform_samples, side="right") - idx = np.array(idx, copy=False, dtype=np.int64) # searchsorted returns a scalar - else: - idx = self.integers(0, pop_size, size=shape, dtype=np.int64, use_masked=False) - else: - if size > pop_size: - raise ValueError("Cannot take a larger sample than " - "population when replace=False") - elif size < 0: - raise ValueError("Negative dimensions are not allowed") - - if p is not None: - if np.count_nonzero(p > 0) < size: - raise ValueError("Fewer non-zero entries in p than size") - n_uniq = 0 - p = p.copy() - _shape = () if shape is None else shape - found = np.zeros(_shape, dtype=np.int64) - flat_found = found.ravel() - while n_uniq < size: - x = self.random((size - n_uniq,)) - if n_uniq > 0: - p[flat_found[0:n_uniq]] = 0 - cdf = np.cumsum(p) - cdf /= cdf[-1] - new = cdf.searchsorted(x, side="right") - _, unique_indices = np.unique(new, return_index=True) - unique_indices.sort() - new = new.take(unique_indices) - flat_found[n_uniq:n_uniq + new.size] = new - n_uniq += new.size - idx = found - else: - size_i = size - pop_size_i = pop_size - # This is a heuristic tuning. should be improvable - if shuffle: - cutoff = 50 - else: - cutoff = 20 - - if pop_size_i > 10000 and (size_i > (pop_size_i // cutoff)): - # Tail shuffle size elements - idx = np.arange(0, pop_size_i, dtype=np.int64) - idx_data = np.PyArray_DATA(idx) - with self.lock, nogil: - self._shuffle_int(pop_size_i, max(pop_size_i - size_i, 1), - idx_data) - # Copy to allow potentially large array backing idx to be gc - idx = idx[(pop_size - size):].copy() - else: - # Floyd's algorithm - idx = np.empty(size, dtype=np.int64) - idx_data = np.PyArray_DATA(idx) - # smallest power of 2 larger than 1.2 * size - set_size = (1.2 * size_i) - mask = _gen_mask(set_size) - set_size = 1 + mask - hash_set = np.full(set_size, -1, np.uint64) - with self.lock, cython.wraparound(False), nogil: - for j in range(pop_size_i - size_i, pop_size_i): - val = random_bounded_uint64(&self._bitgen, 0, j, 0, 0) - loc = val & mask - while hash_set[loc] != -1 and hash_set[loc] != val: - loc = (loc + 1) & mask - if hash_set[loc] == -1: # then val not in hash_set - hash_set[loc] = val - idx_data[j - pop_size_i + size_i] = val - else: # we need to insert j instead - loc = j & mask - while hash_set[loc] != -1: - loc = (loc + 1) & mask - hash_set[loc] = j - idx_data[j - pop_size_i + size_i] = j - if shuffle: - self._shuffle_int(size_i, 1, idx_data) - if shape is not None: - idx.shape = shape - - if shape is None and isinstance(idx, np.ndarray): - # In most cases a scalar will have been made an array - idx = idx.item(0) - - # Use samples as indices for a if a is array-like - if a.ndim == 0: - return idx - - if shape is not None and idx.ndim == 0: - # If size == () then the user requested a 0-d array as opposed to - # a scalar object when size is None. However a[idx] is always a - # scalar and not an array. So this makes sure the result is an - # array, taking into account that np.array(item) may not work - # for object arrays. - res = np.empty((), dtype=a.dtype) - res[()] = a[idx] - return res - - # asarray downcasts on 32-bit platforms, always safe - # no-op on 64-bit platforms - return a.take(np.asarray(idx, dtype=np.intp), axis=axis) - - def uniform(self, low=0.0, high=1.0, size=None): - """ - uniform(low=0.0, high=1.0, size=None) - - Draw samples from a uniform distribution. - - Samples are uniformly distributed over the half-open interval - ``[low, high)`` (includes low, but excludes high). In other words, - any value within the given interval is equally likely to be drawn - by `uniform`. - - Parameters - ---------- - low : float or array_like of floats, optional - Lower boundary of the output interval. All values generated will be - greater than or equal to low. The default value is 0. - high : float or array_like of floats - Upper boundary of the output interval. All values generated will be - less than or equal to high. The default value is 1.0. high - low must be - non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``low`` and ``high`` are both scalars. - Otherwise, ``np.broadcast(low, high).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized uniform distribution. - - See Also - -------- - integers : Discrete uniform distribution, yielding integers. - random : Floats uniformly distributed over ``[0, 1)``. - - Notes - ----- - The probability density function of the uniform distribution is - - .. math:: p(x) = \\frac{1}{b - a} - - anywhere within the interval ``[a, b)``, and zero elsewhere. - - When ``high`` == ``low``, values of ``low`` will be returned. - If ``high`` < ``low``, the results are officially undefined - and may eventually raise an error, i.e. do not rely on this - function to behave when passed arguments satisfying that - inequality condition. The ``high`` limit may be included in the - returned array of floats due to floating-point rounding in the - equation ``low + (high-low) * random_sample()``. For example: - - >>> x = np.float32(5*0.99999999) - >>> x - 5.0 - - Examples - -------- - Draw samples from the distribution: - - >>> s = randomgen.generator.uniform(-1,0,1000) - - All values are within the given interval: - - >>> np.all(s >= -1) - True - >>> np.all(s < 0) - True - - Display the histogram of the samples, along with the - probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 15, density=True) - >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') - >>> plt.show() - - """ - cdef bint is_scalar = True - cdef np.ndarray alow, ahigh, arange - cdef double _low, _high, rng - cdef object temp - - alow = np.PyArray_FROM_OTF(low, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - ahigh = np.PyArray_FROM_OTF(high, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(alow) == np.PyArray_NDIM(ahigh) == 0: - _low = PyFloat_AsDouble(low) - _high = PyFloat_AsDouble(high) - rng = _high - _low - if not np.isfinite(rng): - raise OverflowError('High - low range exceeds valid bounds') - - return cont(&random_uniform, &self._bitgen, size, self.lock, 2, - _low, "", CONS_NONE, - rng, 'high - low', CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - None) - - temp = np.subtract(ahigh, alow) - # needed to get around Pyrex's automatic reference-counting - # rules because EnsureArray steals a reference - Py_INCREF(temp) - - arange = np.PyArray_EnsureArray(temp) - if not np.all(np.isfinite(arange)): - raise OverflowError("Range exceeds valid bounds") - return cont(&random_uniform, &self._bitgen, size, self.lock, 2, - alow, "", CONS_NONE, - arange, 'high - low', CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - None) - - def rand(self, *args, dtype=np.float64): - """ - rand(d0, d1, ..., dn, dtype='d') - - Random values in a given shape. - - .. note:: - This is a convenience function for users porting code from Matlab, - and wraps `randomgen.generator.random`. That function takes a - tuple to specify the size of the output, which is consistent with - other NumPy functions like `numpy.zeros` and `numpy.ones`. - - Create an array of the given shape and populate it with - random samples from a uniform distribution - over ``[0, 1)``. - - Parameters - ---------- - d0, d1, ..., dn : int, optional - The dimensions of the returned array, must be non-negative. - If no argument is given a single Python float is returned. - dtype : {str, dtype}, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - - Returns - ------- - out : ndarray, shape ``(d0, d1, ..., dn)`` - Random values. - - See Also - -------- - random - - Examples - -------- - >>> randomgen.generator.rand(3,2) - array([[ 0.14022471, 0.96360618], # random - [ 0.37601032, 0.25528411], # random - [ 0.49313049, 0.94909878]]) # random - - """ - msg = _rand_dep_message("rand", "random", args, dtype) - warnings.warn(msg, DeprecationWarning) - - if len(args) == 0: - return self.random(dtype=dtype) - else: - return self.random(size=args, dtype=dtype) - - def randn(self, *args, dtype=np.float64): - """ - randn(d0, d1, ..., dn, dtype='d') - - Return a sample (or samples) from the "standard normal" distribution. - - .. note:: - This is a convenience function for users porting code from Matlab, - and wraps `randomgen.generator.standard_normal`. That function takes a - tuple to specify the size of the output, which is consistent with - other NumPy functions like `numpy.zeros` and `numpy.ones`. - - If positive int_like arguments are provided, `randn` generates an array - of shape ``(d0, d1, ..., dn)``, filled - with random floats sampled from a univariate "normal" (Gaussian) - distribution of mean 0 and variance 1. A single float randomly sampled - from the distribution is returned if no argument is provided. - - Parameters - ---------- - d0, d1, ..., dn : int, optional - The dimensions of the returned array, must be non-negative. - If no argument is given a single Python float is returned. - dtype : {str, dtype}, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - - Returns - ------- - Z : ndarray or float - A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from - the standard normal distribution, or a single such float if - no parameters were supplied. - - See Also - -------- - standard_normal : Similar, but takes a tuple as its argument. - normal : Also accepts mu and sigma arguments. - - Notes - ----- - For random samples from :math:`N(\\mu, \\sigma^2)`, use: - - ``sigma * randomgen.generator.randn(...) + mu`` - - Examples - -------- - >>> randomgen.generator.randn() - 2.1923875335537315 # random - - Two-by-four array of samples from N(3, 6.25): - - >>> 3 + 2.5 * randomgen.generator.randn(2, 4) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - msg = _rand_dep_message("randn", "standard_normal", args, dtype) - warnings.warn(msg, DeprecationWarning) - - if len(args) == 0: - return self.standard_normal(dtype=dtype) - else: - return self.standard_normal(size=args, dtype=dtype) - - def random_integers(self, low, high=None, size=None): - """ - random_integers(low, high=None, size=None) - - Random integers of type int between `low` and `high`, inclusive. - - Return random integers of type int from the "discrete uniform" - distribution in the closed interval [`low`, `high`]. If `high` is - None (the default), then results are from [1, `low`]. The int - type translates to the C long integer type and its precision - is platform dependent. - - This function has been deprecated. Use integers instead. - - .. deprecated:: 1.11.0 - - Parameters - ---------- - low : int - Lowest (signed) integer to be drawn from the distribution (unless - ``high=None``, in which case this parameter is the *highest* such - integer). - high : int, optional - If provided, the largest (signed) integer to be drawn from the - distribution (see above for behavior if ``high=None``). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : int or ndarray of ints - `size`-shaped array of random integers from the appropriate - distribution, or a single such random int if `size` not provided. - - See Also - -------- - integers : Similar to `random_integers`, only for the half-open - interval [`low`, `high`), and 0 is the lowest value if `high` is - omitted. - - Notes - ----- - To sample from N evenly spaced floating-point numbers between a and b, - use:: - - a + (b - a) * (randomgen.generator.random_integers(N) - 1) / (N - 1.) - - Examples - -------- - >>> randomgen.generator.random_integers(5) - 4 # random - >>> type(randomgen.generator.random_integers(5)) - - >>> randomgen.generator.random_integers(5, size=(3,2)) - array([[5, 4], # random - [3, 3], - [4, 5]]) - - Choose five random numbers from the set of five evenly-spaced - numbers between 0 and 2.5, inclusive (*i.e.*, from the set - :math:`{0, 5/8, 10/8, 15/8, 20/8}`): - - >>> 2.5 * (randomgen.generator.random_integers(5, size=(5,)) - 1) / 4. - array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ]) # random - - Roll two six sided dice 1000 times and sum the results: - - >>> d1 = randomgen.generator.random_integers(1, 6, 1000) - >>> d2 = randomgen.generator.random_integers(1, 6, 1000) - >>> dsums = d1 + d2 - - Display results as a histogram: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(dsums, 11, density=True) - >>> plt.show() - - """ - if high is None: - warnings.warn(("This function is deprecated. Please call " - "integers(1, {low} + 1) instead".format(low=low)), - DeprecationWarning) - high = low - low = 1 - - else: - warnings.warn(("This function is deprecated. Please call " - "integers({low}, {high} + 1)" - "instead".format(low=low, high=high)), - DeprecationWarning) - - return self.integers(low, high + 1, size=size, dtype="l") - - # Complicated, continuous distributions: - def standard_normal(self, size=None, dtype=np.float64, out=None): - """ - standard_normal(size=None, dtype='d', out=None) - - Draw samples from a standard Normal distribution (mean=0, stdev=1). - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - dtype : {str, dtype}, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - out : ndarray, optional - Alternative output array in which to place the result. If size is not None, - it must have the same shape as the provided size and must match the type of - the output values. - - Returns - ------- - out : float or ndarray - A floating-point array of shape ``size`` of drawn samples, or a - single sample if ``size`` was not specified. - - Notes - ----- - For random samples from :math:`N(\\mu, \\sigma^2)`, use one of:: - - mu + sigma * randomgen.generator.standard_normal(size=...) - randomgen.generator.normal(mu, sigma, size=...) - - See Also - -------- - normal : - Equivalent function with additional ``loc`` and ``scale`` arguments - for setting the mean and standard deviation. - - Examples - -------- - >>> randomgen.generator.standard_normal() - 2.1923875335537315 # random - - >>> s = randomgen.generator.standard_normal(8000) - >>> s - array([ 0.6888893 , 0.78096262, -0.89086505, ..., 0.49876311, # random - -0.38672696, -0.4685006 ]) # random - >>> s.shape - (8000,) - >>> s = randomgen.generator.standard_normal(size=(3, 4, 2)) - >>> s.shape - (3, 4, 2) - - Two-by-four array of samples from :math:`N(3, 6.25)`: - - >>> 3 + 2.5 * randomgen.generator.standard_normal(size=(2, 4)) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - key = np.dtype(dtype).name - if key == "float64": - return double_fill(&random_gauss_zig_fill, &self._bitgen, size, self.lock, out) - elif key == "float32": - return float_fill(&random_gauss_zig_f, &self._bitgen, size, self.lock, out) - - else: - raise TypeError("Unsupported dtype \"{key}\" for standard_normal".format(key=key)) - - def normal(self, loc=0.0, scale=1.0, size=None): - """ - normal(loc=0.0, scale=1.0, size=None) - - Draw random samples from a normal (Gaussian) distribution. - - The probability density function of the normal distribution, first - derived by De Moivre and 200 years later by both Gauss and Laplace - independently [2]_, is often called the bell curve because of - its characteristic shape (see the example below). - - The normal distributions occurs often in nature. For example, it - describes the commonly occurring distribution of samples influenced - by a large number of tiny, random disturbances, each with its own - unique distribution [2]_. - - Parameters - ---------- - loc : float or array_like of floats - Mean ("center") of the distribution. - scale : float or array_like of floats - Standard deviation (spread or "width") of the distribution. Must be - non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized normal distribution. - - See Also - -------- - scipy.stats.norm : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gaussian distribution is - - .. math:: p(x) = \\frac{1}{\\sqrt{ 2 \\pi \\sigma^2 }} - e^{ - \\frac{ (x - \\mu)^2 } {2 \\sigma^2} }, - - where :math:`\\mu` is the mean and :math:`\\sigma` the standard - deviation. The square of the standard deviation, :math:`\\sigma^2`, - is called the variance. - - The function has its peak at the mean, and its "spread" increases with - the standard deviation (the function reaches 0.607 times its maximum at - :math:`x + \\sigma` and :math:`x - \\sigma` [2]_). This implies that - `randomgen.generator.normal` is more likely to return samples lying close to - the mean, rather than those far away. - - References - ---------- - .. [1] Wikipedia, "Normal distribution", - https://en.wikipedia.org/wiki/Normal_distribution - .. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability, - Random Variables and Random Signal Principles", 4th ed., 2001, - pp. 51, 51, 125. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, sigma = 0, 0.1 # mean and standard deviation - >>> s = randomgen.generator.normal(mu, sigma, 1000) - - Verify the mean and the variance: - - >>> abs(mu - np.mean(s)) - 0.0 # may vary - - >>> abs(sigma - np.std(s, ddof=1)) - 0.1 # may vary - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * - ... np.exp( - (bins - mu)**2 / (2 * sigma**2) ), - ... linewidth=2, color='r') - >>> plt.show() - - Two-by-four array of samples from N(3, 6.25): - - >>> randomgen.generator.normal(3, 2.5, size=(2, 4)) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - return cont(&random_normal_zig, &self._bitgen, size, self.lock, 2, - loc, "", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - None) - - def standard_gamma(self, shape, size=None, dtype=np.float64, out=None): - """ - standard_gamma(shape, size=None, dtype='d', out=None) - - Draw samples from a standard Gamma distribution. - - Samples are drawn from a Gamma distribution with specified parameters, - shape (sometimes designated "k") and scale=1. - - Parameters - ---------- - shape : float or array_like of floats - Parameter, must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``shape`` is a scalar. Otherwise, - ``np.array(shape).size`` samples are drawn. - dtype : {str, dtype}, optional - Desired dtype of the result, either 'd' (or 'float64') or 'f' - (or 'float32'). All dtypes are determined by their name. The - default value is 'd'. - out : ndarray, optional - Alternative output array in which to place the result. If size is - not None, it must have the same shape as the provided size and - must match the type of the output values. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized standard gamma distribution. - - See Also - -------- - scipy.stats.gamma : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gamma distribution is - - .. math:: p(x) = x^{k-1}\\frac{e^{-x/\\theta}}{\\theta^k\\Gamma(k)}, - - where :math:`k` is the shape and :math:`\\theta` the scale, - and :math:`\\Gamma` is the Gamma function. - - The Gamma distribution is often used to model the times to failure of - electronic components, and arises naturally in processes for which the - waiting times between Poisson distributed events are relevant. - - References - ---------- - .. [1] Weisstein, Eric W. "Gamma Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/GammaDistribution.html - .. [2] Wikipedia, "Gamma distribution", - https://en.wikipedia.org/wiki/Gamma_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> shape, scale = 2., 1. # mean and width - >>> s = randomgen.generator.standard_gamma(shape, 1000000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps # doctest: +SKIP - >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ # doctest: +SKIP - ... (sps.gamma(shape) * scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - cdef void *func - key = np.dtype(dtype).name - if key == "float64": - return cont(&random_standard_gamma_zig, &self._bitgen, size, self.lock, 1, - shape, "shape", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, - out) - if key == "float32": - return cont_f(&random_standard_gamma_zig_f, &self._bitgen, size, self.lock, - shape, "shape", CONS_NON_NEGATIVE, - out) - else: - raise TypeError("Unsupported dtype \"{key}\" for standard_gamma".format(key=key)) - - def gamma(self, shape, scale=1.0, size=None): - """ - gamma(shape, scale=1.0, size=None) - - Draw samples from a Gamma distribution. - - Samples are drawn from a Gamma distribution with specified parameters, - `shape` (sometimes designated "k") and `scale` (sometimes designated - "theta"), where both parameters are > 0. - - Parameters - ---------- - shape : float or array_like of floats - The shape of the gamma distribution. Must be non-negative. - scale : float or array_like of floats, optional - The scale of the gamma distribution. Must be non-negative. - Default is equal to 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``shape`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(shape, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized gamma distribution. - - See Also - -------- - scipy.stats.gamma : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gamma distribution is - - .. math:: p(x) = x^{k-1}\\frac{e^{-x/\\theta}}{\\theta^k\\Gamma(k)}, - - where :math:`k` is the shape and :math:`\\theta` the scale, - and :math:`\\Gamma` is the Gamma function. - - The Gamma distribution is often used to model the times to failure of - electronic components, and arises naturally in processes for which the - waiting times between Poisson distributed events are relevant. - - References - ---------- - .. [1] Weisstein, Eric W. "Gamma Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/GammaDistribution.html - .. [2] Wikipedia, "Gamma distribution", - https://en.wikipedia.org/wiki/Gamma_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> shape, scale = 2., 2. # mean=4, std=2*sqrt(2) - >>> s = randomgen.generator.gamma(shape, scale, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps # doctest: +SKIP - >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1)*(np.exp(-bins/scale) / # doctest: +SKIP - ... (sps.gamma(shape)*scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return cont(&random_gamma, &self._bitgen, size, self.lock, 2, - shape, "shape", CONS_NON_NEGATIVE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def f(self, dfnum, dfden, size=None): - """ - f(dfnum, dfden, size=None) - - Draw samples from an F distribution. - - Samples are drawn from an F distribution with specified parameters, - `dfnum` (degrees of freedom in numerator) and `dfden` (degrees of - freedom in denominator), where both parameters must be greater than - zero. - - The random variate of the F distribution (also known as the - Fisher distribution) is a continuous probability distribution - that arises in ANOVA tests, and is the ratio of two chi-square - variates. - - Parameters - ---------- - dfnum : float or array_like of floats - Degrees of freedom in numerator, must be > 0. - dfden : float or array_like of float - Degrees of freedom in denominator, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``dfnum`` and ``dfden`` are both scalars. - Otherwise, ``np.broadcast(dfnum, dfden).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Fisher distribution. - - See Also - -------- - scipy.stats.f : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The F statistic is used to compare in-group variances to between-group - variances. Calculating the distribution depends on the sampling, and - so it is a function of the respective degrees of freedom in the - problem. The variable `dfnum` is the number of samples minus one, the - between-groups degrees of freedom, while `dfden` is the within-groups - degrees of freedom, the sum of the number of samples in each group - minus the number of groups. - - References - ---------- - .. [1] Glantz, Stanton A. "Primer of Biostatistics.", McGraw-Hill, - Fifth Edition, 2002. - .. [2] Wikipedia, "F-distribution", - https://en.wikipedia.org/wiki/F-distribution - - Examples - -------- - An example from Glantz[1], pp 47-40: - - Two groups, children of diabetics (25 people) and children from people - without diabetes (25 controls). Fasting blood glucose was measured, - case group had a mean value of 86.1, controls had a mean value of - 82.2. Standard deviations were 2.09 and 2.49 respectively. Are these - data consistent with the null hypothesis that the parents diabetic - status does not affect their children's blood glucose levels? - Calculating the F statistic from the data gives a value of 36.01. - - Draw samples from the distribution: - - >>> dfnum = 1. # between group degrees of freedom - >>> dfden = 48. # within groups degrees of freedom - >>> s = randomgen.generator.f(dfnum, dfden, 1000) - - The lower bound for the top 1% of the samples is : - - >>> np.sort(s)[-10] - 7.61988120985 # random - - So there is about a 1% chance that the F statistic will exceed 7.62, - the measured value is 36, so the null hypothesis is rejected at the 1% - level. - - """ - return cont(&random_f, &self._bitgen, size, self.lock, 2, - dfnum, "dfnum", CONS_POSITIVE, - dfden, "dfden", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def noncentral_f(self, dfnum, dfden, nonc, size=None): - """ - noncentral_f(dfnum, dfden, nonc, size=None) - - Draw samples from the noncentral F distribution. - - Samples are drawn from an F distribution with specified parameters, - `dfnum` (degrees of freedom in numerator) and `dfden` (degrees of - freedom in denominator), where both parameters > 1. - `nonc` is the non-centrality parameter. - - Parameters - ---------- - dfnum : float or array_like of floats - Numerator degrees of freedom, must be > 0. - - .. versionchanged:: 1.14.0 - Earlier NumPy versions required dfnum > 1. - dfden : float or array_like of floats - Denominator degrees of freedom, must be > 0. - nonc : float or array_like of floats - Non-centrality parameter, the sum of the squares of the numerator - means, must be >= 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``dfnum``, ``dfden``, and ``nonc`` - are all scalars. Otherwise, ``np.broadcast(dfnum, dfden, nonc).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized noncentral Fisher distribution. - - Notes - ----- - When calculating the power of an experiment (power = probability of - rejecting the null hypothesis when a specific alternative is true) the - non-central F statistic becomes important. When the null hypothesis is - true, the F statistic follows a central F distribution. When the null - hypothesis is not true, then it follows a non-central F statistic. - - References - ---------- - .. [1] Weisstein, Eric W. "Noncentral F-Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/NoncentralF-Distribution.html - .. [2] Wikipedia, "Noncentral F-distribution", - https://en.wikipedia.org/wiki/Noncentral_F-distribution - - Examples - -------- - In a study, testing for a specific alternative to the null hypothesis - requires use of the Noncentral F distribution. We need to calculate the - area in the tail of the distribution that exceeds the value of the F - distribution for the null hypothesis. We'll plot the two probability - distributions for comparison. - - >>> dfnum = 3 # between group deg of freedom - >>> dfden = 20 # within groups degrees of freedom - >>> nonc = 3.0 - >>> nc_vals = randomgen.generator.noncentral_f(dfnum, dfden, nonc, 1000000) - >>> NF = np.histogram(nc_vals, bins=50, density=True) - >>> c_vals = randomgen.generator.f(dfnum, dfden, 1000000) - >>> F = np.histogram(c_vals, bins=50, density=True) - >>> import matplotlib.pyplot as plt - >>> plt.plot(F[1][1:], F[0]) - >>> plt.plot(NF[1][1:], NF[0]) - >>> plt.show() - - """ - return cont(&random_noncentral_f, &self._bitgen, size, self.lock, 3, - dfnum, "dfnum", CONS_POSITIVE, - dfden, "dfden", CONS_POSITIVE, - nonc, "nonc", CONS_NON_NEGATIVE, None) - - def chisquare(self, df, size=None): - """ - chisquare(df, size=None) - - Draw samples from a chi-square distribution. - - When `df` independent random variables, each with standard normal - distributions (mean 0, variance 1), are squared and summed, the - resulting distribution is chi-square (see Notes). This distribution - is often used in hypothesis testing. - - Parameters - ---------- - df : float or array_like of floats - Number of degrees of freedom, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` is a scalar. Otherwise, - ``np.array(df).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized chi-square distribution. - - Raises - ------ - ValueError - When `df` <= 0 or when an inappropriate `size` (e.g. ``size=-1``) - is given. - - Notes - ----- - The variable obtained by summing the squares of `df` independent, - standard normally distributed random variables: - - .. math:: Q = \\sum_{i=0}^{\\mathtt{df}} X^2_i - - is chi-square distributed, denoted - - .. math:: Q \\sim \\chi^2_k. - - The probability density function of the chi-squared distribution is - - .. math:: p(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)} - x^{k/2 - 1} e^{-x/2}, - - where :math:`\\Gamma` is the gamma function, - - .. math:: \\Gamma(x) = \\int_0^{-\\infty} t^{x - 1} e^{-t} dt. - - References - ---------- - .. [1] NIST "Engineering Statistics Handbook" - https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm - - Examples - -------- - >>> randomgen.generator.chisquare(2,4) - array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272]) # random - - """ - return cont(&random_chisquare, &self._bitgen, size, self.lock, 1, - df, "df", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def noncentral_chisquare(self, df, nonc, size=None): - """ - noncentral_chisquare(df, nonc, size=None) - - Draw samples from a noncentral chi-square distribution. - - The noncentral :math:`\\chi^2` distribution is a generalization of - the :math:`\\chi^2` distribution. - - Parameters - ---------- - df : float or array_like of floats - Degrees of freedom, must be > 0. - - .. versionchanged:: 1.10.0 - Earlier NumPy versions required dfnum > 1. - nonc : float or array_like of floats - Non-centrality, must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` and ``nonc`` are both scalars. - Otherwise, ``np.broadcast(df, nonc).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized noncentral chi-square distribution. - - Notes - ----- - The probability density function for the noncentral Chi-square - distribution is - - .. math:: P(x;df,nonc) = \\sum^{\\infty}_{i=0} - \\frac{e^{-nonc/2}(nonc/2)^{i}}{i!} - P_{Y_{df+2i}}(x), - - where :math:`Y_{q}` is the Chi-square with q degrees of freedom. - - References - ---------- - .. [1] Wikipedia, "Noncentral chi-squared distribution" - https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram - - >>> import matplotlib.pyplot as plt - >>> values = plt.hist(randomgen.generator.noncentral_chisquare(3, 20, 100000), - ... bins=200, density=True) - >>> plt.show() - - Draw values from a noncentral chisquare with very small noncentrality, - and compare to a chisquare. - - >>> plt.figure() - >>> values = plt.hist(randomgen.generator.noncentral_chisquare(3, .0000001, 100000), - ... bins=np.arange(0., 25, .1), density=True) - >>> values2 = plt.hist(randomgen.generator.chisquare(3, 100000), - ... bins=np.arange(0., 25, .1), density=True) - >>> plt.plot(values[1][0:-1], values[0]-values2[0], 'ob') - >>> plt.show() - - Demonstrate how large values of non-centrality lead to a more symmetric - distribution. - - >>> plt.figure() - >>> values = plt.hist(randomgen.generator.noncentral_chisquare(3, 20, 100000), - ... bins=200, density=True) - >>> plt.show() - - """ - return cont(&random_noncentral_chisquare, &self._bitgen, size, self.lock, 2, - df, "df", CONS_POSITIVE, - nonc, "nonc", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def standard_cauchy(self, size=None): - """ - standard_cauchy(size=None) - - Draw samples from a standard Cauchy distribution with mode = 0. - - Also known as the Lorentz distribution. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - samples : ndarray or scalar - The drawn samples. - - Notes - ----- - The probability density function for the full Cauchy distribution is - - .. math:: P(x; x_0, \\gamma) = \\frac{1}{\\pi \\gamma \\bigl[ 1+ - (\\frac{x-x_0}{\\gamma})^2 \\bigr] } - - and the Standard Cauchy distribution just sets :math:`x_0=0` and - :math:`\\gamma=1` - - The Cauchy distribution arises in the solution to the driven harmonic - oscillator problem, and also describes spectral line broadening. It - also describes the distribution of values at which a line tilted at - a random angle will cut the x axis. - - When studying hypothesis tests that assume normality, seeing how the - tests perform on data from a Cauchy distribution is a good indicator of - their sensitivity to a heavy-tailed distribution, since the Cauchy looks - very much like a Gaussian distribution, but with heavier tails. - - References - ---------- - .. [1] NIST/SEMATECH e-Handbook of Statistical Methods, "Cauchy - Distribution", - https://www.itl.nist.gov/div898/handbook/eda/section3/eda3663.htm - .. [2] Weisstein, Eric W. "Cauchy Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/CauchyDistribution.html - .. [3] Wikipedia, "Cauchy distribution" - https://en.wikipedia.org/wiki/Cauchy_distribution - - Examples - -------- - Draw samples and plot the distribution: - - >>> import matplotlib.pyplot as plt - >>> s = randomgen.generator.standard_cauchy(1000000) - >>> s = s[(s>-25) & (s<25)] # truncate distribution so it plots well - >>> plt.hist(s, bins=100) - >>> plt.show() - - """ - return cont(&random_standard_cauchy, &self._bitgen, size, self.lock, 0, - 0.0, "", CONS_NONE, 0.0, "", CONS_NONE, 0.0, "", CONS_NONE, None) - - def standard_t(self, df, size=None): - """ - standard_t(df, size=None) - - Draw samples from a standard Student's t distribution with `df` degrees - of freedom. - - A special case of the hyperbolic distribution. As `df` gets - large, the result resembles that of the standard normal - distribution (`standard_normal`). - - Parameters - ---------- - df : float or array_like of floats - Degrees of freedom, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` is a scalar. Otherwise, - ``np.array(df).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized standard Student's t distribution. - - Notes - ----- - The probability density function for the t distribution is - - .. math:: P(x, df) = \\frac{\\Gamma(\\frac{df+1}{2})}{\\sqrt{\\pi df} - \\Gamma(\\frac{df}{2})}\\Bigl( 1+\\frac{x^2}{df} \\Bigr)^{-(df+1)/2} - - The t test is based on an assumption that the data come from a - Normal distribution. The t test provides a way to test whether - the sample mean (that is the mean calculated from the data) is - a good estimate of the true mean. - - The derivation of the t-distribution was first published in - 1908 by William Gosset while working for the Guinness Brewery - in Dublin. Due to proprietary issues, he had to publish under - a pseudonym, and so he used the name Student. - - References - ---------- - .. [1] Dalgaard, Peter, "Introductory Statistics With R", - Springer, 2002. - .. [2] Wikipedia, "Student's t-distribution" - https://en.wikipedia.org/wiki/Student's_t-distribution - - Examples - -------- - From Dalgaard page 83 [1]_, suppose the daily energy intake for 11 - women in kilojoules (kJ) is: - - >>> intake = np.array([5260., 5470, 5640, 6180, 6390, 6515, 6805, 7515, \\ - ... 7515, 8230, 8770]) - - Does their energy intake deviate systematically from the recommended - value of 7725 kJ? Our null hypothesis will be the absence of deviation, - and the alternate hypothesis will be the presence of an effect that could be - either positive or negative, hence making our test 2-tailed. - - Because we are estimating the mean and we have N=11 values in our sample, - we have N-1=10 degrees of freedom. We set our significance level to 95% and - compute the t statistic using the empirical mean and empirical standard - deviation of our intake. We use a ddof of 1 to base the computation of our - empirical standard deviation on an unbiased estimate of the variance (note: - the final estimate is not unbiased due to the concave nature of the square - root). - - >>> np.mean(intake) - 6753.636363636364 - >>> intake.std(ddof=1) - 1142.1232221373727 - >>> t = (np.mean(intake)-7725)/(intake.std(ddof=1)/np.sqrt(len(intake))) - >>> t - -2.8207540608310198 - - We draw 1000000 samples from Student's t distribution with the adequate - degrees of freedom. - - >>> import matplotlib.pyplot as plt - >>> s = np.random.default_rng().standard_t(10, size=1000000) - >>> h = plt.hist(s, bins=100, density=True) - - Does our t statistic land in one of the two critical regions found at - both tails of the distribution? - - >>> np.sum(np.abs(t) < np.abs(s)) / float(len(s)) - 0.018318 #random < 0.05, statistic is in critical region - - The probability value for this 2-tailed test is about 1.83%, which is - lower than the 5% pre-determined significance threshold. - - Therefore, the probability of observing values as extreme as our intake - conditionally on the null hypothesis being true is too low, and we reject - the null hypothesis of no deviation. - - """ - return cont(&random_standard_t, &self._bitgen, size, self.lock, 1, - df, "df", CONS_POSITIVE, - 0, "", CONS_NONE, - 0, "", CONS_NONE, - None) - - def vonmises(self, mu, kappa, size=None): - """ - vonmises(mu, kappa, size=None) - - Draw samples from a von Mises distribution. - - Samples are drawn from a von Mises distribution with specified mode - (mu) and dispersion (kappa), on the interval [-pi, pi]. - - The von Mises distribution (also known as the circular normal - distribution) is a continuous probability distribution on the unit - circle. It may be thought of as the circular analogue of the normal - distribution. - - Parameters - ---------- - mu : float or array_like of floats - Mode ("center") of the distribution. - kappa : float or array_like of floats - Dispersion of the distribution, has to be >=0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mu`` and ``kappa`` are both scalars. - Otherwise, ``np.broadcast(mu, kappa).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized von Mises distribution. - - See Also - -------- - scipy.stats.vonmises : probability density function, distribution, or - cumulative density function, etc. - - Notes - ----- - The probability density for the von Mises distribution is - - .. math:: p(x) = \\frac{e^{\\kappa cos(x-\\mu)}}{2\\pi I_0(\\kappa)}, - - where :math:`\\mu` is the mode and :math:`\\kappa` the dispersion, - and :math:`I_0(\\kappa)` is the modified Bessel function of order 0. - - The von Mises is named for Richard Edler von Mises, who was born in - Austria-Hungary, in what is now the Ukraine. He fled to the United - States in 1939 and became a professor at Harvard. He worked in - probability theory, aerodynamics, fluid mechanics, and philosophy of - science. - - References - ---------- - .. [1] Abramowitz, M. and Stegun, I. A. (Eds.). "Handbook of - Mathematical Functions with Formulas, Graphs, and Mathematical - Tables, 9th printing," New York: Dover, 1972. - .. [2] von Mises, R., "Mathematical Theory of Probability - and Statistics", New York: Academic Press, 1964. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, kappa = 0.0, 4.0 # mean and dispersion - >>> s = randomgen.generator.vonmises(mu, kappa, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> from scipy.special import i0 # doctest: +SKIP - >>> plt.hist(s, 50, density=True) - >>> x = np.linspace(-np.pi, np.pi, num=51) - >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) # doctest: +SKIP - >>> plt.plot(x, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return cont(&random_vonmises, &self._bitgen, size, self.lock, 2, - mu, "mu", CONS_NONE, - kappa, "kappa", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def pareto(self, a, size=None): - """ - pareto(a, size=None) - - Draw samples from a Pareto II or Lomax distribution with - specified shape. - - The Lomax or Pareto II distribution is a shifted Pareto - distribution. The classical Pareto distribution can be - obtained from the Lomax distribution by adding 1 and - multiplying by the scale parameter ``m`` (see Notes). The - smallest value of the Lomax distribution is zero while for the - classical Pareto distribution it is ``mu``, where the standard - Pareto distribution has location ``mu = 1``. Lomax can also - be considered as a simplified version of the Generalized - Pareto distribution (available in SciPy), with the scale set - to one and the location set to zero. - - The Pareto distribution must be greater than zero, and is - unbounded above. It is also known as the "80-20 rule". In - this distribution, 80 percent of the weights are in the lowest - 20 percent of the range, while the other 20 percent fill the - remaining 80 percent of the range. - - Parameters - ---------- - a : float or array_like of floats - Shape of the distribution. Must be positive. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Pareto distribution. - - See Also - -------- - scipy.stats.lomax : probability density function, distribution or - cumulative density function, etc. - scipy.stats.genpareto : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Pareto distribution is - - .. math:: p(x) = \\frac{am^a}{x^{a+1}} - - where :math:`a` is the shape and :math:`m` the scale. - - The Pareto distribution, named after the Italian economist - Vilfredo Pareto, is a power law probability distribution - useful in many real world problems. Outside the field of - economics it is generally referred to as the Bradford - distribution. Pareto developed the distribution to describe - the distribution of wealth in an economy. It has also found - use in insurance, web page access statistics, oil field sizes, - and many other problems, including the download frequency for - projects in Sourceforge [1]_. It is one of the so-called - "fat-tailed" distributions. - - References - ---------- - .. [1] Francis Hunt and Paul Johnson, On the Pareto Distribution of - Sourceforge projects. - .. [2] Pareto, V. (1896). Course of Political Economy. Lausanne. - .. [3] Reiss, R.D., Thomas, M.(2001), Statistical Analysis of Extreme - Values, Birkhauser Verlag, Basel, pp 23-30. - .. [4] Wikipedia, "Pareto distribution", - https://en.wikipedia.org/wiki/Pareto_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a, m = 3., 2. # shape and mode - >>> s = (randomgen.generator.pareto(a, 1000) + 1) * m - - Display the histogram of the samples, along with the probability - density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, _ = plt.hist(s, 100, density=True) - >>> fit = a*m**a / bins**(a+1) - >>> plt.plot(bins, max(count)*fit/max(fit), linewidth=2, color='r') - >>> plt.show() - - """ - return cont(&random_pareto, &self._bitgen, size, self.lock, 1, - a, "a", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def weibull(self, a, size=None): - """ - weibull(a, size=None) - - Draw samples from a Weibull distribution. - - Draw samples from a 1-parameter Weibull distribution with the given - shape parameter `a`. - - .. math:: X = (-ln(U))^{1/a} - - Here, U is drawn from the uniform distribution over (0,1]. - - The more common 2-parameter Weibull, including a scale parameter - :math:`\\lambda` is just :math:`X = \\lambda(-ln(U))^{1/a}`. - - Parameters - ---------- - a : float or array_like of floats - Shape parameter of the distribution. Must be nonnegative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Weibull distribution. - - See Also - -------- - scipy.stats.weibull_max - scipy.stats.weibull_min - scipy.stats.genextreme - gumbel - - Notes - ----- - The Weibull (or Type III asymptotic extreme value distribution - for smallest values, SEV Type III, or Rosin-Rammler - distribution) is one of a class of Generalized Extreme Value - (GEV) distributions used in modeling extreme value problems. - This class includes the Gumbel and Frechet distributions. - - The probability density for the Weibull distribution is - - .. math:: p(x) = \\frac{a} - {\\lambda}(\\frac{x}{\\lambda})^{a-1}e^{-(x/\\lambda)^a}, - - where :math:`a` is the shape and :math:`\\lambda` the scale. - - The function has its peak (the mode) at - :math:`\\lambda(\\frac{a-1}{a})^{1/a}`. - - When ``a = 1``, the Weibull distribution reduces to the exponential - distribution. - - References - ---------- - .. [1] Waloddi Weibull, Royal Technical University, Stockholm, - 1939 "A Statistical Theory Of The Strength Of Materials", - Ingeniorsvetenskapsakademiens Handlingar Nr 151, 1939, - Generalstabens Litografiska Anstalts Forlag, Stockholm. - .. [2] Waloddi Weibull, "A Statistical Distribution Function of - Wide Applicability", Journal Of Applied Mechanics ASME Paper - 1951. - .. [3] Wikipedia, "Weibull distribution", - https://en.wikipedia.org/wiki/Weibull_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a = 5. # shape - >>> s = randomgen.generator.weibull(a, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> x = np.arange(1,100.)/50. - >>> def weib(x,n,a): - ... return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a) - - >>> count, bins, ignored = plt.hist(randomgen.generator.weibull(5.,1000)) - >>> x = np.arange(1,100.)/50. - >>> scale = count.max()/weib(x, 1., 5.).max() - >>> plt.plot(x, weib(x, 1., 5.)*scale) - >>> plt.show() - - """ - return cont(&random_weibull, &self._bitgen, size, self.lock, 1, - a, "a", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def power(self, a, size=None): - """ - power(a, size=None) - - Draws samples in [0, 1] from a power distribution with positive - exponent a - 1. - - Also known as the power function distribution. - - Parameters - ---------- - a : float or array_like of floats - Parameter of the distribution. Must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized power distribution. - - Raises - ------ - ValueError - If a < 1. - - Notes - ----- - The probability density function is - - .. math:: P(x; a) = ax^{a-1}, 0 \\le x \\le 1, a>0. - - The power function distribution is just the inverse of the Pareto - distribution. It may also be seen as a special case of the Beta - distribution. - - It is used, for example, in modeling the over-reporting of insurance - claims. - - References - ---------- - .. [1] Christian Kleiber, Samuel Kotz, "Statistical size distributions - in economics and actuarial sciences", Wiley, 2003. - .. [2] Heckert, N. A. and Filliben, James J. "NIST Handbook 148: - Dataplot Reference Manual, Volume 2: Let Subcommands and Library - Functions", National Institute of Standards and Technology - Handbook Series, June 2003. - https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/powpdf.pdf - - Examples - -------- - Draw samples from the distribution: - - >>> a = 5. # shape - >>> samples = 1000 - >>> s = randomgen.generator.power(a, samples) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, bins=30) - >>> x = np.linspace(0, 1, 100) - >>> y = a*x**(a-1.) - >>> normed_y = samples*np.diff(bins)[0]*y - >>> plt.plot(x, normed_y) - >>> plt.show() - - Compare the power function distribution to the inverse of the Pareto. - - >>> from scipy import stats # doctest: +SKIP - >>> rvs = randomgen.generator.power(5, 1000000) - >>> rvsp = randomgen.generator.pareto(5, 1000000) - >>> xx = np.linspace(0,1,100) - >>> powpdf = stats.powerlaw.pdf(xx,5) # doctest: +SKIP - - >>> plt.figure() - >>> plt.hist(rvs, bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('randomgen.generator.power(5)') - - >>> plt.figure() - >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('inverse of 1 + randomgen.generator.pareto(5)') - - >>> plt.figure() - >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('inverse of stats.pareto(5)') - - """ - return cont(&random_power, &self._bitgen, size, self.lock, 1, - a, "a", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def laplace(self, loc=0.0, scale=1.0, size=None): - """ - laplace(loc=0.0, scale=1.0, size=None) - - Draw samples from the Laplace or double exponential distribution with - specified location (or mean) and scale (decay). - - The Laplace distribution is similar to the Gaussian/normal distribution, - but is sharper at the peak and has fatter tails. It represents the - difference between two independent, identically distributed exponential - random variables. - - Parameters - ---------- - loc : float or array_like of floats, optional - The position, :math:`\\mu`, of the distribution peak. Default is 0. - scale : float or array_like of floats, optional - :math:`\\lambda`, the exponential decay. Default is 1. Must be non- - negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Laplace distribution. - - Notes - ----- - It has the probability density function - - .. math:: f(x; \\mu, \\lambda) = \\frac{1}{2\\lambda} - \\exp\\left(-\\frac{|x - \\mu|}{\\lambda}\\right). - - The first law of Laplace, from 1774, states that the frequency - of an error can be expressed as an exponential function of the - absolute magnitude of the error, which leads to the Laplace - distribution. For many problems in economics and health - sciences, this distribution seems to model the data better - than the standard Gaussian distribution. - - References - ---------- - .. [1] Abramowitz, M. and Stegun, I. A. (Eds.). "Handbook of - Mathematical Functions with Formulas, Graphs, and Mathematical - Tables, 9th printing," New York: Dover, 1972. - .. [2] Kotz, Samuel, et. al. "The Laplace Distribution and - Generalizations, " Birkhauser, 2001. - .. [3] Weisstein, Eric W. "Laplace Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/LaplaceDistribution.html - .. [4] Wikipedia, "Laplace distribution", - https://en.wikipedia.org/wiki/Laplace_distribution - - Examples - -------- - Draw samples from the distribution - - >>> loc, scale = 0., 1. - >>> s = randomgen.generator.laplace(loc, scale, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> x = np.arange(-8., 8., .01) - >>> pdf = np.exp(-abs(x-loc)/scale)/(2.*scale) - >>> plt.plot(x, pdf) - - Plot Gaussian for comparison: - - >>> g = (1/(scale * np.sqrt(2 * np.pi)) * - ... np.exp(-(x - loc)**2 / (2 * scale**2))) - >>> plt.plot(x,g) - - """ - return cont(&random_laplace, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def gumbel(self, loc=0.0, scale=1.0, size=None): - """ - gumbel(loc=0.0, scale=1.0, size=None) - - Draw samples from a Gumbel distribution. - - Draw samples from a Gumbel distribution with specified location and - scale. For more information on the Gumbel distribution, see - Notes and References below. - - Parameters - ---------- - loc : float or array_like of floats, optional - The location of the mode of the distribution. Default is 0. - scale : float or array_like of floats, optional - The scale parameter of the distribution. Default is 1. Must be non- - negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Gumbel distribution. - - See Also - -------- - scipy.stats.gumbel_l - scipy.stats.gumbel_r - scipy.stats.genextreme - weibull - - Notes - ----- - The Gumbel (or Smallest Extreme Value (SEV) or the Smallest Extreme - Value Type I) distribution is one of a class of Generalized Extreme - Value (GEV) distributions used in modeling extreme value problems. - The Gumbel is a special case of the Extreme Value Type I distribution - for maximums from distributions with "exponential-like" tails. - - The probability density for the Gumbel distribution is - - .. math:: p(x) = \\frac{e^{-(x - \\mu)/ \\beta}}{\\beta} e^{ -e^{-(x - \\mu)/ - \\beta}}, - - where :math:`\\mu` is the mode, a location parameter, and - :math:`\\beta` is the scale parameter. - - The Gumbel (named for German mathematician Emil Julius Gumbel) was used - very early in the hydrology literature, for modeling the occurrence of - flood events. It is also used for modeling maximum wind speed and - rainfall rates. It is a "fat-tailed" distribution - the probability of - an event in the tail of the distribution is larger than if one used a - Gaussian, hence the surprisingly frequent occurrence of 100-year - floods. Floods were initially modeled as a Gaussian process, which - underestimated the frequency of extreme events. - - It is one of a class of extreme value distributions, the Generalized - Extreme Value (GEV) distributions, which also includes the Weibull and - Frechet. - - The function has a mean of :math:`\\mu + 0.57721\\beta` and a variance - of :math:`\\frac{\\pi^2}{6}\\beta^2`. - - References - ---------- - .. [1] Gumbel, E. J., "Statistics of Extremes," - New York: Columbia University Press, 1958. - .. [2] Reiss, R.-D. and Thomas, M., "Statistical Analysis of Extreme - Values from Insurance, Finance, Hydrology and Other Fields," - Basel: Birkhauser Verlag, 2001. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, beta = 0, 0.1 # location and scale - >>> s = randomgen.generator.gumbel(mu, beta, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta) - ... * np.exp( -np.exp( -(bins - mu) /beta) ), - ... linewidth=2, color='r') - >>> plt.show() - - Show how an extreme value distribution can arise from a Gaussian process - and compare to a Gaussian: - - >>> means = [] - >>> maxima = [] - >>> for i in range(0,1000) : - ... a = randomgen.generator.normal(mu, beta, 1000) - ... means.append(a.mean()) - ... maxima.append(a.max()) - >>> count, bins, ignored = plt.hist(maxima, 30, density=True) - >>> beta = np.std(maxima) * np.sqrt(6) / np.pi - >>> mu = np.mean(maxima) - 0.57721*beta - >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta) - ... * np.exp(-np.exp(-(bins - mu)/beta)), - ... linewidth=2, color='r') - >>> plt.plot(bins, 1/(beta * np.sqrt(2 * np.pi)) - ... * np.exp(-(bins - mu)**2 / (2 * beta**2)), - ... linewidth=2, color='g') - >>> plt.show() - - """ - return cont(&random_gumbel, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def logistic(self, loc=0.0, scale=1.0, size=None): - """ - logistic(loc=0.0, scale=1.0, size=None) - - Draw samples from a logistic distribution. - - Samples are drawn from a logistic distribution with specified - parameters, loc (location or mean, also median), and scale (>0). - - Parameters - ---------- - loc : float or array_like of floats, optional - Parameter of the distribution. Default is 0. - scale : float or array_like of floats, optional - Parameter of the distribution. Must be non-negative. - Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized logistic distribution. - - See Also - -------- - scipy.stats.logistic : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Logistic distribution is - - .. math:: P(x) = P(x) = \\frac{e^{-(x-\\mu)/s}}{s(1+e^{-(x-\\mu)/s})^2}, - - where :math:`\\mu` = location and :math:`s` = scale. - - The Logistic distribution is used in Extreme Value problems where it - can act as a mixture of Gumbel distributions, in Epidemiology, and by - the World Chess Federation (FIDE) where it is used in the Elo ranking - system, assuming the performance of each player is a logistically - distributed random variable. - - References - ---------- - .. [1] Reiss, R.-D. and Thomas M. (2001), "Statistical Analysis of - Extreme Values, from Insurance, Finance, Hydrology and Other - Fields," Birkhauser Verlag, Basel, pp 132-133. - .. [2] Weisstein, Eric W. "Logistic Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/LogisticDistribution.html - .. [3] Wikipedia, "Logistic-distribution", - https://en.wikipedia.org/wiki/Logistic_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> loc, scale = 10, 1 - >>> s = randomgen.generator.logistic(loc, scale, 10000) - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, bins=50) - - # plot against distribution - - >>> def logist(x, loc, scale): - ... return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2) - >>> lgst_val = logist(bins, loc, scale) - >>> plt.plot(bins, lgst_val * count.max() / lgst_val.max()) - >>> plt.show() - - """ - return cont(&random_logistic, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def lognormal(self, mean=0.0, sigma=1.0, size=None): - """ - lognormal(mean=0.0, sigma=1.0, size=None) - - Draw samples from a log-normal distribution. - - Draw samples from a log-normal distribution with specified mean, - standard deviation, and array shape. Note that the mean and standard - deviation are not the values for the distribution itself, but of the - underlying normal distribution it is derived from. - - Parameters - ---------- - mean : float or array_like of floats, optional - Mean value of the underlying normal distribution. Default is 0. - sigma : float or array_like of floats, optional - Standard deviation of the underlying normal distribution. Must be - non-negative. Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mean`` and ``sigma`` are both scalars. - Otherwise, ``np.broadcast(mean, sigma).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized log-normal distribution. - - See Also - -------- - scipy.stats.lognorm : probability density function, distribution, - cumulative density function, etc. - - Notes - ----- - A variable `x` has a log-normal distribution if `log(x)` is normally - distributed. The probability density function for the log-normal - distribution is: - - .. math:: p(x) = \\frac{1}{\\sigma x \\sqrt{2\\pi}} - e^{(-\\frac{(ln(x)-\\mu)^2}{2\\sigma^2})} - - where :math:`\\mu` is the mean and :math:`\\sigma` is the standard - deviation of the normally distributed logarithm of the variable. - A log-normal distribution results if a random variable is the *product* - of a large number of independent, identically-distributed variables in - the same way that a normal distribution results if the variable is the - *sum* of a large number of independent, identically-distributed - variables. - - References - ---------- - .. [1] Limpert, E., Stahel, W. A., and Abbt, M., "Log-normal - Distributions across the Sciences: Keys and Clues," - BioScience, Vol. 51, No. 5, May, 2001. - https://stat.ethz.ch/~stahel/lognormal/bioscience.pdf - .. [2] Reiss, R.D. and Thomas, M., "Statistical Analysis of Extreme - Values," Basel: Birkhauser Verlag, 2001, pp. 31-32. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, sigma = 3., 1. # mean and standard deviation - >>> s = randomgen.generator.lognormal(mu, sigma, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 100, density=True, align='mid') - - >>> x = np.linspace(min(bins), max(bins), 10000) - >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) - ... / (x * sigma * np.sqrt(2 * np.pi))) - - >>> plt.plot(x, pdf, linewidth=2, color='r') - >>> plt.axis('tight') - >>> plt.show() - - Demonstrate that taking the products of random samples from a uniform - distribution can be fit well by a log-normal probability density - function. - - >>> # Generate a thousand samples: each is the product of 100 random - >>> # values, drawn from a normal distribution. - >>> b = [] - >>> for i in range(1000): - ... a = 10. + randomgen.generator.standard_normal(100) - ... b.append(np.product(a)) - - >>> b = np.array(b) / np.min(b) # scale values to be positive - >>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid') - >>> sigma = np.std(np.log(b)) - >>> mu = np.mean(np.log(b)) - - >>> x = np.linspace(min(bins), max(bins), 10000) - >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) - ... / (x * sigma * np.sqrt(2 * np.pi))) - - >>> plt.plot(x, pdf, color='r', linewidth=2) - >>> plt.show() - - """ - return cont(&random_lognormal, &self._bitgen, size, self.lock, 2, - mean, "mean", CONS_NONE, - sigma, "sigma", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def rayleigh(self, scale=1.0, size=None): - """ - rayleigh(scale=1.0, size=None) - - Draw samples from a Rayleigh distribution. - - The :math:`\\chi` and Weibull distributions are generalizations of the - Rayleigh. - - Parameters - ---------- - scale : float or array_like of floats, optional - Scale, also equals the mode. Must be non-negative. Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``scale`` is a scalar. Otherwise, - ``np.array(scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Rayleigh distribution. - - Notes - ----- - The probability density function for the Rayleigh distribution is - - .. math:: P(x;scale) = \\frac{x}{scale^2}e^{\\frac{-x^2}{2 \\cdotp scale^2}} - - The Rayleigh distribution would arise, for example, if the East - and North components of the wind velocity had identical zero-mean - Gaussian distributions. Then the wind speed would have a Rayleigh - distribution. - - References - ---------- - .. [1] Brighton Webs Ltd., "Rayleigh Distribution," - https://web.archive.org/web/20090514091424/http://brighton-webs.co.uk:80/distributions/rayleigh.asp - .. [2] Wikipedia, "Rayleigh distribution" - https://en.wikipedia.org/wiki/Rayleigh_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram - - >>> from matplotlib.pyplot import hist - >>> values = hist(randomgen.generator.rayleigh(3, 100000), bins=200, density=True) - - Wave heights tend to follow a Rayleigh distribution. If the mean wave - height is 1 meter, what fraction of waves are likely to be larger than 3 - meters? - - >>> meanvalue = 1 - >>> modevalue = np.sqrt(2 / np.pi) * meanvalue - >>> s = randomgen.generator.rayleigh(modevalue, 1000000) - - The percentage of waves larger than 3 meters is: - - >>> 100.*sum(s>3)/1000000. - 0.087300000000000003 # random - - """ - return cont(&random_rayleigh, &self._bitgen, size, self.lock, 1, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def wald(self, mean, scale, size=None): - """ - wald(mean, scale, size=None) - - Draw samples from a Wald, or inverse Gaussian, distribution. - - As the scale approaches infinity, the distribution becomes more like a - Gaussian. Some references claim that the Wald is an inverse Gaussian - with mean equal to 1, but this is by no means universal. - - The inverse Gaussian distribution was first studied in relationship to - Brownian motion. In 1956 M.C.K. Tweedie used the name inverse Gaussian - because there is an inverse relationship between the time to cover a - unit distance and distance covered in unit time. - - Parameters - ---------- - mean : float or array_like of floats - Distribution mean, must be > 0. - scale : float or array_like of floats - Scale parameter, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mean`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(mean, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Wald distribution. - - Notes - ----- - The probability density function for the Wald distribution is - - .. math:: P(x;mean,scale) = \\sqrt{\\frac{scale}{2\\pi x^3}}e^ - \\frac{-scale(x-mean)^2}{2\\cdotp mean^2x} - - As noted above the inverse Gaussian distribution first arise - from attempts to model Brownian motion. It is also a - competitor to the Weibull for use in reliability modeling and - modeling stock returns and interest rate processes. - - References - ---------- - .. [1] Brighton Webs Ltd., Wald Distribution, - https://web.archive.org/web/20090423014010/http://www.brighton-webs.co.uk:80/distributions/wald.asp - .. [2] Chhikara, Raj S., and Folks, J. Leroy, "The Inverse Gaussian - Distribution: Theory : Methodology, and Applications", CRC Press, - 1988. - .. [3] Wikipedia, "Inverse Gaussian distribution" - https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram: - - >>> import matplotlib.pyplot as plt - >>> h = plt.hist(randomgen.generator.wald(3, 2, 100000), bins=200, density=True) - >>> plt.show() - - """ - return cont(&random_wald, &self._bitgen, size, self.lock, 2, - mean, "mean", CONS_POSITIVE, - scale, "scale", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def triangular(self, left, mode, right, size=None): - """ - triangular(left, mode, right, size=None) - - Draw samples from the triangular distribution over the - interval ``[left, right]``. - - The triangular distribution is a continuous probability - distribution with lower limit left, peak at mode, and upper - limit right. Unlike the other distributions, these parameters - directly define the shape of the pdf. - - Parameters - ---------- - left : float or array_like of floats - Lower limit. - mode : float or array_like of floats - The value where the peak of the distribution occurs. - The value must fulfill the condition ``left <= mode <= right``. - right : float or array_like of floats - Upper limit, must be larger than `left`. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``left``, ``mode``, and ``right`` - are all scalars. Otherwise, ``np.broadcast(left, mode, right).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized triangular distribution. - - Notes - ----- - The probability density function for the triangular distribution is - - .. math:: P(x;l, m, r) = \\begin{cases} - \\frac{2(x-l)}{(r-l)(m-l)}& \\text{for $l \\leq x \\leq m$},\\\\ - \\frac{2(r-x)}{(r-l)(r-m)}& \\text{for $m \\leq x \\leq r$},\\\\ - 0& \\text{otherwise}. - \\end{cases} - - The triangular distribution is often used in ill-defined - problems where the underlying distribution is not known, but - some knowledge of the limits and mode exists. Often it is used - in simulations. - - References - ---------- - .. [1] Wikipedia, "Triangular distribution" - https://en.wikipedia.org/wiki/Triangular_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram: - - >>> import matplotlib.pyplot as plt - >>> h = plt.hist(randomgen.generator.triangular(-3, 0, 8, 100000), bins=200, - ... density=True) - >>> plt.show() - - """ - cdef bint is_scalar = True - cdef double fleft, fmode, fright - cdef np.ndarray oleft, omode, oright - - oleft = np.PyArray_FROM_OTF(left, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - omode = np.PyArray_FROM_OTF(mode, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - oright = np.PyArray_FROM_OTF(right, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(oleft) == np.PyArray_NDIM(omode) == np.PyArray_NDIM(oright) == 0: - fleft = PyFloat_AsDouble(left) - fright = PyFloat_AsDouble(right) - fmode = PyFloat_AsDouble(mode) - - if fleft > fmode: - raise ValueError("left > mode") - if fmode > fright: - raise ValueError("mode > right") - if fleft == fright: - raise ValueError("left == right") - return cont(&random_triangular, &self._bitgen, size, self.lock, 3, - fleft, "", CONS_NONE, - fmode, "", CONS_NONE, - fright, "", CONS_NONE, None) - - if np.any(np.greater(oleft, omode)): - raise ValueError("left > mode") - if np.any(np.greater(omode, oright)): - raise ValueError("mode > right") - if np.any(np.equal(oleft, oright)): - raise ValueError("left == right") - - return cont_broadcast_3(&random_triangular, &self._bitgen, size, self.lock, - oleft, "", CONS_NONE, - omode, "", CONS_NONE, - oright, "", CONS_NONE) - - # Complicated, discrete distributions: - def binomial(self, n, p, size=None): - """ - binomial(n, p, size=None) - - Draw samples from a binomial distribution. - - Samples are drawn from a binomial distribution with specified - parameters, n trials and p probability of success where - n an integer >= 0 and p is in the interval [0,1]. (n may be - input as a float, but it is truncated to an integer in use) - - Parameters - ---------- - n : int or array_like of ints - Parameter of the distribution, >= 0. Floats are also accepted, - but they will be truncated to integers. - p : float or array_like of floats - Parameter of the distribution, >= 0 and <=1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``n`` and ``p`` are both scalars. - Otherwise, ``np.broadcast(n, p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized binomial distribution, where - each sample is equal to the number of successes over the n trials. - - See Also - -------- - scipy.stats.binom : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the binomial distribution is - - .. math:: P(N) = \\binom{n}{N}p^N(1-p)^{n-N}, - - where :math:`n` is the number of trials, :math:`p` is the probability - of success, and :math:`N` is the number of successes. - - When estimating the standard error of a proportion in a population by - using a random sample, the normal distribution works well unless the - product p*n <=5, where p = population proportion estimate, and n = - number of samples, in which case the binomial distribution is used - instead. For example, a sample of 15 people shows 4 who are left - handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4, - so the binomial distribution should be used in this case. - - References - ---------- - .. [1] Dalgaard, Peter, "Introductory Statistics with R", - Springer-Verlag, 2002. - .. [2] Glantz, Stanton A. "Primer of Biostatistics.", McGraw-Hill, - Fifth Edition, 2002. - .. [3] Lentner, Marvin, "Elementary Applied Statistics", Bogden - and Quigley, 1972. - .. [4] Weisstein, Eric W. "Binomial Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/BinomialDistribution.html - .. [5] Wikipedia, "Binomial distribution", - https://en.wikipedia.org/wiki/Binomial_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> n, p = 10, .5 # number of trials, probability of each trial - >>> s = randomgen.generator.binomial(n, p, 1000) - # result of flipping a coin 10 times, tested 1000 times. - - A real world example. A company drills 9 wild-cat oil exploration - wells, each with an estimated probability of success of 0.1. All nine - wells fail. What is the probability of that happening? - - Let's do 20,000 trials of the model, and count the number that - generate zero positive results. - - >>> sum(randomgen.generator.binomial(9, 0.1, 20000) == 0)/20000. - # answer = 0.38885, or 39%. - - """ - - # Uses a custom implementation since self._binomial is required - cdef double _dp = 0 - cdef int64_t _in = 0 - cdef bint is_scalar = True - cdef np.npy_intp i, cnt - cdef np.ndarray randoms - cdef np.int64_t *randoms_data - cdef np.broadcast it - - p_arr = np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - is_scalar = is_scalar and np.PyArray_NDIM(p_arr) == 0 - n_arr = np.PyArray_FROM_OTF(n, np.NPY_INT64, api.NPY_ARRAY_ALIGNED) - is_scalar = is_scalar and np.PyArray_NDIM(n_arr) == 0 - - if not is_scalar: - check_array_constraint(p_arr, "p", CONS_BOUNDED_0_1) - check_array_constraint(n_arr, "n", CONS_NON_NEGATIVE) - if size is not None: - randoms = np.empty(size, np.int64) - else: - it = np.PyArray_MultiIterNew2(p_arr, n_arr) - randoms = np.empty(it.shape, np.int64) - - randoms_data = np.PyArray_DATA(randoms) - cnt = np.PyArray_SIZE(randoms) - - it = np.PyArray_MultiIterNew3(randoms, p_arr, n_arr) - validate_output_shape(it.shape, randoms) - with self.lock, nogil: - for i in range(cnt): - _dp = (np.PyArray_MultiIter_DATA(it, 1))[0] - _in = (np.PyArray_MultiIter_DATA(it, 2))[0] - (np.PyArray_MultiIter_DATA(it, 0))[0] = random_binomial(&self._bitgen, _dp, _in, &self._binomial) - - np.PyArray_MultiIter_NEXT(it) - - return randoms - - _dp = PyFloat_AsDouble(p) - _in = n - check_constraint(_dp, "p", CONS_BOUNDED_0_1) - check_constraint(_in, "n", CONS_NON_NEGATIVE) - - if size is None: - with self.lock: - return random_binomial(&self._bitgen, _dp, _in, &self._binomial) - - randoms = np.empty(size, np.int64) - cnt = np.PyArray_SIZE(randoms) - randoms_data = np.PyArray_DATA(randoms) - - with self.lock, nogil: - for i in range(cnt): - randoms_data[i] = random_binomial(&self._bitgen, _dp, _in, - &self._binomial) - - return randoms - - def negative_binomial(self, n, p, size=None): - """ - negative_binomial(n, p, size=None) - - Draw samples from a negative binomial distribution. - - Samples are drawn from a negative binomial distribution with specified - parameters, `n` successes and `p` probability of success where `n` - is > 0 and `p` is in the interval [0, 1]. - - Parameters - ---------- - n : float or array_like of floats - Parameter of the distribution, > 0. - p : float or array_like of floats - Parameter of the distribution. Must satisfy 0 < p <= 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``n`` and ``p`` are both scalars. - Otherwise, ``np.broadcast(n, p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized negative binomial distribution, - where each sample is equal to N, the number of failures that - occurred before a total of n successes was reached. - - Notes - ----- - The probability mass function of the negative binomial distribution is - - .. math:: P(N;n,p) = \\frac{\\Gamma(N+n)}{N!\\Gamma(n)}p^{n}(1-p)^{N}, - - where :math:`n` is the number of successes, :math:`p` is the - probability of success, :math:`N+n` is the number of trials, and - :math:`\\Gamma` is the gamma function. When :math:`n` is an integer, - :math:`\\frac{\\Gamma(N+n)}{N!\\Gamma(n)} = \\binom{N+n-1}{N}`, which is - the more common form of this term in the the pmf. The negative - binomial distribution gives the probability of N failures given n - successes, with a success on the last trial. - - If one throws a die repeatedly until the third time a "1" appears, - then the probability distribution of the number of non-"1"s that - appear before the third "1" is a negative binomial distribution. - - References - ---------- - .. [1] Weisstein, Eric W. "Negative Binomial Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/NegativeBinomialDistribution.html - .. [2] Wikipedia, "Negative binomial distribution", - https://en.wikipedia.org/wiki/Negative_binomial_distribution - - Examples - -------- - Draw samples from the distribution: - - A real world example. A company drills wild-cat oil - exploration wells, each with an estimated probability of - success of 0.1. What is the probability of having one success - for each successive well, that is what is the probability of a - single success after drilling 5 wells, after 6 wells, etc.? - - >>> s = randomgen.generator.negative_binomial(1, 0.1, 100000) - >>> for i in range(1, 11): # doctest: +SKIP - ... probability = sum(s= 0. A sequence of expectation - intervals must be broadcastable over the requested size. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``lam`` is a scalar. Otherwise, - ``np.array(lam).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Poisson distribution. - - Notes - ----- - The Poisson distribution - - .. math:: f(k; \\lambda)=\\frac{\\lambda^k e^{-\\lambda}}{k!} - - For events with an expected separation :math:`\\lambda` the Poisson - distribution :math:`f(k; \\lambda)` describes the probability of - :math:`k` events occurring within the observed - interval :math:`\\lambda`. - - Because the output is limited to the range of the C int64 type, a - ValueError is raised when `lam` is within 10 sigma of the maximum - representable value. - - References - ---------- - .. [1] Weisstein, Eric W. "Poisson Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/PoissonDistribution.html - .. [2] Wikipedia, "Poisson distribution", - https://en.wikipedia.org/wiki/Poisson_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> import numpy as np - >>> s = randomgen.generator.poisson(5, 10000) - - Display histogram of the sample: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 14, density=True) - >>> plt.show() - - Draw each 100 values for lambda 100 and 500: - - >>> s = randomgen.generator.poisson(lam=(100., 500.), size=(100, 2)) - - """ - return disc(&random_poisson, &self._bitgen, size, self.lock, 1, 0, - lam, "lam", CONS_POISSON, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - - def zipf(self, a, size=None): - """ - zipf(a, size=None) - - Draw samples from a Zipf distribution. - - Samples are drawn from a Zipf distribution with specified parameter - `a` > 1. - - The Zipf distribution (also known as the zeta distribution) is a - continuous probability distribution that satisfies Zipf's law: the - frequency of an item is inversely proportional to its rank in a - frequency table. - - Parameters - ---------- - a : float or array_like of floats - Distribution parameter. Must be greater than 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Zipf distribution. - - See Also - -------- - scipy.stats.zipf : probability density function, distribution, or - cumulative density function, etc. - - Notes - ----- - The probability density for the Zipf distribution is - - .. math:: p(x) = \\frac{x^{-a}}{\\zeta(a)}, - - where :math:`\\zeta` is the Riemann Zeta function. - - It is named for the American linguist George Kingsley Zipf, who noted - that the frequency of any word in a sample of a language is inversely - proportional to its rank in the frequency table. - - References - ---------- - .. [1] Zipf, G. K., "Selected Studies of the Principle of Relative - Frequency in Language," Cambridge, MA: Harvard Univ. Press, - 1932. - - Examples - -------- - Draw samples from the distribution: - - >>> a = 2. # parameter - >>> s = randomgen.generator.zipf(a, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> from scipy import special # doctest: +SKIP - - Truncate s values at 50 so plot is interesting: - - >>> count, bins, ignored = plt.hist(s[s<50], - ... 50, density=True) - >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP - >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return disc(&random_zipf, &self._bitgen, size, self.lock, 1, 0, - a, "a", CONS_GT_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - - def geometric(self, p, size=None): - """ - geometric(p, size=None) - - Draw samples from the geometric distribution. - - Bernoulli trials are experiments with one of two outcomes: - success or failure (an example of such an experiment is flipping - a coin). The geometric distribution models the number of trials - that must be run in order to achieve success. It is therefore - supported on the positive integers, ``k = 1, 2, ...``. - - The probability mass function of the geometric distribution is - - .. math:: f(k) = (1 - p)^{k - 1} p - - where `p` is the probability of success of an individual trial. - - Parameters - ---------- - p : float or array_like of floats - The probability of success of an individual trial. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``p`` is a scalar. Otherwise, - ``np.array(p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized geometric distribution. - - Examples - -------- - Draw ten thousand values from the geometric distribution, - with the probability of an individual success equal to 0.35: - - >>> z = randomgen.generator.geometric(p=0.35, size=10000) - - How many trials succeeded after a single run? - - >>> (z == 1).sum() / 10000. - 0.34889999999999999 # random - - """ - return disc(&random_geometric, &self._bitgen, size, self.lock, 1, 0, - p, "p", CONS_BOUNDED_GT_0_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - - def hypergeometric(self, ngood, nbad, nsample, size=None): - """ - hypergeometric(ngood, nbad, nsample, size=None) - - Draw samples from a Hypergeometric distribution. - - Samples are drawn from a hypergeometric distribution with specified - parameters, `ngood` (ways to make a good selection), `nbad` (ways to make - a bad selection), and `nsample` (number of items sampled, which is less - than or equal to the sum ``ngood + nbad``). - - Parameters - ---------- - ngood : int or array_like of ints - Number of ways to make a good selection. Must be nonnegative. - nbad : int or array_like of ints - Number of ways to make a bad selection. Must be nonnegative. - nsample : int or array_like of ints - Number of items sampled. Must be nonnegative and less than - ``ngood + nbad``. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if `ngood`, `nbad`, and `nsample` - are all scalars. Otherwise, ``np.broadcast(ngood, nbad, nsample).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized hypergeometric distribution. Each - sample is the number of good items within a randomly selected subset of - size `nsample` taken from a set of `ngood` good items and `nbad` bad items. - - See Also - -------- - scipy.stats.hypergeom : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Hypergeometric distribution is - - .. math:: P(x) = \\frac{\\binom{g}{x}\\binom{b}{n-x}}{\\binom{g+b}{n}}, - - where :math:`0 \\le x \\le n` and :math:`n-b \\le x \\le g` - - for P(x) the probability of ``x`` good results in the drawn sample, - g = `ngood`, b = `nbad`, and n = `nsample`. - - Consider an urn with black and white marbles in it, `ngood` of them - are black and `nbad` are white. If you draw `nsample` balls without - replacement, then the hypergeometric distribution describes the - distribution of black balls in the drawn sample. - - Note that this distribution is very similar to the binomial - distribution, except that in this case, samples are drawn without - replacement, whereas in the Binomial case samples are drawn with - replacement (or the sample space is infinite). As the sample space - becomes large, this distribution approaches the binomial. - - References - ---------- - .. [1] Lentner, Marvin, "Elementary Applied Statistics", Bogden - and Quigley, 1972. - .. [2] Weisstein, Eric W. "Hypergeometric Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/HypergeometricDistribution.html - .. [3] Wikipedia, "Hypergeometric distribution", - https://en.wikipedia.org/wiki/Hypergeometric_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> ngood, nbad, nsamp = 100, 2, 10 - # number of good, number of bad, and number of samples - >>> s = randomgen.generator.hypergeometric(ngood, nbad, nsamp, 1000) - >>> from matplotlib.pyplot import hist - >>> hist(s) - # note that it is very unlikely to grab both bad items - - Suppose you have an urn with 15 white and 15 black marbles. - If you pull 15 marbles at random, how likely is it that - 12 or more of them are one color? - - >>> s = randomgen.generator.hypergeometric(15, 15, 15, 100000) - >>> sum(s>=12)/100000. + sum(s<=3)/100000. - # answer = 0.003 ... pretty unlikely! - - """ - DEF HYPERGEOM_MAX = 10**9 - cdef bint is_scalar = True - cdef np.ndarray ongood, onbad, onsample - cdef int64_t lngood, lnbad, lnsample - - ongood = np.PyArray_FROM_OTF(ngood, np.NPY_INT64, api.NPY_ARRAY_ALIGNED) - onbad = np.PyArray_FROM_OTF(nbad, np.NPY_INT64, api.NPY_ARRAY_ALIGNED) - onsample = np.PyArray_FROM_OTF(nsample, np.NPY_INT64, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(ongood) == np.PyArray_NDIM(onbad) == np.PyArray_NDIM(onsample) == 0: - - lngood = ngood - lnbad = nbad - lnsample = nsample - - if lngood >= HYPERGEOM_MAX or lnbad >= HYPERGEOM_MAX: - raise ValueError("both ngood and nbad must be less than " - "{:d}".format(HYPERGEOM_MAX)) - if lngood + lnbad < lnsample: - raise ValueError("ngood + nbad < nsample") - return disc(&random_hypergeometric, &self._bitgen, size, self.lock, 0, 3, - lngood, "ngood", CONS_NON_NEGATIVE, - lnbad, "nbad", CONS_NON_NEGATIVE, - lnsample, "nsample", CONS_NON_NEGATIVE) - - if np.any(ongood >= HYPERGEOM_MAX) or np.any(onbad >= HYPERGEOM_MAX): - raise ValueError("both ngood and nbad must be less than " - "{:d}".format(HYPERGEOM_MAX)) - if np.any(np.less(np.add(ongood, onbad), onsample)): - raise ValueError("ngood + nbad < nsample") - return discrete_broadcast_iii(&random_hypergeometric, &self._bitgen, size, self.lock, - ongood, "ngood", CONS_NON_NEGATIVE, - onbad, "nbad", CONS_NON_NEGATIVE, - onsample, "nsample", CONS_NON_NEGATIVE) - - def logseries(self, p, size=None): - """ - logseries(p, size=None) - - Draw samples from a logarithmic series distribution. - - Samples are drawn from a log series distribution with specified - shape parameter, 0 < ``p`` < 1. - - Parameters - ---------- - p : float or array_like of floats - Shape parameter for the distribution. Must be in the range (0, 1). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``p`` is a scalar. Otherwise, - ``np.array(p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized logarithmic series distribution. - - See Also - -------- - scipy.stats.logser : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability mass function for the Log Series distribution is - - .. math:: P(k) = \\frac{-p^k}{k \\ln(1-p)}, - - where p = probability. - - The log series distribution is frequently used to represent species - richness and occurrence, first proposed by Fisher, Corbet, and - Williams in 1943 [2]. It may also be used to model the numbers of - occupants seen in cars [3]. - - References - ---------- - .. [1] Buzas, Martin A.; Culver, Stephen J., Understanding regional - species diversity through the log series distribution of - occurrences: BIODIVERSITY RESEARCH Diversity & Distributions, - Volume 5, Number 5, September 1999 , pp. 187-195(9). - .. [2] Fisher, R.A,, A.S. Corbet, and C.B. Williams. 1943. The - relation between the number of species and the number of - individuals in a random sample of an animal population. - Journal of Animal Ecology, 12:42-58. - .. [3] D. J. Hand, F. Daly, D. Lunn, E. Ostrowski, A Handbook of Small - Data Sets, CRC Press, 1994. - .. [4] Wikipedia, "Logarithmic distribution", - https://en.wikipedia.org/wiki/Logarithmic_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a = .6 - >>> s = randomgen.generator.logseries(a, 10000) - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s) - - # plot against distribution - - >>> def logseries(k, p): - ... return -p**k/(k*np.log(1-p)) - >>> plt.plot(bins, logseries(bins, a) * count.max()/ - ... logseries(bins, a).max(), 'r') - >>> plt.show() - - """ - return disc(&random_logseries, &self._bitgen, size, self.lock, 1, 0, - p, "p", CONS_BOUNDED_0_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - - # Multivariate distributions: - def multivariate_normal(self, mean, cov, size=None, check_valid="warn", - tol=1e-8, *, method="svd"): - """ - multivariate_normal(mean, cov, size=None, check_valid='warn', tol=1e-8, *, method='svd') - - Draw random samples from a multivariate normal distribution. - - The multivariate normal, multinormal or Gaussian distribution is a - generalization of the one-dimensional normal distribution to higher - dimensions. Such a distribution is specified by its mean and - covariance matrix. These parameters are analogous to the mean - (average or "center") and variance (standard deviation, or "width," - squared) of the one-dimensional normal distribution. - - Parameters - ---------- - mean : array_like - Mean of the distribution. Must have shape (m1, m2, ..., mk, N) where - (m1, m2, ..., mk) would broadcast with (c1, c2, ..., cj). - cov : array_like - Covariance matrix of the distribution. It must be symmetric and - positive-semidefinite for proper sampling. Must have shape - (c1, c2, ..., cj, N, N) where (c1, c2, ..., cj) would broadcast - with (m1, m2, ..., mk). - size : int or tuple of ints, optional - Given a shape of, for example, ``(m,n,k)``, ``m*n*k`` samples are - generated, and packed in an `m`-by-`n`-by-`k` arrangement. Because - each sample is `N`-dimensional, the output shape is ``(m,n,k,N)``. - If no shape is specified, a single (`N`-D) sample is returned. - check_valid : {'warn', 'raise', 'ignore' }, optional - Behavior when the covariance matrix is not positive semidefinite. - tol : float, optional - Tolerance when checking the singular values in covariance matrix. - cov is cast to double before the check. - method : {'svd', 'eigh', 'cholesky'}, optional - The cov input is used to compute a factor matrix A such that - ``A @ A.T = cov``. This argument is used to select the method - used to compute the factor matrix A. The default method 'svd' is - the slowest, while 'cholesky' is the fastest but less robust than - the slowest method. The method `eigh` uses eigen decomposition to - compute A and is faster than svd but slower than cholesky. - - Returns - ------- - out : ndarray - The drawn samples, of shape determined by broadcasting the - leading dimensions of mean and cov with size, if not None. - The final dimension is always N. - - In other words, each entry ``out[i,j,...,:]`` is an N-dimensional - value drawn from the distribution. - - Notes - ----- - The mean is a coordinate in N-dimensional space, which represents the - location where samples are most likely to be generated. This is - analogous to the peak of the bell curve for the one-dimensional or - univariate normal distribution. - - Covariance indicates the level to which two variables vary together. - From the multivariate normal distribution, we draw N-dimensional - samples, :math:`X = [x_1, x_2, ... x_N]`. The covariance matrix - element :math:`C_{ij}` is the covariance of :math:`x_i` and :math:`x_j`. - The element :math:`C_{ii}` is the variance of :math:`x_i` (i.e. its - "spread"). - - Instead of specifying the full covariance matrix, popular - approximations include: - - - Spherical covariance (`cov` is a multiple of the identity matrix) - - Diagonal covariance (`cov` has non-negative elements, and only on - the diagonal) - - This geometrical property can be seen in two dimensions by plotting - generated data-points: - - >>> mean = [0, 0] - >>> cov = [[1, 0], [0, 100]] # diagonal covariance - - Diagonal covariance means that points are oriented along x or y-axis: - - >>> from randomgen import Generator - >>> rg = Generator() - >>> import matplotlib.pyplot as plt - >>> x, y = rg.multivariate_normal(mean, cov, 5000).T - >>> plt.plot(x, y, 'x') - >>> plt.axis('equal') - >>> plt.show() - - Note that the covariance matrix must be positive semidefinite (a.k.a. - nonnegative-definite). Otherwise, the behavior of this method is - undefined and backwards compatibility is not guaranteed. - - References - ---------- - .. [1] Papoulis, A., "Probability, Random Variables, and Stochastic - Processes," 3rd ed., New York: McGraw-Hill, 1991. - .. [2] Duda, R. O., Hart, P. E., and Stork, D. G., "Pattern - Classification," 2nd ed., New York: Wiley, 2001. - - Examples - -------- - >>> from randomgen import Generator - >>> rg = Generator() - >>> mean = (1, 2) - >>> cov = [[1, 0], [0, 1]] - >>> x = rg.multivariate_normal(mean, cov, (3, 3)) - >>> x.shape - (3, 3, 2) - - The following is probably true, given that 0.6 is roughly twice the - standard deviation: - - >>> list((x[0,0,:] - mean) < 0.6) - [True, True] # random - - """ - if check_valid not in ("warn", "raise", "ignore"): - raise ValueError("check_valid must equal 'warn', 'raise', or 'ignore'") - - mean = np.array(mean) - cov = np.array(cov, dtype=np.double) - if mean.ndim < 1: - raise ValueError("mean must have at least 1 dimension") - if cov.ndim < 2: - raise ValueError("cov must have at least 2 dimensions") - n = mean.shape[mean.ndim - 1] - cov_dim = cov.ndim - if not (cov.shape[cov_dim - 1] == cov.shape[cov_dim - 2] == n): - raise ValueError( - f"The final two dimension of cov " - f"({cov.shape[cov_dim - 1], cov.shape[cov_dim - 2]}) must match " - f"the final dimension of mean ({n}). mean must be 1 dimensional" - ) - - drop_dims = (mean.ndim == 1) and (cov.ndim == 2) - if mean.ndim == 1: - mean = mean.reshape((1, n)) - if cov.ndim == 2: - cov = cov.reshape((1, n, n)) - - _factors = np.empty_like(cov) - for loc in np.ndindex(*cov.shape[:len(cov.shape)-2]): - _factors[loc] = _factorize(cov[loc], method, check_valid, tol, n) - - out_shape = np.broadcast(mean[..., 0], cov[..., 0, 0]).shape - if size is not None: - if isinstance(size, (int, np.integer)): - size = (size,) - error = len(size) < len(out_shape) - final_size = list(size[: -len(out_shape)]) - for s, os in zip(size[-len(out_shape) :], out_shape): - if error or not (s == 1 or os == 1 or s == os): - raise ValueError( - f"The desired out size {size} is not compatible with" - f"the broadcast size of mean and cov {out_shape}. The" - f" final {len(out_shape)} elements of size must be " - f"either 1 or the same as the corresponding element " - f"of the broadcast size" - ) - final_size.append(max(s, os)) - out_shape = tuple(final_size) - - out = self.standard_normal(out_shape + (1, n,)) - prod = np.matmul(out, _factors) - final = mean + np.squeeze(prod, axis=prod.ndim - 2) - if drop_dims and final.shape[0] == 1: - final = final.reshape(final.shape[1:]) - return final - - def multinomial(self, object n, object pvals, size=None): - """ - multinomial(n, pvals, size=None) - - Draw samples from a multinomial distribution. - - The multinomial distribution is a multivariate generalization of the - binomial distribution. Take an experiment with one of ``p`` - possible outcomes. An example of such an experiment is throwing a dice, - where the outcome can be 1 through 6. Each sample drawn from the - distribution represents `n` such experiments. Its values, - ``X_i = [X_0, X_1, ..., X_p]``, represent the number of times the - outcome was ``i``. - - Parameters - ---------- - n : int or array-like of ints - Number of experiments. - pvals : array-like of floats - Probabilities of each of the ``p`` different outcomes with shape - ``(k0, k1, ..., kn, p)``. Each element ``pvals[i,j,...,:]`` must - sum to 1 (however, the last element is always assumed to account - for the remaining probability, as long as - ``sum(pvals[..., :-1], axis=-1) <= 1.0``. Must have at least 1 - dimension where pvals.shape[-1] > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn each with ``p`` elements. Default - is None where the output size is determined by the broadcast shape - of ``n`` and all by the final dimension of ``pvals``, which is - denoted as ``b=(b0, b1, ..., bq)`` be this size. If size is not None, - then it must be compatible with the broadcast shape ``b``. - Specifically, size must have ``q`` or more elements and - size[-(q-j):] must equal ``bj``. - - Returns - ------- - out : ndarray - The drawn samples, of shape size, if provided. When size is - provided, the output shape is size + (p,) If not specified, - the shape is determined by the broadcast shape of ``n`` and - ``pvals``, ``(b0, b1, ..., bq)`` augmented with the dimension of - the multinomial, ``p``, so that that output shape is - ``(b0, b1, ..., bq, p)``. - - Each entry ``out[i,j,...,:]`` is a ``p``-dimensional value drawn - from the distribution. - - Examples - -------- - Throw a dice 20 times: - - >>> rng = np.random.default_rng() - >>> rng.multinomial(20, [1/6.]*6, size=1) - array([[4, 1, 7, 5, 2, 1]]) # random - - It landed 4 times on 1, once on 2, etc. - - Now, throw the dice 20 times, and 20 times again: - - >>> rng.multinomial(20, [1/6.]*6, size=2) - array([[3, 4, 3, 3, 4, 3], - [2, 4, 3, 4, 0, 7]]) # random - - For the first run, we threw 3 times 1, 4 times 2, etc. For the second, - we threw 2 times 1, 4 times 2, etc. - - Now, do one experiment throwing the dice 10 time, and 10 times again, - and another throwing the dice 20 times, and 20 times again: - - >>> rng.multinomial([[10], [20]], [1/6.]*6, size=(2, 2)) - array([[[2, 4, 0, 1, 2, 1], - [1, 3, 0, 3, 1, 2]], - [[1, 4, 4, 4, 4, 3], - [3, 3, 2, 5, 5, 2]]]) # random - - The first array shows the outcomes of throwing the dice 10 times, and - the second shows the outcomes from throwing the dice 20 times. - - A loaded die is more likely to land on number 6: - - >>> rng.multinomial(100, [1/7.]*5 + [2/7.]) - array([11, 16, 14, 17, 16, 26]) # random - - Simulate 10 throws of a 4-sided die and 20 throws of a 6-sided die - - >>> rng.multinomial([10, 20],[[1/4]*4 + [0]*2, [1/6]*6]) - array([[2, 1, 4, 3, 0, 0], - [3, 3, 3, 6, 1, 4]], dtype=int64) # random - - Generate categorical random variates from two categories where the - first has 3 outcomes and the second has 2. - - >>> rng.multinomial(1, [[.1, .5, .4 ], [.3, .7, .0]]) - array([[0, 0, 1], - [0, 1, 0]], dtype=int64) # random - - ``argmax(axis=-1)`` is then used to return the categories. - - >>> pvals = [[.1, .5, .4 ], [.3, .7, .0]] - >>> rvs = rng.multinomial(1, pvals, size=(4,2)) - >>> rvs.argmax(axis=-1) - array([[0, 1], - [2, 0], - [2, 1], - [2, 0]], dtype=int64) # random - - The same output dimension can be produced using broadcasting. - - >>> rvs = rng.multinomial([[1]] * 4, pvals) - >>> rvs.argmax(axis=-1) - array([[0, 1], - [2, 0], - [2, 1], - [2, 0]], dtype=int64) # random - - The probability inputs should be normalized. As an implementation - detail, the value of the last entry is ignored and assumed to take - up any leftover probability mass, but this should not be relied on. - A biased coin which has twice as much weight on one side as on the - other should be sampled like so: - - >>> rng.multinomial(100, [1.0 / 3, 2.0 / 3]) # RIGHT - array([38, 62]) # random - - not like: - - >>> rng.multinomial(100, [1.0, 2.0]) # WRONG - Traceback (most recent call last): - ValueError: pvals < 0, pvals > 1 or pvals contains NaNs - """ - - cdef np.npy_intp d, i, sz, offset, pi - cdef np.ndarray parr, mnarr, on, temp_arr - cdef double *pix - cdef int ndim - cdef int64_t *mnix - cdef int64_t ni - cdef np.broadcast it - on = np.PyArray_FROM_OTF(n, - np.NPY_INT64, - np.NPY_ARRAY_ALIGNED | - np.NPY_ARRAY_C_CONTIGUOUS) - parr = np.PyArray_FROM_OTF(pvals, - np.NPY_DOUBLE, - np.NPY_ARRAY_ALIGNED | - np.NPY_ARRAY_C_CONTIGUOUS) - ndim = np.PyArray_NDIM(parr) - d = np.PyArray_DIMS(parr)[ndim - 1] if ndim >= 1 else 0 - if d == 0: - raise ValueError("pvals must have at least 1 dimension with shape[-1] > 0.") - - check_array_constraint(parr, 'pvals', CONS_BOUNDED_0_1) - pix = np.PyArray_DATA(parr) - sz = np.PyArray_SIZE(parr) - # Cython 0.29.20 would not correctly translate the range-based for - # loop to a C for loop - # for offset in range(0, sz, d): - offset = 0 - while offset < sz: - if kahan_sum(pix + offset, d-1) > (1.0 + 1e-12): - if ndim == 1: - msg = "sum(pvals[:-1]) > 1.0" - else: - msg = "At least one element of sum(pvals[..., :-1], axis=-1) > 1.0" - raise ValueError(msg) - offset += d - - if np.PyArray_NDIM(on) != 0 or ndim > 1: # vector - check_array_constraint(on, 'n', CONS_NON_NEGATIVE) - # This provides the offsets to use in the C-contig parr when - # broadcasting - offsets = np.arange( - 0, np.PyArray_SIZE(parr), d, dtype=np.intp - ).reshape((parr).shape[:ndim - 1]) - if size is None: - it = np.PyArray_MultiIterNew2(on, offsets) - else: - temp = np.empty(size, dtype=np.int8) - temp_arr = temp - it = np.PyArray_MultiIterNew3(on, offsets, temp_arr) - # Validate size and the broadcast shape - try: - size = (operator.index(size),) - except: - size = tuple(size) - # This test verifies that an axis with dim 1 in size has not - # been increased by broadcasting with the input - if it.shape != size: - raise ValueError( - f"Output size {size} is not compatible with " - f"broadcast dimensions of inputs {it.shape}." - ) - shape = it.shape + (d,) - multin = np.zeros(shape, dtype=np.int64) - mnarr = multin - mnix = np.PyArray_DATA(mnarr) - offset = 0 - sz = it.size - with self.lock, nogil: - for i in range(sz): - ni = (np.PyArray_MultiIter_DATA(it, 0))[0] - pi = (np.PyArray_MultiIter_DATA(it, 1))[0] - random_multinomial(&self._bitgen, ni, &mnix[offset], &pix[pi], d, &self._binomial) - offset += d - np.PyArray_MultiIter_NEXT(it) - return multin - - if size is None: - shape = (d,) - else: - try: - shape = (operator.index(size), d) - except: - shape = tuple(size) + (d,) - - multin = np.zeros(shape, dtype=np.int64) - mnarr = multin - mnix = np.PyArray_DATA(mnarr) - sz = np.PyArray_SIZE(mnarr) - ni = n - check_constraint(ni, 'n', CONS_NON_NEGATIVE) - offset = 0 - with self.lock, nogil: - for i in range(sz // d): - random_multinomial(&self._bitgen, ni, &mnix[offset], pix, d, &self._binomial) - offset += d - - return multin - - def dirichlet(self, object alpha, size=None): - """ - dirichlet(alpha, size=None) - - Draw samples from the Dirichlet distribution. - - Draw `size` samples of dimension k from a Dirichlet distribution. A - Dirichlet-distributed random variable can be seen as a multivariate - generalization of a Beta distribution. The Dirichlet distribution - is a conjugate prior of a multinomial distribution in Bayesian - inference. - - Parameters - ---------- - alpha : sequence of floats, length k - Parameter of the distribution (length ``k`` for sample of - length ``k``). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - vector of length ``k`` is returned. - - Returns - ------- - samples : ndarray, - The drawn samples, of shape ``(size, k)``. - - Raises - ------ - ValueError - If any value in ``alpha`` is less than or equal to zero. - - Notes - ----- - The Dirichlet distribution is a distribution over vectors - :math:`x` that fulfil the conditions :math:`x_i>0` and - :math:`\\sum_{i=1}^k x_i = 1`. - - The probability density function :math:`p` of a - Dirichlet-distributed random vector :math:`X` is - proportional to - - .. math:: p(x) \\propto \\prod_{i=1}^{k}{x^{\\alpha_i-1}_i}, - - where :math:`\\alpha` is a vector containing the positive - concentration parameters. - - The method uses the following property for computation: let :math:`Y` - be a random vector which has components that follow a standard gamma - distribution, then :math:`X = \\frac{1}{\\sum_{i=1}^k{Y_i}} Y` - is Dirichlet-distributed - - References - ---------- - .. [1] David McKay, "Information Theory, Inference and Learning - Algorithms," chapter 23, - http://www.inference.org.uk/mackay/itila/ - .. [2] Wikipedia, "Dirichlet distribution", - https://en.wikipedia.org/wiki/Dirichlet_distribution - - Examples - -------- - Taking an example cited in Wikipedia, this distribution can be used if - one wanted to cut strings (each of initial length 1.0) into K pieces - with different lengths, where each piece had, on average, a designated - average length, but allowing some variation in the relative sizes of - the pieces. - - >>> s = randomgen.generator.dirichlet((10, 5, 3), 20).transpose() - - >>> import matplotlib.pyplot as plt - >>> plt.barh(range(20), s[0]) - >>> plt.barh(range(20), s[1], left=s[0], color='g') - >>> plt.barh(range(20), s[2], left=s[0]+s[1], color='r') - >>> plt.title("Lengths of Strings") - - """ - - # ================= - # Pure python algo - # ================= - # alpha = N.atleast_1d(alpha) - # k = alpha.size - - # if n == 1: - # val = N.zeros(k) - # for i in range(k): - # val[i] = sgamma(alpha[i], n) - # val /= N.sum(val) - # else: - # val = N.zeros((k, n)) - # for i in range(k): - # val[i] = sgamma(alpha[i], n) - # val /= N.sum(val, axis = 0) - # val = val.T - # return val - - cdef np.npy_intp k, totsize, i, j - cdef np.ndarray alpha_arr, val_arr - cdef double *alpha_data - cdef double *val_data - cdef double acc, invacc - - k = len(alpha) - alpha_arr = np.PyArray_FROMANY(alpha, np.NPY_DOUBLE, 1, 1, np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS) - if np.any(np.less_equal(alpha_arr, 0)): - raise ValueError("alpha <= 0") - alpha_data = np.PyArray_DATA(alpha_arr) - - if size is None: - shape = (k,) - else: - try: - shape = (operator.index(size), k) - except TypeError: - shape = tuple(size) + (k,) - - diric = np.zeros(shape, np.float64) - val_arr = diric - val_data= np.PyArray_DATA(val_arr) - - i = 0 - totsize = np.PyArray_SIZE(val_arr) - - # Select one of the following two algorithms for the generation - # of Dirichlet random variates (RVs) - # - # A) Small alpha case: Use the stick-breaking approach with beta - # random variates (RVs). - # B) Standard case: Perform unit normalisation of a vector - # of gamma random variates - # - # A) prevents NaNs resulting from 0/0 that may occur in B) - # when all values in the vector ':math:\\alpha' are smaller - # than 1, then there is a nonzero probability that all - # generated gamma RVs will be 0. When that happens, the - # normalization process ends up computing 0/0, giving nan. A) - # does not use divisions, so that a situation in which 0/0 has - # to be computed cannot occur. A) is slower than B) as - # generation of beta RVs is slower than generation of gamma - # RVs. A) is selected whenever `alpha.max() < t`, where `t < - # 1` is a threshold that controls the probability of - # generating a NaN value when B) is used. For a given - # threshold `t` this probability can be bounded by - # `gammainc(t, d)` where `gammainc` is the regularized - # incomplete gamma function and `d` is the smallest positive - # floating point number that can be represented with a given - # precision. For the chosen threshold `t=0.1` this probability - # is smaller than `1.8e-31` for double precision floating - # point numbers. - - if (k > 0) and (alpha_arr.max() < 0.1): - # Small alpha case: Use stick-breaking approach with beta - # random variates (RVs). - # alpha_csum_data will hold the cumulative sum, right to - # left, of alpha_arr. - # Use a numpy array for memory management only. We could just as - # well have malloc'd alpha_csum_data. alpha_arr is a C-contiguous - # double array, therefore so is alpha_csum_arr. - alpha_csum_arr = np.empty_like(alpha_arr) - alpha_csum_data = np.PyArray_DATA(alpha_csum_arr) - csum = 0.0 - for j in range(k - 1, -1, -1): - csum += alpha_data[j] - alpha_csum_data[j] = csum - - with self.lock, nogil: - while i < totsize: - acc = 1. - for j in range((k - 1)): - v = random_beta(&self._bitgen, alpha_data[j], - alpha_csum_data[j + 1]) - val_data[i + j] = acc * v - acc *= (1. - v) - val_data[i + k - 1] = acc - i = i + k - - else: - # Standard case: Unit normalisation of a vector of gamma random - # variates - with self.lock, nogil: - while i < totsize: - acc = 0. - for j in range(k): - val_data[i + j] = random_standard_gamma_zig(&self._bitgen, - alpha_data[j]) - acc = acc + val_data[i + j] - invacc = 1. / acc - for j in range(k): - val_data[i + j] = val_data[i + j] * invacc - i = i + k - - return diric - - # Shuffling and permutations: - def shuffle(self, object x): - """ - shuffle(x) - - Modify an array or a mutable sequence in-place by shuffling its contents. - - This function only shuffles the array along the first axis of a - multi-dimensional array. The order of sub-arrays is changed but - their contents remains the same. - - Parameters - ---------- - x : ndarray or MutableSequence - The array, list or mutable sequence to be shuffled. - - Returns - ------- - None - - Examples - -------- - >>> arr = np.arange(10) - >>> randomgen.generator.shuffle(arr) - >>> arr - [1 7 5 2 9 4 3 6 0 8] # random - - Multi-dimensional arrays are only shuffled along the first axis: - - >>> arr = np.arange(9).reshape((3, 3)) - >>> randomgen.generator.shuffle(arr) - >>> arr - array([[3, 4, 5], # random - [6, 7, 8], - [0, 1, 2]]) - - """ - cdef: - np.npy_intp i, j, n = len(x), stride, itemsize - char* x_ptr - char* buf_ptr - - if type(x) is np.ndarray and x.ndim == 1 and x.size: - # Fast, statically typed path: shuffle the underlying buffer. - # Only for non-empty, 1d objects of class ndarray (subclasses such - # as MaskedArrays may not support this approach). - x_ptr = np.PyArray_DATA(x) - stride = x.strides[0] - itemsize = x.dtype.itemsize - # As the array x could contain python objects we use a buffer - # of bytes for the swaps to avoid leaving one of the objects - # within the buffer and erroneously decrementing it's refcount - # when the function exits. - buf = np.empty(itemsize, dtype=np.int8) # GC'd at function exit - buf_ptr = np.PyArray_DATA(buf) - with self.lock: - # We trick gcc into providing a specialized implementation for - # the most common case, yielding a ~33% performance improvement. - # Note that apparently, only one branch can ever be specialized. - if itemsize == sizeof(np.npy_intp): - self._shuffle_raw(n, 1, sizeof(np.npy_intp), stride, x_ptr, buf_ptr) - else: - self._shuffle_raw(n, 1, itemsize, stride, x_ptr, buf_ptr) - elif isinstance(x, np.ndarray) and x.ndim and x.size: - buf = np.empty_like(x[0, ...]) - with self.lock: - for i in reversed(range(1, n)): - j = random_interval(&self._bitgen, i) - if i == j: - # i == j is not needed and memcpy is undefined. - continue - buf[...] = x[j, ...] - x[j, ...] = x[i, ...] - x[i, ...] = buf - else: - # Untyped path. - if not isinstance(x, (np.ndarray, MutableSequence)): - # See gh-18206. We may decide to deprecate here in the future. - warnings.warn( - "`x` isn't a recognized object; `shuffle` is not guaranteed " - "to behave correctly. E.g., non-numpy array/tensor objects " - "with view semantics may contain duplicates after shuffling." - ) - - with self.lock: - for i in reversed(range(1, n)): - j = random_interval(&self._bitgen, i) - x[i], x[j] = x[j], x[i] - - cdef inline _shuffle_raw(self, np.npy_intp n, np.npy_intp first, - np.npy_intp itemsize, np.npy_intp stride, - char* data, char* buf): - """ - Parameters - ---------- - n - Number of elements in data - first - First observation to shuffle. Shuffles n-1, - n-2, ..., first, so that when first=1 the entire - array is shuffled - itemsize - Size in bytes of item - stride - Array stride - data - Location of data - buf - Location of buffer (itemsize) - """ - cdef np.npy_intp i, j - for i in reversed(range(first, n)): - j = random_interval(&self._bitgen, i) - string.memcpy(buf, data + j * stride, itemsize) - string.memcpy(data + j * stride, data + i * stride, itemsize) - string.memcpy(data + i * stride, buf, itemsize) - - cdef inline void _shuffle_int(self, np.npy_intp n, np.npy_intp first, - int64_t* data) nogil: - """ - Parameters - ---------- - n - Number of elements in data - first - First observation to shuffle. Shuffles n-1, - n-2, ..., first, so that when first=1 the entire - array is shuffled - data - Location of data - """ - cdef np.npy_intp i, j - cdef int64_t temp - for i in reversed(range(first, n)): - j = random_bounded_uint64(&self._bitgen, 0, i, 0, 0) - temp = data[j] - data[j] = data[i] - data[i] = temp - - def permutation(self, object x): - """ - permutation(x) - - Randomly permute a sequence, or return a permuted range. - - If `x` is a multi-dimensional array, it is only shuffled along its - first index. - - Parameters - ---------- - x : int or array_like - If `x` is an integer, randomly permute ``np.arange(x)``. - If `x` is an array, make a copy and shuffle the elements - randomly. - - Returns - ------- - out : ndarray - Permuted sequence or array range. - - Examples - -------- - >>> from randomgen import Generator - >>> gen = Generator() - >>> gen.permutation(10) - array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random - - >>> gen.permutation([1, 4, 9, 12, 15]) - array([15, 1, 9, 4, 12]) # random - - >>> arr = np.arange(9).reshape((3, 3)) - >>> gen.permutation(arr) - array([[6, 7, 8], # random - [0, 1, 2], - [3, 4, 5]]) - - """ - if isinstance(x, (int, np.integer)): - arr = np.arange(x) - self.shuffle(arr) - return arr - - arr = np.asarray(x) - if arr.ndim < 1: - raise IndexError("x must be an integer or at least 1-dimensional") - # shuffle has fast-path for 1-d - if arr.ndim == 1: - # Return a copy if same memory - if np.may_share_memory(arr, x): - arr = np.array(arr) - self.shuffle(arr) - return arr - - # Shuffle index array, dtype to ensure fast path - idx = np.arange(arr.shape[0], dtype=np.intp) - self.shuffle(idx) - return arr[idx] - - def complex_normal(self, loc=0.0, gamma=1.0, relation=0.0, size=None): - """ - complex_normal(loc=0.0, gamma=1.0, relation=0.0, size=None) - - Draw random samples from a complex normal (Gaussian) distribution. - - Parameters - ---------- - loc : complex or array_like of complex - Mean of the distribution. - gamma : float, complex or array_like of float or complex - Variance of the distribution - relation : float, complex or array_like of float or complex - Relation between the two component normals - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc``, ``gamma`` and ``relation`` - are all scalars. Otherwise, - ``np.broadcast(loc, gamma, relation).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized complex normal distribution. - - See Also - -------- - randomgen.generator.normal : random values from a real-valued normal - distribution - - Notes - ----- - **EXPERIMENTAL** Not part of official NumPy RandomState, may change until - formal release on PyPi. - - Complex normals are generated from a bivariate normal where the - variance of the real component is 0.5 Re(gamma + relation), the - variance of the imaginary component is 0.5 Re(gamma - relation), and - the covariance between the two is 0.5 Im(relation). The implied - covariance matrix must be positive semi-definite and so both variances - must be zero and the covariance must be weakly smaller than the - product of the two standard deviations. - - References - ---------- - .. [1] Wikipedia, "Complex normal distribution", - https://en.wikipedia.org/wiki/Complex_normal_distribution - .. [2] Leigh J. Halliwell, "Complex Random Variables" in "Casualty - Actuarial Society E-Forum", Fall 2015. - - Examples - -------- - Draw samples from the distribution: +from randomgen.bounded_integers cimport * +from randomgen.common cimport * +from randomgen.distributions cimport * +from randomgen cimport api +from typing import Callable, Any - >>> s = randomgen.generator.complex_normal(size=1000) +__all__ = ["Generator", "beta", "binomial", "bytes", "chisquare", "choice", + "complex_normal", "dirichlet", "exponential", "f", "gamma", + "geometric", "gumbel", "hypergeometric", "integers", "laplace", + "logistic", "lognormal", "logseries", "multinomial", + "multivariate_normal", "negative_binomial", "noncentral_chisquare", + "noncentral_f", "normal", "pareto", "permutation", + "poisson", "power", "randint", "random", "rayleigh", "shuffle", + "standard_cauchy", "standard_exponential", "standard_gamma", + "standard_normal", "standard_t", "triangular", + "uniform", "vonmises", "wald", "weibull", "zipf", "ExtendedGenerator"] - """ - cdef np.ndarray ogamma, orelation, oloc, randoms, v_real, v_imag, rho - cdef double *randoms_data - cdef double fgamma_r, fgamma_i, frelation_r, frelation_i, frho, \ - fvar_r, fvar_i, floc_r, floc_i, f_real, f_imag, f_rho - cdef np.npy_intp i, j, n, n2 - cdef np.broadcast it +np.import_array() - oloc = np.PyArray_FROM_OTF(loc, np.NPY_COMPLEX128, api.NPY_ARRAY_ALIGNED) - ogamma = np.PyArray_FROM_OTF(gamma, np.NPY_COMPLEX128, api.NPY_ARRAY_ALIGNED) - orelation = np.PyArray_FROM_OTF(relation, np.NPY_COMPLEX128, api.NPY_ARRAY_ALIGNED) +cdef object broadcast_shape(tuple x, tuple y, bint strict): + cdef bint cond, bcast=True + if x == () or y == (): + if len(x) > len(y): + return True, x + return True, y + lx = len(x) + ly = len(y) + if lx > ly: + shape = list(x[:lx-ly]) + x = x[lx-ly:] + else: + shape = list(y[:ly-lx]) + y = y[ly-lx:] + for xs, ys in zip(x, y): + cond = xs == ys + if not strict: + cond |= min(xs, ys) == 1 + bcast &= cond + if not bcast: + break + shape.append(max(xs, ys)) + return bcast, tuple(shape) - if np.PyArray_NDIM(ogamma) == np.PyArray_NDIM(orelation) == np.PyArray_NDIM(oloc) == 0: - floc_r = PyComplex_RealAsDouble(loc) - floc_i = PyComplex_ImagAsDouble(loc) - fgamma_r = PyComplex_RealAsDouble(gamma) - fgamma_i = PyComplex_ImagAsDouble(gamma) - frelation_r = PyComplex_RealAsDouble(relation) - frelation_i = 0.5 * PyComplex_ImagAsDouble(relation) - fvar_r = 0.5 * (fgamma_r + frelation_r) - fvar_i = 0.5 * (fgamma_r - frelation_r) - if fgamma_i != 0: - raise ValueError("Im(gamma) != 0") - if fvar_i < 0: - raise ValueError("Re(gamma - relation) < 0") - if fvar_r < 0: - raise ValueError("Re(gamma + relation) < 0") - f_rho = 0.0 - if fvar_i > 0 and fvar_r > 0: - f_rho = frelation_i / sqrt(fvar_i * fvar_r) - if f_rho > 1.0 or f_rho < -1.0: - raise ValueError("Im(relation) ** 2 > Re(gamma ** 2 - relation** 2)") +cdef _factorize(cov, meth, check_valid, tol, rank): + if meth == "svd": + from numpy.linalg import svd - if size is None: - f_real = random_gauss_zig(&self._bitgen) - f_imag = random_gauss_zig(&self._bitgen) + (u, s, vh) = svd(cov) + if rank < cov.shape[0]: + locs = np.argsort(s) + s[locs[:s.shape[0]-rank]] = 0.0 + psd = np.allclose(np.dot(vh.T * s, vh), cov, rtol=tol, atol=tol) + _factor = (u * np.sqrt(s)).T + elif meth == "factor": + return cov + elif meth == "eigh": + from numpy.linalg import eigh - compute_complex(&f_real, &f_imag, floc_r, floc_i, fvar_r, - fvar_i, f_rho) - return PyComplex_FromDoubles(f_real, f_imag) + # could call linalg.svd(hermitian=True), but that calculates a + # vh we don't need + (s, u) = eigh(cov) + if rank < cov.shape[0]: + locs = np.argsort(s) + s[locs[:s.shape[0]-rank]] = 0.0 + psd = not np.any(s < -tol) + _factor = (u * np.sqrt(abs(s))).T + else: + if rank == cov.shape[0]: + from numpy.linalg import cholesky - randoms = np.empty(size, np.complex128) - randoms_data = np.PyArray_DATA(randoms) - n = np.PyArray_SIZE(randoms) + _factor = cholesky(cov).T + psd = True + else: + try: + from scipy.linalg import get_lapack_funcs + except ImportError: + raise ImportError( + "SciPy is required when using Cholesky factorization with " + "reduced rank covariance." + ) - j = 0 - with self.lock, nogil: - for i in range(n): - f_real = random_gauss_zig(&self._bitgen) - f_imag = random_gauss_zig(&self._bitgen) - compute_complex(&f_real, &f_imag, floc_r, floc_i, fvar_r, - fvar_i, f_rho) - randoms_data[j] = f_real - randoms_data[j+1] = f_imag - j += 2 + func = get_lapack_funcs("pstrf") + _factor, _, rank_c, _ = func(cov) + _factor = np.triu(_factor) + psd = rank_c >= rank - return randoms + if not psd and check_valid != "ignore": + if rank < cov.shape[0]: + msg = f"The {rank} is less than the minimum required rank." + else: + msg = "The covariance is not positive-semidefinite." + if check_valid == "warn": + warnings.warn(msg, RuntimeWarning) + else: + raise ValueError(msg) + return _factor - gpc = ogamma + orelation - gmc = ogamma - orelation - v_real = (0.5 * np.real(gpc)) - if np.any(np.less(v_real, 0)): - raise ValueError("Re(gamma + relation) < 0") - v_imag = (0.5 * np.real(gmc)) - if np.any(np.less(v_imag, 0)): - raise ValueError("Re(gamma - relation) < 0") - if np.any(np.not_equal(np.imag(ogamma), 0)): - raise ValueError("Im(gamma) != 0") - cov = 0.5 * np.imag(orelation) - rho = np.zeros_like(cov) - idx = (v_real.flat > 0) & (v_imag.flat > 0) - rho.flat[idx] = cov.flat[idx] / np.sqrt(v_real.flat[idx] * v_imag.flat[idx]) - if np.any(cov.flat[~idx] != 0) or np.any(np.abs(rho) > 1): - raise ValueError("Im(relation) ** 2 > Re(gamma ** 2 - relation ** 2)") +cdef class Generator: + """ + Generator(bit_generator=None) - if size is not None: - randoms = np.empty(size, np.complex128) - else: - it = np.PyArray_MultiIterNew4(oloc, v_real, v_imag, rho) - randoms = np.empty(it.shape, np.complex128) + Generator has been removed in the 1.23 release. - randoms_data = np.PyArray_DATA(randoms) - n = np.PyArray_SIZE(randoms) + Use ``numpy.random.Generator``. Unique features of Generator + have been moved to randomgen.generator.ExtendedGenerator. - it = np.PyArray_MultiIterNew5(randoms, oloc, v_real, v_imag, rho) - validate_output_shape(it.shape, randoms) - with self.lock, nogil: - n2 = 2 * n # Avoid compiler noise for cast - for i in range(n2): - randoms_data[i] = random_gauss_zig(&self._bitgen) - with nogil: - j = 0 - for i in range(n): - floc_r= (np.PyArray_MultiIter_DATA(it, 1))[0] - floc_i= (np.PyArray_MultiIter_DATA(it, 1))[1] - fvar_r = (np.PyArray_MultiIter_DATA(it, 2))[0] - fvar_i = (np.PyArray_MultiIter_DATA(it, 3))[0] - f_rho = (np.PyArray_MultiIter_DATA(it, 4))[0] - compute_complex(&randoms_data[j], &randoms_data[j+1], floc_r, - floc_i, fvar_r, fvar_i, f_rho) - j += 2 - np.PyArray_MultiIter_NEXT(it) + See Also + -------- + numpy.random.Generator + numpy.random.default_rng + ExtendedGenerator + """ - return randoms + def __init__(self, bit_generator=None): + raise NotImplementedError("""\ +Generator has been deprecated removed in the 1.23 release. +Use ``numpy.random.Generator``. Unique features of Generator +have been moved to randomgen.generator.ExtendedGenerator. +""") cdef class ExtendedGenerator: """ @@ -5909,55 +1243,60 @@ and the trailing dimensions must match exactly so that ) return out.view(complex) -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - _random_generator = Generator() - -beta = _random_generator.beta -binomial = _random_generator.binomial -bytes = _random_generator.bytes -chisquare = _random_generator.chisquare -choice = _random_generator.choice -complex_normal = _random_generator.complex_normal -dirichlet = _random_generator.dirichlet -exponential = _random_generator.exponential -f = _random_generator.f -gamma = _random_generator.gamma -geometric = _random_generator.geometric -gumbel = _random_generator.gumbel -hypergeometric = _random_generator.hypergeometric -integers = _random_generator.integers -laplace = _random_generator.laplace -logistic = _random_generator.logistic -lognormal = _random_generator.lognormal -logseries = _random_generator.logseries -multinomial = _random_generator.multinomial -multivariate_normal = _random_generator.multivariate_normal -negative_binomial = _random_generator.negative_binomial -noncentral_chisquare = _random_generator.noncentral_chisquare -noncentral_f = _random_generator.noncentral_f -normal = _random_generator.normal -pareto = _random_generator.pareto -permutation = _random_generator.permutation -poisson = _random_generator.poisson -power = _random_generator.power -rand = _random_generator.rand -randint = _random_generator.randint -randn = _random_generator.randn -random_integers = _random_generator.random_integers -random_sample = _random_generator.random_sample -random = _random_generator.random -rayleigh = _random_generator.rayleigh -shuffle = _random_generator.shuffle -standard_cauchy = _random_generator.standard_cauchy -standard_exponential = _random_generator.standard_exponential -standard_gamma = _random_generator.standard_gamma -standard_normal = _random_generator.standard_normal -standard_t = _random_generator.standard_t -tomaxint = _random_generator.tomaxint -triangular = _random_generator.triangular -uniform = _random_generator.uniform -vonmises = _random_generator.vonmises -wald = _random_generator.wald -weibull = _random_generator.weibull -zipf = _random_generator.zipf + +def _removed(name: str) -> Callable[[Any,...],None]: + def f(*args, **kwargs): + raise NotImplementedError( + f"{name} has been removed. Use NumPy's Generator" + ) + return f + + +beta = _removed("beta") +binomial = _removed("binomial") +bytes = _removed("bytes") +chisquare = _removed("chisquare") +choice = _removed("choice") +complex_normal = _removed("complex_normal") +dirichlet = _removed("dirichlet") +exponential = _removed("exponential") +f = _removed("f") +gamma = _removed("gamma") +geometric = _removed("geometric") +gumbel = _removed("gumbel") +hypergeometric = _removed("hypergeometric") +integers = _removed("integers") +laplace = _removed("laplace") +logistic = _removed("logistic") +lognormal = _removed("lognormal") +logseries = _removed("logseries") +multinomial = _removed("multinomial") +multivariate_normal = _removed("multivariate_normal") +negative_binomial = _removed("negative_binomial") +noncentral_chisquare = _removed("noncentral_chisquare") +noncentral_f = _removed("noncentral_f") +normal = _removed("normal") +pareto = _removed("pareto") +permutation = _removed("permutation") +poisson = _removed("poisson") +power = _removed("power") +rand = _removed("rand") +randint = _removed("randint") +randn = _removed("randn") +random_integers = _removed("random_integers") +random_sample = _removed("random_sample") +random = _removed("random") +rayleigh = _removed("rayleigh") +shuffle = _removed("shuffle") +standard_cauchy = _removed("standard_cauchy") +standard_exponential = _removed("standard_exponential") +standard_gamma = _removed("standard_gamma") +standard_normal = _removed("standard_normal") +standard_t = _removed("standard_t") +tomaxint = _removed("tomaxint") +triangular = _removed("triangular") +uniform = _removed("uniform") +vonmises = _removed("vonmises") +wald = _removed("wald") +weibull = _removed("weibull") +zipf = _removed("zipf") diff --git a/randomgen/hc128.pyx b/randomgen/hc128.pyx index a8519f4c5..33a25ef81 100644 --- a/randomgen/hc128.pyx +++ b/randomgen/hc128.pyx @@ -1,3 +1,5 @@ +#!python +#cython: binding=True # coding=utf-8 import numpy as np cimport numpy as np diff --git a/randomgen/jsf.pyx b/randomgen/jsf.pyx index 49f44e160..d438a307b 100644 --- a/randomgen/jsf.pyx +++ b/randomgen/jsf.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/lxm.pyx b/randomgen/lxm.pyx index 849a2f415..398949155 100644 --- a/randomgen/lxm.pyx +++ b/randomgen/lxm.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + # coding=utf-8 import numpy as np cimport numpy as np diff --git a/randomgen/mt19937.pyx b/randomgen/mt19937.pyx index d04c418f3..ff6b395dc 100644 --- a/randomgen/mt19937.pyx +++ b/randomgen/mt19937.pyx @@ -1,4 +1,7 @@ +#!python +#cython: binding=True # coding=utf-8 + import operator import numpy as np diff --git a/randomgen/mt64.pyx b/randomgen/mt64.pyx index 9efb27d44..5b60e3acc 100644 --- a/randomgen/mt64.pyx +++ b/randomgen/mt64.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + # coding=utf-8 import operator diff --git a/randomgen/mtrand.pyi b/randomgen/mtrand.pyi index 5257599dd..32fe50a2a 100644 --- a/randomgen/mtrand.pyi +++ b/randomgen/mtrand.pyi @@ -1,246 +1,59 @@ -from threading import Lock -from typing import Any, Dict, Optional, Sequence, Tuple, Union - -from numpy import ndarray +from typing import Any, Optional from randomgen.common import BitGenerator -from randomgen.typing import Size class RandomState: - - _bit_generator: BitGenerator - lock: Lock - _poisson_lam_max: int def __init__(self, bit_generator: Optional[BitGenerator] = ...) -> None: ... - def seed(self, *args: Any, **kwargs: Any) -> None: ... - def get_state( - self, legacy: bool = ... - ) -> Union[Tuple[Any, ...], Dict[str, Any]]: ... - def set_state(self, state: Dict[str, Any]) -> None: ... - def random_sample(self, size: Size = ...) -> Union[float, ndarray]: ... - def beta( - self, a: Union[float, ndarray], b: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def exponential( - self, scale: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[float, ndarray]: ... - def standard_exponential(self, size: Size = ...) -> Union[float, ndarray]: ... - def tomaxint(self, size: Size = ...) -> Union[int, ndarray]: ... - def randint( - self, - low: Union[int, ndarray], - high: Optional[Union[int, ndarray]] = ..., - size: Size = ..., - dtype: str = ..., - ) -> Union[int, ndarray]: ... - def bytes(self, length: int) -> ndarray: ... - def choice( - self, - a: Union[int, Sequence[Any]], - size: Size = ..., - replace: Optional[bool] = ..., - p: Optional[ndarray] = ..., - ) -> Union[Any, Sequence[Any]]: ... - def uniform( - self, - low: Optional[Union[float, ndarray]] = ..., - high: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def rand(self, *args: Tuple[int, ...]) -> ndarray: ... - def randn(self, *args: Tuple[int, ...]) -> ndarray: ... - def random_integers( - self, - low: Union[int, ndarray], - high: Optional[Union[int, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def standard_normal(self, size: Size = ...) -> Union[float, ndarray]: ... - def normal( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def standard_gamma( - self, shape: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def gamma( - self, - shape: Union[float, ndarray], - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def f( - self, - dfnum: Union[float, ndarray], - dfden: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def noncentral_f( - self, - dfnum: Union[float, ndarray], - dfden: Union[float, ndarray], - nonc: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def chisquare( - self, df: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def noncentral_chisquare( - self, df: Union[float, ndarray], nonc: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def standard_cauchy(self, size: Size = ...) -> Union[float, ndarray]: ... - def standard_t( - self, df: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def vonmises( - self, mu: Union[float, ndarray], kappa: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def pareto( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def weibull( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def power( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[float, ndarray]: ... - def laplace( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def gumbel( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def logistic( - self, - loc: Optional[Union[float, ndarray]] = ..., - scale: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def lognormal( - self, - mean: Optional[Union[float, ndarray]] = ..., - sigma: Optional[Union[float, ndarray]] = ..., - size: Size = ..., - ) -> Union[float, ndarray]: ... - def rayleigh( - self, scale: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[float, ndarray]: ... - def wald( - self, - mean: Union[float, ndarray], - scale: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - def triangular( - self, - left: Union[float, ndarray], - mode: Union[float, ndarray], - right: Union[float, ndarray], - size: Size = ..., - ) -> Union[float, ndarray]: ... - # Complicated, discrete distributions: - def binomial( - self, n: Union[int, ndarray], p: Union[float, ndarray], size: Size = ... - ) -> ndarray: ... - def negative_binomial( - self, n: Union[int, ndarray], p: Union[float, ndarray], size: Size = ... - ) -> ndarray: ... - def poisson( - self, lam: Optional[Union[float, ndarray]] = ..., size: Size = ... - ) -> Union[int, ndarray]: ... - def zipf( - self, a: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def geometric( - self, p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - def hypergeometric( - self, - ngood: Union[int, ndarray], - nbad: Union[int, ndarray], - nsample: Union[int, ndarray], - size: Size = ..., - ) -> Union[int, ndarray]: ... - def logseries( - self, p: Union[float, ndarray], size: Size = ... - ) -> Union[int, ndarray]: ... - # Multivariate distributions: - def multivariate_normal( - self, - mean: ndarray, - cov: ndarray, - size: Size = ..., - check_valid: str = "warn", - tol: float = 1e-8, - ) -> ndarray: ... - def multinomial(self, n: int, pvals: ndarray, size: Size = ...) -> ndarray: ... - def dirichlet(self, alpha: ndarray, size: Size = ...) -> ndarray: ... - # Shuffling and permutations: - def shuffle(self, x: Sequence[Any]) -> ndarray: ... - def permutation(self, x: Sequence[Any]) -> ndarray: ... - -_rand: RandomState -beta = _rand.beta -binomial = _rand.binomial -bytes = _rand.bytes -chisquare = _rand.chisquare -choice = _rand.choice -dirichlet = _rand.dirichlet -exponential = _rand.exponential -f = _rand.f -gamma = _rand.gamma -get_state = _rand.get_state -geometric = _rand.geometric -gumbel = _rand.gumbel -hypergeometric = _rand.hypergeometric -laplace = _rand.laplace -logistic = _rand.logistic -lognormal = _rand.lognormal -logseries = _rand.logseries -multinomial = _rand.multinomial -multivariate_normal = _rand.multivariate_normal -negative_binomial = _rand.negative_binomial -noncentral_chisquare = _rand.noncentral_chisquare -noncentral_f = _rand.noncentral_f -normal = _rand.normal -pareto = _rand.pareto -permutation = _rand.permutation -poisson = _rand.poisson -power = _rand.power -rand = _rand.rand -randint = _rand.randint -randn = _rand.randn -random = _rand.random_sample -random_integers = _rand.random_integers -random_sample = _rand.random_sample -rayleigh = _rand.rayleigh -seed = _rand.seed -set_state = _rand.set_state -shuffle = _rand.shuffle -standard_cauchy = _rand.standard_cauchy -standard_exponential = _rand.standard_exponential -standard_gamma = _rand.standard_gamma -standard_normal = _rand.standard_normal -standard_t = _rand.standard_t -triangular = _rand.triangular -uniform = _rand.uniform -vonmises = _rand.vonmises -wald = _rand.wald -weibull = _rand.weibull -zipf = _rand.zipf +def _raises_not_implemented(*args: Any, **kwargs: Any) -> None: ... -def sample( - *args: Tuple[int, ...], **kwargs: Dict[str, Tuple[int, ...]] -) -> Union[float, ndarray]: ... -def ranf( - *args: Tuple[int, ...], **kwargs: Dict[str, Tuple[int, ...]] -) -> Union[float, ndarray]: ... +beta = _raises_not_implemented +binomial = _raises_not_implemented +bytes = _raises_not_implemented +chisquare = _raises_not_implemented +choice = _raises_not_implemented +dirichlet = _raises_not_implemented +exponential = _raises_not_implemented +f = _raises_not_implemented +gamma = _raises_not_implemented +get_state = _raises_not_implemented +geometric = _raises_not_implemented +gumbel = _raises_not_implemented +hypergeometric = _raises_not_implemented +laplace = _raises_not_implemented +logistic = _raises_not_implemented +lognormal = _raises_not_implemented +logseries = _raises_not_implemented +multinomial = _raises_not_implemented +multivariate_normal = _raises_not_implemented +negative_binomial = _raises_not_implemented +noncentral_chisquare = _raises_not_implemented +noncentral_f = _raises_not_implemented +normal = _raises_not_implemented +pareto = _raises_not_implemented +permutation = _raises_not_implemented +poisson = _raises_not_implemented +power = _raises_not_implemented +rand = _raises_not_implemented +randint = _raises_not_implemented +randn = _raises_not_implemented +random = _raises_not_implemented +random_integers = _raises_not_implemented +random_sample = _raises_not_implemented +rayleigh = _raises_not_implemented +seed = _raises_not_implemented +set_state = _raises_not_implemented +shuffle = _raises_not_implemented +standard_cauchy = _raises_not_implemented +standard_exponential = _raises_not_implemented +standard_gamma = _raises_not_implemented +standard_normal = _raises_not_implemented +standard_t = _raises_not_implemented +triangular = _raises_not_implemented +uniform = _raises_not_implemented +vonmises = _raises_not_implemented +wald = _raises_not_implemented +weibull = _raises_not_implemented +zipf = _raises_not_implemented +sample = _raises_not_implemented +ranf = _raises_not_implemented diff --git a/randomgen/mtrand.pyx b/randomgen/mtrand.pyx index 4d9792f2c..0a7edbe1f 100644 --- a/randomgen/mtrand.pyx +++ b/randomgen/mtrand.pyx @@ -20,6 +20,7 @@ from randomgen.common cimport * from randomgen.distributions cimport * from randomgen.legacy.distributions cimport * from randomgen cimport api +from typing import Callable, Any np.import_array() @@ -40,4223 +41,78 @@ cdef class RandomState: """ RandomState(bit_generator=None) - Container for the Mersenne Twister pseudo-random number generator. + RandomState has been removed in the 1.23 release. - `RandomState` exposes a number of methods for generating random numbers - drawn from a variety of probability distributions. In addition to the - distribution-specific arguments, each method takes a keyword argument - `size` that defaults to ``None``. If `size` is ``None``, then a single - value is generated and returned. If `size` is an integer, then a 1-D - array filled with generated values is returned. If `size` is a tuple, - then an array with that shape is filled and returned. - - **Compatibility Guarantee** - - A fixed bit generator using a fixed seed and a fixed series of calls to - 'RandomState' methods using the same parameters will always produce the - same results up to roundoff error except when the values were incorrect. - `RandomState` is effectively frozen and will only receive updates that - are required by changes in the the internals of Numpy. More substantial - changes, including algorithmic improvements, are reserved for - `Generator`. - - Parameters - ---------- - bit_generator : {None, int, array_like, BitGenerator}, optional - Random seed used to initialize the pseudo-random number generator or - an instantized BitGenerator. If an integer or array, used as a seed for - the MT19937 BitGenerator. Values can be any integer between 0 and - 2**32 - 1 inclusive, an array (or other sequence) of such integers, - or ``None`` (the default). If `seed` is ``None``, then the `MT19937` - BitGenerator is initialized by reading data from ``/dev/urandom`` - (or the Windows analogue) if available or seed from the clock - otherwise. - - Notes - ----- - The Python stdlib module "random" also contains a Mersenne Twister - pseudo-random number generator with a number of methods that are similar - to the ones available in `RandomState`. `RandomState`, besides being - NumPy-aware, has the advantage that it provides a much larger number - of probability distributions to choose from. + Use ``numpy.random.Generator`` or ``numpy.random.RandomState`` + if backward compataibility to older versions of NumPy is required. See Also -------- - randomgen.generator.Generator - randomgen.mt19937.MT19937 - + numpy.random.Generator + numpy.random.default_rng + numpy.random.RandomState + randomgen.generator.ExtendedGenerator """ - cdef public object _bit_generator - cdef bitgen_t _bitgen - cdef aug_bitgen_t _aug_state - cdef binomial_t _binomial - cdef object lock - _poisson_lam_max = LEGACY_POISSON_LAM_MAX - - def __init__(self, bit_generator=None): - import warnings - - warnings.warn("""\ -RandomState is deprecated and will be removed sometime after the release of -NumPy 1.21 (or 2 releases after 1.19 if there is a major release). - -Now is the time to start using numpy.random.Generator, or if you must have -backward compatibility, numpy.random.RandomState. - -In the mean time RandomState will only be updated for the most egregious bugs. - -You can silence this warning using - -import warnings -warnings.filterwarnings("ignore", "RandomState", FutureWarning) -""", FutureWarning) - - if bit_generator is None: - bit_generator = _MT19937(mode="legacy") - elif not hasattr(bit_generator, "capsule"): - bit_generator = _MT19937(bit_generator, mode="legacy") - - self._bit_generator = bit_generator - capsule = bit_generator.capsule - cdef const char *name = "BitGenerator" - if not PyCapsule_IsValid(capsule, name): - raise ValueError("Invalid bit generator. The bit generator must " - "be instantized.") - self._bitgen = ( PyCapsule_GetPointer(capsule, name))[0] - self._aug_state.bit_generator = &self._bitgen - self._reset_gauss() - self.lock = bit_generator.lock - - def __repr__(self): - return self.__str__() + " at 0x{:X}".format(id(self)) - - def __str__(self): - _str = type(self).__name__ - _str += "(" + type(self._bit_generator).__name__ + ")" - return _str - - # Pickling support: - def __getstate__(self): - return self.get_state(legacy=False) - - def __setstate__(self, state): - self.set_state(state) - - def __reduce__(self): - state = self.get_state(legacy=False) - from randomgen._pickle import __randomstate_ctor - return __randomstate_ctor, (state["bit_generator"],), state - - cdef _reset_gauss(self): - self._aug_state.has_gauss = 0 - self._aug_state.gauss = 0.0 - - def seed(self, *args, **kwargs): - """ - seed(self, *args, **kwargs) - - Reseed the bit generator. - - Parameters depend on the bit generator used. - - Notes - ----- - Arguments are directly passed to the bit generator. This is a convenience - function. - - The best method to access seed is to directly use a bit generator - instance. This example demonstrates this best practice. - - >>> from numpy.random import MT19937 - >>> from numpy.random import RandomState - >>> bit_generator = MT19937(123456789) - >>> rs = RandomState(bit_generator) - >>> bit_generator.seed(987654321) - - These best practice examples are equivalent to - - >>> rs = RandomState(MT19937()) - >>> rs.seed(987654321) - """ - self._bit_generator.seed(*args, **kwargs) - self._reset_gauss() - - def get_state(self, legacy=True): - """ - get_state() - - Return a tuple representing the internal state of the generator. - - For more details, see `set_state`. - - Returns - ------- - out : {tuple(str, ndarray of 624 uints, int, int, float), dict} - The returned tuple has the following items: - - 1. the string 'MT19937'. - 2. a 1-D array of 624 unsigned integer keys. - 3. an integer ``pos``. - 4. an integer ``has_gauss``. - 5. a float ``cached_gaussian``. - - If `legacy` is False, or the bit generator is not NT19937, then - state is returned as a dictionary. - - legacy : bool - Flag indicating the return a legacy tuple state when the bit - generator is MT19937. - - See Also - -------- - set_state - - Notes - ----- - `set_state` and `get_state` are not needed to work with any of the - random distributions in NumPy. If the internal state is manually altered, - the user should know exactly what he/she is doing. - - """ - st = self._bit_generator.state - if st["bit_generator"] != "MT19937" and legacy: - warnings.warn("get_state and legacy can only be used with the " - "MT19937 bit generator. To silence this warning, " - "set `legacy` to False.", RuntimeWarning) - legacy = False - st["has_gauss"] = self._aug_state.has_gauss - st["gauss"] = self._aug_state.gauss - if legacy: - return (st["bit_generator"], st["state"]["key"], st["state"]["pos"], - st["has_gauss"], st["gauss"]) - return st - - def set_state(self, state): - """ - set_state(state) - - Set the internal state of the generator from a tuple. - - For use if one has reason to manually (re-)set the internal state of - the bit generator used by the RandomState instance. By default, - RandomState uses the "Mersenne Twister"[1]_ pseudo-random number - generating algorithm. - - Parameters - ---------- - state : {tuple(str, ndarray of 624 uints, int, int, float), dict} - The `state` tuple has the following items: - - 1. the string 'MT19937', specifying the Mersenne Twister algorithm. - 2. a 1-D array of 624 unsigned integers ``keys``. - 3. an integer ``pos``. - 4. an integer ``has_gauss``. - 5. a float ``cached_gaussian``. - - If state is a dictionary, it is directly set using the BitGenerators - `state` property. - - Returns - ------- - out : None - Returns 'None' on success. - - See Also - -------- - get_state - - Notes - ----- - `set_state` and `get_state` are not needed to work with any of the - random distributions in NumPy. If the internal state is manually altered, - the user should know exactly what he/she is doing. - - For backwards compatibility, the form (str, array of 624 uints, int) is - also accepted although it is missing some information about the cached - Gaussian value: ``state = ('MT19937', keys, pos)``. - - References - ---------- - .. [1] M. Matsumoto and T. Nishimura, "Mersenne Twister: A - 623-dimensionally equidistributed uniform pseudorandom number - generator," *ACM Trans. on Modeling and Computer Simulation*, - Vol. 8, No. 1, pp. 3-30, Jan. 1998. - - """ - if isinstance(state, dict): - if "bit_generator" not in state or "state" not in state: - raise ValueError("state dictionary is not valid.") - st = state - else: - if not isinstance(state, (tuple, list)): - raise TypeError("state must be a dict or a tuple.") - if state[0] != "MT19937": - raise ValueError("set_state can only be used with legacy MT19937" - "state instances.") - st = {"bit_generator": state[0], - "state": {"key": state[1], "pos": state[2]}} - if len(state) > 3: - st["has_gauss"] = state[3] - st["gauss"] = state[4] - value = st - - self._aug_state.gauss = st.get("gauss", 0.0) - self._aug_state.has_gauss = st.get("has_gauss", 0) - self._bit_generator.state = st - - def random_sample(self, size=None): - """ - random_sample(size=None) - - Return random floats in the half-open interval [0.0, 1.0). - - Results are from the "continuous uniform" distribution over the - stated interval. To sample :math:`Unif[a, b), b > a` multiply - the output of `random_sample` by `(b-a)` and add `a`:: - - (b - a) * random_sample() + a - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : float or ndarray of floats - Array of random floats of shape `size` (unless ``size=None``, in which - case a single float is returned). - - Examples - -------- - >>> np.random.random_sample() - 0.47108547995356098 # random - >>> type(np.random.random_sample()) - - >>> np.random.random_sample((5,)) - array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428]) # random - - Three-by-two array of random numbers from [-5, 0): - - >>> 5 * np.random.random_sample((3, 2)) - 5 - array([[-3.99149989, -0.52338984], # random - [-2.99091858, -0.79479508], - [-1.23204345, -1.75224494]]) - - """ - cdef double temp - return double_fill(&random_double_fill, &self._bitgen, size, self.lock, None) - - def beta(self, a, b, size=None): - """ - beta(a, b, size=None) - - Draw samples from a Beta distribution. - - The Beta distribution is a special case of the Dirichlet distribution, - and is related to the Gamma distribution. It has the probability - distribution function - - .. math:: f(x; a,b) = \\frac{1}{B(\\alpha, \\beta)} x^{\\alpha - 1} - (1 - x)^{\\beta - 1}, - - where the normalization, B, is the beta function, - - .. math:: B(\\alpha, \\beta) = \\int_0^1 t^{\\alpha - 1} - (1 - t)^{\\beta - 1} dt. - - It is often seen in Bayesian inference and order statistics. - - Parameters - ---------- - a : float or array_like of floats - Alpha, positive (>0). - b : float or array_like of floats - Beta, positive (>0). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` and ``b`` are both scalars. - Otherwise, ``np.broadcast(a, b).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized beta distribution. - - """ - return cont(&legacy_beta, &self._aug_state, size, self.lock, 2, - a, "a", CONS_POSITIVE, - b, "b", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def exponential(self, scale=1.0, size=None): - """ - exponential(scale=1.0, size=None) - - Draw samples from an exponential distribution. - - Its probability density function is - - .. math:: f(x; \\frac{1}{\\beta}) = \\frac{1}{\\beta} \\exp(-\\frac{x}{\\beta}), - - for ``x > 0`` and 0 elsewhere. :math:`\\beta` is the scale parameter, - which is the inverse of the rate parameter :math:`\\lambda = 1/\\beta`. - The rate parameter is an alternative, widely used parameterization - of the exponential distribution [3]_. - - The exponential distribution is a continuous analogue of the - geometric distribution. It describes many common situations, such as - the size of raindrops measured over many rainstorms [1]_, or the time - between page requests to Wikipedia [2]_. - - Parameters - ---------- - scale : float or array_like of floats - The scale parameter, :math:`\\beta = 1/\\lambda`. Must be - non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``scale`` is a scalar. Otherwise, - ``np.array(scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized exponential distribution. - - References - ---------- - .. [1] Peyton Z. Peebles Jr., "Probability, Random Variables and - Random Signal Principles", 4th ed, 2001, p. 57. - .. [2] Wikipedia, "Poisson process", - https://en.wikipedia.org/wiki/Poisson_process - .. [3] Wikipedia, "Exponential distribution", - https://en.wikipedia.org/wiki/Exponential_distribution - - """ - return cont(&legacy_exponential, &self._aug_state, size, self.lock, 1, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, - None) - - def standard_exponential(self, size=None): - """ - standard_exponential(size=None) - - Draw samples from the standard exponential distribution. - - `standard_exponential` is identical to the exponential distribution - with a scale parameter of 1. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : float or ndarray - Drawn samples. - - Examples - -------- - Output a 3x8000 array: - - >>> n = np.random.standard_exponential((3, 8000)) - - """ - return cont(&legacy_standard_exponential, &self._aug_state, size, self.lock, 0, - None, None, CONS_NONE, - None, None, CONS_NONE, - None, None, CONS_NONE, - None) - - def tomaxint(self, size=None): - """ - tomaxint(size=None) - - Return a sample of uniformly distributed random integers in the interval - [0, ``np.iinfo(int).max``]. The int type translates to the C long - integer type and its precision is platform dependent. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : ndarray - Drawn samples, with shape `size`. - - See Also - -------- - randint : Uniform sampling over a given half-open interval of integers. - random_integers : Uniform sampling over a given closed interval of - integers. - - Examples - -------- - >>> rs = np.random.RandomState() # need a RandomState object - >>> rs.tomaxint((2,2,2)) - array([[[1170048599, 1600360186], # random - [ 739731006, 1947757578]], - [[1871712945, 752307660], - [1601631370, 1479324245]]]) - >>> rs.tomaxint((2,2,2)) < np.iinfo(int).max - array([[[ True, True], - [ True, True]], - [[ True, True], - [ True, True]]]) - - """ - cdef np.npy_intp n - cdef np.ndarray randoms - cdef int64_t *randoms_data - - if size is None: - with self.lock: - return random_positive_int(&self._bitgen) - - randoms = np.empty(size, dtype=np.int64) - randoms_data = np.PyArray_DATA(randoms) - n = np.PyArray_SIZE(randoms) - - for i in range(n): - with self.lock, nogil: - randoms_data[i] = random_positive_int(&self._bitgen) - return randoms - - def randint(self, low, high=None, size=None, dtype=int): - """ - randint(low, high=None, size=None, dtype='l') - - Return random integers from `low` (inclusive) to `high` (exclusive). - - Return random integers from the "discrete uniform" distribution of - the specified dtype in the "half-open" interval [`low`, `high`). If - `high` is None (the default), then results are from [0, `low`). - - Parameters - ---------- - low : int or array-like of ints - Lowest (signed) integers to be drawn from the distribution (unless - ``high=None``, in which case this parameter is one above the - *highest* such integer). - high : int or array-like of ints, optional - If provided, one above the largest (signed) integer to be drawn - from the distribution (see above for behavior if ``high=None``). - If array-like, must contain integer values - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - dtype : dtype, optional - Desired dtype of the result. All dtypes are determined by their - name, i.e., 'int64', 'int', etc, so byteorder is not available - and a specific precision may have different C types depending - on the platform. The default value is 'int'. - - .. versionadded:: 1.11.0 - - Returns - ------- - out : int or ndarray of ints - `size`-shaped array of random integers from the appropriate - distribution, or a single such random int if `size` not provided. - - See Also - -------- - random.random_integers : similar to `randint`, only for the closed - interval [`low`, `high`], and 1 is the lowest value if `high` is - omitted. In particular, this other one is the one to use to generate - uniformly distributed discrete non-integers. - - Examples - -------- - >>> np.random.randint(2, size=10) - array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random - >>> np.random.randint(1, size=10) - array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) - - Generate a 2 x 4 array of ints between 0 and 4, inclusive: - - >>> np.random.randint(5, size=(2, 4)) - array([[4, 0, 2, 1], # random - [3, 2, 2, 0]]) - - Generate a 1 x 3 array with 3 different upper bounds - - >>> np.random.randint(1, [3, 5, 10]) - array([2, 2, 9]) # random - - Generate a 1 by 3 array with 3 different lower bounds - - >>> np.random.randint([1, 5, 7], 10) - array([9, 8, 7]) # random - - Generate a 2 by 4 array using broadcasting with dtype of uint8 - - >>> np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8) - array([[ 8, 6, 9, 7], # random - [ 1, 16, 9, 12]], dtype=uint8) - """ - cdef bint use_masked=1 - - if high is None: - high = low - low = 0 - dt = np.dtype(dtype) - key = dt.name - if key not in _integers_types: - raise TypeError("Unsupported dtype \"{key}\" for randint".format(key=key)) - if dt.byteorder != "=" and dt.byteorder != "|": - import warnings - warnings.warn("Byteorder is not supported. If you require " - "platform-independent byteorder, call byteswap when " - "required.\n\nIn future version, specifying " - "byteorder will raise a ValueError", FutureWarning) - - if key == "int32": - ret = _legacy_rand_int32(low, high, size, &self._aug_state, self.lock) - elif key == "int64": - ret = _legacy_rand_int64(low, high, size, &self._aug_state, self.lock) - elif key == "int16": - ret = _legacy_rand_int16(low, high, size, &self._aug_state, self.lock) - elif key == "int8": - ret = _legacy_rand_int8(low, high, size, &self._aug_state, self.lock) - elif key == "uint64": - ret = _legacy_rand_uint64(low, high, size, &self._aug_state, self.lock) - elif key == "uint32": - ret = _legacy_rand_uint32(low, high, size, &self._aug_state, self.lock) - elif key == "uint16": - ret = _legacy_rand_uint16(low, high, size, &self._aug_state, self.lock) - elif key == "uint8": - ret = _legacy_rand_uint8(low, high, size, &self._aug_state, self.lock) - elif key == "bool": - ret = _legacy_rand_bool(low, high, size, &self._aug_state, self.lock) - - if size is None and dtype in (bool, int): - if np.array(ret).shape == (): - return dtype(ret) - return ret - - def bytes(self, np.npy_intp length): - """ - bytes(length) - - Return random bytes. - - Parameters - ---------- - length : int - Number of random bytes. - - Returns - ------- - out : bytes - String of length `length`. - - Examples - -------- - >>> np.random.bytes(10) - b' eh\\x85\\x022SZ\\xbf\\xa4' # random - - """ - cdef Py_ssize_t n_uint32 = ((length - 1) // 4 + 1) - - return self.randint(0, 4294967296, size=n_uint32, - dtype=np.uint32).astype('>> np.random.choice(5, 3) - array([0, 3, 4]) # random - >>> #This is equivalent to np.random.randint(0,5,3) - - Generate a non-uniform random sample from np.arange(5) of size 3: - - >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) - array([3, 3, 0]) # random - - Generate a uniform random sample from np.arange(5) of size 3 without - replacement: - - >>> np.random.choice(5, 3, replace=False) - array([3,1,0]) # random - >>> #This is equivalent to np.random.permutation(np.arange(5))[:3] - - Generate a non-uniform random sample from np.arange(5) of size - 3 without replacement: - - >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0]) - array([2, 3, 0]) # random - - Any of the above can be repeated with an arbitrary array-like - instead of just integers. For instance: - - >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher'] - >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3]) - array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random - dtype='np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED | api.NPY_ARRAY_C_CONTIGUOUS) - check_array_constraint(p, "p", CONS_BOUNDED_0_1) - pix = np.PyArray_DATA(p) - - if p.ndim != 1: - raise ValueError("\"p\" must be 1-dimensional") - if p.size != pop_size: - raise ValueError("\"a\" and \"p\" must have same size") - p_sum = kahan_sum(pix, d) - if abs(p_sum - 1.) > atol: - raise ValueError("probabilities do not sum to 1") - - shape = size - if shape is not None: - size = np.prod(shape, dtype=np.intp) - else: - size = 1 - - # Actual sampling - if replace: - if p is not None: - cdf = p.cumsum() - cdf /= cdf[-1] - uniform_samples = self.random_sample(shape) - idx = cdf.searchsorted(uniform_samples, side="right") - idx = np.array(idx, copy=False) # searchsorted returns a scalar - else: - idx = self.randint(0, pop_size, size=shape) - else: - if size > pop_size: - raise ValueError("Cannot take a larger sample than " - "population when replace=False") - elif size < 0: - raise ValueError("Negative dimensions are not allowed") - - if p is not None: - if np.count_nonzero(p > 0) < size: - raise ValueError("Fewer non-zero entries in p than size") - n_uniq = 0 - p = p.copy() - _shape = () if shape is None else shape - found = np.zeros(_shape, dtype=np.int64) - flat_found = found.ravel() - while n_uniq < size: - x = self.rand(size - n_uniq) - if n_uniq > 0: - p[flat_found[0:n_uniq]] = 0 - cdf = np.cumsum(p) - cdf /= cdf[-1] - new = cdf.searchsorted(x, side="right") - _, unique_indices = np.unique(new, return_index=True) - unique_indices.sort() - new = new.take(unique_indices) - flat_found[n_uniq:n_uniq + new.size] = new - n_uniq += new.size - idx = found - else: - idx = self.permutation(pop_size)[:size] - if shape is not None: - idx.shape = shape - - if shape is None and isinstance(idx, np.ndarray): - # In most cases a scalar will have been made an array - idx = idx.item(0) - - # Use samples as indices for a if a is array-like - if a.ndim == 0: - return idx - - if shape is not None and idx.ndim == 0: - # If size == () then the user requested a 0-d array as opposed to - # a scalar object when size is None. However a[idx] is always a - # scalar and not an array. So this makes sure the result is an - # array, taking into account that np.array(item) may not work - # for object arrays. - res = np.empty((), dtype=a.dtype) - res[()] = a[idx] - return res - - return a[idx] - - def uniform(self, low=0.0, high=1.0, size=None): - """ - uniform(low=0.0, high=1.0, size=None) - - Draw samples from a uniform distribution. - - Samples are uniformly distributed over the half-open interval - ``[low, high)`` (includes low, but excludes high). In other words, - any value within the given interval is equally likely to be drawn - by `uniform`. - - Parameters - ---------- - low : float or array_like of floats, optional - Lower boundary of the output interval. All values generated will be - greater than or equal to low. The default value is 0. - high : float or array_like of floats - Upper boundary of the output interval. All values generated will be - less than high. The default value is 1.0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``low`` and ``high`` are both scalars. - Otherwise, ``np.broadcast(low, high).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized uniform distribution. - - See Also - -------- - randint : Discrete uniform distribution, yielding integers. - random_integers : Discrete uniform distribution over the closed - interval ``[low, high]``. - random_sample : Floats uniformly distributed over ``[0, 1)``. - random : Alias for `random_sample`. - rand : Convenience function that accepts dimensions as input, e.g., - ``rand(2,2)`` would generate a 2-by-2 array of floats, - uniformly distributed over ``[0, 1)``. - - Notes - ----- - The probability density function of the uniform distribution is - - .. math:: p(x) = \\frac{1}{b - a} - - anywhere within the interval ``[a, b)``, and zero elsewhere. - - When ``high`` == ``low``, values of ``low`` will be returned. - If ``high`` < ``low``, the results are officially undefined - and may eventually raise an error, i.e. do not rely on this - function to behave when passed arguments satisfying that - inequality condition. - - Examples - -------- - Draw samples from the distribution: - - >>> s = np.random.uniform(-1,0,1000) - - All values are within the given interval: - - >>> np.all(s >= -1) - True - >>> np.all(s < 0) - True - - Display the histogram of the samples, along with the - probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 15, density=True) - >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') - >>> plt.show() - - """ - cdef bint is_scalar = True - cdef np.ndarray alow, ahigh, arange - cdef double _low, _high, range - cdef object temp - - alow = np.PyArray_FROM_OTF(low, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - ahigh = np.PyArray_FROM_OTF(high, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(alow) == np.PyArray_NDIM(ahigh) == 0: - _low = PyFloat_AsDouble(low) - _high = PyFloat_AsDouble(high) - range = _high - _low - if not np.isfinite(range): - raise OverflowError("Range exceeds valid bounds") - - return cont(&random_uniform, &self._bitgen, size, self.lock, 2, - _low, "", CONS_NONE, - range, "", CONS_NONE, - 0.0, "", CONS_NONE, - None) - - temp = np.subtract(ahigh, alow) - Py_INCREF(temp) - # needed to get around Pyrex's automatic reference-counting - # rules because EnsureArray steals a reference - arange = np.PyArray_EnsureArray(temp) - if not np.all(np.isfinite(arange)): - raise OverflowError("Range exceeds valid bounds") - return cont(&random_uniform, &self._bitgen, size, self.lock, 2, - alow, "", CONS_NONE, - arange, "", CONS_NONE, - 0.0, "", CONS_NONE, - None) - - def rand(self, *args): - """ - rand(d0, d1, ..., dn) - - Random values in a given shape. - - .. note:: - This is a convenience function for users porting code from Matlab, - and wraps `numpy.random.random_sample`. That function takes a - tuple to specify the size of the output, which is consistent with - other NumPy functions like `numpy.zeros` and `numpy.ones`. - - Create an array of the given shape and populate it with - random samples from a uniform distribution - over ``[0, 1)``. - - Parameters - ---------- - d0, d1, ..., dn : int, optional - The dimensions of the returned array, must be non-negative. - If no argument is given a single Python float is returned. - - Returns - ------- - out : ndarray, shape ``(d0, d1, ..., dn)`` - Random values. - - See Also - -------- - random - - Examples - -------- - >>> np.random.rand(3,2) - array([[ 0.14022471, 0.96360618], # random - [ 0.37601032, 0.25528411], # random - [ 0.49313049, 0.94909878]]) # random - - """ - if len(args) == 0: - return self.random_sample() - else: - return self.random_sample(size=args) - - def randn(self, *args): - """ - randn(d0, d1, ..., dn) - - Return a sample (or samples) from the "standard normal" distribution. - - .. note:: - This is a convenience function for users porting code from Matlab, - and wraps `numpy.random.standard_normal`. That function takes a - tuple to specify the size of the output, which is consistent with - other NumPy functions like `numpy.zeros` and `numpy.ones`. - - If positive int_like arguments are provided, `randn` generates an array - of shape ``(d0, d1, ..., dn)``, filled - with random floats sampled from a univariate "normal" (Gaussian) - distribution of mean 0 and variance 1. A single float randomly sampled - from the distribution is returned if no argument is provided. - - Parameters - ---------- - d0, d1, ..., dn : int, optional - The dimensions of the returned array, must be non-negative. - If no argument is given a single Python float is returned. - - Returns - ------- - Z : ndarray or float - A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from - the standard normal distribution, or a single such float if - no parameters were supplied. - - See Also - -------- - standard_normal : Similar, but takes a tuple as its argument. - normal : Also accepts mu and sigma arguments. - - Notes - ----- - For random samples from :math:`N(\\mu, \\sigma^2)`, use: - - ``sigma * np.random.randn(...) + mu`` - - Examples - -------- - >>> np.random.randn() - 2.1923875335537315 # random - - Two-by-four array of samples from N(3, 6.25): - - >>> 3 + 2.5 * np.random.randn(2, 4) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - if len(args) == 0: - return self.standard_normal() - else: - return self.standard_normal(size=args) - - def random_integers(self, low, high=None, size=None): - """ - random_integers(low, high=None, size=None) - - Random integers of type int between `low` and `high`, inclusive. - - Return random integers of type int from the "discrete uniform" - distribution in the closed interval [`low`, `high`]. If `high` is - None (the default), then results are from [1, `low`]. The int - type translates to the C long integer type and its precision - is platform dependent. - - This function has been deprecated. Use randint instead. - - .. deprecated:: 1.11.0 - - Parameters - ---------- - low : int - Lowest (signed) integer to be drawn from the distribution (unless - ``high=None``, in which case this parameter is the *highest* such - integer). - high : int, optional - If provided, the largest (signed) integer to be drawn from the - distribution (see above for behavior if ``high=None``). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : int or ndarray of ints - `size`-shaped array of random integers from the appropriate - distribution, or a single such random int if `size` not provided. - - See Also - -------- - randint : Similar to `random_integers`, only for the half-open - interval [`low`, `high`), and 0 is the lowest value if `high` is - omitted. - - Notes - ----- - To sample from N evenly spaced floating-point numbers between a and b, - use:: - - a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.) - - Examples - -------- - >>> np.random.random_integers(5) - 4 # random - >>> type(np.random.random_integers(5)) - - >>> np.random.random_integers(5, size=(3,2)) - array([[5, 4], # random - [3, 3], - [4, 5]]) - - Choose five random numbers from the set of five evenly-spaced - numbers between 0 and 2.5, inclusive (*i.e.*, from the set - :math:`{0, 5/8, 10/8, 15/8, 20/8}`): - - >>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4. - array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ]) # random - - Roll two six sided dice 1000 times and sum the results: - - >>> d1 = np.random.random_integers(1, 6, 1000) - >>> d2 = np.random.random_integers(1, 6, 1000) - >>> dsums = d1 + d2 - - Display results as a histogram: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(dsums, 11, density=True) - >>> plt.show() - - """ - if high is None: - warnings.warn(("This function is deprecated. Please call " - "randint(1, {low} + 1) instead".format(low=low)), - DeprecationWarning) - high = low - low = 1 - - else: - warnings.warn(("This function is deprecated. Please call " - "randint({low}, {high} + 1) " - "instead".format(low=low, high=high)), - DeprecationWarning) - - return self.randint(low, int(high) + 1, size=size, dtype="l") - - # Complicated, continuous distributions: - def standard_normal(self, size=None): - """ - standard_normal(size=None) - - Draw samples from a standard Normal distribution (mean=0, stdev=1). - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : float or ndarray - A floating-point array of shape ``size`` of drawn samples, or a - single sample if ``size`` was not specified. - - Notes - ----- - For random samples from :math:`N(\\mu, \\sigma^2)`, use one of:: - - mu + sigma * np.random.standard_normal(size=...) - np.random.normal(mu, sigma, size=...) - - See Also - -------- - normal : - Equivalent function with additional ``loc`` and ``scale`` arguments - for setting the mean and standard deviation. - - Examples - -------- - >>> np.random.standard_normal() - 2.1923875335537315 # random - - >>> s = np.random.standard_normal(8000) - >>> s - array([ 0.6888893 , 0.78096262, -0.89086505, ..., 0.49876311, # random - -0.38672696, -0.4685006 ]) # random - >>> s.shape - (8000,) - >>> s = np.random.standard_normal(size=(3, 4, 2)) - >>> s.shape - (3, 4, 2) - - Two-by-four array of samples from :math:`N(3, 6.25)`: - - >>> 3 + 2.5 * np.random.standard_normal(size=(2, 4)) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - return cont(&legacy_gauss, &self._aug_state, size, self.lock, 0, - None, None, CONS_NONE, - None, None, CONS_NONE, - None, None, CONS_NONE, - None) - - def normal(self, loc=0.0, scale=1.0, size=None): - """ - normal(loc=0.0, scale=1.0, size=None) - - Draw random samples from a normal (Gaussian) distribution. - - The probability density function of the normal distribution, first - derived by De Moivre and 200 years later by both Gauss and Laplace - independently [2]_, is often called the bell curve because of - its characteristic shape (see the example below). - - The normal distributions occurs often in nature. For example, it - describes the commonly occurring distribution of samples influenced - by a large number of tiny, random disturbances, each with its own - unique distribution [2]_. - - Parameters - ---------- - loc : float or array_like of floats - Mean ("center") of the distribution. - scale : float or array_like of floats - Standard deviation (spread or "width") of the distribution. Must be - non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized normal distribution. - - See Also - -------- - scipy.stats.norm : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gaussian distribution is - - .. math:: p(x) = \\frac{1}{\\sqrt{ 2 \\pi \\sigma^2 }} - e^{ - \\frac{ (x - \\mu)^2 } {2 \\sigma^2} }, - - where :math:`\\mu` is the mean and :math:`\\sigma` the standard - deviation. The square of the standard deviation, :math:`\\sigma^2`, - is called the variance. - - The function has its peak at the mean, and its "spread" increases with - the standard deviation (the function reaches 0.607 times its maximum at - :math:`x + \\sigma` and :math:`x - \\sigma` [2]_). This implies that - `numpy.random.normal` is more likely to return samples lying close to - the mean, rather than those far away. - - References - ---------- - .. [1] Wikipedia, "Normal distribution", - https://en.wikipedia.org/wiki/Normal_distribution - .. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability, - Random Variables and Random Signal Principles", 4th ed., 2001, - pp. 51, 51, 125. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, sigma = 0, 0.1 # mean and standard deviation - >>> s = np.random.normal(mu, sigma, 1000) - - Verify the mean and the variance: - - >>> abs(mu - np.mean(s)) - 0.0 # may vary - - >>> abs(sigma - np.std(s, ddof=1)) - 0.1 # may vary - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * - ... np.exp( - (bins - mu)**2 / (2 * sigma**2) ), - ... linewidth=2, color='r') - >>> plt.show() - - Two-by-four array of samples from N(3, 6.25): - - >>> np.random.normal(3, 2.5, size=(2, 4)) - array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random - [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random - - """ - return cont(&legacy_normal, &self._aug_state, size, self.lock, 2, - loc, "", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - None) - - def standard_gamma(self, shape, size=None): - """ - standard_gamma(shape, size=None) - - Draw samples from a standard Gamma distribution. - - Samples are drawn from a Gamma distribution with specified parameters, - shape (sometimes designated "k") and scale=1. - - Parameters - ---------- - shape : float or array_like of floats - Parameter, must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``shape`` is a scalar. Otherwise, - ``np.array(shape).size`` samples are drawn. - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized standard gamma distribution. - See Also - -------- - scipy.stats.gamma : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gamma distribution is - - .. math:: p(x) = x^{k-1}\\frac{e^{-x/\\theta}}{\\theta^k\\Gamma(k)}, - - where :math:`k` is the shape and :math:`\\theta` the scale, - and :math:`\\Gamma` is the Gamma function. - - The Gamma distribution is often used to model the times to failure of - electronic components, and arises naturally in processes for which the - waiting times between Poisson distributed events are relevant. - - References - ---------- - .. [1] Weisstein, Eric W. "Gamma Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/GammaDistribution.html - .. [2] Wikipedia, "Gamma distribution", - https://en.wikipedia.org/wiki/Gamma_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> shape, scale = 2., 1. # mean and width - >>> s = np.random.standard_gamma(shape, 1000000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps # doctest: +SKIP - >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ # doctest: +SKIP - ... (sps.gamma(shape) * scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return cont(&legacy_standard_gamma, &self._aug_state, size, self.lock, 1, - shape, "shape", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, - None) - - def gamma(self, shape, scale=1.0, size=None): - """ - gamma(shape, scale=1.0, size=None) - - Draw samples from a Gamma distribution. - - Samples are drawn from a Gamma distribution with specified parameters, - `shape` (sometimes designated "k") and `scale` (sometimes designated - "theta"), where both parameters are > 0. - - Parameters - ---------- - shape : float or array_like of floats - The shape of the gamma distribution. Must be non-negative. - scale : float or array_like of floats, optional - The scale of the gamma distribution. Must be non-negative. - Default is equal to 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``shape`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(shape, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized gamma distribution. - - See Also - -------- - scipy.stats.gamma : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Gamma distribution is - - .. math:: p(x) = x^{k-1}\\frac{e^{-x/\\theta}}{\\theta^k\\Gamma(k)}, - - where :math:`k` is the shape and :math:`\\theta` the scale, - and :math:`\\Gamma` is the Gamma function. - - The Gamma distribution is often used to model the times to failure of - electronic components, and arises naturally in processes for which the - waiting times between Poisson distributed events are relevant. - - References - ---------- - .. [1] Weisstein, Eric W. "Gamma Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/GammaDistribution.html - .. [2] Wikipedia, "Gamma distribution", - https://en.wikipedia.org/wiki/Gamma_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> shape, scale = 2., 2. # mean=4, std=2*sqrt(2) - >>> s = np.random.gamma(shape, scale, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps # doctest: +SKIP - >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1)*(np.exp(-bins/scale) / # doctest: +SKIP - ... (sps.gamma(shape)*scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return cont(&legacy_gamma, &self._aug_state, size, self.lock, 2, - shape, "shape", CONS_NON_NEGATIVE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def f(self, dfnum, dfden, size=None): - """ - f(dfnum, dfden, size=None) - - Draw samples from an F distribution. - - Samples are drawn from an F distribution with specified parameters, - `dfnum` (degrees of freedom in numerator) and `dfden` (degrees of - freedom in denominator), where both parameters must be greater than - zero. - - The random variate of the F distribution (also known as the - Fisher distribution) is a continuous probability distribution - that arises in ANOVA tests, and is the ratio of two chi-square - variates. - - Parameters - ---------- - dfnum : float or array_like of floats - Degrees of freedom in numerator, must be > 0. - dfden : float or array_like of float - Degrees of freedom in denominator, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``dfnum`` and ``dfden`` are both scalars. - Otherwise, ``np.broadcast(dfnum, dfden).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Fisher distribution. - - See Also - -------- - scipy.stats.f : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The F statistic is used to compare in-group variances to between-group - variances. Calculating the distribution depends on the sampling, and - so it is a function of the respective degrees of freedom in the - problem. The variable `dfnum` is the number of samples minus one, the - between-groups degrees of freedom, while `dfden` is the within-groups - degrees of freedom, the sum of the number of samples in each group - minus the number of groups. - - References - ---------- - .. [1] Glantz, Stanton A. "Primer of Biostatistics.", McGraw-Hill, - Fifth Edition, 2002. - .. [2] Wikipedia, "F-distribution", - https://en.wikipedia.org/wiki/F-distribution - - Examples - -------- - An example from Glantz[1], pp 47-40: - - Two groups, children of diabetics (25 people) and children from people - without diabetes (25 controls). Fasting blood glucose was measured, - case group had a mean value of 86.1, controls had a mean value of - 82.2. Standard deviations were 2.09 and 2.49 respectively. Are these - data consistent with the null hypothesis that the parents diabetic - status does not affect their children's blood glucose levels? - Calculating the F statistic from the data gives a value of 36.01. - - Draw samples from the distribution: - - >>> dfnum = 1. # between group degrees of freedom - >>> dfden = 48. # within groups degrees of freedom - >>> s = np.random.f(dfnum, dfden, 1000) - - The lower bound for the top 1% of the samples is : - - >>> np.sort(s)[-10] - 7.61988120985 # random - - So there is about a 1% chance that the F statistic will exceed 7.62, - the measured value is 36, so the null hypothesis is rejected at the 1% - level. - - """ - return cont(&legacy_f, &self._aug_state, size, self.lock, 2, - dfnum, "dfnum", CONS_POSITIVE, - dfden, "dfden", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def noncentral_f(self, dfnum, dfden, nonc, size=None): - """ - noncentral_f(dfnum, dfden, nonc, size=None) - - Draw samples from the noncentral F distribution. - - Samples are drawn from an F distribution with specified parameters, - `dfnum` (degrees of freedom in numerator) and `dfden` (degrees of - freedom in denominator), where both parameters > 1. - `nonc` is the non-centrality parameter. - - Parameters - ---------- - dfnum : float or array_like of floats - Numerator degrees of freedom, must be > 0. - - .. versionchanged:: 1.14.0 - Earlier NumPy versions required dfnum > 1. - dfden : float or array_like of floats - Denominator degrees of freedom, must be > 0. - nonc : float or array_like of floats - Non-centrality parameter, the sum of the squares of the numerator - means, must be >= 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``dfnum``, ``dfden``, and ``nonc`` - are all scalars. Otherwise, ``np.broadcast(dfnum, dfden, nonc).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized noncentral Fisher distribution. - - Notes - ----- - When calculating the power of an experiment (power = probability of - rejecting the null hypothesis when a specific alternative is true) the - non-central F statistic becomes important. When the null hypothesis is - true, the F statistic follows a central F distribution. When the null - hypothesis is not true, then it follows a non-central F statistic. - - References - ---------- - .. [1] Weisstein, Eric W. "Noncentral F-Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/NoncentralF-Distribution.html - .. [2] Wikipedia, "Noncentral F-distribution", - https://en.wikipedia.org/wiki/Noncentral_F-distribution - - Examples - -------- - In a study, testing for a specific alternative to the null hypothesis - requires use of the Noncentral F distribution. We need to calculate the - area in the tail of the distribution that exceeds the value of the F - distribution for the null hypothesis. We'll plot the two probability - distributions for comparison. - - >>> dfnum = 3 # between group deg of freedom - >>> dfden = 20 # within groups degrees of freedom - >>> nonc = 3.0 - >>> nc_vals = np.random.noncentral_f(dfnum, dfden, nonc, 1000000) - >>> NF = np.histogram(nc_vals, bins=50, density=True) - >>> c_vals = np.random.f(dfnum, dfden, 1000000) - >>> F = np.histogram(c_vals, bins=50, density=True) - >>> import matplotlib.pyplot as plt - >>> plt.plot(F[1][1:], F[0]) - >>> plt.plot(NF[1][1:], NF[0]) - >>> plt.show() - - """ - return cont(&legacy_noncentral_f, &self._aug_state, size, self.lock, 3, - dfnum, "dfnum", CONS_POSITIVE, - dfden, "dfden", CONS_POSITIVE, - nonc, "nonc", CONS_NON_NEGATIVE, None) - - def chisquare(self, df, size=None): - """ - chisquare(df, size=None) - - Draw samples from a chi-square distribution. - - When `df` independent random variables, each with standard normal - distributions (mean 0, variance 1), are squared and summed, the - resulting distribution is chi-square (see Notes). This distribution - is often used in hypothesis testing. - - Parameters - ---------- - df : float or array_like of floats - Number of degrees of freedom, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` is a scalar. Otherwise, - ``np.array(df).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized chi-square distribution. - - Raises - ------ - ValueError - When `df` <= 0 or when an inappropriate `size` (e.g. ``size=-1``) - is given. - - Notes - ----- - The variable obtained by summing the squares of `df` independent, - standard normally distributed random variables: - - .. math:: Q = \\sum_{i=0}^{\\mathtt{df}} X^2_i - - is chi-square distributed, denoted - - .. math:: Q \\sim \\chi^2_k. - - The probability density function of the chi-squared distribution is - - .. math:: p(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)} - x^{k/2 - 1} e^{-x/2}, - - where :math:`\\Gamma` is the gamma function, - - .. math:: \\Gamma(x) = \\int_0^{-\\infty} t^{x - 1} e^{-t} dt. - - References - ---------- - .. [1] NIST "Engineering Statistics Handbook" - https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm - - Examples - -------- - >>> np.random.chisquare(2,4) - array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272]) # random - - """ - return cont(&legacy_chisquare, &self._aug_state, size, self.lock, 1, - df, "df", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def noncentral_chisquare(self, df, nonc, size=None): - """ - noncentral_chisquare(df, nonc, size=None) - - Draw samples from a noncentral chi-square distribution. - - The noncentral :math:`\\chi^2` distribution is a generalization of - the :math:`\\chi^2` distribution. - - Parameters - ---------- - df : float or array_like of floats - Degrees of freedom, must be > 0. - - .. versionchanged:: 1.10.0 - Earlier NumPy versions required dfnum > 1. - nonc : float or array_like of floats - Non-centrality, must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` and ``nonc`` are both scalars. - Otherwise, ``np.broadcast(df, nonc).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized noncentral chi-square distribution. - - Notes - ----- - The probability density function for the noncentral Chi-square - distribution is - - .. math:: P(x;df,nonc) = \\sum^{\\infty}_{i=0} - \\frac{e^{-nonc/2}(nonc/2)^{i}}{i!} - P_{Y_{df+2i}}(x), - - where :math:`Y_{q}` is the Chi-square with q degrees of freedom. - - References - ---------- - .. [1] Wikipedia, "Noncentral chi-squared distribution" - https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram - - >>> import matplotlib.pyplot as plt - >>> values = plt.hist(np.random.noncentral_chisquare(3, 20, 100000), - ... bins=200, density=True) - >>> plt.show() - - Draw values from a noncentral chisquare with very small noncentrality, - and compare to a chisquare. - - >>> plt.figure() - >>> values = plt.hist(np.random.noncentral_chisquare(3, .0000001, 100000), - ... bins=np.arange(0., 25, .1), density=True) - >>> values2 = plt.hist(np.random.chisquare(3, 100000), - ... bins=np.arange(0., 25, .1), density=True) - >>> plt.plot(values[1][0:-1], values[0]-values2[0], 'ob') - >>> plt.show() - - Demonstrate how large values of non-centrality lead to a more symmetric - distribution. - - >>> plt.figure() - >>> values = plt.hist(np.random.noncentral_chisquare(3, 20, 100000), - ... bins=200, density=True) - >>> plt.show() - - """ - return cont(&legacy_noncentral_chisquare, &self._aug_state, size, self.lock, 2, - df, "df", CONS_POSITIVE, - nonc, "nonc", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def standard_cauchy(self, size=None): - """ - standard_cauchy(size=None) - - Draw samples from a standard Cauchy distribution with mode = 0. - - Also known as the Lorentz distribution. - - Parameters - ---------- - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - samples : ndarray or scalar - The drawn samples. - - Notes - ----- - The probability density function for the full Cauchy distribution is - - .. math:: P(x; x_0, \\gamma) = \\frac{1}{\\pi \\gamma \\bigl[ 1+ - (\\frac{x-x_0}{\\gamma})^2 \\bigr] } - - and the Standard Cauchy distribution just sets :math:`x_0=0` and - :math:`\\gamma=1` - - The Cauchy distribution arises in the solution to the driven harmonic - oscillator problem, and also describes spectral line broadening. It - also describes the distribution of values at which a line tilted at - a random angle will cut the x axis. - - When studying hypothesis tests that assume normality, seeing how the - tests perform on data from a Cauchy distribution is a good indicator of - their sensitivity to a heavy-tailed distribution, since the Cauchy looks - very much like a Gaussian distribution, but with heavier tails. - - References - ---------- - .. [1] NIST/SEMATECH e-Handbook of Statistical Methods, "Cauchy - Distribution", - https://www.itl.nist.gov/div898/handbook/eda/section3/eda3663.htm - .. [2] Weisstein, Eric W. "Cauchy Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/CauchyDistribution.html - .. [3] Wikipedia, "Cauchy distribution" - https://en.wikipedia.org/wiki/Cauchy_distribution - - Examples - -------- - Draw samples and plot the distribution: - - >>> import matplotlib.pyplot as plt - >>> s = np.random.standard_cauchy(1000000) - >>> s = s[(s>-25) & (s<25)] # truncate distribution so it plots well - >>> plt.hist(s, bins=100) - >>> plt.show() - - """ - return cont(&legacy_standard_cauchy, &self._aug_state, size, self.lock, 0, - 0.0, "", CONS_NONE, 0.0, "", CONS_NONE, 0.0, "", CONS_NONE, None) - - def standard_t(self, df, size=None): - """ - standard_t(df, size=None) - - Draw samples from a standard Student's t distribution with `df` degrees - of freedom. - - A special case of the hyperbolic distribution. As `df` gets - large, the result resembles that of the standard normal - distribution (`standard_normal`). - - Parameters - ---------- - df : float or array_like of floats - Degrees of freedom, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``df`` is a scalar. Otherwise, - ``np.array(df).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized standard Student's t distribution. - - Notes - ----- - The probability density function for the t distribution is - - .. math:: P(x, df) = \\frac{\\Gamma(\\frac{df+1}{2})}{\\sqrt{\\pi df} - \\Gamma(\\frac{df}{2})}\\Bigl( 1+\\frac{x^2}{df} \\Bigr)^{-(df+1)/2} - - The t test is based on an assumption that the data come from a - Normal distribution. The t test provides a way to test whether - the sample mean (that is the mean calculated from the data) is - a good estimate of the true mean. - - The derivation of the t-distribution was first published in - 1908 by William Gosset while working for the Guinness Brewery - in Dublin. Due to proprietary issues, he had to publish under - a pseudonym, and so he used the name Student. - - References - ---------- - .. [1] Dalgaard, Peter, "Introductory Statistics With R", - Springer, 2002. - .. [2] Wikipedia, "Student's t-distribution" - https://en.wikipedia.org/wiki/Student's_t-distribution - - Examples - -------- - From Dalgaard page 83 [1]_, suppose the daily energy intake for 11 - women in kilojoules (kJ) is: - - >>> intake = np.array([5260., 5470, 5640, 6180, 6390, 6515, 6805, 7515, \\ - ... 7515, 8230, 8770]) - - Does their energy intake deviate systematically from the recommended - value of 7725 kJ? Our null hypothesis will be the absence of deviation, - and the alternate hypothesis will be the presence of an effect that could be - either positive or negative, hence making our test 2-tailed. - - Because we are estimating the mean and we have N=11 values in our sample, - we have N-1=10 degrees of freedom. We set our significance level to 95% and - compute the t statistic using the empirical mean and empirical standard - deviation of our intake. We use a ddof of 1 to base the computation of our - empirical standard deviation on an unbiased estimate of the variance (note: - the final estimate is not unbiased due to the concave nature of the square - root). - - >>> np.mean(intake) - 6753.636363636364 - >>> intake.std(ddof=1) - 1142.1232221373727 - >>> t = (np.mean(intake)-7725)/(intake.std(ddof=1)/np.sqrt(len(intake))) - >>> t - -2.8207540608310198 - - We draw 1000000 samples from Student's t distribution with the adequate - degrees of freedom. - - >>> import matplotlib.pyplot as plt - >>> s = np.random.standard_t(10, size=1000000) - >>> h = plt.hist(s, bins=100, density=True) - - Does our t statistic land in one of the two critical regions found at - both tails of the distribution? - - >>> np.sum(np.abs(t) < np.abs(s)) / float(len(s)) - 0.018318 #random < 0.05, statistic is in critical region - - The probability value for this 2-tailed test is about 1.83%, which is - lower than the 5% pre-determined significance threshold. - - Therefore, the probability of observing values as extreme as our intake - conditionally on the null hypothesis being true is too low, and we reject - the null hypothesis of no deviation. - - """ - return cont(&legacy_standard_t, &self._aug_state, size, self.lock, 1, - df, "df", CONS_POSITIVE, - 0, "", CONS_NONE, - 0, "", CONS_NONE, - None) - - def vonmises(self, mu, kappa, size=None): - """ - vonmises(mu, kappa, size=None) - - Draw samples from a von Mises distribution. - - Samples are drawn from a von Mises distribution with specified mode - (mu) and dispersion (kappa), on the interval [-pi, pi]. - - The von Mises distribution (also known as the circular normal - distribution) is a continuous probability distribution on the unit - circle. It may be thought of as the circular analogue of the normal - distribution. - - Parameters - ---------- - mu : float or array_like of floats - Mode ("center") of the distribution. - kappa : float or array_like of floats - Dispersion of the distribution, has to be >=0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mu`` and ``kappa`` are both scalars. - Otherwise, ``np.broadcast(mu, kappa).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized von Mises distribution. - - See Also - -------- - scipy.stats.vonmises : probability density function, distribution, or - cumulative density function, etc. - - Notes - ----- - The probability density for the von Mises distribution is - - .. math:: p(x) = \\frac{e^{\\kappa cos(x-\\mu)}}{2\\pi I_0(\\kappa)}, - - where :math:`\\mu` is the mode and :math:`\\kappa` the dispersion, - and :math:`I_0(\\kappa)` is the modified Bessel function of order 0. - - The von Mises is named for Richard Edler von Mises, who was born in - Austria-Hungary, in what is now the Ukraine. He fled to the United - States in 1939 and became a professor at Harvard. He worked in - probability theory, aerodynamics, fluid mechanics, and philosophy of - science. - - References - ---------- - .. [1] Abramowitz, M. and Stegun, I. A. (Eds.). "Handbook of - Mathematical Functions with Formulas, Graphs, and Mathematical - Tables, 9th printing," New York: Dover, 1972. - .. [2] von Mises, R., "Mathematical Theory of Probability - and Statistics", New York: Academic Press, 1964. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, kappa = 0.0, 4.0 # mean and dispersion - >>> s = np.random.vonmises(mu, kappa, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> from scipy.special import i0 # doctest: +SKIP - >>> plt.hist(s, 50, density=True) - >>> x = np.linspace(-np.pi, np.pi, num=51) - >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) # doctest: +SKIP - >>> plt.plot(x, y, linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - return cont(&random_vonmises, &self._bitgen, size, self.lock, 2, - mu, "mu", CONS_NONE, - kappa, "kappa", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def pareto(self, a, size=None): - """ - pareto(a, size=None) - - Draw samples from a Pareto II or Lomax distribution with - specified shape. - - The Lomax or Pareto II distribution is a shifted Pareto - distribution. The classical Pareto distribution can be - obtained from the Lomax distribution by adding 1 and - multiplying by the scale parameter ``m`` (see Notes). The - smallest value of the Lomax distribution is zero while for the - classical Pareto distribution it is ``mu``, where the standard - Pareto distribution has location ``mu = 1``. Lomax can also - be considered as a simplified version of the Generalized - Pareto distribution (available in SciPy), with the scale set - to one and the location set to zero. - - The Pareto distribution must be greater than zero, and is - unbounded above. It is also known as the "80-20 rule". In - this distribution, 80 percent of the weights are in the lowest - 20 percent of the range, while the other 20 percent fill the - remaining 80 percent of the range. - - Parameters - ---------- - a : float or array_like of floats - Shape of the distribution. Must be positive. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Pareto distribution. - - See Also - -------- - scipy.stats.lomax : probability density function, distribution or - cumulative density function, etc. - scipy.stats.genpareto : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Pareto distribution is - - .. math:: p(x) = \\frac{am^a}{x^{a+1}} - - where :math:`a` is the shape and :math:`m` the scale. - - The Pareto distribution, named after the Italian economist - Vilfredo Pareto, is a power law probability distribution - useful in many real world problems. Outside the field of - economics it is generally referred to as the Bradford - distribution. Pareto developed the distribution to describe - the distribution of wealth in an economy. It has also found - use in insurance, web page access statistics, oil field sizes, - and many other problems, including the download frequency for - projects in Sourceforge [1]_. It is one of the so-called - "fat-tailed" distributions. - - References - ---------- - .. [1] Francis Hunt and Paul Johnson, On the Pareto Distribution of - Sourceforge projects. - .. [2] Pareto, V. (1896). Course of Political Economy. Lausanne. - .. [3] Reiss, R.D., Thomas, M.(2001), Statistical Analysis of Extreme - Values, Birkhauser Verlag, Basel, pp 23-30. - .. [4] Wikipedia, "Pareto distribution", - https://en.wikipedia.org/wiki/Pareto_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a, m = 3., 2. # shape and mode - >>> s = (np.random.pareto(a, 1000) + 1) * m - - Display the histogram of the samples, along with the probability - density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, _ = plt.hist(s, 100, density=True) - >>> fit = a*m**a / bins**(a+1) - >>> plt.plot(bins, max(count)*fit/max(fit), linewidth=2, color='r') - >>> plt.show() - - """ - return cont(&legacy_pareto, &self._aug_state, size, self.lock, 1, - a, "a", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def weibull(self, a, size=None): - """ - weibull(a, size=None) - - Draw samples from a Weibull distribution. - - Draw samples from a 1-parameter Weibull distribution with the given - shape parameter `a`. - - .. math:: X = (-ln(U))^{1/a} - - Here, U is drawn from the uniform distribution over (0,1]. - - The more common 2-parameter Weibull, including a scale parameter - :math:`\\lambda` is just :math:`X = \\lambda(-ln(U))^{1/a}`. - - Parameters - ---------- - a : float or array_like of floats - Shape parameter of the distribution. Must be nonnegative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Weibull distribution. - - See Also - -------- - scipy.stats.weibull_max - scipy.stats.weibull_min - scipy.stats.genextreme - gumbel - - Notes - ----- - The Weibull (or Type III asymptotic extreme value distribution - for smallest values, SEV Type III, or Rosin-Rammler - distribution) is one of a class of Generalized Extreme Value - (GEV) distributions used in modeling extreme value problems. - This class includes the Gumbel and Frechet distributions. - - The probability density for the Weibull distribution is - - .. math:: p(x) = \\frac{a} - {\\lambda}(\\frac{x}{\\lambda})^{a-1}e^{-(x/\\lambda)^a}, - - where :math:`a` is the shape and :math:`\\lambda` the scale. - - The function has its peak (the mode) at - :math:`\\lambda(\\frac{a-1}{a})^{1/a}`. - - When ``a = 1``, the Weibull distribution reduces to the exponential - distribution. - - References - ---------- - .. [1] Waloddi Weibull, Royal Technical University, Stockholm, - 1939 "A Statistical Theory Of The Strength Of Materials", - Ingeniorsvetenskapsakademiens Handlingar Nr 151, 1939, - Generalstabens Litografiska Anstalts Forlag, Stockholm. - .. [2] Waloddi Weibull, "A Statistical Distribution Function of - Wide Applicability", Journal Of Applied Mechanics ASME Paper - 1951. - .. [3] Wikipedia, "Weibull distribution", - https://en.wikipedia.org/wiki/Weibull_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a = 5. # shape - >>> s = np.random.weibull(a, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> x = np.arange(1,100.)/50. - >>> def weib(x,n,a): - ... return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a) - - >>> count, bins, ignored = plt.hist(np.random.weibull(5.,1000)) - >>> x = np.arange(1,100.)/50. - >>> scale = count.max()/weib(x, 1., 5.).max() - >>> plt.plot(x, weib(x, 1., 5.)*scale) - >>> plt.show() - - """ - return cont(&legacy_weibull, &self._aug_state, size, self.lock, 1, - a, "a", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def power(self, a, size=None): - """ - power(a, size=None) - - Draws samples in [0, 1] from a power distribution with positive - exponent a - 1. - - Also known as the power function distribution. - - Parameters - ---------- - a : float or array_like of floats - Parameter of the distribution. Must be non-negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized power distribution. - - Raises - ------ - ValueError - If a < 1. - - Notes - ----- - The probability density function is - - .. math:: P(x; a) = ax^{a-1}, 0 \\le x \\le 1, a>0. - - The power function distribution is just the inverse of the Pareto - distribution. It may also be seen as a special case of the Beta - distribution. - - It is used, for example, in modeling the over-reporting of insurance - claims. - - References - ---------- - .. [1] Christian Kleiber, Samuel Kotz, "Statistical size distributions - in economics and actuarial sciences", Wiley, 2003. - .. [2] Heckert, N. A. and Filliben, James J. "NIST Handbook 148: - Dataplot Reference Manual, Volume 2: Let Subcommands and Library - Functions", National Institute of Standards and Technology - Handbook Series, June 2003. - https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/powpdf.pdf - - Examples - -------- - Draw samples from the distribution: - - >>> a = 5. # shape - >>> samples = 1000 - >>> s = np.random.power(a, samples) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, bins=30) - >>> x = np.linspace(0, 1, 100) - >>> y = a*x**(a-1.) - >>> normed_y = samples*np.diff(bins)[0]*y - >>> plt.plot(x, normed_y) - >>> plt.show() - - Compare the power function distribution to the inverse of the Pareto. - - >>> from scipy import stats # doctest: +SKIP - >>> rvs = np.random.power(5, 1000000) - >>> rvsp = np.random.pareto(5, 1000000) - >>> xx = np.linspace(0,1,100) - >>> powpdf = stats.powerlaw.pdf(xx,5) # doctest: +SKIP - - >>> plt.figure() - >>> plt.hist(rvs, bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('np.random.power(5)') - - >>> plt.figure() - >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('inverse of 1 + np.random.pareto(5)') - - >>> plt.figure() - >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP - >>> plt.title('inverse of stats.pareto(5)') - - """ - return cont(&legacy_power, &self._aug_state, size, self.lock, 1, - a, "a", CONS_POSITIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def laplace(self, loc=0.0, scale=1.0, size=None): - """ - laplace(loc=0.0, scale=1.0, size=None) - - Draw samples from the Laplace or double exponential distribution with - specified location (or mean) and scale (decay). - - The Laplace distribution is similar to the Gaussian/normal distribution, - but is sharper at the peak and has fatter tails. It represents the - difference between two independent, identically distributed exponential - random variables. - - Parameters - ---------- - loc : float or array_like of floats, optional - The position, :math:`\\mu`, of the distribution peak. Default is 0. - scale : float or array_like of floats, optional - :math:`\\lambda`, the exponential decay. Default is 1. Must be non- - negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Laplace distribution. - - Notes - ----- - It has the probability density function - - .. math:: f(x; \\mu, \\lambda) = \\frac{1}{2\\lambda} - \\exp\\left(-\\frac{|x - \\mu|}{\\lambda}\\right). - - The first law of Laplace, from 1774, states that the frequency - of an error can be expressed as an exponential function of the - absolute magnitude of the error, which leads to the Laplace - distribution. For many problems in economics and health - sciences, this distribution seems to model the data better - than the standard Gaussian distribution. - - References - ---------- - .. [1] Abramowitz, M. and Stegun, I. A. (Eds.). "Handbook of - Mathematical Functions with Formulas, Graphs, and Mathematical - Tables, 9th printing," New York: Dover, 1972. - .. [2] Kotz, Samuel, et. al. "The Laplace Distribution and - Generalizations, " Birkhauser, 2001. - .. [3] Weisstein, Eric W. "Laplace Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/LaplaceDistribution.html - .. [4] Wikipedia, "Laplace distribution", - https://en.wikipedia.org/wiki/Laplace_distribution - - Examples - -------- - Draw samples from the distribution - - >>> loc, scale = 0., 1. - >>> s = np.random.laplace(loc, scale, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> x = np.arange(-8., 8., .01) - >>> pdf = np.exp(-abs(x-loc)/scale)/(2.*scale) - >>> plt.plot(x, pdf) - - Plot Gaussian for comparison: - - >>> g = (1/(scale * np.sqrt(2 * np.pi)) * - ... np.exp(-(x - loc)**2 / (2 * scale**2))) - >>> plt.plot(x,g) - - """ - return cont(&random_laplace, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def gumbel(self, loc=0.0, scale=1.0, size=None): - """ - gumbel(loc=0.0, scale=1.0, size=None) - - Draw samples from a Gumbel distribution. - - Draw samples from a Gumbel distribution with specified location and - scale. For more information on the Gumbel distribution, see - Notes and References below. - - Parameters - ---------- - loc : float or array_like of floats, optional - The location of the mode of the distribution. Default is 0. - scale : float or array_like of floats, optional - The scale parameter of the distribution. Default is 1. Must be non- - negative. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Gumbel distribution. - - See Also - -------- - scipy.stats.gumbel_l - scipy.stats.gumbel_r - scipy.stats.genextreme - weibull - - Notes - ----- - The Gumbel (or Smallest Extreme Value (SEV) or the Smallest Extreme - Value Type I) distribution is one of a class of Generalized Extreme - Value (GEV) distributions used in modeling extreme value problems. - The Gumbel is a special case of the Extreme Value Type I distribution - for maximums from distributions with "exponential-like" tails. - - The probability density for the Gumbel distribution is - - .. math:: p(x) = \\frac{e^{-(x - \\mu)/ \\beta}}{\\beta} e^{ -e^{-(x - \\mu)/ - \\beta}}, - - where :math:`\\mu` is the mode, a location parameter, and - :math:`\\beta` is the scale parameter. - - The Gumbel (named for German mathematician Emil Julius Gumbel) was used - very early in the hydrology literature, for modeling the occurrence of - flood events. It is also used for modeling maximum wind speed and - rainfall rates. It is a "fat-tailed" distribution - the probability of - an event in the tail of the distribution is larger than if one used a - Gaussian, hence the surprisingly frequent occurrence of 100-year - floods. Floods were initially modeled as a Gaussian process, which - underestimated the frequency of extreme events. - - It is one of a class of extreme value distributions, the Generalized - Extreme Value (GEV) distributions, which also includes the Weibull and - Frechet. - - The function has a mean of :math:`\\mu + 0.57721\\beta` and a variance - of :math:`\\frac{\\pi^2}{6}\\beta^2`. - - References - ---------- - .. [1] Gumbel, E. J., "Statistics of Extremes," - New York: Columbia University Press, 1958. - .. [2] Reiss, R.-D. and Thomas, M., "Statistical Analysis of Extreme - Values from Insurance, Finance, Hydrology and Other Fields," - Basel: Birkhauser Verlag, 2001. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, beta = 0, 0.1 # location and scale - >>> s = np.random.gumbel(mu, beta, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 30, density=True) - >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta) - ... * np.exp( -np.exp( -(bins - mu) /beta) ), - ... linewidth=2, color='r') - >>> plt.show() - - Show how an extreme value distribution can arise from a Gaussian process - and compare to a Gaussian: - - >>> means = [] - >>> maxima = [] - >>> for i in range(0,1000) : - ... a = np.random.normal(mu, beta, 1000) - ... means.append(a.mean()) - ... maxima.append(a.max()) - >>> count, bins, ignored = plt.hist(maxima, 30, density=True) - >>> beta = np.std(maxima) * np.sqrt(6) / np.pi - >>> mu = np.mean(maxima) - 0.57721*beta - >>> plt.plot(bins, (1/beta)*np.exp(-(bins - mu)/beta) - ... * np.exp(-np.exp(-(bins - mu)/beta)), - ... linewidth=2, color='r') - >>> plt.plot(bins, 1/(beta * np.sqrt(2 * np.pi)) - ... * np.exp(-(bins - mu)**2 / (2 * beta**2)), - ... linewidth=2, color='g') - >>> plt.show() - - """ - return cont(&random_gumbel, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def logistic(self, loc=0.0, scale=1.0, size=None): - """ - logistic(loc=0.0, scale=1.0, size=None) - - Draw samples from a logistic distribution. - - Samples are drawn from a logistic distribution with specified - parameters, loc (location or mean, also median), and scale (>0). - - Parameters - ---------- - loc : float or array_like of floats, optional - Parameter of the distribution. Default is 0. - scale : float or array_like of floats, optional - Parameter of the distribution. Must be non-negative. - Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``loc`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized logistic distribution. - - See Also - -------- - scipy.stats.logistic : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Logistic distribution is - - .. math:: P(x) = P(x) = \\frac{e^{-(x-\\mu)/s}}{s(1+e^{-(x-\\mu)/s})^2}, - - where :math:`\\mu` = location and :math:`s` = scale. - - The Logistic distribution is used in Extreme Value problems where it - can act as a mixture of Gumbel distributions, in Epidemiology, and by - the World Chess Federation (FIDE) where it is used in the Elo ranking - system, assuming the performance of each player is a logistically - distributed random variable. - - References - ---------- - .. [1] Reiss, R.-D. and Thomas M. (2001), "Statistical Analysis of - Extreme Values, from Insurance, Finance, Hydrology and Other - Fields," Birkhauser Verlag, Basel, pp 132-133. - .. [2] Weisstein, Eric W. "Logistic Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/LogisticDistribution.html - .. [3] Wikipedia, "Logistic-distribution", - https://en.wikipedia.org/wiki/Logistic_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> loc, scale = 10, 1 - >>> s = np.random.logistic(loc, scale, 10000) - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, bins=50) - - # plot against distribution - - >>> def logist(x, loc, scale): - ... return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2) - >>> lgst_val = logist(bins, loc, scale) - >>> plt.plot(bins, lgst_val * count.max() / lgst_val.max()) - >>> plt.show() - - """ - return cont(&random_logistic, &self._bitgen, size, self.lock, 2, - loc, "loc", CONS_NONE, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def lognormal(self, mean=0.0, sigma=1.0, size=None): - """ - lognormal(mean=0.0, sigma=1.0, size=None) - - Draw samples from a log-normal distribution. - - Draw samples from a log-normal distribution with specified mean, - standard deviation, and array shape. Note that the mean and standard - deviation are not the values for the distribution itself, but of the - underlying normal distribution it is derived from. - - Parameters - ---------- - mean : float or array_like of floats, optional - Mean value of the underlying normal distribution. Default is 0. - sigma : float or array_like of floats, optional - Standard deviation of the underlying normal distribution. Must be - non-negative. Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mean`` and ``sigma`` are both scalars. - Otherwise, ``np.broadcast(mean, sigma).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized log-normal distribution. - - See Also - -------- - scipy.stats.lognorm : probability density function, distribution, - cumulative density function, etc. - - Notes - ----- - A variable `x` has a log-normal distribution if `log(x)` is normally - distributed. The probability density function for the log-normal - distribution is: - - .. math:: p(x) = \\frac{1}{\\sigma x \\sqrt{2\\pi}} - e^{(-\\frac{(ln(x)-\\mu)^2}{2\\sigma^2})} - - where :math:`\\mu` is the mean and :math:`\\sigma` is the standard - deviation of the normally distributed logarithm of the variable. - A log-normal distribution results if a random variable is the *product* - of a large number of independent, identically-distributed variables in - the same way that a normal distribution results if the variable is the - *sum* of a large number of independent, identically-distributed - variables. - - References - ---------- - .. [1] Limpert, E., Stahel, W. A., and Abbt, M., "Log-normal - Distributions across the Sciences: Keys and Clues," - BioScience, Vol. 51, No. 5, May, 2001. - https://stat.ethz.ch/~stahel/lognormal/bioscience.pdf - .. [2] Reiss, R.D. and Thomas, M., "Statistical Analysis of Extreme - Values," Basel: Birkhauser Verlag, 2001, pp. 31-32. - - Examples - -------- - Draw samples from the distribution: - - >>> mu, sigma = 3., 1. # mean and standard deviation - >>> s = np.random.lognormal(mu, sigma, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 100, density=True, align='mid') - - >>> x = np.linspace(min(bins), max(bins), 10000) - >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) - ... / (x * sigma * np.sqrt(2 * np.pi))) - - >>> plt.plot(x, pdf, linewidth=2, color='r') - >>> plt.axis('tight') - >>> plt.show() - - Demonstrate that taking the products of random samples from a uniform - distribution can be fit well by a log-normal probability density - function. - - >>> # Generate a thousand samples: each is the product of 100 random - >>> # values, drawn from a normal distribution. - >>> b = [] - >>> for i in range(1000): - ... a = 10. + np.random.standard_normal(100) - ... b.append(np.product(a)) - - >>> b = np.array(b) / np.min(b) # scale values to be positive - >>> count, bins, ignored = plt.hist(b, 100, density=True, align='mid') - >>> sigma = np.std(np.log(b)) - >>> mu = np.mean(np.log(b)) - - >>> x = np.linspace(min(bins), max(bins), 10000) - >>> pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) - ... / (x * sigma * np.sqrt(2 * np.pi))) - - >>> plt.plot(x, pdf, color='r', linewidth=2) - >>> plt.show() - - """ - return cont(&legacy_lognormal, &self._aug_state, size, self.lock, 2, - mean, "mean", CONS_NONE, - sigma, "sigma", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, None) - - def rayleigh(self, scale=1.0, size=None): - """ - rayleigh(scale=1.0, size=None) - - Draw samples from a Rayleigh distribution. - - The :math:`\\chi` and Weibull distributions are generalizations of the - Rayleigh. - - Parameters - ---------- - scale : float or array_like of floats, optional - Scale, also equals the mode. Must be non-negative. Default is 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``scale`` is a scalar. Otherwise, - ``np.array(scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Rayleigh distribution. - - Notes - ----- - The probability density function for the Rayleigh distribution is - - .. math:: P(x;scale) = \\frac{x}{scale^2}e^{\\frac{-x^2}{2 \\cdotp scale^2}} - - The Rayleigh distribution would arise, for example, if the East - and North components of the wind velocity had identical zero-mean - Gaussian distributions. Then the wind speed would have a Rayleigh - distribution. - - References - ---------- - .. [1] Brighton Webs Ltd., "Rayleigh Distribution," - https://web.archive.org/web/20090514091424/http://brighton-webs.co.uk:80/distributions/rayleigh.asp - .. [2] Wikipedia, "Rayleigh distribution" - https://en.wikipedia.org/wiki/Rayleigh_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram - - >>> from matplotlib.pyplot import hist - >>> values = hist(np.random.rayleigh(3, 100000), bins=200, density=True) - - Wave heights tend to follow a Rayleigh distribution. If the mean wave - height is 1 meter, what fraction of waves are likely to be larger than 3 - meters? - - >>> meanvalue = 1 - >>> modevalue = np.sqrt(2 / np.pi) * meanvalue - >>> s = np.random.rayleigh(modevalue, 1000000) - - The percentage of waves larger than 3 meters is: - - >>> 100.*sum(s>3)/1000000. - 0.087300000000000003 # random - - """ - return cont(&random_rayleigh, &self._bitgen, size, self.lock, 1, - scale, "scale", CONS_NON_NEGATIVE, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE, None) - - def wald(self, mean, scale, size=None): - """ - wald(mean, scale, size=None) - - Draw samples from a Wald, or inverse Gaussian, distribution. - - As the scale approaches infinity, the distribution becomes more like a - Gaussian. Some references claim that the Wald is an inverse Gaussian - with mean equal to 1, but this is by no means universal. - - The inverse Gaussian distribution was first studied in relationship to - Brownian motion. In 1956 M.C.K. Tweedie used the name inverse Gaussian - because there is an inverse relationship between the time to cover a - unit distance and distance covered in unit time. - - Parameters - ---------- - mean : float or array_like of floats - Distribution mean, must be > 0. - scale : float or array_like of floats - Scale parameter, must be > 0. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``mean`` and ``scale`` are both scalars. - Otherwise, ``np.broadcast(mean, scale).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Wald distribution. - - Notes - ----- - The probability density function for the Wald distribution is - - .. math:: P(x;mean,scale) = \\sqrt{\\frac{scale}{2\\pi x^3}}e^ - \\frac{-scale(x-mean)^2}{2\\cdotp mean^2x} - - As noted above the inverse Gaussian distribution first arise - from attempts to model Brownian motion. It is also a - competitor to the Weibull for use in reliability modeling and - modeling stock returns and interest rate processes. - - References - ---------- - .. [1] Brighton Webs Ltd., Wald Distribution, - https://web.archive.org/web/20090423014010/http://www.brighton-webs.co.uk:80/distributions/wald.asp - .. [2] Chhikara, Raj S., and Folks, J. Leroy, "The Inverse Gaussian - Distribution: Theory : Methodology, and Applications", CRC Press, - 1988. - .. [3] Wikipedia, "Inverse Gaussian distribution" - https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram: - - >>> import matplotlib.pyplot as plt - >>> h = plt.hist(np.random.wald(3, 2, 100000), bins=200, density=True) - >>> plt.show() - - """ - return cont(&legacy_wald, &self._aug_state, size, self.lock, 2, - mean, "mean", CONS_POSITIVE, - scale, "scale", CONS_POSITIVE, - 0.0, "", CONS_NONE, None) - - def triangular(self, left, mode, right, size=None): - """ - triangular(left, mode, right, size=None) - - Draw samples from the triangular distribution over the - interval ``[left, right]``. - - The triangular distribution is a continuous probability - distribution with lower limit left, peak at mode, and upper - limit right. Unlike the other distributions, these parameters - directly define the shape of the pdf. - - Parameters - ---------- - left : float or array_like of floats - Lower limit. - mode : float or array_like of floats - The value where the peak of the distribution occurs. - The value must fulfill the condition ``left <= mode <= right``. - right : float or array_like of floats - Upper limit, must be larger than `left`. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``left``, ``mode``, and ``right`` - are all scalars. Otherwise, ``np.broadcast(left, mode, right).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized triangular distribution. - - Notes - ----- - The probability density function for the triangular distribution is - - .. math:: P(x;l, m, r) = \\begin{cases} - \\frac{2(x-l)}{(r-l)(m-l)}& \\text{for $l \\leq x \\leq m$},\\\\ - \\frac{2(r-x)}{(r-l)(r-m)}& \\text{for $m \\leq x \\leq r$},\\\\ - 0& \\text{otherwise}. - \\end{cases} - - The triangular distribution is often used in ill-defined - problems where the underlying distribution is not known, but - some knowledge of the limits and mode exists. Often it is used - in simulations. - - References - ---------- - .. [1] Wikipedia, "Triangular distribution" - https://en.wikipedia.org/wiki/Triangular_distribution - - Examples - -------- - Draw values from the distribution and plot the histogram: - - >>> import matplotlib.pyplot as plt - >>> h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, - ... density=True) - >>> plt.show() - - """ - cdef bint is_scalar = True - cdef double fleft, fmode, fright - cdef np.ndarray oleft, omode, oright - - oleft = np.PyArray_FROM_OTF(left, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - omode = np.PyArray_FROM_OTF(mode, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - oright = np.PyArray_FROM_OTF(right, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(oleft) == np.PyArray_NDIM(omode) == np.PyArray_NDIM(oright) == 0: - fleft = PyFloat_AsDouble(left) - fright = PyFloat_AsDouble(right) - fmode = PyFloat_AsDouble(mode) - - if fleft > fmode: - raise ValueError("left > mode") - if fmode > fright: - raise ValueError("mode > right") - if fleft == fright: - raise ValueError("left == right") - return cont(&random_triangular, &self._bitgen, size, self.lock, 3, - fleft, "", CONS_NONE, - fmode, "", CONS_NONE, - fright, "", CONS_NONE, None) - - if np.any(np.greater(oleft, omode)): - raise ValueError("left > mode") - if np.any(np.greater(omode, oright)): - raise ValueError("mode > right") - if np.any(np.equal(oleft, oright)): - raise ValueError("left == right") - - return cont_broadcast_3(&random_triangular, &self._bitgen, size, self.lock, - oleft, "", CONS_NONE, - omode, "", CONS_NONE, - oright, "", CONS_NONE) - - # Complicated, discrete distributions: - def binomial(self, n, p, size=None): - """ - binomial(n, p, size=None) - - Draw samples from a binomial distribution. - - Samples are drawn from a binomial distribution with specified - parameters, n trials and p probability of success where - n an integer >= 0 and p is in the interval [0,1]. (n may be - input as a float, but it is truncated to an integer in use) - - Parameters - ---------- - n : int or array_like of ints - Parameter of the distribution, >= 0. Floats are also accepted, - but they will be truncated to integers. - p : float or array_like of floats - Parameter of the distribution, >= 0 and <=1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``n`` and ``p`` are both scalars. - Otherwise, ``np.broadcast(n, p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized binomial distribution, where - each sample is equal to the number of successes over the n trials. - - See Also - -------- - scipy.stats.binom : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the binomial distribution is - - .. math:: P(N) = \\binom{n}{N}p^N(1-p)^{n-N}, - - where :math:`n` is the number of trials, :math:`p` is the probability - of success, and :math:`N` is the number of successes. - - When estimating the standard error of a proportion in a population by - using a random sample, the normal distribution works well unless the - product p*n <=5, where p = population proportion estimate, and n = - number of samples, in which case the binomial distribution is used - instead. For example, a sample of 15 people shows 4 who are left - handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4, - so the binomial distribution should be used in this case. - - References - ---------- - .. [1] Dalgaard, Peter, "Introductory Statistics with R", - Springer-Verlag, 2002. - .. [2] Glantz, Stanton A. "Primer of Biostatistics.", McGraw-Hill, - Fifth Edition, 2002. - .. [3] Lentner, Marvin, "Elementary Applied Statistics", Bogden - and Quigley, 1972. - .. [4] Weisstein, Eric W. "Binomial Distribution." From MathWorld--A - Wolfram Web Resource. - https://mathworld.wolfram.com/BinomialDistribution.html - .. [5] Wikipedia, "Binomial distribution", - https://en.wikipedia.org/wiki/Binomial_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> n, p = 10, .5 # number of trials, probability of each trial - >>> s = np.random.binomial(n, p, 1000) - # result of flipping a coin 10 times, tested 1000 times. - - A real world example. A company drills 9 wild-cat oil exploration - wells, each with an estimated probability of success of 0.1. All nine - wells fail. What is the probability of that happening? - - Let's do 20,000 trials of the model, and count the number that - generate zero positive results. - - >>> sum(np.random.binomial(9, 0.1, 20000) == 0)/20000. - # answer = 0.38885, or 39%. - - """ - - # Uses a custom implementation since self._binomial is required - cdef double _dp = 0 - cdef long _in = 0 - cdef bint is_scalar = True - cdef np.npy_intp i, cnt - cdef np.ndarray randoms - cdef long *randoms_data - cdef np.broadcast it - - p_arr = np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, api.NPY_ARRAY_ALIGNED) - is_scalar = is_scalar and np.PyArray_NDIM(p_arr) == 0 - n_arr = np.PyArray_FROM_OTF(n, np.NPY_LONG, api.NPY_ARRAY_ALIGNED) - is_scalar = is_scalar and np.PyArray_NDIM(n_arr) == 0 - - if not is_scalar: - check_array_constraint(p_arr, "p", CONS_BOUNDED_0_1) - check_array_constraint(n_arr, "n", CONS_NON_NEGATIVE) - if size is not None: - randoms = np.empty(size, int) - else: - it = np.PyArray_MultiIterNew2(p_arr, n_arr) - randoms = np.empty(it.shape, int) - - randoms_data = np.PyArray_DATA(randoms) - cnt = np.PyArray_SIZE(randoms) - - it = np.PyArray_MultiIterNew3(randoms, p_arr, n_arr) - validate_output_shape(it.shape, randoms) - with self.lock, nogil: - for i in range(cnt): - _dp = (np.PyArray_MultiIter_DATA(it, 1))[0] - _in = (np.PyArray_MultiIter_DATA(it, 2))[0] - (np.PyArray_MultiIter_DATA(it, 0))[0] = random_binomial(&self._bitgen, _dp, _in, &self._binomial) - - np.PyArray_MultiIter_NEXT(it) - - return randoms - - _dp = PyFloat_AsDouble(p) - _in = n - check_constraint(_dp, "p", CONS_BOUNDED_0_1) - check_constraint(_in, "n", CONS_NON_NEGATIVE) - - if size is None: - with self.lock: - return random_binomial(&self._bitgen, _dp, _in, &self._binomial) - - randoms = np.empty(size, int) - cnt = np.PyArray_SIZE(randoms) - randoms_data = np.PyArray_DATA(randoms) - - with self.lock, nogil: - for i in range(cnt): - randoms_data[i] = random_binomial(&self._bitgen, _dp, _in, - &self._binomial) - - return randoms - - def negative_binomial(self, n, p, size=None): - """ - negative_binomial(n, p, size=None) - - Draw samples from a negative binomial distribution. - - Samples are drawn from a negative binomial distribution with specified - parameters, `n` successes and `p` probability of success where `n` - is > 0 and `p` is in the interval (0, 1]. - - Parameters - ---------- - n : float or array_like of floats - Parameter of the distribution, > 0. - p : float or array_like of floats - Parameter of the distribution. Must satisfy 0 < p <= 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``n`` and ``p`` are both scalars. - Otherwise, ``np.broadcast(n, p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized negative binomial distribution, - where each sample is equal to N, the number of failures that - occurred before a total of n successes was reached. - - Notes - ----- - The probability mass function of the negative binomial distribution is - - .. math:: P(N;n,p) = \\frac{\\Gamma(N+n)}{N!\\Gamma(n)}p^{n}(1-p)^{N}, - - where :math:`n` is the number of successes, :math:`p` is the - probability of success, :math:`N+n` is the number of trials, and - :math:`\\Gamma` is the gamma function. When :math:`n` is an integer, - :math:`\\frac{\\Gamma(N+n)}{N!\\Gamma(n)} = \\binom{N+n-1}{N}`, which is - the more common form of this term in the the pmf. The negative - binomial distribution gives the probability of N failures given n - successes, with a success on the last trial. - - If one throws a die repeatedly until the third time a "1" appears, - then the probability distribution of the number of non-"1"s that - appear before the third "1" is a negative binomial distribution. - - References - ---------- - .. [1] Weisstein, Eric W. "Negative Binomial Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/NegativeBinomialDistribution.html - .. [2] Wikipedia, "Negative binomial distribution", - https://en.wikipedia.org/wiki/Negative_binomial_distribution - - Examples - -------- - Draw samples from the distribution: - - A real world example. A company drills wild-cat oil - exploration wells, each with an estimated probability of - success of 0.1. What is the probability of having one success - for each successive well, that is what is the probability of a - single success after drilling 5 wells, after 6 wells, etc.? - - >>> s = np.random.negative_binomial(1, 0.1, 100000) - >>> for i in range(1, 11): # doctest: +SKIP - ... probability = sum(s= 0. A sequence of expectation - intervals must be broadcastable over the requested size. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``lam`` is a scalar. Otherwise, - ``np.array(lam).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Poisson distribution. - - Notes - ----- - The Poisson distribution - - .. math:: f(k; \\lambda)=\\frac{\\lambda^k e^{-\\lambda}}{k!} - - For events with an expected separation :math:`\\lambda` the Poisson - distribution :math:`f(k; \\lambda)` describes the probability of - :math:`k` events occurring within the observed - interval :math:`\\lambda`. - - Because the output is limited to the range of the C int64 type, a - ValueError is raised when `lam` is within 10 sigma of the maximum - representable value. - - References - ---------- - .. [1] Weisstein, Eric W. "Poisson Distribution." - From MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/PoissonDistribution.html - .. [2] Wikipedia, "Poisson distribution", - https://en.wikipedia.org/wiki/Poisson_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> import numpy as np - >>> s = np.random.poisson(5, 10000) - - Display histogram of the sample: - - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s, 14, density=True) - >>> plt.show() - - Draw each 100 values for lambda 100 and 500: - - >>> s = np.random.poisson(lam=(100., 500.), size=(100, 2)) - - """ - out = disc(&legacy_random_poisson, &self._bitgen, size, self.lock, 1, 0, - lam, "lam", LEGACY_CONS_POISSON, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - # Match historical output type - return int64_to_long(out) - - def zipf(self, a, size=None): - """ - zipf(a, size=None) - - Draw samples from a Zipf distribution. - - Samples are drawn from a Zipf distribution with specified parameter - `a` > 1. - - The Zipf distribution (also known as the zeta distribution) is a - continuous probability distribution that satisfies Zipf's law: the - frequency of an item is inversely proportional to its rank in a - frequency table. - - Parameters - ---------- - a : float or array_like of floats - Distribution parameter. Must be greater than 1. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``a`` is a scalar. Otherwise, - ``np.array(a).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized Zipf distribution. - - See Also - -------- - scipy.stats.zipf : probability density function, distribution, or - cumulative density function, etc. - - Notes - ----- - The probability density for the Zipf distribution is - - .. math:: p(x) = \\frac{x^{-a}}{\\zeta(a)}, - - where :math:`\\zeta` is the Riemann Zeta function. - - It is named for the American linguist George Kingsley Zipf, who noted - that the frequency of any word in a sample of a language is inversely - proportional to its rank in the frequency table. - - References - ---------- - .. [1] Zipf, G. K., "Selected Studies of the Principle of Relative - Frequency in Language," Cambridge, MA: Harvard Univ. Press, - 1932. - - Examples - -------- - Draw samples from the distribution: - - >>> a = 2. # parameter - >>> s = np.random.zipf(a, 1000) - - Display the histogram of the samples, along with - the probability density function: - - >>> import matplotlib.pyplot as plt - >>> from scipy import special # doctest: +SKIP - - Truncate s values at 50 so plot is interesting: - - >>> count, bins, ignored = plt.hist(s[s<50], 50, density=True) - >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP - >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP - >>> plt.show() - - """ - out = disc(&legacy_random_zipf, &self._bitgen, size, self.lock, 1, 0, - a, "a", CONS_GT_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - # Match historical output type - return int64_to_long(out) - - def geometric(self, p, size=None): - """ - geometric(p, size=None) - - Draw samples from the geometric distribution. - - Bernoulli trials are experiments with one of two outcomes: - success or failure (an example of such an experiment is flipping - a coin). The geometric distribution models the number of trials - that must be run in order to achieve success. It is therefore - supported on the positive integers, ``k = 1, 2, ...``. - - The probability mass function of the geometric distribution is - - .. math:: f(k) = (1 - p)^{k - 1} p - - where `p` is the probability of success of an individual trial. - - Parameters - ---------- - p : float or array_like of floats - The probability of success of an individual trial. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``p`` is a scalar. Otherwise, - ``np.array(p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized geometric distribution. - - Examples - -------- - Draw ten thousand values from the geometric distribution, - with the probability of an individual success equal to 0.35: - - >>> z = np.random.geometric(p=0.35, size=10000) - - How many trials succeeded after a single run? - - >>> (z == 1).sum() / 10000. - 0.34889999999999999 # random - - """ - out = disc(&legacy_random_geometric, &self._bitgen, size, self.lock, 1, 0, - p, "p", CONS_BOUNDED_GT_0_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - # Match historical output type - return int64_to_long(out) - - def hypergeometric(self, ngood, nbad, nsample, size=None): - """ - hypergeometric(ngood, nbad, nsample, size=None) - - Draw samples from a Hypergeometric distribution. - - Samples are drawn from a hypergeometric distribution with specified - parameters, `ngood` (ways to make a good selection), `nbad` (ways to make - a bad selection), and `nsample` (number of items sampled, which is less - than or equal to the sum ``ngood + nbad``). - - Parameters - ---------- - ngood : int or array_like of ints - Number of ways to make a good selection. Must be nonnegative. - nbad : int or array_like of ints - Number of ways to make a bad selection. Must be nonnegative. - nsample : int or array_like of ints - Number of items sampled. Must be at least 1 and at most - ``ngood + nbad``. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if `ngood`, `nbad`, and `nsample` - are all scalars. Otherwise, ``np.broadcast(ngood, nbad, nsample).size`` - samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized hypergeometric distribution. Each - sample is the number of good items within a randomly selected subset of - size `nsample` taken from a set of `ngood` good items and `nbad` bad items. - - See Also - -------- - scipy.stats.hypergeom : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Hypergeometric distribution is - - .. math:: P(x) = \\frac{\\binom{g}{x}\\binom{b}{n-x}}{\\binom{g+b}{n}}, - - where :math:`0 \\le x \\le n` and :math:`n-b \\le x \\le g` - - for P(x) the probability of ``x`` good results in the drawn sample, - g = `ngood`, b = `nbad`, and n = `nsample`. - - Consider an urn with black and white marbles in it, `ngood` of them - are black and `nbad` are white. If you draw `nsample` balls without - replacement, then the hypergeometric distribution describes the - distribution of black balls in the drawn sample. - - Note that this distribution is very similar to the binomial - distribution, except that in this case, samples are drawn without - replacement, whereas in the Binomial case samples are drawn with - replacement (or the sample space is infinite). As the sample space - becomes large, this distribution approaches the binomial. - - References - ---------- - .. [1] Lentner, Marvin, "Elementary Applied Statistics", Bogden - and Quigley, 1972. - .. [2] Weisstein, Eric W. "Hypergeometric Distribution." From - MathWorld--A Wolfram Web Resource. - https://mathworld.wolfram.com/HypergeometricDistribution.html - .. [3] Wikipedia, "Hypergeometric distribution", - https://en.wikipedia.org/wiki/Hypergeometric_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> ngood, nbad, nsamp = 100, 2, 10 - # number of good, number of bad, and number of samples - >>> s = np.random.hypergeometric(ngood, nbad, nsamp, 1000) - >>> from matplotlib.pyplot import hist - >>> hist(s) - # note that it is very unlikely to grab both bad items - - Suppose you have an urn with 15 white and 15 black marbles. - If you pull 15 marbles at random, how likely is it that - 12 or more of them are one color? - - >>> s = np.random.hypergeometric(15, 15, 15, 100000) - >>> sum(s>=12)/100000. + sum(s<=3)/100000. - # answer = 0.003 ... pretty unlikely! - - """ - cdef np.ndarray ongood, onbad, onsample - cdef int64_t lngood, lnbad, lnsample - - # This cast to long is required to ensure that the values are inbounds - ongood = np.PyArray_FROM_OTF(ngood, np.NPY_LONG, api.NPY_ARRAY_ALIGNED) - onbad = np.PyArray_FROM_OTF(nbad, np.NPY_LONG, api.NPY_ARRAY_ALIGNED) - onsample = np.PyArray_FROM_OTF(nsample, np.NPY_LONG, api.NPY_ARRAY_ALIGNED) - - if np.PyArray_NDIM(ongood) == np.PyArray_NDIM(onbad) == np.PyArray_NDIM(onsample) == 0: - - lngood = ngood - lnbad = nbad - lnsample = nsample - - if lngood + lnbad < lnsample: - raise ValueError("ngood + nbad < nsample") - out = disc(&legacy_random_hypergeometric, &self._bitgen, size, self.lock, 0, 3, - lngood, "ngood", CONS_NON_NEGATIVE, - lnbad, "nbad", CONS_NON_NEGATIVE, - lnsample, "nsample", CONS_GTE_1) - # Match historical output type - return int64_to_long(out) - - if np.any(np.less(np.add(ongood, onbad), onsample)): - raise ValueError("ngood + nbad < nsample") - # Convert to int64, if necessary, to use int64 infrastructure - ongood = ongood.astype(np.int64) - onbad = onbad.astype(np.int64) - onsample = onsample.astype(np.int64) - out = discrete_broadcast_iii(&legacy_random_hypergeometric, &self._bitgen, size, self.lock, - ongood, "ngood", CONS_NON_NEGATIVE, - onbad, "nbad", CONS_NON_NEGATIVE, - onsample, "nsample", CONS_GTE_1) - # Match historical output type - return int64_to_long(out) - - def logseries(self, p, size=None): - """ - logseries(p, size=None) - - Draw samples from a logarithmic series distribution. - - Samples are drawn from a log series distribution with specified - shape parameter, 0 < ``p`` < 1. - - Parameters - ---------- - p : float or array_like of floats - Shape parameter for the distribution. Must be in the range (0, 1). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. If size is ``None`` (default), - a single value is returned if ``p`` is a scalar. Otherwise, - ``np.array(p).size`` samples are drawn. - - Returns - ------- - out : ndarray or scalar - Drawn samples from the parameterized logarithmic series distribution. - - See Also - -------- - scipy.stats.logser : probability density function, distribution or - cumulative density function, etc. - - Notes - ----- - The probability density for the Log Series distribution is - - .. math:: P(k) = \\frac{-p^k}{k \\ln(1-p)}, - - where p = probability. - - The log series distribution is frequently used to represent species - richness and occurrence, first proposed by Fisher, Corbet, and - Williams in 1943 [2]. It may also be used to model the numbers of - occupants seen in cars [3]. - - References - ---------- - .. [1] Buzas, Martin A.; Culver, Stephen J., Understanding regional - species diversity through the log series distribution of - occurrences: BIODIVERSITY RESEARCH Diversity & Distributions, - Volume 5, Number 5, September 1999 , pp. 187-195(9). - .. [2] Fisher, R.A,, A.S. Corbet, and C.B. Williams. 1943. The - relation between the number of species and the number of - individuals in a random sample of an animal population. - Journal of Animal Ecology, 12:42-58. - .. [3] D. J. Hand, F. Daly, D. Lunn, E. Ostrowski, A Handbook of Small - Data Sets, CRC Press, 1994. - .. [4] Wikipedia, "Logarithmic distribution", - https://en.wikipedia.org/wiki/Logarithmic_distribution - - Examples - -------- - Draw samples from the distribution: - - >>> a = .6 - >>> s = np.random.logseries(a, 10000) - >>> import matplotlib.pyplot as plt - >>> count, bins, ignored = plt.hist(s) - - # plot against distribution - - >>> def logseries(k, p): - ... return -p**k/(k*np.log(1-p)) - >>> plt.plot(bins, logseries(bins, a)*count.max()/ - ... logseries(bins, a).max(), 'r') - >>> plt.show() - - """ - out = disc(&legacy_random_logseries, &self._bitgen, size, self.lock, 1, 0, - p, "p", CONS_BOUNDED_0_1, - 0.0, "", CONS_NONE, - 0.0, "", CONS_NONE) - # Match historical output type - return int64_to_long(out) - - # Multivariate distributions: - def multivariate_normal(self, mean, cov, size=None, check_valid="warn", - tol=1e-8): - """ - multivariate_normal(mean, cov, size=None, check_valid='warn', tol=1e-8) - - Draw random samples from a multivariate normal distribution. - - The multivariate normal, multinormal or Gaussian distribution is a - generalization of the one-dimensional normal distribution to higher - dimensions. Such a distribution is specified by its mean and - covariance matrix. These parameters are analogous to the mean - (average or "center") and variance (standard deviation, or "width," - squared) of the one-dimensional normal distribution. - - Parameters - ---------- - mean : 1-D array_like, of length N - Mean of the N-dimensional distribution. - cov : 2-D array_like, of shape (N, N) - Covariance matrix of the distribution. It must be symmetric and - positive-semidefinite for proper sampling. - size : int or tuple of ints, optional - Given a shape of, for example, ``(m,n,k)``, ``m*n*k`` samples are - generated, and packed in an `m`-by-`n`-by-`k` arrangement. Because - each sample is `N`-dimensional, the output shape is ``(m,n,k,N)``. - If no shape is specified, a single (`N`-D) sample is returned. - check_valid : { 'warn', 'raise', 'ignore' }, optional - Behavior when the covariance matrix is not positive semidefinite. - tol : float, optional - Tolerance when checking the singular values in covariance matrix. - cov is cast to double before the check. - - Returns - ------- - out : ndarray - The drawn samples, of shape *size*, if that was provided. If not, - the shape is ``(N,)``. - - In other words, each entry ``out[i,j,...,:]`` is an N-dimensional - value drawn from the distribution. - - Notes - ----- - The mean is a coordinate in N-dimensional space, which represents the - location where samples are most likely to be generated. This is - analogous to the peak of the bell curve for the one-dimensional or - univariate normal distribution. - - Covariance indicates the level to which two variables vary together. - From the multivariate normal distribution, we draw N-dimensional - samples, :math:`X = [x_1, x_2, ... x_N]`. The covariance matrix - element :math:`C_{ij}` is the covariance of :math:`x_i` and :math:`x_j`. - The element :math:`C_{ii}` is the variance of :math:`x_i` (i.e. its - "spread"). - - Instead of specifying the full covariance matrix, popular - approximations include: - - - Spherical covariance (`cov` is a multiple of the identity matrix) - - Diagonal covariance (`cov` has non-negative elements, and only on - the diagonal) - - This geometrical property can be seen in two dimensions by plotting - generated data-points: - - >>> mean = [0, 0] - >>> cov = [[1, 0], [0, 100]] # diagonal covariance - - Diagonal covariance means that points are oriented along x or y-axis: - - >>> import matplotlib.pyplot as plt - >>> x, y = np.random.multivariate_normal(mean, cov, 5000).T - >>> plt.plot(x, y, 'x') - >>> plt.axis('equal') - >>> plt.show() - - Note that the covariance matrix must be positive semidefinite (a.k.a. - nonnegative-definite). Otherwise, the behavior of this method is - undefined and backwards compatibility is not guaranteed. - - References - ---------- - .. [1] Papoulis, A., "Probability, Random Variables, and Stochastic - Processes," 3rd ed., New York: McGraw-Hill, 1991. - .. [2] Duda, R. O., Hart, P. E., and Stork, D. G., "Pattern - Classification," 2nd ed., New York: Wiley, 2001. - - Examples - -------- - >>> mean = (1, 2) - >>> cov = [[1, 0], [0, 1]] - >>> x = np.random.multivariate_normal(mean, cov, (3, 3)) - >>> x.shape - (3, 3, 2) - - The following is probably true, given that 0.6 is roughly twice the - standard deviation: - - >>> list((x[0,0,:] - mean) < 0.6) - [True, True] # random - - """ - from numpy.linalg import svd - - # Check preconditions on arguments - mean = np.array(mean) - cov = np.array(cov) - if size is None: - shape = [] - elif isinstance(size, (int, np.integer)): - shape = [size] - else: - shape = size - - if len(mean.shape) != 1: - raise ValueError("mean must be 1 dimensional") - if (len(cov.shape) != 2) or (cov.shape[0] != cov.shape[1]): - raise ValueError("cov must be 2 dimensional and square") - if mean.shape[0] != cov.shape[0]: - raise ValueError("mean and cov must have same length") - - # Compute shape of output and create a matrix of independent - # standard normally distributed random numbers. The matrix has rows - # with the same length as mean and as many rows are necessary to - # form a matrix of shape final_shape. - final_shape = list(shape[:]) - final_shape.append(mean.shape[0]) - x = self.standard_normal(final_shape).reshape(-1, mean.shape[0]) - - # Transform matrix of standard normals into matrix where each row - # contains multivariate normals with the desired covariance. - # Compute A such that dot(transpose(A),A) == cov. - # Then the matrix products of the rows of x and A has the desired - # covariance. Note that sqrt(s)*v where (u,s,v) is the singular value - # decomposition of cov is such an A. - # - # Also check that cov is positive-semidefinite. If so, the u.T and v - # matrices should be equal up to roundoff error if cov is - # symmetric and the singular value of the corresponding row is - # not zero. We continue to use the SVD rather than Cholesky in - # order to preserve current outputs. Note that symmetry has not - # been checked. - - # GH10839, ensure double to make tol meaningful - cov = cov.astype(np.double) - (u, s, v) = svd(cov) - - if check_valid != "ignore": - if check_valid != "warn" and check_valid != "raise": - raise ValueError( - "check_valid must equal \"warn\", \"raise\", or \"ignore\"") - - psd = np.allclose(np.dot(v.T * s, v), cov, rtol=tol, atol=tol) - if not psd: - if check_valid == "warn": - warnings.warn("covariance is not positive-semidefinite.", - RuntimeWarning) - else: - raise ValueError( - "covariance is not positive-semidefinite.") - - x = np.dot(x, np.sqrt(s)[:, None] * v) - x += mean - x.shape = tuple(final_shape) - return x - - def multinomial(self, np.npy_intp n, object pvals, size=None): - """ - multinomial(n, pvals, size=None) - - Draw samples from a multinomial distribution. - - The multinomial distribution is a multivariate generalization of the - binomial distribution. Take an experiment with one of ``p`` - possible outcomes. An example of such an experiment is throwing a dice, - where the outcome can be 1 through 6. Each sample drawn from the - distribution represents `n` such experiments. Its values, - ``X_i = [X_0, X_1, ..., X_p]``, represent the number of times the - outcome was ``i``. - - Parameters - ---------- - n : int - Number of experiments. - pvals : sequence of floats, length p - Probabilities of each of the ``p`` different outcomes. These - must sum to 1 (however, the last element is always assumed to - account for the remaining probability, as long as - ``sum(pvals[:-1]) <= 1)``. - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. - - Returns - ------- - out : ndarray - The drawn samples, of shape *size*, if that was provided. If not, - the shape is ``(N,)``. - - In other words, each entry ``out[i,j,...,:]`` is an N-dimensional - value drawn from the distribution. - - Examples - -------- - Throw a dice 20 times: - - >>> np.random.multinomial(20, [1/6.]*6, size=1) - array([[4, 1, 7, 5, 2, 1]]) # random - - It landed 4 times on 1, once on 2, etc. - - Now, throw the dice 20 times, and 20 times again: - - >>> np.random.multinomial(20, [1/6.]*6, size=2) - array([[3, 4, 3, 3, 4, 3], # random - [2, 4, 3, 4, 0, 7]]) - - For the first run, we threw 3 times 1, 4 times 2, etc. For the second, - we threw 2 times 1, 4 times 2, etc. - - A loaded die is more likely to land on number 6: - - >>> np.random.multinomial(100, [1/7.]*5 + [2/7.]) - array([11, 16, 14, 17, 16, 26]) # random - - The probability inputs should be normalized. As an implementation - detail, the value of the last entry is ignored and assumed to take - up any leftover probability mass, but this should not be relied on. - A biased coin which has twice as much weight on one side as on the - other should be sampled like so: - - >>> np.random.multinomial(100, [1.0 / 3, 2.0 / 3]) # RIGHT - array([38, 62]) # random - - not like: - - >>> np.random.multinomial(100, [1.0, 2.0]) # WRONG - Traceback (most recent call last): - ValueError: pvals < 0, pvals > 1 or pvals contains NaNs - - """ - cdef np.npy_intp d, i, sz, offset - cdef np.ndarray parr, mnarr - cdef double *pix - cdef long *mnix - cdef long ni - - d = len(pvals) - parr = np.PyArray_FROMANY( - pvals, np.NPY_DOUBLE, 1, 1, np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS +def _removed(name: str) -> Callable[[Any,...],None]: + def f(*args, **kwargs): + raise NotImplementedError( + f"{name} has been removed. Use NumPy's Generator" ) - check_array_constraint(parr, "pvals", CONS_BOUNDED_0_1) - pix = np.PyArray_DATA(parr) - if kahan_sum(pix, d-1) > (1.0 + 1e-12): - raise ValueError("sum(pvals[:-1]) > 1.0") - - if size is None: - shape = (d,) - else: - try: - shape = (operator.index(size), d) - except TypeError: - shape = tuple(size) + (d,) - - multin = np.zeros(shape, int) - mnarr = multin - mnix = np.PyArray_DATA(mnarr) - sz = np.PyArray_SIZE(mnarr) - ni = n - check_constraint(ni, "n", CONS_NON_NEGATIVE) - offset = 0 - with self.lock, nogil: - for i in range(sz // d): - legacy_random_multinomial(&self._bitgen, ni, &mnix[offset], pix, d, &self._binomial) - offset += d - - return multin - - def dirichlet(self, object alpha, size=None): - """ - dirichlet(alpha, size=None) - - Draw samples from the Dirichlet distribution. - - Draw `size` samples of dimension k from a Dirichlet distribution. A - Dirichlet-distributed random variable can be seen as a multivariate - generalization of a Beta distribution. The Dirichlet distribution - is a conjugate prior of a multinomial distribution in Bayesian - inference. - - Parameters - ---------- - alpha : sequence of floats, length k - Parameter of the distribution (length ``k`` for sample of - length ``k``). - size : int or tuple of ints, optional - Output shape. If the given shape is, e.g., ``(m, n)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - vector of length ``k`` is returned. - - Returns - ------- - samples : ndarray, - The drawn samples, of shape ``(size, k)``. - - Raises - ------ - ValueError - If any value in ``alpha`` is less than or equal to zero. - - Notes - ----- - The Dirichlet distribution is a distribution over vectors - :math:`x` that fulfil the conditions :math:`x_i>0` and - :math:`\\sum_{i=1}^k x_i = 1`. - - The probability density function :math:`p` of a - Dirichlet-distributed random vector :math:`X` is - proportional to - - .. math:: p(x) \\propto \\prod_{i=1}^{k}{x^{\\alpha_i-1}_i}, - - where :math:`\\alpha` is a vector containing the positive - concentration parameters. - - The method uses the following property for computation: let :math:`Y` - be a random vector which has components that follow a standard gamma - distribution, then :math:`X = \\frac{1}{\\sum_{i=1}^k{Y_i}} Y` - is Dirichlet-distributed - - References - ---------- - .. [1] David McKay, "Information Theory, Inference and Learning - Algorithms," chapter 23, - http://www.inference.org.uk/mackay/itila/ - .. [2] Wikipedia, "Dirichlet distribution", - https://en.wikipedia.org/wiki/Dirichlet_distribution - - Examples - -------- - Taking an example cited in Wikipedia, this distribution can be used if - one wanted to cut strings (each of initial length 1.0) into K pieces - with different lengths, where each piece had, on average, a designated - average length, but allowing some variation in the relative sizes of - the pieces. - - >>> s = np.random.dirichlet((10, 5, 3), 20).transpose() - - >>> import matplotlib.pyplot as plt - >>> plt.barh(range(20), s[0]) - >>> plt.barh(range(20), s[1], left=s[0], color='g') - >>> plt.barh(range(20), s[2], left=s[0]+s[1], color='r') - >>> plt.title("Lengths of Strings") - - """ - - # ================= - # Pure python algo - # ================= - # alpha = N.atleast_1d(alpha) - # k = alpha.size - - # if n == 1: - # val = N.zeros(k) - # for i in range(k): - # val[i] = sgamma(alpha[i], n) - # val /= N.sum(val) - # else: - # val = N.zeros((k, n)) - # for i in range(k): - # val[i] = sgamma(alpha[i], n) - # val /= N.sum(val, axis = 0) - # val = val.T - # return val - - cdef np.npy_intp k, totsize, i, j - cdef np.ndarray alpha_arr, val_arr - cdef double *alpha_data - cdef double *val_data - cdef double acc, invacc - - k = len(alpha) - alpha_arr = np.PyArray_FROMANY(alpha, np.NPY_DOUBLE, 1, 1, np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS) - if np.any(np.less_equal(alpha_arr, 0)): - raise ValueError("alpha <= 0") - alpha_data = np.PyArray_DATA(alpha_arr) - - if size is None: - shape = (k,) - else: - try: - shape = (operator.index(size), k) - except TypeError: - shape = tuple(size) + (k,) - - diric = np.zeros(shape, np.float64) - val_arr = diric - val_data = np.PyArray_DATA(val_arr) - - i = 0 - totsize = np.PyArray_SIZE(val_arr) - with self.lock, nogil: - while i < totsize: - acc = 0.0 - for j in range(k): - val_data[i+j] = legacy_standard_gamma(&self._aug_state, - alpha_data[j]) - acc = acc + val_data[i + j] - invacc = 1/acc - for j in range(k): - val_data[i + j] = val_data[i + j] * invacc - i = i + k - - return diric - - # Shuffling and permutations: - def shuffle(self, object x): - """ - shuffle(x) - - Modify a sequence in-place by shuffling its contents. - - This function only shuffles the array along the first axis of a - multi-dimensional array. The order of sub-arrays is changed but - their contents remains the same. - - Parameters - ---------- - x : ndarray or MutableSequence - The array, list or mutable sequence to be shuffled. - - Returns - ------- - None - - Examples - -------- - >>> arr = np.arange(10) - >>> np.random.shuffle(arr) - >>> arr - [1 7 5 2 9 4 3 6 0 8] # random - - Multi-dimensional arrays are only shuffled along the first axis: - - >>> arr = np.arange(9).reshape((3, 3)) - >>> np.random.shuffle(arr) - >>> arr - array([[3, 4, 5], # random - [6, 7, 8], - [0, 1, 2]]) - - """ - cdef: - np.npy_intp i, j, n = len(x), stride, itemsize - char* x_ptr - char* buf_ptr - - if type(x) is np.ndarray and x.ndim == 1 and x.size: - # Fast, statically typed path: shuffle the underlying buffer. - # Only for non-empty, 1d objects of class ndarray (subclasses such - # as MaskedArrays may not support this approach). - x_ptr = np.PyArray_DATA(x) - stride = x.strides[0] - itemsize = x.dtype.itemsize - # As the array x could contain python objects we use a buffer - # of bytes for the swaps to avoid leaving one of the objects - # within the buffer and erroneously decrementing it's refcount - # when the function exits. - buf = np.empty(itemsize, dtype=np.int8) # GC'd at function exit - buf_ptr = np.PyArray_DATA(buf) - with self.lock: - # We trick gcc into providing a specialized implementation for - # the most common case, yielding a ~33% performance improvement. - # Note that apparently, only one branch can ever be specialized. - if itemsize == sizeof(np.npy_intp): - self._shuffle_raw(n, sizeof(np.npy_intp), stride, x_ptr, buf_ptr) - else: - self._shuffle_raw(n, itemsize, stride, x_ptr, buf_ptr) - elif isinstance(x, np.ndarray) and x.ndim and x.size: - buf = np.empty_like(x[0, ...]) - with self.lock: - for i in reversed(range(1, n)): - j = random_interval(&self._bitgen, i) - if i == j: - continue # i == j is not needed and memcpy is undefined. - buf[...] = x[j, ...] - x[j, ...] = x[i, ...] - x[i, ...] = buf - else: - # Untyped path. - if not isinstance(x, (np.ndarray, MutableSequence)): - # See gh-18206. We may decide to deprecate here in the future. - warnings.warn( - "`x` isn't a recognized object; `shuffle` is not guaranteed " - "to behave correctly. E.g., non-numpy array/tensor objects " - "with view semantics may contain duplicates after shuffling." - ) - with self.lock: - for i in reversed(range(1, n)): - j = random_interval(&self._bitgen, i) - x[i], x[j] = x[j], x[i] - - cdef inline _shuffle_raw(self, np.npy_intp n, np.npy_intp itemsize, - np.npy_intp stride, char* data, char* buf): - cdef np.npy_intp i, j - for i in reversed(range(1, n)): - j = random_interval(&self._bitgen, i) - string.memcpy(buf, data + j * stride, itemsize) - string.memcpy(data + j * stride, data + i * stride, itemsize) - string.memcpy(data + i * stride, buf, itemsize) - - def permutation(self, object x): - """ - permutation(x) - - Randomly permute a sequence, or return a permuted range. - - If `x` is a multi-dimensional array, it is only shuffled along its - first index. - - Parameters - ---------- - x : int or array_like - If `x` is an integer, randomly permute ``np.arange(x)``. - If `x` is an array, make a copy and shuffle the elements - randomly. - - Returns - ------- - out : ndarray - Permuted sequence or array range. - - Examples - -------- - >>> np.random.permutation(10) - array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random - - >>> np.random.permutation([1, 4, 9, 12, 15]) - array([15, 1, 9, 4, 12]) # random - - >>> arr = np.arange(9).reshape((3, 3)) - >>> np.random.permutation(arr) - array([[6, 7, 8], # random - [0, 1, 2], - [3, 4, 5]]) - - """ - if isinstance(x, (int, np.integer)): - arr = np.arange(x) - self.shuffle(arr) - return arr - - arr = np.asarray(x) - if arr.ndim < 1: - raise IndexError("x must be an integer or at least 1-dimensional") - - # shuffle has fast-path for 1-d - if arr.ndim == 1: - # Return a copy if same memory - if np.may_share_memory(arr, x): - arr = np.array(arr) - self.shuffle(arr) - return arr - - # Shuffle index array, dtype to ensure fast path - idx = np.arange(arr.shape[0], dtype=np.intp) - self.shuffle(idx) - return arr[idx] - -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - _rand = RandomState(_MT19937(mode="legacy")) - - -beta = _rand.beta -binomial = _rand.binomial -bytes = _rand.bytes -chisquare = _rand.chisquare -choice = _rand.choice -dirichlet = _rand.dirichlet -exponential = _rand.exponential -f = _rand.f -gamma = _rand.gamma -get_state = _rand.get_state -geometric = _rand.geometric -gumbel = _rand.gumbel -hypergeometric = _rand.hypergeometric -laplace = _rand.laplace -logistic = _rand.logistic -lognormal = _rand.lognormal -logseries = _rand.logseries -multinomial = _rand.multinomial -multivariate_normal = _rand.multivariate_normal -negative_binomial = _rand.negative_binomial -noncentral_chisquare = _rand.noncentral_chisquare -noncentral_f = _rand.noncentral_f -normal = _rand.normal -pareto = _rand.pareto -permutation = _rand.permutation -poisson = _rand.poisson -power = _rand.power -rand = _rand.rand -randint = _rand.randint -randn = _rand.randn -random = _rand.random_sample -random_integers = _rand.random_integers -random_sample = _rand.random_sample -rayleigh = _rand.rayleigh -seed = _rand.seed -set_state = _rand.set_state -shuffle = _rand.shuffle -standard_cauchy = _rand.standard_cauchy -standard_exponential = _rand.standard_exponential -standard_gamma = _rand.standard_gamma -standard_normal = _rand.standard_normal -standard_t = _rand.standard_t -triangular = _rand.triangular -uniform = _rand.uniform -vonmises = _rand.vonmises -wald = _rand.wald -weibull = _rand.weibull -zipf = _rand.zipf - - -# Old aliases that should not be removed -def sample(*args, **kwargs): - """ - This is an alias of `random_sample`. See `random_sample` for the complete - documentation. - """ - return _rand.random_sample(*args, **kwargs) - - -def ranf(*args, **kwargs): - """ - This is an alias of `random_sample`. See `random_sample` for the complete - documentation. - """ - return _rand.random_sample(*args, **kwargs) + return f + + +beta = _removed("beta") +binomial = _removed("binomial") +bytes = _removed("bytes") +chisquare = _removed("chisquare") +choice = _removed("choice") +dirichlet = _removed("dirichlet") +exponential = _removed("exponential") +f = _removed("f") +gamma = _removed("gamma") +get_state = _removed("get_state") +geometric = _removed("geometric") +gumbel = _removed("gumbel") +hypergeometric = _removed("hypergeometric") +laplace = _removed("laplace") +logistic = _removed("logistic") +lognormal = _removed("lognormal") +logseries = _removed("logseries") +multinomial = _removed("multinomial") +multivariate_normal = _removed("multivariate_normal") +negative_binomial = _removed("negative_binomial") +noncentral_chisquare = _removed("noncentral_chisquare") +noncentral_f = _removed("noncentral_f") +normal = _removed("normal") +pareto = _removed("pareto") +permutation = _removed("permutation") +poisson = _removed("poisson") +power = _removed("power") +rand = _removed("rand") +randint = _removed("randint") +randn = _removed("randn") +random = _removed("random_sample") +random_integers = _removed("random_integers") +random_sample = _removed("random_sample") +rayleigh = _removed("rayleigh") +seed = _removed("seed") +set_state = _removed("set_state") +shuffle = _removed("shuffle") +standard_cauchy = _removed("standard_cauchy") +standard_exponential = _removed("standard_exponential") +standard_gamma = _removed("standard_gamma") +standard_normal = _removed("standard_normal") +standard_t = _removed("standard_t") +triangular = _removed("triangular") +uniform = _removed("uniform") +vonmises = _removed("vonmises") +wald = _removed("wald") +weibull = _removed("weibull") +zipf = _removed("zipf") +sample = _removed("sample") +ranf = _removed("ranf") __all__ = [ diff --git a/randomgen/pcg32.pyx b/randomgen/pcg32.pyx index 540b50949..3c073dcd2 100644 --- a/randomgen/pcg32.pyx +++ b/randomgen/pcg32.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/pcg64.pyx b/randomgen/pcg64.pyx index c79c0cbe1..0178546b4 100644 --- a/randomgen/pcg64.pyx +++ b/randomgen/pcg64.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/philox.pyx b/randomgen/philox.pyx index 5066dd291..43f6c5104 100644 --- a/randomgen/philox.pyx +++ b/randomgen/philox.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np from randomgen.common cimport * diff --git a/randomgen/rdrand.pyx b/randomgen/rdrand.pyx index 039e0e744..7265739e3 100644 --- a/randomgen/rdrand.pyx +++ b/randomgen/rdrand.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/romu.pyx b/randomgen/romu.pyx index e63ea4b7b..183d37b3d 100644 --- a/randomgen/romu.pyx +++ b/randomgen/romu.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import warnings import numpy as np diff --git a/randomgen/sfc.pyx b/randomgen/sfc.pyx index 12764fe3c..32977cd87 100644 --- a/randomgen/sfc.pyx +++ b/randomgen/sfc.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import warnings import numpy as np @@ -275,10 +278,8 @@ cdef class SFC64(BitGenerator): cum_count_arr[i-_min_bits] += cum_count_arr[i-_min_bits-1] total = cum_count_arr[nbits-1] - from randomgen.generator import Generator - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=FutureWarning) - gen = Generator(self) + from numpy.random import Generator + gen = Generator(self) out = np.empty(n, dtype=np.uint64) out_arr = np.PyArray_DATA(out) diff --git a/randomgen/sfmt.pyx b/randomgen/sfmt.pyx index 16c74e8ea..0ef0be9c5 100644 --- a/randomgen/sfmt.pyx +++ b/randomgen/sfmt.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import operator import numpy as np diff --git a/randomgen/speck128.pyx b/randomgen/speck128.pyx index be9a68e6f..a6529e9c7 100644 --- a/randomgen/speck128.pyx +++ b/randomgen/speck128.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np from randomgen.common cimport * diff --git a/randomgen/tests/test_against_numpy.py b/randomgen/tests/test_against_numpy.py deleted file mode 100644 index 775ce6aca..000000000 --- a/randomgen/tests/test_against_numpy.py +++ /dev/null @@ -1,619 +0,0 @@ -import numpy as np -import numpy.random -from numpy.testing import ( - assert_allclose, - assert_array_equal, - assert_equal, - suppress_warnings, -) -from packaging.version import parse -import pytest - -import randomgen -from randomgen import MT19937, Generator -import randomgen.generator -from randomgen.mtrand import RandomState - -NP_VERSION = parse(np.__version__) -NP_118 = parse("1.18") <= NP_VERSION < parse("1.19") - - -def compare_0_input(f1, f2): - inputs = [ - (tuple([]), {}), - (tuple([]), {"size": 10}), - (tuple([]), {"size": (20, 31)}), - (tuple([]), {"size": (20, 31, 5)}), - ] - - for i in inputs: - v1 = f1(*i[0], **i[1]) - v2 = f2(*i[0], **i[1]) - assert_allclose(v1, v2) - - -def compare_1_input(f1, f2, is_small=False): - a = 0.3 if is_small else 10 - inputs = [ - ((a,), {}), - ((a,), {"size": 10}), - ((np.array([a] * 10),), {}), - ((np.array([a] * 10),), {"size": 10}), - ((np.array([a] * 10),), {"size": (100, 10)}), - ] - for i in inputs: - v1 = f1(*i[0], **i[1]) - v2 = f2(*i[0], **i[1]) - assert_allclose(v1, v2) - - -def compare_2_input(f1, f2, is_np=False, is_scalar=False): - if is_np: - a, b = 10, 0.3 - dtype = int - else: - a, b = 2, 3 - dtype = np.double - inputs = [ - ((a, b), {}), - ((a, b), {"size": 10}), - ((a, b), {"size": (23, 7)}), - ((np.array([a] * 10), b), {}), - ((a, np.array([b] * 10)), {}), - ((a, np.array([b] * 10)), {"size": 10}), - ( - (np.reshape(np.array([[a] * 100]), (100, 1)), np.array([b] * 10)), - {"size": (100, 10)}, - ), - ((np.ones((7, 31), dtype=dtype) * a, np.array([b] * 31)), {"size": (7, 31)}), - ( - (np.ones((7, 31), dtype=dtype) * a, np.array([b] * 31)), - {"size": (10, 7, 31)}, - ), - ] - - if is_scalar: - inputs = inputs[:3] - - for i in inputs: - v1 = f1(*i[0], **i[1]) - v2 = f2(*i[0], **i[1]) - assert_allclose(v1, v2) - - -def compare_3_input(f1, f2, is_np=False): - a, b, c = 10, 20, 25 - inputs = [ - ((a, b, c), {}), - ((a, b, c), {"size": 10}), - ((a, b, c), {"size": (23, 7)}), - ((np.array([a] * 10), b, c), {}), - ((a, np.array([b] * 10), c), {}), - ((a, b, np.array([c] * 10)), {}), - ((a, np.array([b] * 10), np.array([c] * 10)), {}), - ((a, np.array([b] * 10), c), {"size": 10}), - ( - ( - np.ones((1, 37), dtype=int) * a, - np.ones((23, 1), dtype=int) * [b], - c * np.ones((7, 1, 1), dtype=int), - ), - {}, - ), - ( - ( - np.ones((1, 37), dtype=int) * a, - np.ones((23, 1), dtype=int) * [b], - c * np.ones((7, 1, 1), dtype=int), - ), - {"size": (7, 23, 37)}, - ), - ] - - for i in inputs: - v1 = f1(*i[0], **i[1]) - v2 = f2(*i[0], **i[1]) - assert_allclose(v1, v2) - - -class TestAgainstNumPy(object): - @classmethod - def setup_class(cls): - cls.np = numpy.random - cls.bit_generator = MT19937 - cls.seed = [2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) - cls.rs = RandomState(cls.bit_generator(*cls.seed, mode="legacy")) - cls.nprs = cls.np.RandomState(*cls.seed) - cls.initial_state = cls.rg.bit_generator.state - cls._set_common_state() - - @classmethod - def _set_common_state(cls): - state = cls.rg.bit_generator.state - st = [[]] * 5 - st[0] = "MT19937" - st[1] = state["state"]["key"] - st[2] = state["state"]["pos"] - st[3] = 0 - st[4] = 0.0 - cls.nprs.set_state(st) - - @classmethod - def _set_common_state_legacy(cls): - state = cls.rs.get_state(legacy=False) - st = [[]] * 5 - st[0] = "MT19937" - st[1] = state["state"]["key"] - st[2] = state["state"]["pos"] - st[3] = state["has_gauss"] - st[4] = state["gauss"] - cls.nprs.set_state(st) - - def _is_state_common(self): - state = self.nprs.get_state() - state2 = self.rg.bit_generator.state - assert (state[1] == state2["state"]["key"]).all() - assert state[2] == state2["state"]["pos"] - - def _is_state_common_legacy(self): - state = self.nprs.get_state() - state2 = self.rs.get_state(legacy=False) - assert (state[1] == state2["state"]["key"]).all() - assert state[2] == state2["state"]["pos"] - assert state[3] == state2["has_gauss"] - assert_allclose(state[4], state2["gauss"], atol=1e-10) - - def test_common_seed(self): - self.rg.bit_generator.seed(1234) - self.nprs.seed(1234) - self._is_state_common() - self.rg.bit_generator.seed(23456) - self.nprs.seed(23456) - self._is_state_common() - - def test_numpy_state(self): - nprs = np.random.RandomState() - nprs.standard_normal(99) - state = nprs.get_state() - self.rg.bit_generator.state = state - state2 = self.rg.bit_generator.state - assert (state[1] == state2["state"]["key"]).all() - assert state[2] == state2["state"]["pos"] - - def test_random(self): - self._set_common_state() - self._is_state_common() - v1 = self.nprs.random_sample(10) - v2 = self.rg.random(10) - - assert_array_equal(v1, v2) - - def test_standard_normal(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_0_input(self.nprs.standard_normal, self.rs.standard_normal) - self._is_state_common_legacy() - - def test_standard_cauchy(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_0_input(self.nprs.standard_cauchy, self.rs.standard_cauchy) - self._is_state_common_legacy() - - def test_standard_exponential(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_0_input(self.nprs.standard_exponential, self.rs.standard_exponential) - self._is_state_common_legacy() - - @pytest.mark.xfail(reason="Stream broken for simplicity", strict=True) - def test_tomaxint(self): - self._set_common_state() - self._is_state_common() - with pytest.deprecated_call(): - compare_0_input(self.nprs.tomaxint, self.rg.tomaxint) - self._is_state_common() - - def test_poisson(self): - self._set_common_state() - self._is_state_common() - compare_1_input(self.nprs.poisson, self.rg.poisson) - self._is_state_common() - - def test_rayleigh(self): - self._set_common_state() - self._is_state_common() - compare_1_input(self.nprs.rayleigh, self.rg.rayleigh) - self._is_state_common() - - def test_zipf(self): - self._set_common_state() - self._is_state_common() - compare_1_input(self.nprs.zipf, self.rg.zipf) - self._is_state_common() - - def test_logseries(self): - self._set_common_state() - self._is_state_common() - compare_1_input(self.nprs.logseries, self.rg.logseries, is_small=True) - self._is_state_common() - - def test_geometric(self): - self._set_common_state() - self._is_state_common() - compare_1_input(self.nprs.geometric, self.rg.geometric, is_small=True) - self._is_state_common() - - def test_logistic(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.logistic, self.rg.logistic) - self._is_state_common() - - def test_gumbel(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.gumbel, self.rg.gumbel) - self._is_state_common() - - def test_laplace(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.laplace, self.rg.laplace) - self._is_state_common() - - def test_uniform(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.uniform, self.rg.uniform) - self._is_state_common() - - def test_vonmises(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.vonmises, self.rg.vonmises) - self._is_state_common() - - def test_random_integers(self): - self._set_common_state() - self._is_state_common() - with suppress_warnings() as sup: - sup.record(DeprecationWarning) - compare_2_input( - self.nprs.random_integers, self.rg.random_integers, is_scalar=True - ) - self._is_state_common() - - def test_binomial(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.nprs.binomial, self.rg.binomial, is_np=True) - self._is_state_common() - - def test_rand(self): - self._set_common_state() - self._is_state_common() - f = self.rg.rand - g = self.nprs.rand - with pytest.deprecated_call(): - assert_allclose(f(10), g(10)) - with pytest.deprecated_call(): - assert_allclose(f(3, 4, 5), g(3, 4, 5)) - - @pytest.mark.xfail(reason="poisson_lam_max changed", strict=True) - def test_poisson_lam_max(self): - assert_allclose(self.rg.poisson_lam_max, self.nprs.poisson_lam_max) - - def test_triangular(self): - self._set_common_state() - self._is_state_common() - compare_3_input(self.nprs.triangular, self.rg.triangular) - self._is_state_common() - - @pytest.mark.xfail(reason="Changes to hypergeometic", strict=True) - def test_hypergeometric(self): - self._set_common_state() - self._is_state_common() - compare_3_input(self.nprs.hypergeometric, self.rg.hypergeometric) - self._is_state_common() - - def test_bytes(self): - self._set_common_state() - self._is_state_common() - assert_equal(self.rg.bytes(8), self.nprs.bytes(8)) - self._is_state_common() - assert_equal(self.rg.bytes(103), self.nprs.bytes(103)) - self._is_state_common() - assert_equal(self.rg.bytes(8), self.nprs.bytes(8)) - self._is_state_common() - - def test_multinomial(self): - self._set_common_state() - self._is_state_common() - f = self.rg.multinomial - g = self.nprs.multinomial - p = [0.1, 0.3, 0.4, 0.2] - assert_equal(f(100, p), g(100, p)) - assert_equal(f(100, np.array(p)), g(100, np.array(p))) - assert_equal( - f(100, np.array(p), size=(7, 23)), g(100, np.array(p), size=(7, 23)) - ) - self._is_state_common() - - @pytest.mark.xfail(reason="Stream broken for performance", strict=True) - def test_choice(self): - self._set_common_state() - self._is_state_common() - f = self.rg.choice - g = self.nprs.choice - a = np.arange(100) - size = 25 - for replace in (True, False): - assert_equal(f(a, size, replace), g(a, size, replace)) - assert_equal(f(100, size, replace), g(100, size, replace)) - self._is_state_common() - - def test_permutation(self): - self._set_common_state() - self._is_state_common() - f = self.rg.permutation - g = self.nprs.permutation - a = np.arange(100) - assert_equal(f(a), g(a)) - assert_equal(f(23), g(23)) - self._is_state_common() - - def test_shuffle(self): - self._set_common_state() - self._is_state_common() - f = self.rg.shuffle - g = self.nprs.shuffle - a = np.arange(100) - fa = a.copy() - ga = a.copy() - g(ga) - f(fa) - assert_equal(fa, ga) - self._is_state_common() - - def test_randint(self): - self._set_common_state() - self._is_state_common() - compare_2_input(self.rg.integers, self.nprs.randint, is_scalar=True) - self._is_state_common() - - def test_scalar(self): - s = Generator(MT19937(0, mode="legacy")) - assert_equal(s.integers(1000), 684) - s1 = np.random.RandomState(0) - assert_equal(s1.randint(1000), 684) - assert_equal(s1.randint(1000), s.integers(1000)) - - s = Generator(MT19937(4294967295, mode="legacy")) - assert_equal(s.integers(1000), 419) - s1 = np.random.RandomState(4294967295) - assert_equal(s1.randint(1000), 419) - assert_equal(s1.randint(1000), s.integers(1000)) - - self.rg.bit_generator.seed(4294967295) - self.nprs.seed(4294967295) - self._is_state_common() - - def test_array(self): - s = Generator(MT19937(range(10), mode="legacy")) - assert_equal(s.integers(1000), 468) - s = np.random.RandomState(range(10)) - assert_equal(s.randint(1000), 468) - - s = Generator(MT19937(np.arange(10), mode="legacy")) - assert_equal(s.integers(1000), 468) - s = Generator(MT19937([0], mode="legacy")) - assert_equal(s.integers(1000), 973) - s = Generator(MT19937([4294967295], mode="legacy")) - assert_equal(s.integers(1000), 265) - - @pytest.mark.skipif(not NP_118, reason="Only value for NumPy 1.18") - def test_dir(self): - nprs_d = set(dir(self.nprs)) - rs_d = dir(self.rg) - excluded = {"get_state", "set_state", "poisson_lam_max"} - nprs_d.difference_update(excluded) - assert len(nprs_d.difference(rs_d)) == 0 - - npmod = dir(numpy.random) - mod = dir(randomgen.generator) - known_exlcuded = [ - "BitGenerator", - "MT19937", - "PCG64", - "Philox", - "RandomState", - "SFC64", - "SeedSequence", - "__RandomState_ctor", - "__cached__", - "__path__", - "_bit_generator", - "_bounded_integers", - "_common", - "_generator", - "_mt19937", - "_pcg64", - "_philox", - "_pickle", - "_sfc64", - "absolute_import", - "default_rng", - "division", - "get_state", - "mtrand", - "print_function", - "ranf", - "sample", - "seed", - "set_state", - "test", - ] - mod += known_exlcuded - diff = set(npmod).difference(mod) - assert_equal(len(diff), 0) - - # Tests using legacy generator - def test_chisquare(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.chisquare, self.rs.chisquare) - self._is_state_common_legacy() - - def test_standard_gamma(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.standard_gamma, self.rs.standard_gamma) - self._is_state_common_legacy() - - def test_standard_t(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.standard_t, self.rs.standard_t) - self._is_state_common_legacy() - - def test_pareto(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.pareto, self.rs.pareto) - self._is_state_common_legacy() - - def test_power(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.power, self.rs.power) - self._is_state_common_legacy() - - def test_weibull(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.weibull, self.rs.weibull) - self._is_state_common_legacy() - - def test_beta(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.beta, self.rs.beta) - self._is_state_common_legacy() - - def test_exponential(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_1_input(self.nprs.exponential, self.rs.exponential) - self._is_state_common_legacy() - - def test_f(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.f, self.rs.f) - self._is_state_common_legacy() - - def test_gamma(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.gamma, self.rs.gamma) - self._is_state_common_legacy() - - def test_lognormal(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.lognormal, self.rs.lognormal) - self._is_state_common_legacy() - - def test_noncentral_chisquare(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.noncentral_chisquare, self.rs.noncentral_chisquare) - self._is_state_common_legacy() - - def test_normal(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.normal, self.rs.normal) - self._is_state_common_legacy() - - def test_wald(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input(self.nprs.wald, self.rs.wald) - self._is_state_common_legacy() - - def test_negative_binomial(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_2_input( - self.nprs.negative_binomial, self.rs.negative_binomial, is_np=True - ) - self._is_state_common_legacy() - - def test_randn(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - f = self.rs.randn - g = self.nprs.randn - assert_allclose(f(10), g(10)) - assert_allclose(f(3, 4, 5), g(3, 4, 5)) - self._is_state_common_legacy() - - def test_dirichlet(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - f = self.rs.dirichlet - g = self.nprs.dirichlet - a = [3, 4, 5, 6, 7, 10] - assert_allclose(f(a), g(a)) - assert_allclose(f(np.array(a), 10), g(np.array(a), 10)) - assert_allclose(f(np.array(a), (3, 37)), g(np.array(a), (3, 37))) - self._is_state_common_legacy() - - def test_noncentral_f(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - compare_3_input(self.nprs.noncentral_f, self.rs.noncentral_f) - self._is_state_common_legacy() - - def test_multivariate_normal(self): - self._set_common_state_legacy() - self._is_state_common_legacy() - mu = [1, 2, 3] - cov = [[1, 0.2, 0.3], [0.2, 4, 1], [0.3, 1, 10]] - f = self.rs.multivariate_normal - g = self.nprs.multivariate_normal - assert_allclose(f(mu, cov), g(mu, cov)) - assert_allclose(f(np.array(mu), cov), g(np.array(mu), cov)) - assert_allclose(f(np.array(mu), np.array(cov)), g(np.array(mu), np.array(cov))) - assert_allclose( - f(np.array(mu), np.array(cov), size=(7, 31)), - g(np.array(mu), np.array(cov), size=(7, 31)), - ) - self._is_state_common_legacy() - - -funcs = [ - randomgen.generator.zipf, - randomgen.generator.logseries, - randomgen.generator.poisson, -] -ids = [f.__name__ for f in funcs] - - -@pytest.mark.filterwarnings("ignore:invalid value encountered:RuntimeWarning") -@pytest.mark.parametrize("func", funcs, ids=ids) -def test_nan_guard(func): - with pytest.raises(ValueError): - func([np.nan]) - with pytest.raises(ValueError): - func(np.nan) - - -def test_cons_gte1_nan_guard(): - with pytest.raises(ValueError): - randomgen.generator.hypergeometric(10, 10, [np.nan]) - with pytest.raises(ValueError): - randomgen.generator.hypergeometric(10, 10, np.nan) diff --git a/randomgen/tests/test_direct.py b/randomgen/tests/test_direct.py index a378f334f..3e421a602 100644 --- a/randomgen/tests/test_direct.py +++ b/randomgen/tests/test_direct.py @@ -28,9 +28,7 @@ SPECK128, AESCounter, ChaCha, - Generator, Philox, - RandomState, Romu, ThreeFry, Xoroshiro128, @@ -122,19 +120,19 @@ def uniform32_from_uint64(x): lower = np.uint64(0xFFFFFFFF) lower = np.array(x & lower, dtype=np.uint32) joined = np.column_stack([lower, upper]).ravel() - out = (joined >> np.uint32(9)) * (1.0 / 2**23) + out = (joined >> np.uint32(8)) * (1.0 / 2**24) return out.astype(np.float32) def uniform32_from_uint53(x): x = np.uint64(x) >> np.uint64(16) x = np.uint32(x & np.uint64(0xFFFFFFFF)) - out = (x >> np.uint32(9)) * (1.0 / 2**23) + out = (x >> np.uint32(8)) * (1.0 / 2**24) return out.astype(np.float32) def uniform32_from_uint32(x): - return (x >> np.uint32(9)) * (1.0 / 2**23) + return (x >> np.uint32(8)) * (1.0 / 2**24) def uniform32_from_uint(x, bits): @@ -260,37 +258,37 @@ def test_random_raw(self): def test_gauss_inv(self): n = 25 - rs = RandomState(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.RandomState(self.setup_bitgenerator(self.data1["seed"])) gauss = rs.standard_normal(n) bits = getattr(self, "bit_name", self.bits) assert_allclose(gauss, gauss_from_uint(self.data1["data"], n, bits), rtol=3e-6) - rs = RandomState(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.RandomState(self.setup_bitgenerator(self.data2["seed"])) gauss = rs.standard_normal(25) assert_allclose(gauss, gauss_from_uint(self.data2["data"], n, bits), rtol=3e-6) def test_uniform_double(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) bits = getattr(self, "bit_name", self.bits) vals = uniform_from_uint(self.data1["data"], bits) uniforms = rs.random(len(vals)) assert_allclose(uniforms, vals, atol=1e-8) assert_equal(uniforms.dtype, np.float64) - rs = Generator(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) vals = uniform_from_uint(self.data2["data"], bits) uniforms = rs.random(len(vals)) assert_allclose(uniforms, vals, atol=1e-8) assert_equal(uniforms.dtype, np.float64) def test_uniform_float(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) vals = uniform32_from_uint(self.data1["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) assert_allclose(uniforms, vals) assert_equal(uniforms.dtype, np.float32) - rs = Generator(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) vals = uniform32_from_uint(self.data2["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) assert_allclose(uniforms, vals) @@ -298,13 +296,13 @@ def test_uniform_float(self): def test_seed_float(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.pi) assert_raises(self.seed_error_type, rs.bit_generator.seed, -np.pi) def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) with pytest.raises(self.seed_error_type): rs.bit_generator.seed(np.array([np.pi])) with pytest.raises((ValueError, TypeError)): @@ -323,7 +321,7 @@ def test_seed_out_of_range(self): if self.seed_sequence_only: # Not valid on PRNG that only support seed sequence return - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, 2 ** (4 * self.bits + 1)) assert_raises(ValueError, rs.bit_generator.seed, -1) @@ -332,17 +330,17 @@ def test_seed_out_of_range_array(self): if self.seed_sequence_only: # Not valid on PRNG that only support seed sequence return - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, [2 ** (2 * self.bits + 1)]) assert_raises(ValueError, rs.bit_generator.seed, [-1]) def test_repr(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert "Generator" in repr(rs) assert type(rs.bit_generator).__name__ in repr(rs) def test_str(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert "Generator" in str(rs) assert str(type(rs.bit_generator).__name__) in str(rs) assert "{:#x}".format(id(rs)).upper().replace("X", "x")[2:] not in str(rs) @@ -360,8 +358,8 @@ def test_pickle(self): bit_generator = self.setup_bitgenerator(self.data1["seed"]) bit_generator_pkl = pickle.dumps(bit_generator) reloaded = pickle.loads(bit_generator_pkl) - orig_gen = Generator(bit_generator) - reloaded_gen = Generator(reloaded) + orig_gen = np.random.Generator(bit_generator) + reloaded_gen = np.random.Generator(reloaded) assert_array_equal( orig_gen.standard_normal(1000), reloaded_gen.standard_normal(1000) ) @@ -376,7 +374,7 @@ def test_invalid_state_type(self): def test_invalid_state_value(self): bit_generator = self.setup_bitgenerator(self.data1["seed"]) state = bit_generator.state - state["bit_generator"] = "otherBitGenerator" + state["bit_generator"] = "otherBitnp.random.Generator" with pytest.raises(ValueError): bit_generator.state = state @@ -423,7 +421,7 @@ def test_getstate(self): def test_uinteger_reset_seed(self): bg = self.setup_bitgenerator([None]) - g = Generator(bg) + g = np.random.Generator(bg) g.integers(0, 2**32, dtype=np.uint32) if "has_uint32" not in bg.state or bg.state["has_uint32"] == 0: name = bg.__class__.__name__ @@ -435,9 +433,9 @@ def test_uinteger_reset_jump(self): bg = self.setup_bitgenerator([None]) if not hasattr(bg, "jumped"): pytest.skip("bit generator does not support jumping") - g = Generator(bg) + g = np.random.Generator(bg) g.integers(0, 2**32, dtype=np.uint32) - jumped = Generator(bg.jumped()) + jumped = np.random.Generator(bg.jumped()) if "has_uint32" in jumped.bit_generator.state: assert jumped.bit_generator.state["has_uint32"] == 0 return @@ -450,7 +448,7 @@ def test_uinteger_reset_advance(self): bg = self.setup_bitgenerator([None]) if not hasattr(bg, "advance"): pytest.skip("bit generator does not support advancing") - g = Generator(bg) + g = np.random.Generator(bg) g.integers(0, 2**32, dtype=np.uint32) state = bg.state if isinstance(bg, (Philox, ThreeFry)): @@ -809,7 +807,7 @@ def setup_bitgenerator(self, seed, mode="legacy", inc: Optional[int] = 0): return self.bit_generator(*seed, mode=mode, variant="xsl-rr", inc=inc) # type: ignore def test_seed_float_array(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([np.pi])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([-np.pi])) assert_raises( @@ -820,14 +818,14 @@ def test_seed_float_array(self): assert_raises(self.seed_error_type, rs.bit_generator.seed, [0, np.pi]) def test_seed_out_of_range_array(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises( self.seed_error_type, rs.bit_generator.seed, [2 ** (2 * self.bits + 1)] ) assert_raises(self.seed_error_type, rs.bit_generator.seed, [-1]) def test_advance_symmetry(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) state = rs.bit_generator.state step = -0x9E3779B97F4A7C150000000000000000 rs.bit_generator.advance(step) @@ -996,7 +994,7 @@ def test_set_key(self): def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([np.pi])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([-np.pi])) assert_raises( @@ -1122,27 +1120,27 @@ def setup_class(cls): def test_seed_out_of_range(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, 2 ** (self.seed_bits + 1)) assert_raises(ValueError, rs.bit_generator.seed, -1) assert_raises(ValueError, rs.bit_generator.seed, 2 ** (2 * self.seed_bits + 1)) def test_seed_out_of_range_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, [2 ** (self.seed_bits + 1)]) assert_raises(ValueError, rs.bit_generator.seed, [-1]) assert_raises(TypeError, rs.bit_generator.seed, [2 ** (2 * self.seed_bits + 1)]) def test_seed_float(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(TypeError, rs.bit_generator.seed, np.pi) assert_raises(TypeError, rs.bit_generator.seed, -np.pi) def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) bit_generator = rs.bit_generator assert_raises(TypeError, bit_generator.seed, np.array([np.pi])) assert_raises(TypeError, bit_generator.seed, np.array([-np.pi])) @@ -1152,7 +1150,7 @@ def test_seed_float_array(self): assert_raises(TypeError, bit_generator.seed, [0, np.pi]) def test_state_tuple(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) bit_generator = rs.bit_generator state = bit_generator.state desired = rs.integers(2**16) @@ -1166,7 +1164,7 @@ def test_state_tuple(self): assert_equal(actual, desired) def test_invalid_state(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) state = rs.bit_generator.state state["state"][self.state_name] = state["state"][self.state_name][:10] with pytest.raises(ValueError): @@ -1229,38 +1227,38 @@ def setup_class(cls): ] def test_uniform_double(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_array_equal(uniform_from_dsfmt(self.data1["data"]), rs.random(1000)) - rs = Generator(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) assert_equal(uniform_from_dsfmt(self.data2["data"]), rs.random(1000)) def test_gauss_inv(self): n = 25 - rs = RandomState(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.RandomState(self.setup_bitgenerator(self.data1["seed"])) gauss = rs.standard_normal(n) assert_allclose(gauss, gauss_from_uint(self.data1["data"], n, "dsfmt")) - rs = RandomState(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.RandomState(self.setup_bitgenerator(self.data2["seed"])) gauss = rs.standard_normal(25) assert_allclose(gauss, gauss_from_uint(self.data2["data"], n, "dsfmt")) def test_seed_out_of_range_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, [2 ** (self.bits + 1)]) assert_raises(ValueError, rs.bit_generator.seed, [-1]) assert_raises(TypeError, rs.bit_generator.seed, [2 ** (2 * self.bits + 1)]) def test_seed_float(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(TypeError, rs.bit_generator.seed, np.pi) assert_raises(TypeError, rs.bit_generator.seed, -np.pi) def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(TypeError, rs.bit_generator.seed, np.array([np.pi])) assert_raises(TypeError, rs.bit_generator.seed, np.array([-np.pi])) assert_raises(TypeError, rs.bit_generator.seed, np.array([np.pi, -np.pi])) @@ -1269,20 +1267,20 @@ def test_seed_float_array(self): assert_raises(TypeError, rs.bit_generator.seed, [0, np.pi]) def test_uniform_float(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) vals = uniform32_from_uint(self.data1["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) assert_allclose(uniforms, vals) assert_equal(uniforms.dtype, np.float32) - rs = Generator(self.setup_bitgenerator(self.data2["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) vals = uniform32_from_uint(self.data2["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) assert_allclose(uniforms, vals) assert_equal(uniforms.dtype, np.float32) def test_buffer_reset(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) rs.random(1) assert rs.bit_generator.state["buffer_loc"] != 382 rs.bit_generator.seed(*self.data1["seed"]) @@ -1371,7 +1369,7 @@ def setup_bitgenerator(self, seed, mode="legacy", inc=0): return self.bit_generator(*seed, mode=mode, inc=inc) def test_advance_symmetry(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) state = rs.bit_generator.state step = -0x9E3779B97F4A7C16 rs.bit_generator.advance(step) @@ -1402,7 +1400,7 @@ def setup_class(cls): def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) bit_generator = rs.bit_generator assert_raises(ValueError, bit_generator.seed, np.array([np.pi])) assert_raises(ValueError, bit_generator.seed, np.array([-np.pi])) @@ -1440,7 +1438,7 @@ def test_initialization(self): assert (state["buffer"] == np.iinfo(np.uint64).max).all() assert state["retries"] == 10 assert state["status"] == 1 - gen = Generator(bit_generator) + gen = np.random.Generator(bit_generator) gen.integers(0, 2**64, dtype=np.uint64, size=10) state = bit_generator.state # Incredibly conservative test @@ -1457,7 +1455,7 @@ def test_generator_raises(self): state = bit_generator.state state["retries"] = -1 bit_generator.state = state - gen = Generator(bit_generator) + gen = np.random.Generator(bit_generator) with pytest.raises(RuntimeError): gen.integers(0, 2**64, dtype=np.uint64, size=10) assert bit_generator.state["status"] == 0 @@ -1500,17 +1498,17 @@ def test_raw(self): def test_gauss_inv(self): n = 25 - rs = RandomState(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.RandomState(self.setup_bitgenerator(self.data1["seed"])) gauss = rs.standard_normal(n) assert (gauss.max() - gauss.min()) > 0 def test_uniform_double(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) uniforms = rs.random(1000) assert_equal(uniforms.dtype, np.float64) def test_uniform_float(self): - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) uniforms = rs.random(1000, dtype=np.float32) assert_equal(uniforms.dtype, np.float32) @@ -1704,7 +1702,7 @@ def setup_class(cls): def test_seed_float_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([np.pi])) assert_raises(self.seed_error_type, rs.bit_generator.seed, np.array([-np.pi])) assert_raises( @@ -1716,7 +1714,7 @@ def test_seed_float_array(self): def test_seed_out_of_range(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, -1) def test_invalid_seed_type(self): @@ -1755,7 +1753,7 @@ def setup_class(cls): def test_seed_out_of_range(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, 2**257) assert_raises(ValueError, rs.bit_generator.seed, -1) @@ -1775,7 +1773,7 @@ def test_key_init(self): def test_seed_out_of_range_array(self): # GH #82 - rs = Generator(self.setup_bitgenerator(self.data1["seed"])) + rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) assert_raises(ValueError, rs.bit_generator.seed, [2 ** (4 * self.bits + 1)]) assert_raises(ValueError, rs.bit_generator.seed, [-1]) diff --git a/randomgen/tests/test_extended_generator.py b/randomgen/tests/test_extended_generator.py index 0dbf19a7c..08e57067d 100644 --- a/randomgen/tests/test_extended_generator.py +++ b/randomgen/tests/test_extended_generator.py @@ -3,6 +3,7 @@ import numpy as np from numpy.linalg import LinAlgError +from numpy.random import Generator from numpy.testing import ( assert_allclose, assert_array_almost_equal, @@ -17,11 +18,6 @@ from randomgen import MT19937, PCG64, ExtendedGenerator -try: - from numpy.random import Generator -except ImportError: - from randomgen import Generator # type: ignore[misc] - try: from scipy import linalg # noqa: F401 diff --git a/randomgen/tests/test_final_release_changes.py b/randomgen/tests/test_final_release_changes.py index a3974fc91..53325a3e2 100644 --- a/randomgen/tests/test_final_release_changes.py +++ b/randomgen/tests/test_final_release_changes.py @@ -5,7 +5,6 @@ MT19937, PCG32, PCG64, - Generator, Philox, ThreeFry, Xoroshiro128, @@ -14,8 +13,6 @@ Xoshiro512, ) -random_gen = Generator() - bit_generators = [ DSFMT, MT19937, @@ -40,46 +37,7 @@ def endpoint(request): return request.param -def test_random_sample_deprecated(): - with pytest.deprecated_call(): - random_gen.random_sample() - - -def test_randint_deprecated(): - with pytest.deprecated_call(): - random_gen.randint(10) - - -def test_rand_deprecated(): - with pytest.deprecated_call(): - random_gen.rand(10) - - -def test_randn_deprecated(): - with pytest.deprecated_call(): - random_gen.randn(10) - - def test_generator_raises(bit_generator): bg = bit_generator(mode="sequence") with pytest.raises(NotImplementedError): bg.generator - - -def test_integers_closed(): - with pytest.deprecated_call(): - random_gen.integers(0, 10, closed=True) - with pytest.deprecated_call(): - random_gen.integers(0, 10, closed=False) - - -def test_integers_use_masked(): - with pytest.deprecated_call(): - random_gen.integers(0, 10, use_masked=True) - - -def test_integers_large_negative_value(): - with pytest.raises(ValueError): - random_gen.integers(0, -(2**65), endpoint=endpoint) - with pytest.raises(ValueError): - random_gen.integers(0, [-(2**65)], endpoint=endpoint) diff --git a/randomgen/tests/test_generator_117.py b/randomgen/tests/test_generator_117.py deleted file mode 100644 index 234d493f1..000000000 --- a/randomgen/tests/test_generator_117.py +++ /dev/null @@ -1,325 +0,0 @@ -from itertools import product -from typing import cast - -import numpy as np -from numpy.testing import assert_allclose, assert_array_equal -from packaging.version import parse -import pytest - -from randomgen import Generator -import randomgen.common - -try: - from numpy.random import PCG64, Generator as NPGenerator - - pcg = PCG64() - initial_state = pcg.state - np_gen = NPGenerator(pcg) - gen = Generator(cast(randomgen.common.BitGenerator, pcg)) -except ImportError: - from randomgen import PCG64 # type: ignore[misc] - - -NP_LT_1174 = parse(np.__version__) < parse("1.17.4") -NP_GTE_118 = parse(np.__version__) >= parse("1.18") -NP_GTE_120 = parse(np.__version__) >= parse("1.20") -NP_GTE_121 = parse(np.__version__) >= parse("1.21") - -pytestmark = pytest.mark.skipif(NP_LT_1174, reason="Only test 1.17.4+") - - -def positive_param(): - base = Generator(PCG64()) - return [ - base.chisquare(10), - base.chisquare(10, (5, 1, 3)), - base.chisquare(10, (6, 5, 4, 3)), - ] - - -def positive(num_args): - args = list(product(*[positive_param() for _ in range(num_args)])) - - def param_generator(): - return args - - return param_generator - - -def int_prob(): - base = Generator(PCG64()) - return ( - [100, 0.5], - [100, 0.5, (6, 5, 4, 3)], - [base.integers(10, 100, size=(10, 2)), 0.3], - [10, base.random((20, 2, 2))], - [base.integers(10, 100, size=(5, 4, 3)), base.random(3)], - ) - - -def prob(): - base = Generator(PCG64()) - return ( - [0.5], - [0.5, (6, 5, 4, 3)], - [0.3], - [base.random((20, 2, 2))], - [base.random(3)], - ) - - -def length(): - return [(100,), (2500,)] - - -def input_0(): - return (tuple([]), (5,), ((5, 4, 3),)) - - -def loc_scale(): - return positive(2)() - - -def above_1(): - return [(1 + val,) for val in positive_param()] - - -def triangular(): - out = product(*[positive_param() for _ in range(3)]) - out = [(lft, lft + mid, lft + mid + rgt) for lft, mid, rgt in out] - return out - - -def uniform(): - low = positive_param() - high = positive_param() - scale = positive_param() - out = [] - for lo, hi, sc in zip(low, high, scale): - out.append((lo, lo + hi + sc)) - assert np.all(out[-1][1] >= out[-1][0]) - return out - - -def integers(): - dtypes = [ - np.int8, - np.int16, - np.int32, - np.int64, - np.uint8, - np.uint16, - np.uint32, - np.uint64, - ] - base = Generator(PCG64()) - shape = tuple(base.integers(5, 10, size=2)) - configs = [] - - for dt in dtypes: - s1 = np.ones(shape, dtype=dt) - s2 = np.ones((1,) + shape, dtype=dt) - lo = np.iinfo(dt).min - hi = np.iinfo(dt).max - configs.extend( - [ - (0, np.iinfo(dt).max, None, dt), - (lo, hi // 2, None, dt), - (lo, hi, (10, 2), dt), - (lo // 2 * s1, hi // 2 * s2, None, dt), - ] - ) - return configs - - -def dirichlet(): - base = Generator(PCG64()) - probs = base.random(10) - probs = probs / probs.sum() - return [(probs,), (probs, (3, 4, 5))] - - -def hypergeometric(): - base = Generator(PCG64()) - good = [10, base.integers(10, 100, size=(3, 4))] - bad = [10, base.integers(10, 100, size=(1, 4))] - out = [] - for g, b in product(good, bad): - nsample = g + b // 2 - if isinstance(nsample, int): - nsample = max(nsample, 1) - else: - nsample.flat[nsample.flat < 1] = 1 - out.append((g, b, nsample)) - return out - - -def multinomial(): - base = Generator(PCG64()) - probs = base.random(10) - probs /= probs.sum() - return (10, probs), (base.integers(10, 100, size=(3, 4)), probs) - - -distributions = { - "beta": positive(2), - "binomial": int_prob, - "bytes": length, - "chisquare": positive(1), - "dirichlet": dirichlet, - "exponential": positive(1), - "f": positive(2), - "gamma": positive(2), - "gumbel": positive(2), - "laplace": loc_scale, - "logistic": loc_scale, - "lognormal": loc_scale, - "logseries": prob, - "multinomial": multinomial, - "multivariate_normal": "", - "negative_binomial": int_prob, - "noncentral_chisquare": positive(2), - "noncentral_f": positive(3), - "normal": loc_scale, - "pareto": positive(1), - "poisson": positive(1), - "power": positive(1), - "random": input_0, - "standard_cauchy": input_0, - "standard_exponential": input_0, - "standard_gamma": positive(1), - "standard_normal": input_0, - "standard_t": positive(1), - "triangular": triangular, - "uniform": uniform, - "vonmises": loc_scale, - "wald": positive(2), - "weibull": positive(1), - "zipf": above_1, -} - - -if not NP_GTE_121: - distributions.update({"geometric": prob, "rayleigh": positive(1)}) - -tests = [] -ids = [] -for key in distributions: - if not distributions[key]: - continue - params = distributions[key]() - for i, param in enumerate(params): - tests.append((key, param)) - ids.append(key + "-config-{0}".format(i)) - - -@pytest.mark.parametrize("distribution, args", tests, ids=ids) -def test_equivalence(distribution, args): - np_gen.bit_generator.state = initial_state - np_rvs = getattr(np_gen, distribution) - rvs = getattr(gen, distribution) - expected = np_rvs(*args) - - gen.bit_generator.state = initial_state - result = rvs(*args) - if isinstance(result, (np.ndarray, float)): - dtype = getattr(result, "dtype", None) - if isinstance(result, float) or dtype in (np.float32, np.float64): - assert_allclose(result, expected) - else: - assert_array_equal(result, expected) - else: - assert result == expected - - -def test_shuffle(): - np_gen.bit_generator.state = initial_state - expected = np.arange(100) - np_gen.shuffle(expected) - - gen.bit_generator.state = initial_state - result = np.arange(100) - gen.shuffle(result) - assert_array_equal(result, expected) - - -def test_permutation(): - np_gen.bit_generator.state = initial_state - expected = np_gen.permutation(100) - - gen.bit_generator.state = initial_state - result = gen.permutation(100) - assert_array_equal(result, expected) - - -@pytest.mark.parametrize("replace", [True, False]) -def test_choice_with_p(replace): - x = np.arange(100) - np_gen.bit_generator.state = initial_state - p = (x + 1) / (x + 1).sum() - expected = np_gen.choice(x, size=10, replace=replace, p=p) - - gen.bit_generator.state = initial_state - result = gen.choice(x, size=10, replace=replace, p=p) - assert_array_equal(result, expected) - - -@pytest.mark.parametrize("replace", [True, False]) -def test_choice(replace): - np_gen.bit_generator.state = initial_state - x = np.arange(100) - expected = np_gen.choice(x, size=10, replace=replace) - - gen.bit_generator.state = initial_state - result = gen.choice(x, size=10, replace=replace) - assert_array_equal(result, expected) - - -configs = integers() - - -@pytest.mark.skipif(NP_LT_1174, reason="Changes to lemire generators") -@pytest.mark.parametrize("args", configs) -def test_integers(args): - np_gen.bit_generator.state = initial_state - expected = np_gen.integers(*args) - - gen.bit_generator.state = initial_state - result = gen.integers(*args, use_masked=False) - assert_array_equal(result, expected) - - -@pytest.mark.parametrize("args", hypergeometric()) -def test_hypergeometric(args): - np_gen.bit_generator.state = initial_state - expected = np_gen.hypergeometric(*args) - - gen.bit_generator.state = initial_state - result = gen.hypergeometric(*args) - assert_allclose(result, expected) - - -def test_missing(): - KNOWN_SPECIAL_CASES = [ - "bit_generator", - "choice", - "hypergeometric", - "integers", - "permutation", - "shuffle", - ] - if NP_GTE_121: - KNOWN_SPECIAL_CASES += ["geometric", "rayleigh"] - missing = [ - f - for f in dir(np_gen) - if not f.startswith("_") - and f not in distributions - and f not in KNOWN_SPECIAL_CASES - ] - missing_funcs = [] - if NP_GTE_118: - missing_funcs += ["multivariate_hypergeometric"] - if NP_GTE_120: - missing_funcs += ["permuted"] - assert missing == missing_funcs diff --git a/randomgen/tests/test_generator_mt19937.py b/randomgen/tests/test_generator_mt19937.py index 1d919f535..fe4b9b608 100644 --- a/randomgen/tests/test_generator_mt19937.py +++ b/randomgen/tests/test_generator_mt19937.py @@ -1,27 +1,12 @@ import hashlib import sys -import warnings import numpy as np -from numpy.linalg import LinAlgError -from numpy.testing import ( - assert_, - assert_allclose, - assert_array_almost_equal, - assert_array_equal, - assert_equal, - assert_no_warnings, - assert_raises, - assert_warns, - suppress_warnings, -) +from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_raises from packaging.version import parse import pytest -from randomgen import MT19937, Generator -from randomgen.tests.test_direct import assert_state_equal - -random = Generator(MT19937(mode="legacy")) +from randomgen import MT19937 NP_LT_118 = parse(np.__version__) < parse("1.18.0") @@ -59,22 +44,6 @@ def endpoint(request): class TestSeed(object): - def test_scalar(self): - s = Generator(MT19937(0, mode="legacy")) - assert_equal(s.integers(1000), 684) - s = Generator(MT19937(4294967295, mode="legacy")) - assert_equal(s.integers(1000), 419) - - def test_array(self): - s = Generator(MT19937(range(10), mode="legacy")) - assert_equal(s.integers(1000), 468) - s = Generator(MT19937(np.arange(10), mode="legacy")) - assert_equal(s.integers(1000), 468) - s = Generator(MT19937([0], mode="legacy")) - assert_equal(s.integers(1000), 973) - s = Generator(MT19937([4294967295], mode="legacy")) - assert_equal(s.integers(1000), 265) - def test_invalid_scalar(self): # seed must be an unsigned 32 bit integer assert_raises(TypeError, MT19937, -0.5, mode="legacy") @@ -88,2693 +57,6 @@ def test_invalid_array(self): assert_raises(ValueError, MT19937, [1, 2, 4294967296], mode="legacy") assert_raises(ValueError, MT19937, [1, -2, 4294967296], mode="legacy") - def test_noninstantized_bitgen(self): - assert_raises(ValueError, Generator, MT19937) - - -class TestBinomial(object): - def test_n_zero(self): - # Tests the corner case of n == 0 for the binomial distribution. - # binomial(0, p) should be zero for any p in [0, 1]. - # This test addresses issue #3480. - zeros = np.zeros(2, dtype="int") - for p in [0, 0.5, 1]: - assert_(random.binomial(0, p) == 0) - assert_array_equal(random.binomial(zeros, p), zeros) - - def test_p_is_nan(self): - # Issue #4571. - assert_raises(ValueError, random.binomial, 1, np.nan) - - -class TestMultinomial(object): - def test_basic(self): - random.multinomial(100, [0.2, 0.8]) - - def test_zero_probability(self): - random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0]) - - def test_int_negative_interval(self): - assert_(-5 <= random.integers(-5, -1) < -1) - x = random.integers(-5, -1, 5) - assert_(np.all(-5 <= x)) - assert_(np.all(x < -1)) - - def test_size(self): - # gh-3173 - p = [0.5, 0.5] - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, [2, 2]).shape, (2, 2, 2)) - assert_equal(random.multinomial(1, p, (2, 2)).shape, (2, 2, 2)) - assert_equal(random.multinomial(1, p, np.array((2, 2))).shape, (2, 2, 2)) - - assert_raises(TypeError, random.multinomial, 1, p, float(1)) - - def test_invalid_prob(self): - assert_raises(ValueError, random.multinomial, 100, [1.1, 0.2]) - assert_raises(ValueError, random.multinomial, 100, [-0.1, 0.9]) - - def test_invalid_n(self): - assert_raises(ValueError, random.multinomial, -1, [0.8, 0.2]) - assert_raises(ValueError, random.multinomial, [-1] * 10, [0.8, 0.2]) - - def test_p_noncontiguous(self): - p = np.arange(15.0) - p /= np.sum(p[1::3]) - pvals = p[1::3] - random.seed(1432985819) - non_contig = random.multinomial(100, pvals=pvals) - random.seed(1432985819) - contig = random.multinomial(100, pvals=np.ascontiguousarray(pvals)) - assert_array_equal(non_contig, contig) - - def test_large_p(self): - with pytest.raises(ValueError, match=r"sum\(pvals"): - random.multinomial(100, np.array([0.7, 0.6, 0.5, 0])) - - -class TestSetState(object): - def setup(self): - self.seed = 1234567890 - self.rg = Generator(MT19937(self.seed, mode="legacy")) - self.bit_generator = self.rg.bit_generator - self.state = self.bit_generator.state - self.legacy_state = ( - self.state["bit_generator"], - self.state["state"]["key"], - self.state["state"]["pos"], - ) - - def test_basic(self): - with pytest.deprecated_call(): - old = self.rg.tomaxint(16) - self.bit_generator.state = self.state - with pytest.deprecated_call(): - new = self.rg.tomaxint(16) - assert_(np.all(old == new)) - - def test_gaussian_reset(self): - # Make sure the cached every-other-Gaussian is reset. - old = self.rg.standard_normal(size=3) - self.bit_generator.state = self.state - new = self.rg.standard_normal(size=3) - assert_(np.all(old == new)) - - def test_gaussian_reset_in_media_res(self): - # When the state is saved with a cached Gaussian, make sure the - # cached Gaussian is restored. - - self.rg.standard_normal() - state = self.bit_generator.state - old = self.rg.standard_normal(size=3) - self.bit_generator.state = state - new = self.rg.standard_normal(size=3) - assert_(np.all(old == new)) - - def test_negative_binomial(self): - # Ensure that the negative binomial results take floating point - # arguments without truncation. - self.rg.negative_binomial(0.5, 0.5) - - -class TestIntegers(object): - rfunc = random.integers - - # valid integer/boolean types - itype = [ - bool, - np.int8, - np.uint8, - np.int16, - np.uint16, - np.int32, - np.uint32, - np.int64, - np.uint64, - ] - - def test_unsupported_type(self, endpoint): - assert_raises(TypeError, self.rfunc, 1, endpoint=endpoint, dtype=float) - - def test_bounds_checking(self, endpoint): - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - assert_raises( - ValueError, self.rfunc, lbnd - 1, ubnd, endpoint=endpoint, dtype=dt - ) - assert_raises( - ValueError, self.rfunc, lbnd, ubnd + 1, endpoint=endpoint, dtype=dt - ) - assert_raises( - ValueError, self.rfunc, ubnd, lbnd, endpoint=endpoint, dtype=dt - ) - assert_raises(ValueError, self.rfunc, 1, 0, endpoint=endpoint, dtype=dt) - - assert_raises( - ValueError, self.rfunc, [lbnd - 1], ubnd, endpoint=endpoint, dtype=dt - ) - assert_raises( - ValueError, self.rfunc, [lbnd], [ubnd + 1], endpoint=endpoint, dtype=dt - ) - assert_raises( - ValueError, self.rfunc, [ubnd], [lbnd], endpoint=endpoint, dtype=dt - ) - assert_raises(ValueError, self.rfunc, 1, [0], endpoint=endpoint, dtype=dt) - - def test_bounds_checking_array(self, endpoint): - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + (not endpoint) - - assert_raises( - ValueError, - self.rfunc, - [lbnd - 1] * 2, - [ubnd] * 2, - endpoint=endpoint, - dtype=dt, - ) - assert_raises( - ValueError, - self.rfunc, - [lbnd] * 2, - [ubnd + 1] * 2, - endpoint=endpoint, - dtype=dt, - ) - assert_raises( - ValueError, self.rfunc, ubnd, [lbnd] * 2, endpoint=endpoint, dtype=dt - ) - assert_raises( - ValueError, self.rfunc, [1] * 2, 0, endpoint=endpoint, dtype=dt - ) - - def test_rng_zero_and_extremes(self, endpoint): - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - is_open = not endpoint - - tgt = ubnd - 1 - assert_equal( - self.rfunc(tgt, tgt + is_open, size=1000, endpoint=endpoint, dtype=dt), - tgt, - ) - assert_equal( - self.rfunc( - [tgt], tgt + is_open, size=1000, endpoint=endpoint, dtype=dt - ), - tgt, - ) - - tgt = lbnd - assert_equal( - self.rfunc(tgt, tgt + is_open, size=1000, endpoint=endpoint, dtype=dt), - tgt, - ) - assert_equal( - self.rfunc( - tgt, [tgt + is_open], size=1000, endpoint=endpoint, dtype=dt - ), - tgt, - ) - - tgt = (lbnd + ubnd) // 2 - assert_equal( - self.rfunc(tgt, tgt + is_open, size=1000, endpoint=endpoint, dtype=dt), - tgt, - ) - assert_equal( - self.rfunc( - [tgt], [tgt + is_open], size=1000, endpoint=endpoint, dtype=dt - ), - tgt, - ) - - def test_rng_zero_and_extremes_array(self, endpoint): - size = 1000 - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - tgt = ubnd - 1 - assert_equal(self.rfunc([tgt], [tgt + 1], size=size, dtype=dt), tgt) - assert_equal(self.rfunc([tgt] * size, [tgt + 1] * size, dtype=dt), tgt) - assert_equal( - self.rfunc([tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt - ) - - tgt = lbnd - assert_equal(self.rfunc([tgt], [tgt + 1], size=size, dtype=dt), tgt) - assert_equal(self.rfunc([tgt] * size, [tgt + 1] * size, dtype=dt), tgt) - assert_equal( - self.rfunc([tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt - ) - - tgt = (lbnd + ubnd) // 2 - assert_equal(self.rfunc([tgt], [tgt + 1], size=size, dtype=dt), tgt) - assert_equal(self.rfunc([tgt] * size, [tgt + 1] * size, dtype=dt), tgt) - assert_equal( - self.rfunc([tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt - ) - - def test_full_range(self, endpoint): - # Test for ticket #1690 - - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - try: - self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt) - except Exception as e: - raise AssertionError( - "No error should have been raised, " - "but one was with the following " - "message:\n\n%s" % str(e) - ) - - def test_full_range_array(self, endpoint): - # Test for ticket #1690 - - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - try: - self.rfunc([lbnd] * 2, [ubnd], endpoint=endpoint, dtype=dt) - except Exception as e: - raise AssertionError( - "No error should have been raised, " - "but one was with the following " - "message:\n\n%s" % str(e) - ) - - def test_in_bounds_fuzz(self, endpoint): - # Don"t use fixed seed - random.bit_generator.seed() - - for dt in self.itype[1:]: - for ubnd in [4, 8, 16]: - vals = self.rfunc( - 2, ubnd - endpoint, size=2**16, endpoint=endpoint, dtype=dt - ) - assert_(vals.max() < ubnd) - assert_(vals.min() >= 2) - - vals = self.rfunc(0, 2 - endpoint, size=2**16, endpoint=endpoint, dtype=bool) - assert_(vals.max() < 2) - assert_(vals.min() >= 0) - - def test_scalar_array_equiv(self, endpoint): - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - size = 1000 - random.bit_generator.seed(1234) - scalar = self.rfunc(lbnd, ubnd, size=size, endpoint=endpoint, dtype=dt) - - random.bit_generator.seed(1234) - scalar_array = self.rfunc( - [lbnd], [ubnd], size=size, endpoint=endpoint, dtype=dt - ) - - random.bit_generator.seed(1234) - array = self.rfunc( - [lbnd] * size, [ubnd] * size, size=size, endpoint=endpoint, dtype=dt - ) - assert_array_equal(scalar, scalar_array) - assert_array_equal(scalar, array) - - def test_repeatability(self, endpoint): - import hashlib - - # We use a md5 hash of generated sequences of 1000 samples - # in the range [0, 6) for all but bool, where the range - # is [0, 2). Hashes are for little endian numbers. - tgt = { - "bool": "7dd3170d7aa461d201a65f8bcf3944b0", - "int16": "1b7741b80964bb190c50d541dca1cac1", - "int32": "4dc9fcc2b395577ebb51793e58ed1a05", - "int64": "17db902806f448331b5a758d7d2ee672", - "int8": "27dd30c4e08a797063dffac2490b0be6", - "uint16": "1b7741b80964bb190c50d541dca1cac1", - "uint32": "4dc9fcc2b395577ebb51793e58ed1a05", - "uint64": "17db902806f448331b5a758d7d2ee672", - "uint8": "27dd30c4e08a797063dffac2490b0be6", - } - - for dt in self.itype[1:]: - random.bit_generator.seed(1234) - - # view as little endian for hash - if sys.byteorder == "little": - val = self.rfunc( - 0, 6 - endpoint, size=1000, endpoint=endpoint, dtype=dt - ) - else: - val = self.rfunc( - 0, 6 - endpoint, size=1000, endpoint=endpoint, dtype=dt - ).byteswap() - - res = hashlib.md5(val.view(np.int8)).hexdigest() - assert_(tgt[np.dtype(dt).name] == res) - - # bools do not depend on endianness - random.bit_generator.seed(1234) - val = self.rfunc( - 0, 2 - endpoint, size=1000, endpoint=endpoint, dtype=bool - ).view(np.int8) - res = hashlib.md5(val).hexdigest() - assert_(tgt[np.dtype(bool).name] == res) - - def test_repeatability_broadcasting(self, endpoint): - for dt in self.itype: - lbnd = 0 if dt in (bool, np.bool_) else np.iinfo(dt).min - ubnd = 2 if dt in (bool, np.bool_) else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - # view as little endian for hash - random.bit_generator.seed(1234) - val = self.rfunc(lbnd, ubnd, size=1000, endpoint=endpoint, dtype=dt) - - random.bit_generator.seed(1234) - val_bc = self.rfunc([lbnd] * 1000, ubnd, endpoint=endpoint, dtype=dt) - - assert_array_equal(val, val_bc) - - random.bit_generator.seed(1234) - val_bc = self.rfunc( - [lbnd] * 1000, [ubnd] * 1000, endpoint=endpoint, dtype=dt - ) - - assert_array_equal(val, val_bc) - - def test_repeatability_32bit_boundary_broadcasting(self): - desired = np.array( - [ - [ - [4184714646, 2953452547, 3636115811], - [3137091686, 500004980, 1758274813], - [827841543, 2071399968, 2653935293], - ], - [ - [1980473914, 2331635770, 643122924], - [806373568, 3436742405, 3326492796], - [819438482, 2041859381, 1972373725], - ], - [ - [2973988042, 1073437830, 395026719], - [2154927168, 964445294, 449660552], - [4126967444, 1410100955, 3481829584], - ], - [ - [136169376, 332583752, 1486552164], - [2199706765, 2840948792, 1367639842], - [3733647586, 810727718, 3455450384], - ], - [ - [2374161015, 433367801, 3216002152], - [595355362, 342429046, 2159480359], - [3577969687, 2369902420, 764825175], - ], - ] - ) - for size in [None, (5, 3, 3)]: - random = Generator(MT19937(12345, mode="sequence")) - x = random.integers( - [[-1], [0], [1]], [2**32 - 1, 2**32, 2**32 + 1], size=size - ) - assert_array_equal(x, desired if size is not None else desired[0]) - - def test_int64_uint64_broadcast_exceptions(self, endpoint): - configs = { - np.uint64: ((0, 2**65), (-1, 2**62), (10, 9), (0, 0)), - np.int64: ( - (0, 2**64), - (-(2**64), 2**62), - (10, 9), - (0, 0), - (-(2**63) - 1, -(2**63) - 1), - ), - } - for dtype in configs: - for config in configs[dtype]: - low, high = config - high = high - endpoint - low_a = np.array([[low] * 10]) - high_a = np.array([high] * 10) - assert_raises( - ValueError, - random.integers, - low, - high, - endpoint=endpoint, - dtype=dtype, - ) - assert_raises( - ValueError, - random.integers, - low_a, - high, - endpoint=endpoint, - dtype=dtype, - ) - assert_raises( - ValueError, - random.integers, - low, - high_a, - endpoint=endpoint, - dtype=dtype, - ) - assert_raises( - ValueError, - random.integers, - low_a, - high_a, - endpoint=endpoint, - dtype=dtype, - ) - - low_o = np.array([[low] * 10], dtype=object) - high_o = np.array([high] * 10, dtype=object) - assert_raises( - ValueError, - random.integers, - low_o, - high, - endpoint=endpoint, - dtype=dtype, - ) - assert_raises( - ValueError, - random.integers, - low, - high_o, - endpoint=endpoint, - dtype=dtype, - ) - assert_raises( - ValueError, - random.integers, - low_o, - high_o, - endpoint=endpoint, - dtype=dtype, - ) - - def test_int64_uint64_corner_case(self, endpoint): - # When stored in Numpy arrays, `lbnd` is casted - # as np.int64, and `ubnd` is casted as np.uint64. - # Checking whether `lbnd` >= `ubnd` used to be - # done solely via direct comparison, which is incorrect - # because when Numpy tries to compare both numbers, - # it casts both to np.float64 because there is - # no integer superset of np.int64 and np.uint64. However, - # `ubnd` is too large to be represented in np.float64, - # causing it be round down to np.iinfo(np.int64).max, - # leading to a ValueError because `lbnd` now equals - # the new `ubnd`. - - dt = np.int64 - tgt = np.iinfo(np.int64).max - lbnd = np.int64(np.iinfo(np.int64).max) - ubnd = np.uint64(np.iinfo(np.int64).max + 1 - endpoint) - - # None of these function calls should - # generate a ValueError now. - actual = random.integers(lbnd, ubnd, endpoint=endpoint, dtype=dt) - assert_equal(actual, tgt) - - def test_respect_dtype_singleton(self, endpoint): - # See gh-7203 - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - dt = np.bool_ if dt is bool else dt - - sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt) - assert_equal(sample.dtype, dt) - - for dt in (bool, int): - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - - # gh-7284: Ensure that we get Python data types - sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt) - assert not hasattr(sample, "dtype") - assert_equal(type(sample), dt) - - def test_respect_dtype_array(self, endpoint): - # See gh-7203 - for dt in self.itype: - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - ubnd = ubnd - 1 if endpoint else ubnd - dt = np.bool_ if dt is bool else dt - - sample = self.rfunc([lbnd], [ubnd], endpoint=endpoint, dtype=dt) - assert_equal(sample.dtype, dt) - sample = self.rfunc([lbnd] * 2, [ubnd] * 2, endpoint=endpoint, dtype=dt) - assert_equal(sample.dtype, dt) - - def test_zero_size(self, endpoint): - # See gh-7203 - for dt in self.itype: - sample = self.rfunc(0, 0, (3, 0, 4), endpoint=endpoint, dtype=dt) - assert sample.shape == (3, 0, 4) - assert sample.dtype == dt - assert self.rfunc(0, -10, 0, endpoint=endpoint, dtype=dt).shape == (0,) - assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape, (3, 0, 4)) - assert_equal(random.integers(0, -10, size=0).shape, (0,)) - assert_equal(random.integers(10, 10, size=0).shape, (0,)) - - def test_warns_byteorder(self): - other_byteord_dt = "i4" - with pytest.warns(FutureWarning): - random.integers(0, 200, size=10, dtype=other_byteord_dt) - - -class TestRandomDist(object): - # Make sure the random distribution returns the correct value for a - # given seed - - def setup(self): - self.seed = 1234567890 - - def test_rand(self): - random.bit_generator.seed(self.seed) - with pytest.deprecated_call(): - actual = random.rand(3, 2) - desired = np.array( - [ - [0.61879477158567997, 0.59162362775974664], - [0.88868358904449662, 0.89165480011560816], - [0.4575674820298663, 0.7781880808593471], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_rand_singleton(self): - random.bit_generator.seed(self.seed) - with pytest.deprecated_call(): - actual = random.rand() - desired = 0.61879477158567997 - assert_array_almost_equal(actual, desired, decimal=15) - - def test_randn(self): - random.bit_generator.seed(self.seed) - with pytest.deprecated_call(): - actual = random.randn(3, 2) - desired = np.array( - [ - [-3.472754000610961, -0.108938564229143], - [-0.245965753396411, -0.704101550261701], - [0.360102487116356, 0.127832101772367], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.bit_generator.seed(self.seed) - with pytest.deprecated_call(): - actual = random.randn() - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_integers(self): - random.bit_generator.seed(self.seed) - actual = random.integers(-99, 99, size=(3, 2)) - desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - assert_array_equal(actual, desired) - - def test_integers_masked(self): - # Test masked rejection sampling algorithm to generate array of - # uint32 in an interval. - random.bit_generator.seed(self.seed) - with pytest.deprecated_call(): - actual = random.integers( - 0, 99, size=(3, 2), dtype=np.uint32, use_masked=True - ) - desired = np.array([[2, 47], [12, 51], [33, 43]], dtype=np.uint32) - assert_array_equal(actual, desired) - - def test_integers_lemire_32(self): - # Test lemire algorithm to generate array of uint32 in an interval. - random.bit_generator.seed(self.seed) - actual = random.integers(0, 99, size=(3, 2), dtype=np.uint32, use_masked=False) - desired = np.array([[61, 33], [58, 14], [87, 23]], dtype=np.uint32) - assert_array_equal(actual, desired) - - def test_integers_lemire_64(self): - # Test lemire algorithm to generate array of uint64 in an interval. - random.bit_generator.seed(self.seed) - actual = random.integers( - 0, 99 + 0xFFFFFFFFF, size=(3, 2), dtype=np.uint64, use_masked=False - ) - desired = np.array( - [ - [42523252834, 40656066204], - [61069871386, 61274051182], - [31443797706, 53476677934], - ], - dtype=np.uint64, - ) - assert_array_equal(actual, desired) - - def test_random_integers(self): - random.bit_generator.seed(self.seed) - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(-99, 99, size=(3, 2)) - assert_(len(w) == 1) - desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - assert_array_equal(actual, desired) - random.bit_generator.seed(self.seed) - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(198, size=(3, 2)) - assert_array_equal(actual, desired + 100) - - def test_random_integers_max_int(self): - # Tests whether random_integers can generate the - # maximum allowed Python int that can be converted - # into a C long. Previous implementations of this - # method have thrown an OverflowError when attempting - # to generate this integer. - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(np.iinfo("l").max, np.iinfo("l").max) - assert_(len(w) == 1) - - desired = np.iinfo("l").max - assert_equal(actual, desired) - - def test_random_integers_deprecated(self): - with warnings.catch_warnings(): - warnings.simplefilter("error", DeprecationWarning) - - # DeprecationWarning raised with high == None - assert_raises(DeprecationWarning, random.random_integers, np.iinfo("l").max) - - # DeprecationWarning raised with high != None - assert_raises( - DeprecationWarning, - random.random_integers, - np.iinfo("l").max, - np.iinfo("l").max, - ) - - def test_random(self): - random.bit_generator.seed(self.seed) - actual = random.random((3, 2)) - desired = np.array( - [ - [0.61879477158567997, 0.59162362775974664], - [0.88868358904449662, 0.89165480011560816], - [0.4575674820298663, 0.7781880808593471], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.bit_generator.seed(self.seed) - actual = random.random() - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_random_float(self): - random.bit_generator.seed(self.seed) - actual = random.random((3, 2)) - desired = np.array( - [[0.6187948, 0.5916236], [0.8886836, 0.8916548], [0.4575675, 0.7781881]] - ) - assert_array_almost_equal(actual, desired, decimal=7) - - def test_random_float_scalar(self): - random.bit_generator.seed(self.seed) - actual = random.random(dtype=np.float32) - desired = 0.6187948 - assert_array_almost_equal(actual, desired, decimal=7) - - def test_random_unsupported_type(self): - assert_raises(TypeError, random.random, dtype="int32") - - def test_choice_uniform_replace(self): - random.bit_generator.seed(self.seed) - actual = random.choice(4, 4) - desired = np.array([2, 1, 2, 0], dtype=np.int64) - assert_array_equal(actual, desired) - - def test_choice_nonuniform_replace(self): - random.bit_generator.seed(self.seed) - actual = random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1]) - desired = np.array([1, 1, 2, 2], dtype=np.int64) - assert_array_equal(actual, desired) - - def test_choice_uniform_noreplace(self): - random.bit_generator.seed(self.seed) - actual = random.choice(4, 3, replace=False) - desired = np.array([3, 2, 1], dtype=np.int64) - assert_array_equal(actual, desired) - - def test_choice_nonuniform_noreplace(self): - random.bit_generator.seed(self.seed) - actual = random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1]) - desired = np.array([2, 3, 1], dtype=np.int64) - assert_array_equal(actual, desired) - - def test_choice_noninteger(self): - random.bit_generator.seed(self.seed) - actual = random.choice(["a", "b", "c", "d"], 4) - desired = np.array(["c", "b", "c", "a"]) - assert_array_equal(actual, desired) - - def test_choice_multidimensional_default_axis(self): - random.bit_generator.seed(self.seed) - actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 3) - desired = np.array([[4, 5], [2, 3], [4, 5]]) - assert_array_equal(actual, desired) - - def test_choice_multidimensional_custom_axis(self): - random.bit_generator.seed(self.seed) - actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 1, axis=1) - desired = np.array([[1], [3], [5], [7]]) - assert_array_equal(actual, desired) - - def test_choice_exceptions(self): - sample = random.choice - assert_raises(ValueError, sample, -1, 3) - assert_raises(ValueError, sample, 3.0, 3) - assert_raises(ValueError, sample, [], 3) - assert_raises( - ValueError, sample, [1, 2, 3, 4], 3, p=[[0.25, 0.25], [0.25, 0.25]] - ) - assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2]) - assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1]) - assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4]) - assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False) - # gh-13087 - assert_raises(ValueError, sample, [1, 2, 3], -2, replace=False) - assert_raises(ValueError, sample, [1, 2, 3], (-1,), replace=False) - assert_raises(ValueError, sample, [1, 2, 3], (-1, 1), replace=False) - assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False, p=[1, 0, 0]) - - def test_choice_return_shape(self): - p = [0.1, 0.9] - # Check scalar - assert_(np.isscalar(random.choice(2, replace=True))) - assert_(np.isscalar(random.choice(2, replace=False))) - assert_(np.isscalar(random.choice(2, replace=True, p=p))) - assert_(np.isscalar(random.choice(2, replace=False, p=p))) - assert_(np.isscalar(random.choice([1, 2], replace=True))) - assert_(random.choice([None], replace=True) is None) - a = np.array([1, 2]) - arr = np.empty(1, dtype=object) - arr[0] = a - assert_(random.choice(arr, replace=True) is a) - - # Check 0-d array - s = tuple() - assert_(not np.isscalar(random.choice(2, s, replace=True))) - assert_(not np.isscalar(random.choice(2, s, replace=False))) - assert_(not np.isscalar(random.choice(2, s, replace=True, p=p))) - assert_(not np.isscalar(random.choice(2, s, replace=False, p=p))) - assert_(not np.isscalar(random.choice([1, 2], s, replace=True))) - assert_(random.choice([None], s, replace=True).ndim == 0) - a = np.array([1, 2]) - arr = np.empty(1, dtype=object) - arr[0] = a - assert_(random.choice(arr, s, replace=True).item() is a) - - # Check multi dimensional array - s = (2, 3) - p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2] - assert_equal(random.choice(6, s, replace=True).shape, s) - assert_equal(random.choice(6, s, replace=False).shape, s) - assert_equal(random.choice(6, s, replace=True, p=p).shape, s) - assert_equal(random.choice(6, s, replace=False, p=p).shape, s) - assert_equal(random.choice(np.arange(6), s, replace=True).shape, s) - - # Check zero-size - assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape, (3, 0, 4)) - assert_equal(random.integers(0, -10, size=0).shape, (0,)) - assert_equal(random.integers(10, 10, size=0).shape, (0,)) - assert_equal(random.choice(0, size=0).shape, (0,)) - assert_equal(random.choice([], size=(0,)).shape, (0,)) - assert_equal(random.choice(["a", "b"], size=(3, 0, 4)).shape, (3, 0, 4)) - assert_raises(ValueError, random.choice, [], 10) - - def test_choice_nan_probabilities(self): - a = np.array([42, 1, 2]) - p = [None, None, None] - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.choice, a, p=p) - - def test_choice_nontintiguous(self): - p = np.ones(10) / 5 - p[1::2] = 3.0 - random.seed(self.seed) - choice1 = random.choice(5, 3, p=p[::2]) - random.seed(self.seed) - choice2 = random.choice(5, 3, p=np.ascontiguousarray(p[::2])) - assert_array_equal(choice1, choice2) - - def test_choice_return_type(self): - # gh 9867 - p = np.ones(4) / 4.0 - actual = random.choice(4, 2) - assert actual.dtype == np.int64 - actual = random.choice(4, 2, replace=False) - assert actual.dtype == np.int64 - actual = random.choice(4, 2, p=p) - assert actual.dtype == np.int64 - actual = random.choice(4, 2, p=p, replace=False) - assert actual.dtype == np.int64 - - def test_choice_large_sample(self): - import hashlib - - choice_hash = "7d65d45dea0cacb950de86582f37ff74" - random.bit_generator.seed(self.seed) - actual = random.choice(10000, 5000, replace=False) - if sys.byteorder != "little": - actual = actual.byteswap() - res = hashlib.md5(actual.view(np.int8)).hexdigest() - assert choice_hash == res - - def test_choice_very_large_sample(self): - import hashlib - - choice_hash = "c1adc3c51a477b4ca642a5643e3dcad85e10a74c600b5299c64f5257bb060155" - random.bit_generator.seed(self.seed) - actual = random.choice(25000, 12500, replace=False) - assert actual.shape == (12500,) - if sys.byteorder != "little": - actual = actual.byteswap() - res = hashlib.sha256(actual.view(np.int8)).hexdigest() - assert choice_hash == res - - def test_bytes(self): - random.bit_generator.seed(self.seed) - actual = random.bytes(10) - desired = b"\x82Ui\x9e\xff\x97+Wf\xa5" - assert_equal(actual, desired) - - def test_shuffle(self): - # Test lists, arrays (of various dtypes), and multidimensional versions - # of both, c-contiguous or not: - for conv in [ - lambda x: np.array([]), - lambda x: x, - lambda x: np.asarray(x).astype(np.int8), - lambda x: np.asarray(x).astype(np.float32), - lambda x: np.asarray(x).astype(np.complex64), - lambda x: np.asarray(x).astype(object), - lambda x: [(i, i) for i in x], - lambda x: np.asarray([[i, i] for i in x]), - lambda x: np.vstack([x, x]).T, - # gh-11442 - lambda x: ( - np.asarray([(i, i) for i in x], [("a", int), ("b", int)]).view( - np.recarray - ) - ), - # gh-4270 - lambda x: np.asarray( - [(i, i) for i in x], [("a", (object, (1,))), ("b", (np.int32, (1,)))] - ), - ]: - random.bit_generator.seed(self.seed) - alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) - random.shuffle(alist) - actual = alist - desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) - assert_array_equal(actual, desired) - - def test_shuffle_masked(self): - # gh-3263 - a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1) - b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) - a_orig = a.copy() - b_orig = b.copy() - for _ in range(50): - random.shuffle(a) - assert_equal(sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask])) - random.shuffle(b) - assert_equal(sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask])) - - def test_permutation(self): - random.bit_generator.seed(self.seed) - alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] - actual = random.permutation(alist) - desired = [0, 1, 9, 6, 2, 4, 5, 8, 7, 3] - assert_array_equal(actual, desired) - - random.bit_generator.seed(self.seed) - arr_2d = np.atleast_2d([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]).T - actual = random.permutation(arr_2d) - assert_array_equal(actual, np.atleast_2d(desired).T) - - def test_beta(self): - random.bit_generator.seed(self.seed) - actual = random.beta(0.1, 0.9, size=(3, 2)) - desired = np.array( - [ - [1.45341850513746058e-02, 5.31297615662868145e-04], - [1.85366619058432324e-06, 4.19214516800110563e-03], - [1.58405155108498093e-04, 1.26252891949397652e-04], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_binomial(self): - random.bit_generator.seed(self.seed) - actual = random.binomial(100.123, 0.456, size=(3, 2)) - desired = np.array([[37, 43], [42, 48], [46, 45]]) - assert_array_equal(actual, desired) - - random.bit_generator.seed(self.seed) - actual = random.binomial(100.123, 0.456) - desired = 37 - assert_array_equal(actual, desired) - - def test_chisquare(self): - random.bit_generator.seed(self.seed) - actual = random.chisquare(50, size=(3, 2)) - desired = np.array( - [ - [22.2534560369812, 46.9302393710074], - [52.9974164611614, 85.3559029505718], - [46.1580841240719, 36.1933148548090], - ] - ) - assert_array_almost_equal(actual, desired, decimal=13) - - def test_dirichlet(self): - random.bit_generator.seed(self.seed) - alpha = np.array([51.72840233779265162, 39.74494232180943953]) - actual = random.dirichlet(alpha, size=(3, 2)) - desired = np.array( - [ - [ - [0.444382290764855, 0.555617709235145], - [0.468440809291970, 0.531559190708030], - ], - [ - [0.613461427360549, 0.386538572639451], - [0.529103072088183, 0.470896927911817], - ], - [ - [0.513490650101800, 0.486509349898200], - [0.558550925712797, 0.441449074287203], - ], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - bad_alpha = np.array([5.4e-01, -1.0e-16]) - assert_raises(ValueError, random.dirichlet, bad_alpha) - - random.bit_generator.seed(self.seed) - alpha = np.array([51.72840233779265162, 39.74494232180943953]) - actual = random.dirichlet(alpha) - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_dirichlet_size(self): - # gh-3173 - p = np.array([51.72840233779265162, 39.74494232180943953]) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, [2, 2]).shape, (2, 2, 2)) - assert_equal(random.dirichlet(p, (2, 2)).shape, (2, 2, 2)) - assert_equal(random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2)) - - assert_raises(TypeError, random.dirichlet, p, float(1)) - - def test_dirichlet_bad_alpha(self): - # gh-2089 - alpha = np.array([5.4e-01, -1.0e-16]) - assert_raises(ValueError, random.dirichlet, alpha) - - assert_raises(ValueError, random.dirichlet, [[5, 1]]) - assert_raises(ValueError, random.dirichlet, [[5], [1]]) - assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]]) - assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]])) - - def test_dirichlet_non_contiguous_alpha(self): - a = np.array([51.72840233779265162, -1.0, 39.74494232180943953]) - alpha = a[::2] - random.bit_generator.seed(self.seed) - non_contig = random.dirichlet(alpha, size=(3, 2)) - random.bit_generator.seed(self.seed) - contig = random.dirichlet(np.ascontiguousarray(alpha), size=(3, 2)) - assert_array_almost_equal(contig, non_contig) - - def test_dirichlet_small_alpha(self): - eps = 1.0e-9 # 1.0e-10 -> runtime x 10; 1e-11 -> runtime x 200, etc. - alpha = eps * np.array([1.0, 1.0e-3]) - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.dirichlet(alpha, size=(3, 2)) - expected = np.array( - [ - [[1.0, 0.0], [1.0, 0.0]], - [[1.0, 0.0], [1.0, 0.0]], - [[1.0, 0.0], [1.0, 0.0]], - ] - ) - assert_array_almost_equal(actual, expected, decimal=15) - - @pytest.mark.slow - def test_dirichlet_moderately_small_alpha(self): - # Use alpha.max() < 0.1 to trigger stick breaking code path - alpha = np.array([0.02, 0.04, 0.03]) - exact_mean = alpha / alpha.sum() - random = Generator(MT19937(self.seed, mode="sequence")) - sample = random.dirichlet(alpha, size=20000000) - sample_mean = sample.mean(axis=0) - assert_allclose(sample_mean, exact_mean, rtol=1e-3) - - def test_exponential(self): - random.bit_generator.seed(self.seed) - actual = random.exponential(1.1234, size=(3, 2)) - desired = np.array( - [ - [5.350682337747634, 1.152307441755771], - [3.867015473358779, 1.538765912839396], - [0.347846818048527, 2.715656549872026], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_exponential_0(self): - assert_equal(random.exponential(scale=0), 0) - assert_raises(ValueError, random.exponential, scale=-0.0) - - def test_f(self): - random.bit_generator.seed(self.seed) - actual = random.f(12, 77, size=(3, 2)) - desired = np.array( - [ - [0.809498839488467, 2.867222762455471], - [0.588036831639353, 1.012185639664636], - [1.147554281917365, 1.150886518432105], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_gamma(self): - random.bit_generator.seed(self.seed) - actual = random.gamma(5, 3, size=(3, 2)) - desired = np.array( - [ - [12.46569350177219, 16.46580642087044], - [43.65744473309084, 11.98722785682592], - [6.50371499559955, 7.48465689751638], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_gamma_0(self): - assert_equal(random.gamma(shape=0, scale=0), 0) - assert_raises(ValueError, random.gamma, shape=-0.0, scale=-0.0) - - def test_geometric(self): - random.bit_generator.seed(self.seed) - actual = random.geometric(0.123456789, size=(3, 2)) - desired = np.array([[8, 7], [17, 17], [5, 12]]) - assert_array_equal(actual, desired) - - def test_geometric_exceptions(self): - assert_raises(ValueError, random.geometric, 1.1) - assert_raises(ValueError, random.geometric, [1.1] * 10) - assert_raises(ValueError, random.geometric, -0.1) - assert_raises(ValueError, random.geometric, [-0.1] * 10) - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.geometric, np.nan) - assert_raises(ValueError, random.geometric, [np.nan] * 10) - - def test_gumbel(self): - random.bit_generator.seed(self.seed) - actual = random.gumbel(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [0.19591898743416816, 0.34405539668096674], - [-1.4492522252274278, -1.47374816298446865], - [1.10651090478803416, -0.69535848626236174], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_gumbel_0(self): - assert_equal(random.gumbel(scale=0), 0) - assert_raises(ValueError, random.gumbel, scale=-0.0) - - def test_hypergeometric(self): - random.bit_generator.seed(self.seed) - actual = random.hypergeometric(10.1, 5.5, 14, size=(3, 2)) - desired = np.array([[9, 9], [10, 9], [9, 10]]) - assert_array_equal(actual, desired) - - # Test nbad = 0 - actual = random.hypergeometric(5, 0, 3, size=4) - desired = np.array([3, 3, 3, 3]) - assert_array_equal(actual, desired) - - actual = random.hypergeometric(15, 0, 12, size=4) - desired = np.array([12, 12, 12, 12]) - assert_array_equal(actual, desired) - - # Test ngood = 0 - actual = random.hypergeometric(0, 5, 3, size=4) - desired = np.array([0, 0, 0, 0]) - assert_array_equal(actual, desired) - - actual = random.hypergeometric(0, 15, 12, size=4) - desired = np.array([0, 0, 0, 0]) - assert_array_equal(actual, desired) - - def test_laplace(self): - random.bit_generator.seed(self.seed) - actual = random.laplace(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [0.66599721112760157, 0.52829452552221945], - [3.12791959514407125, 3.18202813572992005], - [-0.05391065675859356, 1.74901336242837324], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_laplace_0(self): - assert_equal(random.laplace(scale=0), 0) - assert_raises(ValueError, random.laplace, scale=-0.0) - - def test_logistic(self): - random.bit_generator.seed(self.seed) - actual = random.logistic(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [1.09232835305011444, 0.8648196662399954], - [4.27818590694950185, 4.33897006346929714], - [-0.21682183359214885, 2.63373365386060332], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_lognormal(self): - random.bit_generator.seed(self.seed) - actual = random.lognormal(mean=0.123456789, sigma=2.0, size=(3, 2)) - desired = np.array( - [ - [1.0894838661036e-03, 9.0990021488311e-01], - [6.9178869932225e-01, 2.7672077560016e-01], - [2.3248645126975e00, 1.4609997951330e00], - ] - ) - assert_array_almost_equal(actual, desired, decimal=13) - - def test_lognormal_0(self): - assert_equal(random.lognormal(sigma=0), 1) - assert_raises(ValueError, random.lognormal, sigma=-0.0) - - def test_logseries(self): - random.bit_generator.seed(self.seed) - actual = random.logseries(p=0.923456789, size=(3, 2)) - desired = np.array([[2, 2], [6, 17], [3, 6]]) - assert_array_equal(actual, desired) - - def test_logseries_exceptions(self): - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.logseries, np.nan) - assert_raises(ValueError, random.logseries, [np.nan] * 10) - - def test_multinomial(self): - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.multinomial(20, [1 / 6.0] * 6, size=(3, 2)) - desired = np.array( - [ - [[4, 4, 3, 2, 5, 2], [2, 8, 4, 0, 2, 4]], - [[4, 4, 5, 1, 3, 3], [2, 4, 1, 5, 2, 6]], - [[1, 2, 7, 5, 2, 3], [5, 4, 4, 2, 3, 2]], - ] - ) - assert_array_equal(actual, desired) - - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.multinomial([5, 20], [1 / 6.0] * 6) - desired = np.array([[1, 1, 1, 0, 2, 0], [2, 8, 4, 0, 2, 4]], dtype=np.int64) - assert_array_equal(actual, desired) - - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.multinomial([5, 20], [[1 / 6.0] * 6] * 2) - desired = np.array([[1, 1, 1, 0, 2, 0], [2, 8, 4, 0, 2, 4]], dtype=np.int64) - assert_array_equal(actual, desired) - - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.multinomial([[5], [20]], [[1 / 6.0] * 6] * 2) - desired = np.array( - [ - [[1, 1, 1, 0, 2, 0], [0, 4, 1, 0, 0, 0]], - [[1, 2, 5, 5, 5, 2], [2, 3, 3, 4, 2, 6]], - ], - dtype=np.int64, - ) - assert_array_equal(actual, desired) - - @pytest.mark.parametrize("n", [10, np.array([10, 10]), np.array([[[10]], [[10]]])]) - def test_multinomial_pval_broadcast(self, n): - random = Generator(MT19937(self.seed, mode="sequence")) - pvals = np.array([1 / 4] * 4) - actual = random.multinomial(n, pvals) - assert actual.shape == np.broadcast(n, 1).shape + (4,) - pvals = np.vstack([pvals, pvals]) - actual = random.multinomial(n, pvals) - assert actual.shape == np.broadcast(n, np.ones(2)).shape + (4,) - - pvals = np.vstack([[pvals], [pvals]]) - actual = random.multinomial(n, pvals) - expected_shape = np.broadcast(n, np.ones((2, 2))).shape - assert actual.shape == expected_shape + (4,) - actual = random.multinomial(n, pvals, size=(3, 2) + expected_shape) - assert actual.shape == (3, 2) + expected_shape + (4,) - - with pytest.raises(ValueError): - # Ensure that size is not broadcast - actual = random.multinomial(n, pvals, size=(1,) * 6) - - def test_invalid_pvals_broadcast(self): - random = Generator(MT19937(self.seed, mode="sequence")) - pvals = [[1 / 6] * 6, [1 / 4] * 6] - assert_raises(ValueError, random.multinomial, 1, pvals) - assert_raises(ValueError, random.multinomial, 6, 0.5) - - def test_empty_outputs(self): - random = Generator(MT19937(self.seed, mode="sequence")) - actual = random.multinomial(np.empty((10, 0, 6), "i8"), [1 / 6] * 6) - assert actual.shape == (10, 0, 6, 6) - actual = random.multinomial(12, np.empty((10, 0, 10))) - assert actual.shape == (10, 0, 10) - actual = random.multinomial(np.empty((3, 0, 7), "i8"), np.empty((3, 0, 7, 4))) - assert actual.shape == (3, 0, 7, 4) - - @pytest.mark.skipif(NP_LT_118, reason="Can only test with NumPy >= 1.18") - @pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"]) - def test_multivariate_normal_method(self, method): - from numpy.random import MT19937 as NPMT19937 - - random = Generator(NPMT19937(self.seed)) - mean = (0.123456789, 10) - cov = [[1, 0], [0, 1]] - size = (3, 2) - actual = random.multivariate_normal(mean, cov, size, method=method) - desired = np.array( - [ - [ - [-1.747478062846581, 11.25613495182354], - [-0.9967333370066214, 10.342002097029821], - ], - [ - [0.7850019631242964, 11.181113712443013], - [0.8901349653255224, 8.873825399642492], - ], - [ - [0.7130260107430003, 9.551628690083056], - [0.7127098726541128, 11.991709234143173], - ], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - # Check for default size, was raising deprecation warning - actual = random.multivariate_normal(mean, cov, method=method) - desired = np.array([0.233278563284287, 9.424140804347195]) - assert_array_almost_equal(actual, desired, decimal=15) - - # Check path with scalar size works correctly - scalar = random.multivariate_normal(mean, cov, 3, method=method) - tuple1d = random.multivariate_normal(mean, cov, (3,), method=method) - assert scalar.shape == tuple1d.shape == (3, 2) - - # Check that non symmetric covariance input raises exception when - # check_valid='raises' if using default svd method. - mean = [0, 0] - cov = [[1, 2], [1, 2]] - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="raise" - ) - - # Check that non positive-semidefinite covariance warns with - # RuntimeWarning - cov = [[1, 2], [2, 1]] - assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov) - assert_warns( - RuntimeWarning, random.multivariate_normal, mean, cov, method="eigh" - ) - assert_raises( - LinAlgError, random.multivariate_normal, mean, cov, method="cholesky" - ) - - # and that it doesn't warn with RuntimeWarning check_valid='ignore' - assert_no_warnings(random.multivariate_normal, mean, cov, check_valid="ignore") - - # and that it raises with RuntimeWarning check_valid='raises' - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="raise" - ) - assert_raises( - ValueError, - random.multivariate_normal, - mean, - cov, - check_valid="raise", - method="eigh", - ) - - # check degenerate samples from singular covariance matrix - cov = [[1, 1], [1, 1]] - if method in ("svd", "eigh"): - samples = random.multivariate_normal(mean, cov, size=(3, 2), method=method) - assert_array_almost_equal(samples[..., 0], samples[..., 1], decimal=6) - else: - assert_raises( - LinAlgError, random.multivariate_normal, mean, cov, method="cholesky" - ) - - cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32) - with suppress_warnings() as sup: - random.multivariate_normal(mean, cov, method=method) - w = sup.record(RuntimeWarning) - assert len(w) == 0 - - mu = np.zeros(2) - cov = np.eye(2) - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="other" - ) - assert_raises(ValueError, random.multivariate_normal, np.zeros((2, 1, 1)), cov) - assert_raises(ValueError, random.multivariate_normal, mu, np.empty((3, 2))) - assert_raises(ValueError, random.multivariate_normal, mu, np.eye(3)) - - @pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"]) - def test_multivariate_normal_basic_stats(self, method): - random = Generator(MT19937(self.seed, mode="sequence")) - n_s = 1000 - mean = np.array([1, 2]) - cov = np.array([[2, 1], [1, 2]]) - s = random.multivariate_normal(mean, cov, size=(n_s,), method=method) - s_center = s - mean - cov_emp = (s_center.T @ s_center) / (n_s - 1) - # these are pretty loose and are only designed to detect major errors - assert np.all(np.abs(s_center.mean(-2)) < 0.1) - assert np.all(np.abs(cov_emp - cov) < 0.2) - - @pytest.mark.parametrize("size", [(4, 3, 2), (5, 4, 3, 2)]) - @pytest.mark.parametrize("mean", [np.zeros(2), np.zeros((3, 3))]) - def test_multivariate_normal_bad_size(self, mean, size): - cov = np.eye(4) - with pytest.raises(ValueError): - random.multivariate_normal(mean, cov) - mean = np.zeros((2, 3, 4)) - with pytest.raises(ValueError): - random.multivariate_normal(mean, cov, size=size) - - with pytest.raises(ValueError): - random.multivariate_normal(0, [[1]], size=size) - with pytest.raises(ValueError): - random.multivariate_normal([0], [1], size=size) - - def test_multivariate_normal(self): - random.bit_generator.seed(self.seed) - mean = (0.123456789, 10) - cov = [[1, 0], [0, 1]] - size = (3, 2) - actual = random.multivariate_normal(mean, cov, size) - desired = np.array( - [ - [ - [-3.34929721161096100, 9.891061435770858], - [-0.12250896439641100, 9.295898449738300], - ], - [ - [0.48355927611635563, 10.127832101772366], - [3.11093021424924300, 10.283109168794352], - ], - [ - [-0.20332082341774727, 9.868532121697195], - [-1.33806889550667330, 9.813657233804179], - ], - ] - ) - - assert_array_almost_equal(actual, desired, decimal=15) - - # Check for default size, was raising deprecation warning - actual = random.multivariate_normal(mean, cov) - desired = np.array([-1.097443117192574, 10.535787051184261]) - assert_array_almost_equal(actual, desired, decimal=15) - - # Check that non positive-semidefinite covariance warns with - # RuntimeWarning - mean = [0, 0] - cov = [[1, 2], [2, 1]] - assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov) - - # and that it doesn"t warn with RuntimeWarning check_valid="ignore" - assert_no_warnings(random.multivariate_normal, mean, cov, check_valid="ignore") - - # and that it raises with RuntimeWarning check_valid="raises" - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="raise" - ) - - cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32) - with suppress_warnings() as sup: - random.multivariate_normal(mean, cov) - w = sup.record(RuntimeWarning) - assert len(w) == 0 - - mu = np.zeros(2) - cov = np.eye(2) - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="other" - ) - assert_raises(ValueError, random.multivariate_normal, np.zeros((2, 1, 1)), cov) - assert_raises(ValueError, random.multivariate_normal, mu, np.empty((3, 2))) - assert_raises(ValueError, random.multivariate_normal, mu, np.eye(3)) - - def test_negative_binomial(self): - random.bit_generator.seed(self.seed) - actual = random.negative_binomial(n=100, p=0.12345, size=(3, 2)) - desired = np.array([[521, 736], [665, 690], [723, 751]]) - assert_array_equal(actual, desired) - - def test_negative_binomial_exceptions(self): - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.negative_binomial, 100, np.nan) - assert_raises(ValueError, random.negative_binomial, 100, [np.nan] * 10) - - def test_noncentral_chisquare(self): - random.bit_generator.seed(self.seed) - actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2)) - desired = np.array( - [ - [9.47783251920357, 10.02066178260461], - [3.15869984192364, 10.5581565031544], - [5.01652540543548, 13.7689551218441], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - actual = random.noncentral_chisquare(df=0.5, nonc=0.2, size=(3, 2)) - desired = np.array( - [ - [0.00145153051285, 0.22432468724778], - [0.02956713468556, 0.00207192946898], - [1.41985055641800, 0.15451287602753], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - random.bit_generator.seed(self.seed) - actual = random.noncentral_chisquare(df=5, nonc=0, size=(3, 2)) - desired = np.array( - [ - [3.64881368071039, 5.48224544747803], - [20.41999842025404, 3.44075915187367], - [1.29765160605552, 1.64125033268606], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_noncentral_f(self): - random.bit_generator.seed(self.seed) - actual = random.noncentral_f(dfnum=5, dfden=2, nonc=1, size=(3, 2)) - desired = np.array( - [ - [1.22680230963236, 2.56457837623956], - [2.7653304499494, 7.4336268865443], - [1.16362730891403, 2.54104276581491], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_noncentral_f_nan(self): - random.bit_generator.seed(self.seed) - actual = random.noncentral_f(dfnum=5, dfden=2, nonc=np.nan) - assert np.isnan(actual) - - def test_normal(self): - random.bit_generator.seed(self.seed) - actual = random.normal(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [-6.822051212221923, -0.094420339458285], - [-0.368474717792823, -1.284746311523402], - [0.843661763232711, 0.379120992544734], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_normal_0(self): - assert_equal(random.normal(scale=0), 0) - assert_raises(ValueError, random.normal, scale=-0.0) - - def test_pareto(self): - random.bit_generator.seed(self.seed) - actual = random.pareto(a=0.123456789, size=(3, 2)) - desired = np.array( - [ - [5.6883528121891552e16, 4.0569373841667057e03], - [1.2854967019379475e12, 6.5833156486851483e04], - [1.1281132447159091e01, 3.1895968171107006e08], - ] - ) - # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this - # matrix differs by 24 nulps. Discussion: - # https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html - # Consensus is that this is probably some gcc quirk that affects - # rounding but not in any important way, so we just use a looser - # tolerance on this test: - np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30) - - def test_poisson(self): - random.bit_generator.seed(self.seed) - actual = random.poisson(lam=0.123456789, size=(3, 2)) - desired = np.array([[0, 0], [1, 0], [0, 0]]) - assert_array_equal(actual, desired) - - def test_poisson_exceptions(self): - lambig = np.iinfo("int64").max - lamneg = -1 - assert_raises(ValueError, random.poisson, lamneg) - assert_raises(ValueError, random.poisson, [lamneg] * 10) - assert_raises(ValueError, random.poisson, lambig) - assert_raises(ValueError, random.poisson, [lambig] * 10) - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.poisson, np.nan) - assert_raises(ValueError, random.poisson, [np.nan] * 10) - - def test_power(self): - random.bit_generator.seed(self.seed) - actual = random.power(a=0.123456789, size=(3, 2)) - desired = np.array( - [ - [9.328833342693975e-01, 2.742250409261003e-02], - [7.684513237993961e-01, 9.297548209160028e-02], - [2.214811188828573e-05, 4.693448360603472e-01], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_rayleigh(self): - random.bit_generator.seed(self.seed) - actual = random.rayleigh(scale=10, size=(3, 2)) - desired = np.array( - [ - [13.8882496494248393, 13.383318339044731], - [20.95413364294492098, 21.08285015800712614], - [11.06066537006854311, 17.35468505778271009], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_rayleigh_0(self): - assert_equal(random.rayleigh(scale=0), 0) - assert_raises(ValueError, random.rayleigh, scale=-0.0) - - def test_standard_cauchy(self): - random.bit_generator.seed(self.seed) - actual = random.standard_cauchy(size=(3, 2)) - desired = np.array( - [ - [31.87809592667601, 0.349332782046838], - [2.816995747731641, 10.552372563459114], - [2.485608017991235, 7.843211273201831], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_exponential(self): - random.bit_generator.seed(self.seed) - actual = random.standard_exponential(size=(3, 2), method="inv") - desired = np.array( - [ - [0.96441739162374596, 0.89556604882105506], - [2.1953785836319808, 2.22243285392490542], - [0.6116915921431676, 1.50592546727413201], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_expoential_type_error(self): - assert_raises(TypeError, random.standard_exponential, dtype=np.int32) - - def test_standard_gamma(self): - random.bit_generator.seed(self.seed) - actual = random.standard_gamma(shape=3, size=(3, 2)) - desired = np.array( - [ - [2.28483515569645, 3.29899524967824], - [11.12492298902645, 2.16784417297277], - [0.92121813690910, 1.12853552328470], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_standard_gammma_scalar_float(self): - random.bit_generator.seed(self.seed) - actual = random.standard_gamma(3, dtype=np.float32) - desired = 1.3877466 - assert_array_almost_equal(actual, desired, decimal=6) - - def test_standard_gamma_float(self): - random.bit_generator.seed(self.seed) - actual = random.standard_gamma(shape=3, size=(3, 2)) - desired = np.array( - [[2.2848352, 3.2989952], [11.124923, 2.1678442], [0.9212181, 1.1285355]] - ) - assert_array_almost_equal(actual, desired, decimal=5) - - def test_standard_gammma_float_out(self): - actual = np.zeros((3, 2), dtype=np.float32) - random.bit_generator.seed(self.seed) - random.standard_gamma(10.0, out=actual, dtype=np.float32) - desired = np.array( - [[6.9824033, 7.3731737], [14.860578, 7.5327270], [11.767487, 6.2320185]], - dtype=np.float32, - ) - assert_array_almost_equal(actual, desired, decimal=5) - - random.bit_generator.seed(self.seed) - random.standard_gamma(10.0, out=actual, size=(3, 2), dtype=np.float32) - assert_array_almost_equal(actual, desired, decimal=5) - - def test_standard_gamma_unknown_type(self): - assert_raises(TypeError, random.standard_gamma, 1.0, dtype="int32") - - def test_out_size_mismatch(self): - out = np.zeros(10) - assert_raises(ValueError, random.standard_gamma, 10.0, size=20, out=out) - assert_raises(ValueError, random.standard_gamma, 10.0, size=(10, 1), out=out) - - def test_standard_gamma_0(self): - assert_equal(random.standard_gamma(shape=0), 0) - assert_raises(ValueError, random.standard_gamma, shape=-0.0) - - def test_standard_normal(self): - random.bit_generator.seed(self.seed) - actual = random.standard_normal(size=(3, 2)) - desired = np.array( - [ - [-3.472754000610961, -0.108938564229143], - [-0.245965753396411, -0.704101550261701], - [0.360102487116356, 0.127832101772367], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_normal_unsupported_type(self): - assert_raises(TypeError, random.standard_normal, dtype=np.int32) - - def test_standard_t(self): - random.bit_generator.seed(self.seed) - actual = random.standard_t(df=10, size=(3, 2)) - desired = np.array( - [ - [-3.68722108185508, -0.672031186266171], - [2.900224996448669, -0.199656996187739], - [-1.12179956985969, 1.85668262342106], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_triangular(self): - random.bit_generator.seed(self.seed) - actual = random.triangular(left=5.12, mode=10.23, right=20.34, size=(3, 2)) - desired = np.array( - [ - [12.68117178949215784, 12.4129206149193152], - [16.20131377335158263, 16.25692138747600524], - [11.20400690911820263, 14.4978144835829923], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_uniform(self): - random.bit_generator.seed(self.seed) - actual = random.uniform(low=1.23, high=10.54, size=(3, 2)) - desired = np.array( - [ - [6.99097932346268003, 6.73801597444323974], - [9.50364421400426274, 9.53130618907631089], - [5.48995325769805476, 8.47493103280052118], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_uniform_range_bounds(self): - fmin = np.finfo("float").min - fmax = np.finfo("float").max - - func = random.uniform - assert_raises(OverflowError, func, -np.inf, 0) - assert_raises(OverflowError, func, 0, np.inf) - assert_raises(OverflowError, func, fmin, fmax) - assert_raises(OverflowError, func, [-np.inf], [0]) - assert_raises(OverflowError, func, [0], [np.inf]) - - # (fmax / 1e17) - fmin is within range, so this should not throw - # account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX > - # DBL_MAX by increasing fmin a bit - random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17) - - def test_uniform_neg_range(self): - func = random.uniform - assert_raises(ValueError, func, 2, 1) - assert_raises(ValueError, func, [1, 2], [1, 1]) - assert_raises(ValueError, func, [[0, 1], [2, 3]], 2) - - def test_scalar_exception_propagation(self): - # Tests that exceptions are correctly propagated in distributions - # when called with objects that throw exceptions when converted to - # scalars. - # - # Regression test for gh: 8865 - - class ThrowingFloat(np.ndarray): - def __float__(self): - raise TypeError - - throwing_float = np.array(1.0).view(ThrowingFloat) - assert_raises(TypeError, random.uniform, throwing_float, throwing_float) - - class ThrowingInteger(np.ndarray): - def __int__(self): - raise TypeError - - throwing_int = np.array(1).view(ThrowingInteger) - assert_raises(TypeError, random.hypergeometric, throwing_int, 1, 1) - - def test_vonmises(self): - random.bit_generator.seed(self.seed) - actual = random.vonmises(mu=1.23, kappa=1.54, size=(3, 2)) - desired = np.array( - [ - [2.28567572673902042, 2.89163838442285037], - [0.38198375564286025, 2.57638023113890746], - [1.19153771588353052, 1.83509849681825354], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_vonmises_small(self): - # check infinite loop, gh-4720 - random.bit_generator.seed(self.seed) - r = random.vonmises(mu=0.0, kappa=1.1e-8, size=10**6) - assert_(np.isfinite(r).all()) - - def test_vonmises_nan(self): - random.bit_generator.seed(self.seed) - r = random.vonmises(mu=0.0, kappa=np.nan) - assert_(np.isnan(r)) - - def test_wald(self): - random.bit_generator.seed(self.seed) - actual = random.wald(mean=1.23, scale=1.54, size=(3, 2)) - desired = np.array( - [ - [0.10653278160339, 0.98771068102461], - [0.89276055317879, 0.13640126419923], - [0.9194319091599, 0.36037816317472], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_weibull(self): - random.bit_generator.seed(self.seed) - actual = random.weibull(a=1.23, size=(3, 2)) - desired = np.array( - [ - [3.557276979846361, 1.020870580998542], - [2.731847777612348, 1.29148068905082], - [0.385531483942839, 2.049551716717254], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_weibull_0(self): - random.bit_generator.seed(self.seed) - assert_equal(random.weibull(a=0, size=12), np.zeros(12)) - assert_raises(ValueError, random.weibull, a=-0.0) - - def test_zipf(self): - random.bit_generator.seed(self.seed) - actual = random.zipf(a=1.23, size=(3, 2)) - desired = np.array([[66, 29], [1, 1], [3, 13]]) - assert_array_equal(actual, desired) - - def test_complex_normal(self): - random.bit_generator.seed(self.seed) - actual = random.complex_normal(loc=1.0, gamma=1.0, relation=0.5, size=(3, 2)) - desired = np.array( - [ - [ - -2.007493185623132 - 0.05446928211457126j, - 0.7869874090977291 - 0.35205077513085050j, - ], - [ - 1.3118579018087224 + 0.06391605088618339j, - 3.5872278793967554 + 0.14155458439717636j, - ], - [ - 0.7170022862582056 - 0.06573393915140235j, - -0.26571837106621987 - 0.0931713830979103j, - ], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.bit_generator.seed(self.seed) - actual = random.complex_normal(loc=0, gamma=1.0, relation=0.5, size=3) - assert_array_almost_equal(actual, desired.flat[:3] - 1.0, decimal=15) - - random.bit_generator.seed(self.seed) - actual = random.complex_normal(loc=2.0, gamma=1.0, relation=0.5) - assert_array_almost_equal(actual, 1.0 + desired[0, 0], decimal=15) - - def test_complex_normal_invalid(self): - assert_raises(ValueError, random.complex_normal, gamma=1 + 0.5j) - assert_raises(ValueError, random.complex_normal, relation=2) - assert_raises(ValueError, random.complex_normal, relation=-3) - assert_raises(ValueError, random.complex_normal, relation=10j) - - assert_raises(ValueError, random.complex_normal, gamma=[1 + 0.5j]) - assert_raises(ValueError, random.complex_normal, relation=[2]) - assert_raises(ValueError, random.complex_normal, relation=[-3]) - assert_raises(ValueError, random.complex_normal, relation=[10j]) - - -class TestBroadcast(object): - # tests that functions that broadcast behave - # correctly when presented with non-scalar arguments - def setup(self): - self.seed = 123456789 - - def set_seed(self): - random.bit_generator.seed(self.seed) - - def test_uniform(self): - low = [0] - high = [1] - uniform = random.uniform - desired = np.array( - [0.53283302478975902, 0.53413660089041659, 0.50955303552646702] - ) - - self.set_seed() - actual = uniform(low * 3, high) - assert_array_almost_equal(actual, desired, decimal=14) - - self.set_seed() - actual = uniform(low, high * 3) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_normal(self): - loc = [0] - scale = [1] - bad_scale = [-1] - normal = random.normal - desired = np.array([0.454879818179180, -0.62749179463661, -0.06063266769872]) - - self.set_seed() - actual = normal(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, normal, loc * 3, bad_scale) - assert_raises(ValueError, random.normal, loc * 3, bad_scale) - - self.set_seed() - actual = normal(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, normal, loc, bad_scale * 3) - assert_raises(ValueError, random.normal, loc, bad_scale * 3) - - def test_beta(self): - a = [1] - b = [2] - bad_a = [-1] - bad_b = [-2] - beta = random.beta - desired = np.array([0.63222080311226, 0.33310522220774, 0.64494078460190]) - - self.set_seed() - actual = beta(a * 3, b) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, beta, bad_a * 3, b) - assert_raises(ValueError, beta, a * 3, bad_b) - - self.set_seed() - actual = beta(a, b * 3) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_exponential(self): - scale = [1] - bad_scale = [-1] - exponential = random.exponential - desired = np.array([1.68591211640990, 3.14186859487914, 0.67717375919228]) - - self.set_seed() - actual = exponential(scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, exponential, bad_scale * 3) - - def test_standard_gamma(self): - shape = [1] - bad_shape = [-1] - std_gamma = random.standard_gamma - desired = np.array([1.68591211640990, 3.14186859487914, 0.67717375919228]) - - self.set_seed() - actual = std_gamma(shape * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, std_gamma, bad_shape * 3) - - def test_gamma(self): - shape = [1] - scale = [2] - bad_shape = [-1] - bad_scale = [-2] - gamma = random.gamma - desired = np.array([3.37182423281980, 6.28373718975827, 1.35434751838456]) - - self.set_seed() - actual = gamma(shape * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gamma, bad_shape * 3, scale) - assert_raises(ValueError, gamma, shape * 3, bad_scale) - - self.set_seed() - actual = gamma(shape, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gamma, bad_shape, scale * 3) - assert_raises(ValueError, gamma, shape, bad_scale * 3) - - def test_f(self): - dfnum = [1] - dfden = [2] - bad_dfnum = [-1] - bad_dfden = [-2] - f = random.f - desired = np.array([0.84207044881810, 3.08607209903483, 3.12823105933169]) - - self.set_seed() - actual = f(dfnum * 3, dfden) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, f, bad_dfnum * 3, dfden) - assert_raises(ValueError, f, dfnum * 3, bad_dfden) - - self.set_seed() - actual = f(dfnum, dfden * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, f, bad_dfnum, dfden * 3) - assert_raises(ValueError, f, dfnum, bad_dfden * 3) - - def test_noncentral_f(self): - dfnum = [2] - dfden = [3] - nonc = [4] - bad_dfnum = [0] - bad_dfden = [-1] - bad_nonc = [-2] - nonc_f = random.noncentral_f - desired = np.array([3.83710578542563, 8.74926819712029, 0.48892943835401]) - - self.set_seed() - actual = nonc_f(dfnum * 3, dfden, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert np.all(np.isnan(nonc_f(dfnum, dfden, [np.nan] * 3))) - - assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc) - assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc) - assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc) - - self.set_seed() - actual = nonc_f(dfnum, dfden * 3, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc) - assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc) - assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc) - - self.set_seed() - actual = nonc_f(dfnum, dfden, nonc * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3) - assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3) - assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3) - - def test_noncentral_f_small_df(self): - self.set_seed() - desired = np.array([21.57878070681719, 1.17110217503908]) - actual = random.noncentral_f(0.9, 0.9, 2, size=2) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_chisquare(self): - df = [1] - bad_df = [-1] - chisquare = random.chisquare - desired = np.array( - [0.57022801133088286, 0.51947702108840776, 0.1320969254923558] - ) - - self.set_seed() - actual = chisquare(df * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, chisquare, bad_df * 3) - - def test_noncentral_chisquare(self): - df = [1] - nonc = [2] - bad_df = [-1] - bad_nonc = [-2] - nonc_chi = random.noncentral_chisquare - desired = np.array([2.20478739452297, 1.45177405755115, 1.00418921695354]) - - self.set_seed() - actual = nonc_chi(df * 3, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_chi, bad_df * 3, nonc) - assert_raises(ValueError, nonc_chi, df * 3, bad_nonc) - - self.set_seed() - actual = nonc_chi(df, nonc * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_chi, bad_df, nonc * 3) - assert_raises(ValueError, nonc_chi, df, bad_nonc * 3) - - def test_standard_t(self): - df = [1] - bad_df = [-1] - t = random.standard_t - desired = np.array([0.60081050724244, -0.90380889829210, -0.64499590504117]) - - self.set_seed() - actual = t(df * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, t, bad_df * 3) - assert_raises(ValueError, random.standard_t, bad_df * 3) - - def test_vonmises(self): - mu = [2] - kappa = [1] - bad_kappa = [-1] - vonmises = random.vonmises - desired = np.array( - [2.9883443664201312, -2.7064099483995943, -1.8672476700665914] - ) - - self.set_seed() - actual = vonmises(mu * 3, kappa) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, vonmises, mu * 3, bad_kappa) - - self.set_seed() - actual = vonmises(mu, kappa * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, vonmises, mu, bad_kappa * 3) - - def test_pareto(self): - a = [1] - bad_a = [-1] - pareto = random.pareto - desired = np.array([4.397371719158540, 22.14707898642946, 0.968306954322200]) - - self.set_seed() - actual = pareto(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, pareto, bad_a * 3) - assert_raises(ValueError, random.pareto, bad_a * 3) - - def test_weibull(self): - a = [1] - bad_a = [-1] - weibull = random.weibull - desired = np.array([1.68591211640990, 3.14186859487914, 0.67717375919228]) - - self.set_seed() - actual = weibull(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, weibull, bad_a * 3) - assert_raises(ValueError, random.weibull, bad_a * 3) - - def test_power(self): - a = [1] - bad_a = [-1] - power = random.power - desired = np.array([0.81472463783615, 0.95679800459547, 0.49194916077287]) - - self.set_seed() - actual = power(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, power, bad_a * 3) - assert_raises(ValueError, random.power, bad_a * 3) - - def test_laplace(self): - loc = [0] - scale = [1] - bad_scale = [-1] - laplace = random.laplace - desired = np.array( - [0.067921356028507157, 0.070715642226971326, 0.019290950698972624] - ) - - self.set_seed() - actual = laplace(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, laplace, loc * 3, bad_scale) - - self.set_seed() - actual = laplace(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, laplace, loc, bad_scale * 3) - - def test_gumbel(self): - loc = [0] - scale = [1] - bad_scale = [-1] - gumbel = random.gumbel - desired = np.array( - [0.2730318639556768, 0.26936705726291116, 0.33906220393037939] - ) - - self.set_seed() - actual = gumbel(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gumbel, loc * 3, bad_scale) - - self.set_seed() - actual = gumbel(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gumbel, loc, bad_scale * 3) - - def test_logistic(self): - loc = [0] - scale = [1] - bad_scale = [-1] - logistic = random.logistic - desired = np.array( - [0.13152135837586171, 0.13675915696285773, 0.038216792802833396] - ) - - self.set_seed() - actual = logistic(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, logistic, loc * 3, bad_scale) - - self.set_seed() - actual = logistic(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, logistic, loc, bad_scale * 3) - assert_equal(random.logistic(1.0, 0.0), 1.0) - - def test_lognormal(self): - mean = [0] - sigma = [1] - bad_sigma = [-1] - lognormal = random.lognormal - desired = np.array([1.57598396702930, 0.53392932731280, 0.94116889802361]) - - self.set_seed() - actual = lognormal(mean * 3, sigma) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, lognormal, mean * 3, bad_sigma) - assert_raises(ValueError, random.lognormal, mean * 3, bad_sigma) - - self.set_seed() - actual = lognormal(mean, sigma * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, lognormal, mean, bad_sigma * 3) - assert_raises(ValueError, random.lognormal, mean, bad_sigma * 3) - - def test_rayleigh(self): - scale = [1] - bad_scale = [-1] - rayleigh = random.rayleigh - desired = np.array([1.2337491937897689, 1.2360119924878694, 1.1936818095781789]) - - self.set_seed() - actual = rayleigh(scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, rayleigh, bad_scale * 3) - - def test_wald(self): - mean = [0.5] - scale = [1] - bad_mean = [0] - bad_scale = [-2] - wald = random.wald - desired = np.array([0.36297361471752, 0.52190135028254, 0.55111022040727]) - - self.set_seed() - actual = wald(mean * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, wald, bad_mean * 3, scale) - assert_raises(ValueError, wald, mean * 3, bad_scale) - assert_raises(ValueError, random.wald, bad_mean * 3, scale) - assert_raises(ValueError, random.wald, mean * 3, bad_scale) - - self.set_seed() - actual = wald(mean, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, wald, bad_mean, scale * 3) - assert_raises(ValueError, wald, mean, bad_scale * 3) - assert_raises(ValueError, random.wald, bad_mean, scale * 3) - assert_raises(ValueError, random.wald, mean, bad_scale * 3) - - def test_triangular(self): - left = [1] - right = [3] - mode = [2] - bad_left_one = [3] - bad_mode_one = [4] - bad_left_two, bad_mode_two = right * 2 - triangular = random.triangular - desired = np.array([2.03339048710429, 2.0347400359389356, 2.0095991069536208]) - - self.set_seed() - actual = triangular(left * 3, mode, right) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one * 3, mode, right) - assert_raises(ValueError, triangular, left * 3, bad_mode_one, right) - assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two, right) - - self.set_seed() - actual = triangular(left, mode * 3, right) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one, mode * 3, right) - assert_raises(ValueError, triangular, left, bad_mode_one * 3, right) - assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3, right) - - self.set_seed() - actual = triangular(left, mode, right * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one, mode, right * 3) - assert_raises(ValueError, triangular, left, bad_mode_one, right * 3) - assert_raises(ValueError, triangular, bad_left_two, bad_mode_two, right * 3) - - assert_raises(ValueError, triangular, 10.0, 0.0, 20.0) - assert_raises(ValueError, triangular, 10.0, 25.0, 20.0) - assert_raises(ValueError, triangular, 10.0, 10.0, 10.0) - - def test_binomial(self): - n = [1] - p = [0.5] - bad_n = [-1] - bad_p_one = [-1] - bad_p_two = [1.5] - binom = random.binomial - desired = np.array([1, 1, 1]) - - self.set_seed() - actual = binom(n * 3, p) - assert_array_equal(actual, desired) - self.set_seed() - actual = binom(n * 3, p, size=(3,)) - assert_array_equal(actual, desired) - assert_raises(ValueError, binom, bad_n * 3, p) - assert_raises(ValueError, binom, n * 3, bad_p_one) - assert_raises(ValueError, binom, n * 3, bad_p_two) - - self.set_seed() - actual = binom(n, p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, binom, bad_n, p * 3) - assert_raises(ValueError, binom, n, bad_p_one * 3) - assert_raises(ValueError, binom, n, bad_p_two * 3) - - def test_negative_binomial(self): - n = [1] - p = [0.5] - bad_n = [-1] - bad_p_one = [-1] - bad_p_two = [1.5] - neg_binom = random.negative_binomial - desired = np.array([3, 1, 2], dtype=np.int64) - - self.set_seed() - actual = neg_binom(n * 3, p) - assert_array_equal(actual, desired) - assert_raises(ValueError, neg_binom, bad_n * 3, p) - assert_raises(ValueError, neg_binom, n * 3, bad_p_one) - assert_raises(ValueError, neg_binom, n * 3, bad_p_two) - - self.set_seed() - actual = neg_binom(n, p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, neg_binom, bad_n, p * 3) - assert_raises(ValueError, neg_binom, n, bad_p_one * 3) - assert_raises(ValueError, neg_binom, n, bad_p_two * 3) - - def test_poisson(self): - max_lam = random._poisson_lam_max - - lam = [1] - bad_lam_one = [-1] - bad_lam_two = [max_lam * 2] - poisson = random.poisson - desired = np.array([1, 1, 0]) - - self.set_seed() - actual = poisson(lam * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, poisson, bad_lam_one * 3) - assert_raises(ValueError, poisson, bad_lam_two * 3) - - def test_zipf(self): - a = [2] - bad_a = [0] - zipf = random.zipf - desired = np.array([2, 2, 1]) - - self.set_seed() - actual = zipf(a * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, zipf, bad_a * 3) - with np.errstate(invalid="ignore"): - assert_raises(ValueError, zipf, np.nan) - assert_raises(ValueError, zipf, [0, 0, np.nan]) - - def test_geometric(self): - p = [0.5] - bad_p_one = [-1] - bad_p_two = [1.5] - geom = random.geometric - desired = np.array([2, 2, 2]) - - self.set_seed() - actual = geom(p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, geom, bad_p_one * 3) - assert_raises(ValueError, geom, bad_p_two * 3) - - def test_hypergeometric(self): - ngood = [1] - nbad = [2] - nsample = [2] - bad_ngood = [-1] - bad_nbad = [-2] - bad_nsample_one = [-1] - bad_nsample_two = [4] - desired = np.array([0, 0, 1]) - - random = Generator(MT19937(self.seed, mode="legacy")) - actual = random.hypergeometric(ngood * 3, nbad, nsample) - assert_array_equal(actual, desired) - assert_raises(ValueError, random.hypergeometric, bad_ngood * 3, nbad, nsample) - assert_raises(ValueError, random.hypergeometric, ngood * 3, bad_nbad, nsample) - assert_raises( - ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_one - ) - assert_raises( - ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_two - ) - - random = Generator(MT19937(self.seed, mode="legacy")) - actual = random.hypergeometric(ngood, nbad * 3, nsample) - assert_array_equal(actual, desired) - assert_raises(ValueError, random.hypergeometric, bad_ngood, nbad * 3, nsample) - assert_raises(ValueError, random.hypergeometric, ngood, bad_nbad * 3, nsample) - assert_raises( - ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_one - ) - assert_raises( - ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_two - ) - - random = Generator(MT19937(self.seed, mode="legacy")) - hypergeom = random.hypergeometric - actual = hypergeom(ngood, nbad, nsample * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3) - assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3) - assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3) - assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3) - - assert_raises(ValueError, hypergeom, -1, 10, 20) - assert_raises(ValueError, hypergeom, 10, -1, 20) - assert_raises(ValueError, hypergeom, 10, 10, -1) - assert_raises(ValueError, hypergeom, 10, 10, 25) - - # ValueError for arguments that are too big. - assert_raises(ValueError, hypergeom, 2**30, 10, 20) - assert_raises(ValueError, hypergeom, 999, 2**31, 50) - assert_raises(ValueError, hypergeom, 999, [2**29, 2**30], 1000) - - def test_logseries(self): - p = [0.5] - bad_p_one = [2] - bad_p_two = [-1] - logseries = random.logseries - desired = np.array([1, 1, 1]) - - self.set_seed() - actual = logseries(p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, logseries, bad_p_one * 3) - assert_raises(ValueError, logseries, bad_p_two * 3) - - def test_complex_normal(self): - random.bit_generator.seed(self.seed) - loc = np.ones((1, 2)) - gamma = np.ones((3, 1)) - relation = 0.5 * np.ones((3, 2)) - actual = random.complex_normal(loc=loc, gamma=gamma, relation=relation) - desired = np.array( - [ - [ - 1.393937478212015 - 0.31374589731830593j, - 0.9474905694736895 - 0.16424530802218726j, - ], - [ - 1.119247463119766 + 0.023956373851168843j, - 0.8776366291514774 + 0.2865220655803411j, - ], - [ - 0.5515508326417458 - 0.15986016780453596j, - -0.6803993941303332 + 1.1782711493556892j, - ], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.bit_generator.seed(self.seed) - actual = random.complex_normal(loc=loc, gamma=1.0, relation=0.5, size=(3, 2)) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_multinomial(self): - random.bit_generator.seed(self.seed) - actual = random.multinomial([5, 20], [1 / 6.0] * 6, size=(3, 2)) - desired = np.array( - [ - [[1, 1, 1, 1, 0, 1], [4, 5, 1, 4, 3, 3]], - [[1, 1, 1, 0, 0, 2], [2, 0, 4, 3, 7, 4]], - [[1, 2, 0, 0, 2, 0], [3, 2, 3, 4, 2, 6]], - ], - dtype=np.int64, - ) - assert_array_equal(actual, desired) - - random.bit_generator.seed(self.seed) - actual = random.multinomial([5, 20], [1 / 6.0] * 6) - desired = np.array([[1, 1, 1, 1, 0, 1], [4, 5, 1, 4, 3, 3]], dtype=np.int64) - assert_array_equal(actual, desired) - - -class TestThread(object): - # make sure each state produces the same sequence even in threads - def setup(self): - self.seeds = range(4) - - def check_function(self, function, sz): - from threading import Thread - - out1 = np.empty((len(self.seeds),) + sz) - out2 = np.empty((len(self.seeds),) + sz) - - # threaded generation - t = [ - Thread(target=function, args=(Generator(MT19937(s, mode="legacy")), o)) - for s, o in zip(self.seeds, out1) - ] - [x.start() for x in t] - [x.join() for x in t] - - # the same serial - for s, o in zip(self.seeds, out2): - function(Generator(MT19937(s, mode="legacy")), o) - - # these platforms change x87 fpu precision mode in threads - if np.intp().dtype.itemsize == 4 and sys.platform == "win32": - assert_array_almost_equal(out1, out2) - else: - assert_array_equal(out1, out2) - - def test_normal(self): - def gen_random(state, out): - out[...] = state.normal(size=10000) - - self.check_function(gen_random, sz=(10000,)) - - def test_exp(self): - def gen_random(state, out): - out[...] = state.exponential(scale=np.ones((100, 1000))) - - self.check_function(gen_random, sz=(100, 1000)) - - def test_multinomial(self): - def gen_random(state, out): - out[...] = state.multinomial(10, [1 / 6.0] * 6, size=10000) - - self.check_function(gen_random, sz=(10000, 6)) - - -# See Issue #4263 -class TestSingleEltArrayInput(object): - def setup(self): - self.argOne = np.array([2]) - self.argTwo = np.array([3]) - self.argThree = np.array([4]) - self.tgtShape = (1,) - - def test_one_arg_funcs(self): - funcs = ( - random.exponential, - random.standard_gamma, - random.chisquare, - random.standard_t, - random.pareto, - random.weibull, - random.power, - random.rayleigh, - random.poisson, - random.zipf, - random.geometric, - random.logseries, - ) - - probfuncs = (random.geometric, random.logseries) - - for func in funcs: - if func in probfuncs: # p < 1.0 - out = func(np.array([0.5])) - - else: - out = func(self.argOne) - - assert_equal(out.shape, self.tgtShape) - - def test_two_arg_funcs(self): - funcs = ( - random.uniform, - random.normal, - random.beta, - random.gamma, - random.f, - random.noncentral_chisquare, - random.vonmises, - random.laplace, - random.gumbel, - random.logistic, - random.lognormal, - random.wald, - random.binomial, - random.negative_binomial, - ) - - probfuncs = (random.binomial, random.negative_binomial) - - for func in funcs: - if func in probfuncs: # p <= 1 - argTwo = np.array([0.5]) - - else: - argTwo = self.argTwo - - out = func(self.argOne, argTwo) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne[0], argTwo) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne, argTwo[0]) - assert_equal(out.shape, self.tgtShape) - - def test_integers(self, endpoint): - itype = [ - bool, - np.int8, - np.uint8, - np.int16, - np.uint16, - np.int32, - np.uint32, - np.int64, - np.uint64, - ] - func = random.integers - high = np.array([1]) - low = np.array([0]) - - for dt in itype: - out = func(low, high, endpoint=endpoint, dtype=dt) - assert_equal(out.shape, self.tgtShape) - - out = func(low[0], high, endpoint=endpoint, dtype=dt) - assert_equal(out.shape, self.tgtShape) - - out = func(low, high[0], endpoint=endpoint, dtype=dt) - assert_equal(out.shape, self.tgtShape) - - def test_three_arg_funcs(self): - funcs = [random.noncentral_f, random.triangular, random.hypergeometric] - - for func in funcs: - out = func(self.argOne, self.argTwo, self.argThree) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne[0], self.argTwo, self.argThree) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne, self.argTwo[0], self.argThree) - assert_equal(out.shape, self.tgtShape) - - -def test_seed_equivalence(): - random.seed(0) - state = random.state - random.seed(1) - random.bit_generator.seed(0) - bit_generator_state = random.bit_generator.state - assert_state_equal(state, bit_generator_state) - random.seed(1) - random.state = state - assert_state_equal(state, random.state) - - -def test_get_state(): - state = random.state - get_state = random.__getstate__() - assert state["state"]["pos"] == get_state["state"]["pos"] - assert np.all(state["state"]["key"] == get_state["state"]["key"]) - @pytest.mark.skipif(NP_LT_118, reason="Can only test with NumPy >= 1.18") @pytest.mark.parametrize("config", list(JUMP_TEST_DATA.keys())) @@ -2797,54 +79,3 @@ def test_jumped(config): md5 = hashlib.md5(key) assert md5.hexdigest() == values["jumped"]["key_md5"] assert jumped.state["state"]["pos"] == values["jumped"]["pos"] - - -def test_broadcast_size_error(): - mu = np.ones(3) - sigma = np.ones((4, 3)) - size = (10, 4, 2) - assert random.normal(mu, sigma, size=(5, 4, 3)).shape == (5, 4, 3) - with pytest.raises(ValueError): - random.normal(mu, sigma, size=size) - with pytest.raises(ValueError): - random.normal(mu, sigma, size=(1, 3)) - with pytest.raises(ValueError): - random.normal(mu, sigma, size=(4, 1, 1)) - # 1 arg - shape = np.ones((4, 3)) - with pytest.raises(ValueError): - random.standard_gamma(shape, size=size) - with pytest.raises(ValueError): - random.standard_gamma(shape, size=(3,)) - with pytest.raises(ValueError): - random.standard_gamma(shape, size=3) - # Check out - out = np.empty(size) - with pytest.raises(ValueError): - random.standard_gamma(shape, out=out) - - # 2 arg - with pytest.raises(ValueError): - random.binomial(1, [0.3, 0.7], size=(2, 1)) - with pytest.raises(ValueError): - random.binomial([1, 2], 0.3, size=(2, 1)) - with pytest.raises(ValueError): - random.binomial([1, 2], [0.3, 0.7], size=(2, 1)) - with pytest.raises(ValueError): - random.multinomial([2, 2], [0.3, 0.7], size=(2, 1)) - - # 3 arg - a = random.chisquare(5, size=3) - b = random.chisquare(5, size=(4, 3)) - c = random.chisquare(5, size=(5, 4, 3)) - assert random.noncentral_f(a, b, c).shape == (5, 4, 3) - with pytest.raises(ValueError, match=r"Output size \(6, 5, 1, 1\) is"): - random.noncentral_f(a, b, c, size=(6, 5, 1, 1)) - - -def test_broadcast_size_scalar(): - mu = np.ones(3) - sigma = np.ones(3) - random.normal(mu, sigma, size=3) - with pytest.raises(ValueError): - random.normal(mu, sigma, size=2) diff --git a/randomgen/tests/test_generator_mt19937_regressions.py b/randomgen/tests/test_generator_mt19937_regressions.py deleted file mode 100644 index f2627a618..000000000 --- a/randomgen/tests/test_generator_mt19937_regressions.py +++ /dev/null @@ -1,156 +0,0 @@ -import numpy as np -from numpy.testing import assert_, assert_array_equal -import pytest - -from randomgen import MT19937, Generator - -mt19937 = Generator(MT19937(mode="legacy")) - - -class TestRegression(object): - def test_VonMises_range(self): - # Make sure generated random variables are in [-pi, pi]. - # Regression test for ticket #986. - for mu in np.linspace(-7.0, 7.0, 5): - r = mt19937.vonmises(mu, 1, 50) - assert_(np.all(r > -np.pi) and np.all(r <= np.pi)) - - def test_hypergeometric_range(self): - # Test for ticket #921 - assert_(np.all(mt19937.hypergeometric(3, 18, 11, size=10) < 4)) - assert_(np.all(mt19937.hypergeometric(18, 3, 11, size=10) > 0)) - - # Test for ticket #5623 - args = (2**20 - 2, 2**20 - 2, 2**20 - 2) # Check for 32-bit systems - assert_(mt19937.hypergeometric(*args) > 0) - - def test_logseries_convergence(self): - # Test for ticket #923 - N = 1000 - mt19937.bit_generator.seed(0) - rvsn = mt19937.logseries(0.8, size=N) - # these two frequency counts should be close to theoretical - # numbers with this large sample - # theoretical large N result is 0.49706795 - freq = np.sum(rvsn == 1) / float(N) - msg = "Frequency was %f, should be > 0.45" % freq - assert_(freq > 0.45, msg) - # theoretical large N result is 0.19882718 - freq = np.sum(rvsn == 2) / float(N) - msg = "Frequency was %f, should be < 0.23" % freq - assert_(freq < 0.23, msg) - - def test_shuffle_mixed_dimension(self): - # Test for trac ticket #2074 - for t in [ - [1, 2, 3, None], - [(1, 1), (2, 2), (3, 3), None], - [1, (2, 2), (3, 3), None], - [(1, 1), 2, 3, None], - ]: - mt19937.bit_generator.seed(12345) - shuffled = np.array(list(t), dtype=object) - mt19937.shuffle(shuffled) - assert_array_equal( - shuffled, np.array([t[0], t[3], t[1], t[2]], dtype=object) - ) - - def test_call_within_randomstate(self): - # Check that custom RandomState does not call into global state - m = Generator(MT19937(mode="legacy")) # mt19937.RandomState() - res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3]) - for i in range(3): - mt19937.bit_generator.seed(i) - m.bit_generator.seed(4321) - # If m.state is not honored, the result will change - assert_array_equal(m.choice(10, size=10, p=np.ones(10) / 10.0), res) - - def test_multivariate_normal_size_types(self): - # Test for multivariate_normal issue with "size" argument. - # Check that the multivariate_normal size argument can be a - # numpy integer. - mt19937.multivariate_normal([0], [[0]], size=1) - mt19937.multivariate_normal([0], [[0]], size=np.int_(1)) - mt19937.multivariate_normal([0], [[0]], size=np.int64(1)) - - def test_beta_small_parameters(self): - # Test that beta with small a and b parameters does not produce - # NaNs due to roundoff errors causing 0 / 0, gh-5851 - mt19937.bit_generator.seed(1234567890) - x = mt19937.beta(0.0001, 0.0001, size=100) - assert_(not np.any(np.isnan(x)), "Nans in mt19937.beta") - - def test_choice_sum_of_probs_tolerance(self): - # The sum of probs should be 1.0 with some tolerance. - # For low precision dtypes the tolerance was too tight. - # See numpy github issue 6123. - mt19937.bit_generator.seed(1234) - a = [1, 2, 3] - counts = [4, 4, 2] - for dt in np.float16, np.float32, np.float64: - probs = np.array(counts, dtype=dt) / sum(counts) - c = mt19937.choice(a, p=probs) - assert_(c in a) - with pytest.raises(ValueError): - mt19937.choice(a, p=probs * 0.9) - - def test_shuffle_of_array_of_different_length_strings(self): - # Test that permuting an array of different length strings - # will not cause a segfault on garbage collection - # Tests gh-7710 - mt19937.bit_generator.seed(1234) - - a = np.array(["a", "a" * 1000]) - - for _ in range(100): - mt19937.shuffle(a) - - # Force Garbage Collection - should not segfault. - import gc - - gc.collect() - - def test_shuffle_of_array_of_objects(self): - # Test that permuting an array of objects will not cause - # a segfault on garbage collection. - # See gh-7719 - mt19937.bit_generator.seed(1234) - a = np.array([np.arange(1), np.arange(4)], dtype=object) - - for _ in range(1000): - mt19937.shuffle(a) - - # Force Garbage Collection - should not segfault. - import gc - - gc.collect() - - def test_permutation_subclass(self): - class N(np.ndarray): - pass - - mt19937.bit_generator.seed(1) - orig = np.arange(3).view(N) - perm = mt19937.permutation(orig) - assert_array_equal(perm, np.array([0, 2, 1])) - assert_array_equal(orig, np.arange(3).view(N)) - - class M(object): - a = np.arange(5) - - def __array__(self): - return self.a - - mt19937.bit_generator.seed(1) - m = M() - perm = mt19937.permutation(m) - assert_array_equal(perm, np.array([2, 1, 4, 0, 3])) - assert_array_equal(m.__array__(), np.arange(5)) - - def test_gamma_0(self): - assert mt19937.standard_gamma(0.0) == 0.0 - assert_array_equal(mt19937.standard_gamma([0.0]), 0.0) - - actual = mt19937.standard_gamma([0.0], dtype="float") - expected = np.array([0.0], dtype=np.float32) - assert_array_equal(actual, expected) diff --git a/randomgen/tests/test_randomstate.py b/randomgen/tests/test_randomstate.py deleted file mode 100644 index 258e05bb8..000000000 --- a/randomgen/tests/test_randomstate.py +++ /dev/null @@ -1,2132 +0,0 @@ -import hashlib -import pickle -import sys -import warnings - -import numpy as np -from numpy.testing import ( - assert_, - assert_array_almost_equal, - assert_array_equal, - assert_equal, - assert_no_warnings, - assert_raises, - assert_warns, - suppress_warnings, -) -import pytest - -from randomgen.mt19937 import MT19937 -import randomgen.mtrand -from randomgen.xoshiro256 import Xoshiro256 - -random = randomgen.mtrand - -INT_FUNCS = { - "binomial": (100.0, 0.6), - "geometric": (0.5,), - "hypergeometric": (20, 20, 10), - "logseries": (0.5,), - "multinomial": (20, np.ones(6) / 6.0), - "negative_binomial": (100, 0.5), - "poisson": (10.0,), - "zipf": (2,), -} - -if np.iinfo(int).max < 2**32: - # Windows and some 32-bit platforms, e.g., ARM - INT_FUNC_HASHES = { - "binomial": "670e1c04223ffdbab27e08fbbad7bdba", - "logseries": "6bd0183d2f8030c61b0d6e11aaa60caf", - "geometric": "6e9df886f3e1e15a643168568d5280c0", - "hypergeometric": "7964aa611b046aecd33063b90f4dec06", - "multinomial": "68a0b049c16411ed0aa4aff3572431e4", - "negative_binomial": "dc265219eec62b4338d39f849cd36d09", - "poisson": "7b4dce8e43552fc82701c2fa8e94dc6e", - "zipf": "fcd2a2095f34578723ac45e43aca48c5", - } -else: - INT_FUNC_HASHES = { - "binomial": "b5f8dcd74f172836536deb3547257b14", - "geometric": "8814571f45c87c59699d62ccd3d6c350", - "hypergeometric": "bc64ae5976eac452115a16dad2dcf642", - "logseries": "84be924b37485a27c4a98797bc88a7a4", - "multinomial": "ec3c7f9cf9664044bb0c6fb106934200", - "negative_binomial": "210533b2234943591364d0117a552969", - "poisson": "0536a8850c79da0c78defd742dccc3e0", - "zipf": "f2841f504dd2525cd67cdcad7561e532", - } - - -@pytest.fixture(scope="module", params=INT_FUNCS) -def int_func(request): - return (request.param, INT_FUNCS[request.param], INT_FUNC_HASHES[request.param]) - - -def assert_mt19937_state_equal(a, b): - assert_equal(a["bit_generator"], b["bit_generator"]) - assert_array_equal(a["state"]["key"], b["state"]["key"]) - assert_array_equal(a["state"]["pos"], b["state"]["pos"]) - assert_equal(a["has_gauss"], b["has_gauss"]) - assert_equal(a["gauss"], b["gauss"]) - - -class TestSeed(object): - def test_scalar(self): - s = random.RandomState(0) - assert_equal(s.randint(1000), 684) - s = random.RandomState(4294967295) - assert_equal(s.randint(1000), 419) - - def test_array(self): - s = random.RandomState(range(10)) - assert_equal(s.randint(1000), 468) - s = random.RandomState(np.arange(10)) - assert_equal(s.randint(1000), 468) - s = random.RandomState([0]) - assert_equal(s.randint(1000), 973) - s = random.RandomState([4294967295]) - assert_equal(s.randint(1000), 265) - - def test_invalid_scalar(self): - # seed must be an unsigned 32 bit integer - assert_raises(TypeError, random.RandomState, -0.5) - assert_raises(ValueError, random.RandomState, -1) - - def test_invalid_array(self): - # seed must be an unsigned 32 bit integer - assert_raises(TypeError, random.RandomState, [-0.5]) - assert_raises(ValueError, random.RandomState, [-1]) - assert_raises(ValueError, random.RandomState, [4294967296]) - assert_raises(ValueError, random.RandomState, [1, 2, 4294967296]) - assert_raises(ValueError, random.RandomState, [1, -2, 4294967296]) - - def test_invalid_array_shape(self): - # gh-9832 - assert_raises(ValueError, random.RandomState, np.array([], dtype=np.int64)) - assert_raises(ValueError, random.RandomState, [[1, 2, 3]]) - assert_raises(ValueError, random.RandomState, [[1, 2, 3], [4, 5, 6]]) - - def test_seed_equivalency(self): - rs = random.RandomState(0) - rs2 = random.RandomState(MT19937(0, mode="legacy")) - assert_mt19937_state_equal( - rs.get_state(legacy=False), rs2.get_state(legacy=False) - ) - - def test_invalid_initialization(self): - assert_raises(ValueError, random.RandomState, MT19937) - - -class TestBinomial(object): - def test_n_zero(self): - # Tests the corner case of n == 0 for the binomial distribution. - # binomial(0, p) should be zero for any p in [0, 1]. - # This test addresses issue #3480. - zeros = np.zeros(2, dtype="int") - for p in [0, 0.5, 1]: - assert_(random.binomial(0, p) == 0) - assert_array_equal(random.binomial(zeros, p), zeros) - - def test_p_is_nan(self): - # Issue #4571. - assert_raises(ValueError, random.binomial, 1, np.nan) - - -class TestMultinomial(object): - def test_basic(self): - random.multinomial(100, [0.2, 0.8]) - - def test_zero_probability(self): - random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0]) - - def test_int_negative_interval(self): - assert_(-5 <= random.randint(-5, -1) < -1) - x = random.randint(-5, -1, 5) - assert_(np.all(-5 <= x)) - assert_(np.all(x < -1)) - - def test_multidimensional_pvals(self): - assert_raises(ValueError, random.multinomial, 10, [[0, 1]]) - assert_raises(ValueError, random.multinomial, 10, [[0], [1]]) - assert_raises(ValueError, random.multinomial, 10, [[[0], [1]], [[1], [0]]]) - assert_raises(ValueError, random.multinomial, 10, np.array([[0, 1], [1, 0]])) - - def test_size(self): - # gh-3173 - p = [0.5, 0.5] - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.multinomial(1, p, [2, 2]).shape, (2, 2, 2)) - assert_equal(random.multinomial(1, p, (2, 2)).shape, (2, 2, 2)) - assert_equal(random.multinomial(1, p, np.array((2, 2))).shape, (2, 2, 2)) - - assert_raises(TypeError, random.multinomial, 1, p, float(1)) - - def test_invalid_prob(self): - assert_raises(ValueError, random.multinomial, 100, [1.1, 0.2]) - assert_raises(ValueError, random.multinomial, 100, [-0.1, 0.9]) - - def test_invalid_n(self): - assert_raises(ValueError, random.multinomial, -1, [0.8, 0.2]) - - def test_p_noncontiguous(self): - p = np.arange(15.0) - p /= np.sum(p[1::3]) - pvals = p[1::3] - random.seed(1432985819) - non_contig = random.multinomial(100, pvals=pvals) - random.seed(1432985819) - contig = random.multinomial(100, pvals=np.ascontiguousarray(pvals)) - assert_array_equal(non_contig, contig) - - def test_large_p(self): - with pytest.raises(ValueError, match=r"sum\(pvals"): - random.multinomial(100, np.array([0.7, 0.6, 0.5, 0])) - - -class TestSetState(object): - def setup(self): - self.seed = 1234567890 - self.random_state = random.RandomState(self.seed) - self.state = self.random_state.get_state() - - def test_basic(self): - old = self.random_state.tomaxint(16) - self.random_state.set_state(self.state) - new = self.random_state.tomaxint(16) - assert_(np.all(old == new)) - - def test_gaussian_reset(self): - # Make sure the cached every-other-Gaussian is reset. - old = self.random_state.standard_normal(size=3) - self.random_state.set_state(self.state) - new = self.random_state.standard_normal(size=3) - assert_(np.all(old == new)) - - def test_gaussian_reset_in_media_res(self): - # When the state is saved with a cached Gaussian, make sure the - # cached Gaussian is restored. - - self.random_state.standard_normal() - state = self.random_state.get_state() - old = self.random_state.standard_normal(size=3) - self.random_state.set_state(state) - new = self.random_state.standard_normal(size=3) - assert_(np.all(old == new)) - - def test_backwards_compatibility(self): - # Make sure we can accept old state tuples that do not have the - # cached Gaussian value. - old_state = self.state[:-2] - x1 = self.random_state.standard_normal(size=16) - self.random_state.set_state(old_state) - x2 = self.random_state.standard_normal(size=16) - self.random_state.set_state(self.state) - x3 = self.random_state.standard_normal(size=16) - assert_(np.all(x1 == x2)) - assert_(np.all(x1 == x3)) - - def test_negative_binomial(self): - # Ensure that the negative binomial results take floating point - # arguments without truncation. - self.random_state.negative_binomial(0.5, 0.5) - - def test_get_state_warning(self): - rs = random.RandomState(Xoshiro256(mode="legacy")) - with suppress_warnings() as sup: - w = sup.record(RuntimeWarning) - state = rs.get_state() - assert_(len(w) == 1) - assert isinstance(state, dict) - assert state["bit_generator"] == "Xoshiro256" - - def test_invalid_legacy_state_setting(self): - state = self.random_state.get_state() - new_state = ("Unknown",) + state[1:] - assert_raises(ValueError, self.random_state.set_state, new_state) - assert_raises( - TypeError, self.random_state.set_state, np.array(new_state, dtype=object) - ) - state = self.random_state.get_state(legacy=False) - del state["bit_generator"] - assert_raises(ValueError, self.random_state.set_state, state) - - def test_pickle(self): - self.random_state.seed(0) - self.random_state.random_sample(100) - self.random_state.standard_normal() - pickled = self.random_state.get_state(legacy=False) - assert_equal(pickled["has_gauss"], 1) - rs_unpick = pickle.loads(pickle.dumps(self.random_state)) - unpickled = rs_unpick.get_state(legacy=False) - assert_mt19937_state_equal(pickled, unpickled) - - def test_state_setting(self): - attr_state = self.random_state.__getstate__() - self.random_state.standard_normal() - self.random_state.__setstate__(attr_state) - state = self.random_state.get_state(legacy=False) - assert_mt19937_state_equal(attr_state, state) - - def test_repr(self): - assert repr(self.random_state).startswith("RandomState(MT19937)") - - -class TestRandint(object): - - rfunc = random.randint - - # valid integer/boolean types - itype = [ - np.bool_, - np.int8, - np.uint8, - np.int16, - np.uint16, - np.int32, - np.uint32, - np.int64, - np.uint64, - ] - - def test_unsupported_type(self): - assert_raises(TypeError, self.rfunc, 1, dtype=float) - - def test_bounds_checking(self): - for dt in self.itype: - lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min - ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 - assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt) - assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt) - assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt) - assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt) - - def test_rng_zero_and_extremes(self): - for dt in self.itype: - lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min - ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 - - tgt = ubnd - 1 - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) - - tgt = lbnd - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) - - tgt = (lbnd + ubnd) // 2 - assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt) - - def test_full_range(self): - # Test for ticket #1690 - - for dt in self.itype: - lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min - ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 - - try: - self.rfunc(lbnd, ubnd, dtype=dt) - except Exception as e: - raise AssertionError( - "No error should have been raised, " - "but one was with the following " - "message:\n\n%s" % str(e) - ) - - def test_in_bounds_fuzz(self): - # Don"t use fixed seed - random.seed() - - for dt in self.itype[1:]: - for ubnd in [4, 8, 16]: - vals = self.rfunc(2, ubnd, size=2**16, dtype=dt) - assert_(vals.max() < ubnd) - assert_(vals.min() >= 2) - - vals = self.rfunc(0, 2, size=2**16, dtype=np.bool_) - - assert_(vals.max() < 2) - assert_(vals.min() >= 0) - - def test_repeatability(self): - # We use a md5 hash of generated sequences of 1000 samples - # in the range [0, 6) for all but bool, where the range - # is [0, 2). Hashes are for little endian numbers. - tgt = { - "bool": "7dd3170d7aa461d201a65f8bcf3944b0", - "int16": "1b7741b80964bb190c50d541dca1cac1", - "int32": "4dc9fcc2b395577ebb51793e58ed1a05", - "int64": "17db902806f448331b5a758d7d2ee672", - "int8": "27dd30c4e08a797063dffac2490b0be6", - "uint16": "1b7741b80964bb190c50d541dca1cac1", - "uint32": "4dc9fcc2b395577ebb51793e58ed1a05", - "uint64": "17db902806f448331b5a758d7d2ee672", - "uint8": "27dd30c4e08a797063dffac2490b0be6", - } - - for dt in self.itype[1:]: - random.seed(1234) - - # view as little endian for hash - if sys.byteorder == "little": - val = self.rfunc(0, 6, size=1000, dtype=dt) - else: - val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap() - - res = hashlib.md5(val.view(np.int8)).hexdigest() - assert_(tgt[np.dtype(dt).name] == res) - - # bools do not depend on endianness - random.seed(1234) - val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8) - res = hashlib.md5(val).hexdigest() - assert_(tgt[np.dtype(bool).name] == res) - - def test_int64_uint64_corner_case(self): - # When stored in Numpy arrays, `lbnd` is casted - # as np.int64, and `ubnd` is casted as np.uint64. - # Checking whether `lbnd` >= `ubnd` used to be - # done solely via direct comparison, which is incorrect - # because when Numpy tries to compare both numbers, - # it casts both to np.float64 because there is - # no integer superset of np.int64 and np.uint64. However, - # `ubnd` is too large to be represented in np.float64, - # causing it be round down to np.iinfo(np.int64).max, - # leading to a ValueError because `lbnd` now equals - # the new `ubnd`. - - dt = np.int64 - tgt = np.iinfo(np.int64).max - lbnd = np.int64(np.iinfo(np.int64).max) - ubnd = np.uint64(np.iinfo(np.int64).max + 1) - - # None of these function calls should - # generate a ValueError now. - actual = random.randint(lbnd, ubnd, dtype=dt) - assert_equal(actual, tgt) - - def test_respect_dtype_singleton(self): - # See gh-7203 - for dt in self.itype: - lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min - ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 - - sample = self.rfunc(lbnd, ubnd, dtype=dt) - assert_equal(sample.dtype, np.dtype(dt)) - - for dt in (bool, int): - lbnd = 0 if dt is bool else np.iinfo(dt).min - ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 - - # gh-7284: Ensure that we get Python data types - sample = self.rfunc(lbnd, ubnd, dtype=dt) - assert_(not hasattr(sample, "dtype")) - assert_equal(type(sample), dt) - - -class TestRandomDist(object): - # Make sure the random distribution returns the correct value for a - # given seed - - def setup(self): - self.seed = 1234567890 - - def test_rand(self): - random.seed(self.seed) - actual = random.rand(3, 2) - desired = np.array( - [ - [0.61879477158567997, 0.59162362775974664], - [0.88868358904449662, 0.89165480011560816], - [0.4575674820298663, 0.7781880808593471], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_rand_singleton(self): - random.seed(self.seed) - actual = random.rand() - desired = 0.61879477158567997 - assert_array_almost_equal(actual, desired, decimal=15) - - def test_randn(self): - random.seed(self.seed) - actual = random.randn(3, 2) - desired = np.array( - [ - [1.34016345771863121, 1.73759122771936081], - [1.498988344300628, -0.2286433324536169], - [2.031033998682787, 2.17032494605655257], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.seed(self.seed) - actual = random.randn() - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_randint(self): - random.seed(self.seed) - actual = random.randint(-99, 99, size=(3, 2)) - desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - assert_array_equal(actual, desired) - - def test_random_integers(self): - random.seed(self.seed) - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(-99, 99, size=(3, 2)) - assert_(len(w) == 1) - desired = np.array([[31, 3], [-52, 41], [-48, -66]]) - assert_array_equal(actual, desired) - - random.seed(self.seed) - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(198, size=(3, 2)) - assert_(len(w) == 1) - assert_array_equal(actual, desired + 100) - - def test_tomaxint(self): - random.seed(self.seed) - rs = random.RandomState(self.seed) - actual = rs.tomaxint(size=(3, 2)) - if np.iinfo(int).max == 2147483647: - desired = np.array( - [ - [1328851649, 731237375], - [1270502067, 320041495], - [1908433478, 499156889], - ], - dtype=np.int64, - ) - else: - desired = np.array( - [ - [5707374374421908479, 5456764827585442327], - [8196659375100692377, 8224063923314595285], - [4220315081820346526, 7177518203184491332], - ], - dtype=np.int64, - ) - - assert_equal(actual, desired) - - rs.seed(self.seed) - actual = rs.tomaxint() - assert_equal(actual, desired[0, 0]) - - def test_random_integers_max_int(self): - # Tests whether random_integers can generate the - # maximum allowed Python int that can be converted - # into a C long. Previous implementations of this - # method have thrown an OverflowError when attempting - # to generate this integer. - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - actual = random.random_integers(np.iinfo("l").max, np.iinfo("l").max) - assert_(len(w) == 1) - - desired = np.iinfo("l").max - assert_equal(actual, desired) - with suppress_warnings() as sup: - w = sup.record(DeprecationWarning) - typer = np.dtype("l").type - actual = random.random_integers( - typer(np.iinfo("l").max), typer(np.iinfo("l").max) - ) - assert_(len(w) == 1) - assert_equal(actual, desired) - - def test_random_integers_deprecated(self): - with warnings.catch_warnings(): - warnings.simplefilter("error", DeprecationWarning) - - # DeprecationWarning raised with high == None - assert_raises(DeprecationWarning, random.random_integers, np.iinfo("l").max) - - # DeprecationWarning raised with high != None - assert_raises( - DeprecationWarning, - random.random_integers, - np.iinfo("l").max, - np.iinfo("l").max, - ) - - def test_random_sample(self): - random.seed(self.seed) - actual = random.random_sample((3, 2)) - desired = np.array( - [ - [0.61879477158567997, 0.59162362775974664], - [0.88868358904449662, 0.89165480011560816], - [0.4575674820298663, 0.7781880808593471], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - random.seed(self.seed) - actual = random.random_sample() - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_choice_uniform_replace(self): - random.seed(self.seed) - actual = random.choice(4, 4) - desired = np.array([2, 3, 2, 3]) - assert_array_equal(actual, desired) - - def test_choice_nonuniform_replace(self): - random.seed(self.seed) - actual = random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1]) - desired = np.array([1, 1, 2, 2]) - assert_array_equal(actual, desired) - - def test_choice_uniform_noreplace(self): - random.seed(self.seed) - actual = random.choice(4, 3, replace=False) - desired = np.array([0, 1, 3]) - assert_array_equal(actual, desired) - - def test_choice_nonuniform_noreplace(self): - random.seed(self.seed) - actual = random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1]) - desired = np.array([2, 3, 1]) - assert_array_equal(actual, desired) - - def test_choice_noninteger(self): - random.seed(self.seed) - actual = random.choice(["a", "b", "c", "d"], 4) - desired = np.array(["c", "d", "c", "d"]) - assert_array_equal(actual, desired) - - def test_choice_exceptions(self): - sample = random.choice - assert_raises(ValueError, sample, -1, 3) - assert_raises(ValueError, sample, 3.0, 3) - assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3) - assert_raises(ValueError, sample, [], 3) - assert_raises( - ValueError, sample, [1, 2, 3, 4], 3, p=[[0.25, 0.25], [0.25, 0.25]] - ) - assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2]) - assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1]) - assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4]) - assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False) - # gh-13087 - assert_raises(ValueError, sample, [1, 2, 3], -2, replace=False) - assert_raises(ValueError, sample, [1, 2, 3], (-1,), replace=False) - assert_raises(ValueError, sample, [1, 2, 3], (-1, 1), replace=False) - assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False, p=[1, 0, 0]) - - def test_choice_return_shape(self): - p = [0.1, 0.9] - # Check scalar - assert_(np.isscalar(random.choice(2, replace=True))) - assert_(np.isscalar(random.choice(2, replace=False))) - assert_(np.isscalar(random.choice(2, replace=True, p=p))) - assert_(np.isscalar(random.choice(2, replace=False, p=p))) - assert_(np.isscalar(random.choice([1, 2], replace=True))) - assert_(random.choice([None], replace=True) is None) - a = np.array([1, 2]) - arr = np.empty(1, dtype=object) - arr[0] = a - assert_(random.choice(arr, replace=True) is a) - - # Check 0-d array - s = tuple() - assert_(not np.isscalar(random.choice(2, s, replace=True))) - assert_(not np.isscalar(random.choice(2, s, replace=False))) - assert_(not np.isscalar(random.choice(2, s, replace=True, p=p))) - assert_(not np.isscalar(random.choice(2, s, replace=False, p=p))) - assert_(not np.isscalar(random.choice([1, 2], s, replace=True))) - assert_(random.choice([None], s, replace=True).ndim == 0) - a = np.array([1, 2]) - arr = np.empty(1, dtype=object) - arr[0] = a - assert_(random.choice(arr, s, replace=True).item() is a) - - # Check multi dimensional array - s = (2, 3) - p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2] - assert_equal(random.choice(6, s, replace=True).shape, s) - assert_equal(random.choice(6, s, replace=False).shape, s) - assert_equal(random.choice(6, s, replace=True, p=p).shape, s) - assert_equal(random.choice(6, s, replace=False, p=p).shape, s) - assert_equal(random.choice(np.arange(6), s, replace=True).shape, s) - - # Check zero-size - assert_equal(random.randint(0, 0, size=(3, 0, 4)).shape, (3, 0, 4)) - assert_equal(random.randint(0, -10, size=0).shape, (0,)) - assert_equal(random.randint(10, 10, size=0).shape, (0,)) - assert_equal(random.choice(0, size=0).shape, (0,)) - assert_equal(random.choice([], size=(0,)).shape, (0,)) - assert_equal(random.choice(["a", "b"], size=(3, 0, 4)).shape, (3, 0, 4)) - assert_raises(ValueError, random.choice, [], 10) - - def test_choice_nan_probabilities(self): - a = np.array([42, 1, 2]) - p = [None, None, None] - with np.errstate(invalid="ignore"): - assert_raises(ValueError, random.choice, a, p=p) - - def test_choice_nontintiguous(self): - p = np.ones(10) / 5 - p[1::2] = 3.0 - random.seed(self.seed) - choice1 = random.choice(5, 3, p=p[::2]) - random.seed(self.seed) - choice2 = random.choice(5, 3, p=np.ascontiguousarray(p[::2])) - assert_array_equal(choice1, choice2) - - def test_bytes(self): - random.seed(self.seed) - actual = random.bytes(10) - desired = b"\x82Ui\x9e\xff\x97+Wf\xa5" - assert_equal(actual, desired) - - def test_shuffle(self): - # Test lists, arrays (of various dtypes), and multidimensional versions - # of both, c-contiguous or not: - for conv in [ - lambda x: np.array([]), - lambda x: x, - lambda x: np.asarray(x).astype(np.int8), - lambda x: np.asarray(x).astype(np.float32), - lambda x: np.asarray(x).astype(np.complex64), - lambda x: np.asarray(x).astype(object), - lambda x: [(i, i) for i in x], - lambda x: np.asarray([[i, i] for i in x]), - lambda x: np.vstack([x, x]).T, - # gh-11442 - lambda x: ( - np.asarray([(i, i) for i in x], [("a", int), ("b", int)]).view( - np.recarray - ) - ), - # gh-4270 - lambda x: np.asarray( - [(i, i) for i in x], [("a", (object, (1,))), ("b", (np.int32, (1,)))] - ), - ]: - random.seed(self.seed) - alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) - random.shuffle(alist) - actual = alist - desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) - assert_array_equal(actual, desired) - - def test_shuffle_masked(self): - # gh-3263 - a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1) - b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) - a_orig = a.copy() - b_orig = b.copy() - for _ in range(50): - random.shuffle(a) - assert_equal(sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask])) - random.shuffle(b) - assert_equal(sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask])) - - def test_permutation(self): - random.seed(self.seed) - alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] - actual = random.permutation(alist) - desired = [0, 1, 9, 6, 2, 4, 5, 8, 7, 3] - assert_array_equal(actual, desired) - - random.seed(self.seed) - arr_2d = np.atleast_2d([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]).T - actual = random.permutation(arr_2d) - assert_array_equal(actual, np.atleast_2d(desired).T) - - def test_beta(self): - random.seed(self.seed) - actual = random.beta(0.1, 0.9, size=(3, 2)) - desired = np.array( - [ - [1.45341850513746058e-02, 5.31297615662868145e-04], - [1.85366619058432324e-06, 4.19214516800110563e-03], - [1.58405155108498093e-04, 1.26252891949397652e-04], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_binomial(self): - random.seed(self.seed) - actual = random.binomial(100.123, 0.456, size=(3, 2)) - desired = np.array([[37, 43], [42, 48], [46, 45]]) - assert_array_equal(actual, desired) - - random.seed(self.seed) - actual = random.binomial(100.123, 0.456) - desired = 37 - assert_array_equal(actual, desired) - - def test_chisquare(self): - random.seed(self.seed) - actual = random.chisquare(50, size=(3, 2)) - desired = np.array( - [ - [63.87858175501090585, 68.68407748911370447], - [65.77116116901505904, 47.09686762438974483], - [72.3828403199695174, 74.18408615260374006], - ] - ) - assert_array_almost_equal(actual, desired, decimal=13) - - def test_dirichlet(self): - random.seed(self.seed) - alpha = np.array([51.72840233779265162, 39.74494232180943953]) - actual = random.dirichlet(alpha, size=(3, 2)) - desired = np.array( - [ - [ - [0.54539444573611562, 0.45460555426388438], - [0.62345816822039413, 0.37654183177960598], - ], - [ - [0.55206000085785778, 0.44793999914214233], - [0.58964023305154301, 0.41035976694845688], - ], - [ - [0.59266909280647828, 0.40733090719352177], - [0.56974431743975207, 0.43025568256024799], - ], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - bad_alpha = np.array([5.4e-01, -1.0e-16]) - assert_raises(ValueError, random.dirichlet, bad_alpha) - - random.seed(self.seed) - alpha = np.array([51.72840233779265162, 39.74494232180943953]) - actual = random.dirichlet(alpha) - assert_array_almost_equal(actual, desired[0, 0], decimal=15) - - def test_dirichlet_size(self): - # gh-3173 - p = np.array([51.72840233779265162, 39.74494232180943953]) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2)) - assert_equal(random.dirichlet(p, [2, 2]).shape, (2, 2, 2)) - assert_equal(random.dirichlet(p, (2, 2)).shape, (2, 2, 2)) - assert_equal(random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2)) - - assert_raises(TypeError, random.dirichlet, p, float(1)) - - def test_dirichlet_bad_alpha(self): - # gh-2089 - alpha = np.array([5.4e-01, -1.0e-16]) - assert_raises(ValueError, random.dirichlet, alpha) - - assert_raises(ValueError, random.dirichlet, [[5, 1]]) - assert_raises(ValueError, random.dirichlet, [[5], [1]]) - assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]]) - assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]])) - - def test_dirichlet_non_contiguous_alpha(self): - a = np.array([51.72840233779265162, -1.0, 39.74494232180943953]) - alpha = a[::2] - random.seed(self.seed) - non_contig = random.dirichlet(alpha, size=(3, 2)) - random.seed(self.seed) - contig = random.dirichlet(np.ascontiguousarray(alpha), size=(3, 2)) - assert_array_almost_equal(contig, non_contig) - - def test_exponential(self): - random.seed(self.seed) - actual = random.exponential(1.1234, size=(3, 2)) - desired = np.array( - [ - [1.08342649775011624, 1.00607889924557314], - [2.46628830085216721, 2.49668106809923884], - [0.68717433461363442, 1.69175666993575979], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_exponential_0(self): - assert_equal(random.exponential(scale=0), 0) - assert_raises(ValueError, random.exponential, scale=-0.0) - - def test_f(self): - random.seed(self.seed) - actual = random.f(12, 77, size=(3, 2)) - desired = np.array( - [ - [1.21975394418575878, 1.75135759791559775], - [1.44803115017146489, 1.22108959480396262], - [1.02176975757740629, 1.34431827623300415], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_gamma(self): - random.seed(self.seed) - actual = random.gamma(5, 3, size=(3, 2)) - desired = np.array( - [ - [24.60509188649287182, 28.54993563207210627], - [26.13476110204064184, 12.56988482927716078], - [31.71863275789960568, 33.30143302795922011], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_gamma_0(self): - assert_equal(random.gamma(shape=0, scale=0), 0) - assert_raises(ValueError, random.gamma, shape=-0.0, scale=-0.0) - - def test_geometric(self): - random.seed(self.seed) - actual = random.geometric(0.123456789, size=(3, 2)) - desired = np.array([[8, 7], [17, 17], [5, 12]]) - assert_array_equal(actual, desired) - - def test_geometric_exceptions(self): - assert_raises(ValueError, random.geometric, 1.1) - assert_raises(ValueError, random.geometric, [1.1] * 10) - assert_raises(ValueError, random.geometric, -0.1) - assert_raises(ValueError, random.geometric, [-0.1] * 10) - with suppress_warnings() as sup: - sup.record(RuntimeWarning) - assert_raises(ValueError, random.geometric, np.nan) - assert_raises(ValueError, random.geometric, [np.nan] * 10) - - def test_gumbel(self): - random.seed(self.seed) - actual = random.gumbel(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [0.19591898743416816, 0.34405539668096674], - [-1.4492522252274278, -1.47374816298446865], - [1.10651090478803416, -0.69535848626236174], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_gumbel_0(self): - assert_equal(random.gumbel(scale=0), 0) - assert_raises(ValueError, random.gumbel, scale=-0.0) - - def test_hypergeometric(self): - random.seed(self.seed) - actual = random.hypergeometric(10.1, 5.5, 14, size=(3, 2)) - desired = np.array([[10, 10], [10, 10], [9, 9]]) - assert_array_equal(actual, desired) - - # Test nbad = 0 - actual = random.hypergeometric(5, 0, 3, size=4) - desired = np.array([3, 3, 3, 3]) - assert_array_equal(actual, desired) - - actual = random.hypergeometric(15, 0, 12, size=4) - desired = np.array([12, 12, 12, 12]) - assert_array_equal(actual, desired) - - # Test ngood = 0 - actual = random.hypergeometric(0, 5, 3, size=4) - desired = np.array([0, 0, 0, 0]) - assert_array_equal(actual, desired) - - actual = random.hypergeometric(0, 15, 12, size=4) - desired = np.array([0, 0, 0, 0]) - assert_array_equal(actual, desired) - - def test_laplace(self): - random.seed(self.seed) - actual = random.laplace(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [0.66599721112760157, 0.52829452552221945], - [3.12791959514407125, 3.18202813572992005], - [-0.05391065675859356, 1.74901336242837324], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_laplace_0(self): - assert_equal(random.laplace(scale=0), 0) - assert_raises(ValueError, random.laplace, scale=-0.0) - - def test_logistic(self): - random.seed(self.seed) - actual = random.logistic(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [1.09232835305011444, 0.8648196662399954], - [4.27818590694950185, 4.33897006346929714], - [-0.21682183359214885, 2.63373365386060332], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_lognormal(self): - random.seed(self.seed) - actual = random.lognormal(mean=0.123456789, sigma=2.0, size=(3, 2)) - desired = np.array( - [ - [16.50698631688883822, 36.54846706092654784], - [22.67886599981281748, 0.71617561058995771], - [65.72798501792723869, 86.84341601437161273], - ] - ) - assert_array_almost_equal(actual, desired, decimal=13) - - def test_lognormal_0(self): - assert_equal(random.lognormal(sigma=0), 1) - assert_raises(ValueError, random.lognormal, sigma=-0.0) - - def test_logseries(self): - random.seed(self.seed) - actual = random.logseries(p=0.923456789, size=(3, 2)) - desired = np.array([[2, 2], [6, 17], [3, 6]]) - assert_array_equal(actual, desired) - - def test_logseries_exceptions(self): - with suppress_warnings() as sup: - sup.record(RuntimeWarning) - assert_raises(ValueError, random.logseries, np.nan) - assert_raises(ValueError, random.logseries, [np.nan] * 10) - - def test_multinomial(self): - random.seed(self.seed) - actual = random.multinomial(20, [1 / 6.0] * 6, size=(3, 2)) - desired = np.array( - [ - [[4, 3, 5, 4, 2, 2], [5, 2, 8, 2, 2, 1]], - [[3, 4, 3, 6, 0, 4], [2, 1, 4, 3, 6, 4]], - [[4, 4, 2, 5, 2, 3], [4, 3, 4, 2, 3, 4]], - ] - ) - assert_array_equal(actual, desired) - - def test_multivariate_normal(self): - random.seed(self.seed) - mean = (0.123456789, 10) - cov = [[1, 0], [0, 1]] - size = (3, 2) - actual = random.multivariate_normal(mean, cov, size) - desired = np.array( - [ - [ - [1.463620246718631, 11.73759122771936], - [1.622445133300628, 9.771356667546383], - ], - [ - [2.154490787682787, 12.170324946056553], - [1.719909438201865, 9.230548443648306], - ], - [ - [0.689515026297799, 9.880729819607714], - [-0.023054015651998, 9.201096623542879], - ], - ] - ) - - assert_array_almost_equal(actual, desired, decimal=15) - - # Check for default size, was raising deprecation warning - actual = random.multivariate_normal(mean, cov) - desired = np.array([0.895289569463708, 9.17180864067987]) - assert_array_almost_equal(actual, desired, decimal=15) - - # Check that non positive-semidefinite covariance warns with - # RuntimeWarning - mean = [0, 0] - cov = [[1, 2], [2, 1]] - assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov) - - # and that it doesn"t warn with RuntimeWarning check_valid="ignore" - assert_no_warnings(random.multivariate_normal, mean, cov, check_valid="ignore") - - # and that it raises with RuntimeWarning check_valid="raises" - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="raise" - ) - - cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32) - with suppress_warnings() as sup: - random.multivariate_normal(mean, cov) - w = sup.record(RuntimeWarning) - assert len(w) == 0 - - mu = np.zeros(2) - cov = np.eye(2) - assert_raises( - ValueError, random.multivariate_normal, mean, cov, check_valid="other" - ) - assert_raises(ValueError, random.multivariate_normal, np.zeros((2, 1, 1)), cov) - assert_raises(ValueError, random.multivariate_normal, mu, np.empty((3, 2))) - assert_raises(ValueError, random.multivariate_normal, mu, np.eye(3)) - - def test_negative_binomial(self): - random.seed(self.seed) - actual = random.negative_binomial(n=100, p=0.12345, size=(3, 2)) - desired = np.array([[848, 841], [892, 611], [779, 647]]) - assert_array_equal(actual, desired) - - def test_negative_binomial_exceptions(self): - with suppress_warnings() as sup: - sup.record(RuntimeWarning) - assert_raises(ValueError, random.negative_binomial, 100, np.nan) - assert_raises(ValueError, random.negative_binomial, 100, [np.nan] * 10) - - def test_noncentral_chisquare(self): - random.seed(self.seed) - actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2)) - desired = np.array( - [ - [23.91905354498517511, 13.35324692733826346], - [31.22452661329736401, 16.60047399466177254], - [5.03461598262724586, 17.94973089023519464], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - actual = random.noncentral_chisquare(df=0.5, nonc=0.2, size=(3, 2)) - desired = np.array( - [ - [1.47145377828516666, 0.15052899268012659], - [0.00943803056963588, 1.02647251615666169], - [0.332334982684171, 0.15451287602753125], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - random.seed(self.seed) - actual = random.noncentral_chisquare(df=5, nonc=0, size=(3, 2)) - desired = np.array( - [ - [9.597154162763948, 11.725484450296079], - [10.413711048138335, 3.694475922923986], - [13.484222138963087, 14.377255424602957], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_noncentral_f(self): - random.seed(self.seed) - actual = random.noncentral_f(dfnum=5, dfden=2, nonc=1, size=(3, 2)) - desired = np.array( - [ - [1.40598099674926669, 0.34207973179285761], - [3.57715069265772545, 7.92632662577829805], - [0.43741599463544162, 1.1774208752428319], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_noncentral_f_nan(self): - random.seed(self.seed) - actual = random.noncentral_f(dfnum=5, dfden=2, nonc=np.nan) - assert np.isnan(actual) - - def test_normal(self): - random.seed(self.seed) - actual = random.normal(loc=0.123456789, scale=2.0, size=(3, 2)) - desired = np.array( - [ - [2.80378370443726244, 3.59863924443872163], - [3.121433477601256, -0.33382987590723379], - [4.18552478636557357, 4.46410668111310471], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_normal_0(self): - assert_equal(random.normal(scale=0), 0) - assert_raises(ValueError, random.normal, scale=-0.0) - - def test_pareto(self): - random.seed(self.seed) - actual = random.pareto(a=0.123456789, size=(3, 2)) - desired = np.array( - [ - [2.46852460439034849e03, 1.41286880810518346e03], - [5.28287797029485181e07, 6.57720981047328785e07], - [1.40840323350391515e02, 1.98390255135251704e05], - ] - ) - # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this - # matrix differs by 24 nulps. Discussion: - # https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html - # Consensus is that this is probably some gcc quirk that affects - # rounding but not in any important way, so we just use a looser - # tolerance on this test: - np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30) - - def test_poisson(self): - random.seed(self.seed) - actual = random.poisson(lam=0.123456789, size=(3, 2)) - desired = np.array([[0, 0], [1, 0], [0, 0]]) - assert_array_equal(actual, desired) - - def test_poisson_exceptions(self): - lambig = np.iinfo("l").max - lamneg = -1 - assert_raises(ValueError, random.poisson, lamneg) - assert_raises(ValueError, random.poisson, [lamneg] * 10) - assert_raises(ValueError, random.poisson, lambig) - assert_raises(ValueError, random.poisson, [lambig] * 10) - with suppress_warnings() as sup: - sup.record(RuntimeWarning) - assert_raises(ValueError, random.poisson, np.nan) - assert_raises(ValueError, random.poisson, [np.nan] * 10) - - def test_power(self): - random.seed(self.seed) - actual = random.power(a=0.123456789, size=(3, 2)) - desired = np.array( - [ - [0.02048932883240791, 0.01424192241128213], - [0.38446073748535298, 0.39499689943484395], - [0.00177699707563439, 0.13115505880863756], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_rayleigh(self): - random.seed(self.seed) - actual = random.rayleigh(scale=10, size=(3, 2)) - desired = np.array( - [ - [13.8882496494248393, 13.383318339044731], - [20.95413364294492098, 21.08285015800712614], - [11.06066537006854311, 17.35468505778271009], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_rayleigh_0(self): - assert_equal(random.rayleigh(scale=0), 0) - assert_raises(ValueError, random.rayleigh, scale=-0.0) - - def test_standard_cauchy(self): - random.seed(self.seed) - actual = random.standard_cauchy(size=(3, 2)) - desired = np.array( - [ - [0.77127660196445336, -6.55601161955910605], - [0.93582023391158309, -2.07479293013759447], - [-4.74601644297011926, 0.18338989290760804], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_exponential(self): - random.seed(self.seed) - actual = random.standard_exponential(size=(3, 2)) - desired = np.array( - [ - [0.96441739162374596, 0.89556604882105506], - [2.1953785836319808, 2.22243285392490542], - [0.6116915921431676, 1.50592546727413201], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_gamma(self): - random.seed(self.seed) - actual = random.standard_gamma(shape=3, size=(3, 2)) - desired = np.array( - [ - [5.50841531318455058, 6.62953470301903103], - [5.93988484943779227, 2.31044849402133989], - [7.54838614231317084, 8.012756093271868], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_standard_gamma_0(self): - assert_equal(random.standard_gamma(shape=0), 0) - assert_raises(ValueError, random.standard_gamma, shape=-0.0) - - def test_standard_normal(self): - random.seed(self.seed) - actual = random.standard_normal(size=(3, 2)) - desired = np.array( - [ - [1.34016345771863121, 1.73759122771936081], - [1.498988344300628, -0.2286433324536169], - [2.031033998682787, 2.17032494605655257], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_randn_singleton(self): - random.seed(self.seed) - actual = random.randn() - desired = np.array(1.34016345771863121) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_standard_t(self): - random.seed(self.seed) - actual = random.standard_t(df=10, size=(3, 2)) - desired = np.array( - [ - [0.97140611862659965, -0.08830486548450577], - [1.36311143689505321, -0.55317463909867071], - [-0.18473749069684214, 0.61181537341755321], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_triangular(self): - random.seed(self.seed) - actual = random.triangular(left=5.12, mode=10.23, right=20.34, size=(3, 2)) - desired = np.array( - [ - [12.68117178949215784, 12.4129206149193152], - [16.20131377335158263, 16.25692138747600524], - [11.20400690911820263, 14.4978144835829923], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_uniform(self): - random.seed(self.seed) - actual = random.uniform(low=1.23, high=10.54, size=(3, 2)) - desired = np.array( - [ - [6.99097932346268003, 6.73801597444323974], - [9.50364421400426274, 9.53130618907631089], - [5.48995325769805476, 8.47493103280052118], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_uniform_range_bounds(self): - fmin = np.finfo("float").min - fmax = np.finfo("float").max - - func = random.uniform - assert_raises(OverflowError, func, -np.inf, 0) - assert_raises(OverflowError, func, 0, np.inf) - assert_raises(OverflowError, func, fmin, fmax) - assert_raises(OverflowError, func, [-np.inf], [0]) - assert_raises(OverflowError, func, [0], [np.inf]) - - # (fmax / 1e17) - fmin is within range, so this should not throw - # account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX > - # DBL_MAX by increasing fmin a bit - random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17) - - def test_scalar_exception_propagation(self): - # Tests that exceptions are correctly propagated in distributions - # when called with objects that throw exceptions when converted to - # scalars. - # - # Regression test for gh: 8865 - - class ThrowingFloat(np.ndarray): - def __float__(self): - raise TypeError - - throwing_float = np.array(1.0).view(ThrowingFloat) - assert_raises(TypeError, random.uniform, throwing_float, throwing_float) - - class ThrowingInteger(np.ndarray): - def __int__(self): - raise TypeError - - throwing_int = np.array(1).view(ThrowingInteger) - assert_raises(TypeError, random.hypergeometric, throwing_int, 1, 1) - - def test_vonmises(self): - random.seed(self.seed) - actual = random.vonmises(mu=1.23, kappa=1.54, size=(3, 2)) - desired = np.array( - [ - [2.28567572673902042, 2.89163838442285037], - [0.38198375564286025, 2.57638023113890746], - [1.19153771588353052, 1.83509849681825354], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_vonmises_small(self): - # check infinite loop, gh-4720 - random.seed(self.seed) - r = random.vonmises(mu=0.0, kappa=1.1e-8, size=10**6) - assert_(np.isfinite(r).all()) - - def test_vonmises_nan(self): - random.seed(self.seed) - r = random.vonmises(mu=0.0, kappa=np.nan) - assert_(np.isnan(r)) - - def test_wald(self): - random.seed(self.seed) - actual = random.wald(mean=1.23, scale=1.54, size=(3, 2)) - desired = np.array( - [ - [3.82935265715889983, 5.13125249184285526], - [0.35045403618358717, 1.50832396872003538], - [0.24124319895843183, 0.22031101461955038], - ] - ) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_weibull(self): - random.seed(self.seed) - actual = random.weibull(a=1.23, size=(3, 2)) - desired = np.array( - [ - [0.97097342648766727, 0.91422896443565516], - [1.89517770034962929, 1.91414357960479564], - [0.67057783752390987, 1.39494046635066793], - ] - ) - assert_array_almost_equal(actual, desired, decimal=15) - - def test_weibull_0(self): - random.seed(self.seed) - assert_equal(random.weibull(a=0, size=12), np.zeros(12)) - assert_raises(ValueError, random.weibull, a=-0.0) - - def test_zipf(self): - random.seed(self.seed) - actual = random.zipf(a=1.23, size=(3, 2)) - desired = np.array([[66, 29], [1, 1], [3, 13]]) - assert_array_equal(actual, desired) - - -class TestBroadcast(object): - # tests that functions that broadcast behave - # correctly when presented with non-scalar arguments - def setup(self): - self.seed = 123456789 - - def set_seed(self): - random.seed(self.seed) - - def test_uniform(self): - low = [0] - high = [1] - uniform = random.uniform - desired = np.array( - [0.53283302478975902, 0.53413660089041659, 0.50955303552646702] - ) - - self.set_seed() - actual = uniform(low * 3, high) - assert_array_almost_equal(actual, desired, decimal=14) - - self.set_seed() - actual = uniform(low, high * 3) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_normal(self): - loc = [0] - scale = [1] - bad_scale = [-1] - normal = random.normal - desired = np.array([2.2129019979039612, 2.1283977976520019, 1.8417114045748335]) - - self.set_seed() - actual = normal(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, normal, loc * 3, bad_scale) - - self.set_seed() - actual = normal(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, normal, loc, bad_scale * 3) - - def test_beta(self): - a = [1] - b = [2] - bad_a = [-1] - bad_b = [-2] - beta = random.beta - desired = np.array( - [0.19843558305989056, 0.075230336409423643, 0.24976865978980844] - ) - - self.set_seed() - actual = beta(a * 3, b) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, beta, bad_a * 3, b) - assert_raises(ValueError, beta, a * 3, bad_b) - - self.set_seed() - actual = beta(a, b * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, beta, bad_a, b * 3) - assert_raises(ValueError, beta, a, bad_b * 3) - - def test_exponential(self): - scale = [1] - bad_scale = [-1] - exponential = random.exponential - desired = np.array( - [0.76106853658845242, 0.76386282278691653, 0.71243813125891797] - ) - - self.set_seed() - actual = exponential(scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, exponential, bad_scale * 3) - - def test_standard_gamma(self): - shape = [1] - bad_shape = [-1] - std_gamma = random.standard_gamma - desired = np.array( - [0.76106853658845242, 0.76386282278691653, 0.71243813125891797] - ) - - self.set_seed() - actual = std_gamma(shape * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, std_gamma, bad_shape * 3) - - def test_gamma(self): - shape = [1] - scale = [2] - bad_shape = [-1] - bad_scale = [-2] - gamma = random.gamma - desired = np.array([1.5221370731769048, 1.5277256455738331, 1.4248762625178359]) - - self.set_seed() - actual = gamma(shape * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gamma, bad_shape * 3, scale) - assert_raises(ValueError, gamma, shape * 3, bad_scale) - - self.set_seed() - actual = gamma(shape, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gamma, bad_shape, scale * 3) - assert_raises(ValueError, gamma, shape, bad_scale * 3) - - def test_f(self): - dfnum = [1] - dfden = [2] - bad_dfnum = [-1] - bad_dfden = [-2] - f = random.f - desired = np.array( - [0.80038951638264799, 0.86768719635363512, 2.7251095168386801] - ) - - self.set_seed() - actual = f(dfnum * 3, dfden) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, f, bad_dfnum * 3, dfden) - assert_raises(ValueError, f, dfnum * 3, bad_dfden) - - self.set_seed() - actual = f(dfnum, dfden * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, f, bad_dfnum, dfden * 3) - assert_raises(ValueError, f, dfnum, bad_dfden * 3) - - def test_noncentral_f(self): - dfnum = [2] - dfden = [3] - nonc = [4] - bad_dfnum = [0] - bad_dfden = [-1] - bad_nonc = [-2] - nonc_f = random.noncentral_f - desired = np.array([9.1393943263705211, 13.025456344595602, 8.8018098359100545]) - - self.set_seed() - actual = nonc_f(dfnum * 3, dfden, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert np.all(np.isnan(nonc_f(dfnum, dfden, [np.nan] * 3))) - - assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc) - assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc) - assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc) - - self.set_seed() - actual = nonc_f(dfnum, dfden * 3, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc) - assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc) - assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc) - - self.set_seed() - actual = nonc_f(dfnum, dfden, nonc * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3) - assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3) - assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3) - - def test_noncentral_f_small_df(self): - self.set_seed() - desired = np.array([6.869638627492048, 0.785880199263955]) - actual = random.noncentral_f(0.9, 0.9, 2, size=2) - assert_array_almost_equal(actual, desired, decimal=14) - - def test_chisquare(self): - df = [1] - bad_df = [-1] - chisquare = random.chisquare - desired = np.array( - [0.57022801133088286, 0.51947702108840776, 0.1320969254923558] - ) - - self.set_seed() - actual = chisquare(df * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, chisquare, bad_df * 3) - - def test_noncentral_chisquare(self): - df = [1] - nonc = [2] - bad_df = [-1] - bad_nonc = [-2] - nonc_chi = random.noncentral_chisquare - desired = np.array([9.0015599467913763, 4.5804135049718742, 6.0872302432834564]) - - self.set_seed() - actual = nonc_chi(df * 3, nonc) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_chi, bad_df * 3, nonc) - assert_raises(ValueError, nonc_chi, df * 3, bad_nonc) - - self.set_seed() - actual = nonc_chi(df, nonc * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, nonc_chi, bad_df, nonc * 3) - assert_raises(ValueError, nonc_chi, df, bad_nonc * 3) - - def test_standard_t(self): - df = [1] - bad_df = [-1] - t = random.standard_t - desired = np.array([3.0702872575217643, 5.8560725167361607, 1.0274791436474273]) - - self.set_seed() - actual = t(df * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, t, bad_df * 3) - assert_raises(ValueError, random.standard_t, bad_df * 3) - - def test_vonmises(self): - mu = [2] - kappa = [1] - bad_kappa = [-1] - vonmises = random.vonmises - desired = np.array( - [2.9883443664201312, -2.7064099483995943, -1.8672476700665914] - ) - - self.set_seed() - actual = vonmises(mu * 3, kappa) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, vonmises, mu * 3, bad_kappa) - - self.set_seed() - actual = vonmises(mu, kappa * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, vonmises, mu, bad_kappa * 3) - - def test_pareto(self): - a = [1] - bad_a = [-1] - pareto = random.pareto - desired = np.array([1.1405622680198362, 1.1465519762044529, 1.0389564467453547]) - - self.set_seed() - actual = pareto(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, pareto, bad_a * 3) - assert_raises(ValueError, random.pareto, bad_a * 3) - - def test_weibull(self): - a = [1] - bad_a = [-1] - weibull = random.weibull - desired = np.array( - [0.76106853658845242, 0.76386282278691653, 0.71243813125891797] - ) - - self.set_seed() - actual = weibull(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, weibull, bad_a * 3) - assert_raises(ValueError, random.weibull, bad_a * 3) - - def test_power(self): - a = [1] - bad_a = [-1] - power = random.power - desired = np.array( - [0.53283302478975902, 0.53413660089041659, 0.50955303552646702] - ) - - self.set_seed() - actual = power(a * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, power, bad_a * 3) - assert_raises(ValueError, random.power, bad_a * 3) - - def test_laplace(self): - loc = [0] - scale = [1] - bad_scale = [-1] - laplace = random.laplace - desired = np.array( - [0.067921356028507157, 0.070715642226971326, 0.019290950698972624] - ) - - self.set_seed() - actual = laplace(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, laplace, loc * 3, bad_scale) - - self.set_seed() - actual = laplace(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, laplace, loc, bad_scale * 3) - - def test_gumbel(self): - loc = [0] - scale = [1] - bad_scale = [-1] - gumbel = random.gumbel - desired = np.array( - [0.2730318639556768, 0.26936705726291116, 0.33906220393037939] - ) - - self.set_seed() - actual = gumbel(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gumbel, loc * 3, bad_scale) - - self.set_seed() - actual = gumbel(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, gumbel, loc, bad_scale * 3) - - def test_logistic(self): - loc = [0] - scale = [1] - bad_scale = [-1] - logistic = random.logistic - desired = np.array( - [0.13152135837586171, 0.13675915696285773, 0.038216792802833396] - ) - - self.set_seed() - actual = logistic(loc * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, logistic, loc * 3, bad_scale) - - self.set_seed() - actual = logistic(loc, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, logistic, loc, bad_scale * 3) - assert_equal(random.logistic(1.0, 0.0), 1.0) - - def test_lognormal(self): - mean = [0] - sigma = [1] - bad_sigma = [-1] - lognormal = random.lognormal - desired = np.array([9.1422086044848427, 8.4013952870126261, 6.3073234116578671]) - - self.set_seed() - actual = lognormal(mean * 3, sigma) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, lognormal, mean * 3, bad_sigma) - assert_raises(ValueError, random.lognormal, mean * 3, bad_sigma) - - self.set_seed() - actual = lognormal(mean, sigma * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, lognormal, mean, bad_sigma * 3) - assert_raises(ValueError, random.lognormal, mean, bad_sigma * 3) - - def test_rayleigh(self): - scale = [1] - bad_scale = [-1] - rayleigh = random.rayleigh - desired = np.array([1.2337491937897689, 1.2360119924878694, 1.1936818095781789]) - - self.set_seed() - actual = rayleigh(scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, rayleigh, bad_scale * 3) - - def test_wald(self): - mean = [0.5] - scale = [1] - bad_mean = [0] - bad_scale = [-2] - wald = random.wald - desired = np.array( - [0.11873681120271318, 0.12450084820795027, 0.9096122728408238] - ) - - self.set_seed() - actual = wald(mean * 3, scale) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, wald, bad_mean * 3, scale) - assert_raises(ValueError, wald, mean * 3, bad_scale) - assert_raises(ValueError, random.wald, bad_mean * 3, scale) - assert_raises(ValueError, random.wald, mean * 3, bad_scale) - - self.set_seed() - actual = wald(mean, scale * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, wald, bad_mean, scale * 3) - assert_raises(ValueError, wald, mean, bad_scale * 3) - assert_raises(ValueError, wald, 0.0, 1) - assert_raises(ValueError, wald, 0.5, 0.0) - - def test_triangular(self): - left = [1] - right = [3] - mode = [2] - bad_left_one = [3] - bad_mode_one = [4] - bad_left_two, bad_mode_two = right * 2 - triangular = random.triangular - desired = np.array([2.03339048710429, 2.0347400359389356, 2.0095991069536208]) - - self.set_seed() - actual = triangular(left * 3, mode, right) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one * 3, mode, right) - assert_raises(ValueError, triangular, left * 3, bad_mode_one, right) - assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two, right) - - self.set_seed() - actual = triangular(left, mode * 3, right) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one, mode * 3, right) - assert_raises(ValueError, triangular, left, bad_mode_one * 3, right) - assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3, right) - - self.set_seed() - actual = triangular(left, mode, right * 3) - assert_array_almost_equal(actual, desired, decimal=14) - assert_raises(ValueError, triangular, bad_left_one, mode, right * 3) - assert_raises(ValueError, triangular, left, bad_mode_one, right * 3) - assert_raises(ValueError, triangular, bad_left_two, bad_mode_two, right * 3) - - assert_raises(ValueError, triangular, 10.0, 0.0, 20.0) - assert_raises(ValueError, triangular, 10.0, 25.0, 20.0) - assert_raises(ValueError, triangular, 10.0, 10.0, 10.0) - - def test_binomial(self): - n = [1] - p = [0.5] - bad_n = [-1] - bad_p_one = [-1] - bad_p_two = [1.5] - binom = random.binomial - desired = np.array([1, 1, 1]) - - self.set_seed() - actual = binom(n * 3, p) - assert_array_equal(actual, desired) - self.set_seed() - actual = binom(n * 3, p, size=(3,)) - assert_array_equal(actual, desired) - assert_raises(ValueError, binom, bad_n * 3, p) - assert_raises(ValueError, binom, n * 3, bad_p_one) - assert_raises(ValueError, binom, n * 3, bad_p_two) - - self.set_seed() - actual = binom(n, p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, binom, bad_n, p * 3) - assert_raises(ValueError, binom, n, bad_p_one * 3) - assert_raises(ValueError, binom, n, bad_p_two * 3) - - def test_negative_binomial(self): - n = [1] - p = [0.5] - bad_n = [-1] - bad_p_one = [-1] - bad_p_two = [1.5] - neg_binom = random.negative_binomial - desired = np.array([1, 0, 1]) - - self.set_seed() - actual = neg_binom(n * 3, p) - assert_array_equal(actual, desired) - assert_raises(ValueError, neg_binom, bad_n * 3, p) - assert_raises(ValueError, neg_binom, n * 3, bad_p_one) - assert_raises(ValueError, neg_binom, n * 3, bad_p_two) - - self.set_seed() - actual = neg_binom(n, p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, neg_binom, bad_n, p * 3) - assert_raises(ValueError, neg_binom, n, bad_p_one * 3) - assert_raises(ValueError, neg_binom, n, bad_p_two * 3) - - def test_poisson(self): - max_lam = random.RandomState()._poisson_lam_max - - lam = [1] - bad_lam_one = [-1] - bad_lam_two = [max_lam * 2] - poisson = random.poisson - desired = np.array([1, 1, 0]) - - self.set_seed() - actual = poisson(lam * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, poisson, bad_lam_one * 3) - assert_raises(ValueError, poisson, bad_lam_two * 3) - - def test_zipf(self): - a = [2] - bad_a = [0] - zipf = random.zipf - desired = np.array([2, 2, 1]) - - self.set_seed() - actual = zipf(a * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, zipf, bad_a * 3) - with np.errstate(invalid="ignore"): - assert_raises(ValueError, zipf, np.nan) - assert_raises(ValueError, zipf, [0, 0, np.nan]) - - def test_geometric(self): - p = [0.5] - bad_p_one = [-1] - bad_p_two = [1.5] - geom = random.geometric - desired = np.array([2, 2, 2]) - - self.set_seed() - actual = geom(p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, geom, bad_p_one * 3) - assert_raises(ValueError, geom, bad_p_two * 3) - - def test_hypergeometric(self): - ngood = [1] - nbad = [2] - nsample = [2] - bad_ngood = [-1] - bad_nbad = [-2] - bad_nsample_one = [0] - bad_nsample_two = [4] - hypergeom = random.hypergeometric - desired = np.array([1, 1, 1]) - - self.set_seed() - actual = hypergeom(ngood * 3, nbad, nsample) - assert_array_equal(actual, desired) - assert_raises(ValueError, hypergeom, bad_ngood * 3, nbad, nsample) - assert_raises(ValueError, hypergeom, ngood * 3, bad_nbad, nsample) - assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_one) - assert_raises(ValueError, hypergeom, ngood * 3, nbad, bad_nsample_two) - - self.set_seed() - actual = hypergeom(ngood, nbad * 3, nsample) - assert_array_equal(actual, desired) - assert_raises(ValueError, hypergeom, bad_ngood, nbad * 3, nsample) - assert_raises(ValueError, hypergeom, ngood, bad_nbad * 3, nsample) - assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_one) - assert_raises(ValueError, hypergeom, ngood, nbad * 3, bad_nsample_two) - - self.set_seed() - actual = hypergeom(ngood, nbad, nsample * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3) - assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3) - assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3) - assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3) - - assert_raises(ValueError, hypergeom, -1, 10, 20) - assert_raises(ValueError, hypergeom, 10, -1, 20) - assert_raises(ValueError, hypergeom, 10, 10, 0) - assert_raises(ValueError, hypergeom, 10, 10, 25) - - def test_logseries(self): - p = [0.5] - bad_p_one = [2] - bad_p_two = [-1] - logseries = random.logseries - desired = np.array([1, 1, 1]) - - self.set_seed() - actual = logseries(p * 3) - assert_array_equal(actual, desired) - assert_raises(ValueError, logseries, bad_p_one * 3) - assert_raises(ValueError, logseries, bad_p_two * 3) - - -class TestThread(object): - # make sure each state produces the same sequence even in threads - def setup(self): - self.seeds = range(4) - - def check_function(self, function, sz): - from threading import Thread - - out1 = np.empty((len(self.seeds),) + sz) - out2 = np.empty((len(self.seeds),) + sz) - - # threaded generation - t = [ - Thread(target=function, args=(random.RandomState(s), o)) - for s, o in zip(self.seeds, out1) - ] - [x.start() for x in t] - [x.join() for x in t] - - # the same serial - for s, o in zip(self.seeds, out2): - function(random.RandomState(s), o) - - # these platforms change x87 fpu precision mode in threads - if np.intp().dtype.itemsize == 4 and sys.platform == "win32": - assert_array_almost_equal(out1, out2) - else: - assert_array_equal(out1, out2) - - def test_normal(self): - def gen_random(state, out): - out[...] = state.normal(size=10000) - - self.check_function(gen_random, sz=(10000,)) - - def test_exp(self): - def gen_random(state, out): - out[...] = state.exponential(scale=np.ones((100, 1000))) - - self.check_function(gen_random, sz=(100, 1000)) - - def test_multinomial(self): - def gen_random(state, out): - out[...] = state.multinomial(10, [1 / 6.0] * 6, size=10000) - - self.check_function(gen_random, sz=(10000, 6)) - - -# See Issue #4263 -class TestSingleEltArrayInput(object): - def setup(self): - self.argOne = np.array([2]) - self.argTwo = np.array([3]) - self.argThree = np.array([4]) - self.tgtShape = (1,) - - def test_one_arg_funcs(self): - funcs = ( - random.exponential, - random.standard_gamma, - random.chisquare, - random.standard_t, - random.pareto, - random.weibull, - random.power, - random.rayleigh, - random.poisson, - random.zipf, - random.geometric, - random.logseries, - ) - - probfuncs = (random.geometric, random.logseries) - - for func in funcs: - if func in probfuncs: # p < 1.0 - out = func(np.array([0.5])) - - else: - out = func(self.argOne) - - assert_equal(out.shape, self.tgtShape) - - def test_two_arg_funcs(self): - funcs = ( - random.uniform, - random.normal, - random.beta, - random.gamma, - random.f, - random.noncentral_chisquare, - random.vonmises, - random.laplace, - random.gumbel, - random.logistic, - random.lognormal, - random.wald, - random.binomial, - random.negative_binomial, - ) - - probfuncs = (random.binomial, random.negative_binomial) - - for func in funcs: - if func in probfuncs: # p <= 1 - argTwo = np.array([0.5]) - - else: - argTwo = self.argTwo - - out = func(self.argOne, argTwo) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne[0], argTwo) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne, argTwo[0]) - assert_equal(out.shape, self.tgtShape) - - def test_three_arg_funcs(self): - funcs = [random.noncentral_f, random.triangular, random.hypergeometric] - - for func in funcs: - out = func(self.argOne, self.argTwo, self.argThree) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne[0], self.argTwo, self.argThree) - assert_equal(out.shape, self.tgtShape) - - out = func(self.argOne, self.argTwo[0], self.argThree) - assert_equal(out.shape, self.tgtShape) - - -# Ensure returned array dtype is corect for platform -def test_integer_dtype(int_func): - random.seed(123456789) - fname, args, md5 = int_func - f = getattr(random, fname) - actual = f(*args, size=2) - assert_(actual.dtype == np.dtype("l")) - - -def test_integer_repeat(int_func): - random.seed(123456789) - fname, args, md5 = int_func - f = getattr(random, fname) - val = f(*args, size=1000000) - if sys.byteorder != "little": - val = val.byteswap() - res = hashlib.md5(val.view(np.int8)).hexdigest() - assert_(res == md5) - - -def test_broadcast_size_error(): - # GH-16833 - with pytest.raises(ValueError): - random.binomial(1, [0.3, 0.7], size=(2, 1)) - with pytest.raises(ValueError): - random.binomial([1, 2], 0.3, size=(2, 1)) - with pytest.raises(ValueError): - random.binomial([1, 2], [0.3, 0.7], size=(2, 1)) - - -def test_aliases(): - mtrand = randomgen.mtrand - assert isinstance(mtrand.random_sample(), float) - assert isinstance(mtrand.sample(), float) - assert isinstance(mtrand.ranf(), float) diff --git a/randomgen/tests/test_randomstate_regression.py b/randomgen/tests/test_randomstate_regression.py deleted file mode 100644 index eb19921a8..000000000 --- a/randomgen/tests/test_randomstate_regression.py +++ /dev/null @@ -1,182 +0,0 @@ -import sys - -import numpy as np -from numpy.testing import assert_, assert_array_equal, assert_raises -import pytest - -from randomgen import mtrand as random - -HAS_32BIT_CLONG = np.iinfo("l").max < 2**32 - - -class TestRegression(object): - def test_VonMises_range(self): - # Make sure generated random variables are in [-pi, pi]. - # Regression test for ticket #986. - for mu in np.linspace(-7.0, 7.0, 5): - r = random.vonmises(mu, 1, 50) - assert_(np.all(r > -np.pi) and np.all(r <= np.pi)) - - def test_hypergeometric_range(self): - # Test for ticket #921 - assert_(np.all(random.hypergeometric(3, 18, 11, size=10) < 4)) - assert_(np.all(random.hypergeometric(18, 3, 11, size=10) > 0)) - - # Test for ticket #5623 - args = [ - (2**20 - 2, 2**20 - 2, 2**20 - 2), # Check for 32-bit systems - ] - is_64bits = sys.maxsize > 2**32 - if is_64bits and sys.platform != "win32": - # Check for 64-bit systems - args.append((2**40 - 2, 2**40 - 2, 2**40 - 2)) - for arg in args: - assert_(random.hypergeometric(*arg) > 0) - - def test_logseries_convergence(self): - # Test for ticket #923 - N = 1000 - random.seed(0) - rvsn = random.logseries(0.8, size=N) - # these two frequency counts should be close to theoretical - # numbers with this large sample - # theoretical large N result is 0.49706795 - freq = np.sum(rvsn == 1) / float(N) - msg = "Frequency was %f, should be > 0.45" % freq - assert_(freq > 0.45, msg) - # theoretical large N result is 0.19882718 - freq = np.sum(rvsn == 2) / float(N) - msg = "Frequency was %f, should be < 0.23" % freq - assert_(freq < 0.23, msg) - - def test_shuffle_mixed_dimension(self): - # Test for trac ticket #2074 - for t in [ - [1, 2, 3, None], - [(1, 1), (2, 2), (3, 3), None], - [1, (2, 2), (3, 3), None], - [(1, 1), 2, 3, None], - ]: - random.seed(12345) - shuffled = np.array(list(t), dtype=object) - random.shuffle(shuffled) - assert_array_equal( - shuffled, np.array([t[0], t[3], t[1], t[2]], dtype=object) - ) - - def test_call_within_randomstate(self): - # Check that custom RandomState does not call into global state - m = random.RandomState() - res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3]) - for i in range(3): - random.seed(i) - m.seed(4321) - # If m.state is not honored, the result will change - assert_array_equal(m.choice(10, size=10, p=np.ones(10) / 10.0), res) - - def test_multivariate_normal_size_types(self): - # Test for multivariate_normal issue with "size" argument. - # Check that the multivariate_normal size argument can be a - # numpy integer. - random.multivariate_normal([0], [[0]], size=1) - random.multivariate_normal([0], [[0]], size=np.int_(1)) - random.multivariate_normal([0], [[0]], size=np.int64(1)) - - def test_beta_small_parameters(self): - # Test that beta with small a and b parameters does not produce - # NaNs due to roundoff errors causing 0 / 0, gh-5851 - random.seed(1234567890) - x = random.beta(0.0001, 0.0001, size=100) - assert_(not np.any(np.isnan(x)), "Nans in random.beta") - - def test_choice_sum_of_probs_tolerance(self): - # The sum of probs should be 1.0 with some tolerance. - # For low precision dtypes the tolerance was too tight. - # See numpy github issue 6123. - random.seed(1234) - a = [1, 2, 3] - counts = [4, 4, 2] - for dt in np.float16, np.float32, np.float64: - probs = np.array(counts, dtype=dt) / sum(counts) - c = random.choice(a, p=probs) - assert_(c in a) - assert_raises(ValueError, random.choice, a, p=probs * 0.9) - - def test_shuffle_of_array_of_different_length_strings(self): - # Test that permuting an array of different length strings - # will not cause a segfault on garbage collection - # Tests gh-7710 - random.seed(1234) - - a = np.array(["a", "a" * 1000]) - - for _ in range(100): - random.shuffle(a) - - # Force Garbage Collection - should not segfault. - import gc - - gc.collect() - - def test_shuffle_of_array_of_objects(self): - # Test that permuting an array of objects will not cause - # a segfault on garbage collection. - # See gh-7719 - random.seed(1234) - a = np.array([np.arange(1), np.arange(4)], dtype=object) - - for _ in range(1000): - random.shuffle(a) - - # Force Garbage Collection - should not segfault. - import gc - - gc.collect() - - def test_permutation_subclass(self): - class N(np.ndarray): - pass - - random.seed(1) - orig = np.arange(3).view(N) - perm = random.permutation(orig) - assert_array_equal(perm, np.array([0, 2, 1])) - assert_array_equal(orig, np.arange(3).view(N)) - - class M(object): - a = np.arange(5) - - def __array__(self): - return self.a - - random.seed(1) - m = M() - perm = random.permutation(m) - assert_array_equal(perm, np.array([2, 1, 4, 0, 3])) - assert_array_equal(m.__array__(), np.arange(5)) - - def test_warns_byteorder(self): - other_byteord_dt = "i4" - with pytest.warns(FutureWarning): - random.randint(0, 200, size=10, dtype=other_byteord_dt) - - @pytest.mark.skipif(HAS_32BIT_CLONG, reason="Cannot test with 32-bit C long") - def test_randint_117(self): - random.seed(0) - expected = np.array( - [ - 2357136044, - 2546248239, - 3071714933, - 3626093760, - 2588848963, - 3684848379, - 2340255427, - 3638918503, - 1819583497, - 2678185683, - ], - dtype="int64", - ) - actual = random.randint(2**32, size=10) - assert_array_equal(actual, expected) diff --git a/randomgen/tests/test_recent_numpy_changes.py b/randomgen/tests/test_recent_numpy_changes.py deleted file mode 100644 index 01a30c733..000000000 --- a/randomgen/tests/test_recent_numpy_changes.py +++ /dev/null @@ -1,141 +0,0 @@ -from typing import cast - -import numpy as np -from numpy.testing import assert_equal -from packaging.version import parse -import pytest - -from randomgen import Generator - -v119 = parse("1.19") -NP_LT_119 = parse(np.__version__) < v119 - - -pytestmark = pytest.mark.skipif(NP_LT_119, reason="Only test NumPy 1.19+") - - -# Catch when using internal MT19937 - - -@pytest.fixture(scope="function") -def random(): - import randomgen.common - - return Generator(cast(randomgen.common.BitGenerator, np.random.MT19937(1234))) - - -@pytest.mark.parametrize( - "bound, expected", - [ - ( - 2**32 - 1, - np.array( - [ - 517043486, - 1364798665, - 1733884389, - 1353720612, - 3769704066, - 1170797179, - 4108474671, - ] - ), - ), - ( - 2**32, - np.array( - [ - 517043487, - 1364798666, - 1733884390, - 1353720613, - 3769704067, - 1170797180, - 4108474672, - ] - ), - ), - ( - 2**32 + 1, - np.array( - [ - 517043487, - 1733884390, - 3769704068, - 4108474673, - 1831631863, - 1215661561, - 3869512430, - ] - ), - ), - ], -) -def test_repeatability_32bit_boundary(random, bound, expected): - state = random.state - for size in [None, len(expected)]: - random.state = state - x = random.integers(bound, size=size, use_masked=False) - assert_equal(x, expected if size is not None else expected[0]) - - -def test_dirichelet_alpha(random): - # numpy/numpy#15951 - with pytest.raises(ValueError): - random.dirichlet([[5, 1]]) - with pytest.raises(ValueError): - random.dirichlet([[5], [1]]) - with pytest.raises(ValueError): - random.dirichlet([[[5], [1]], [[1], [5]]]) - with pytest.raises(ValueError): - random.dirichlet(np.array([[5, 1], [1, 5]])) - - -def test_negative_binomial_p0_exception(random): - # numpy/numpy#15913 - # Verify that p=0 raises an exception. - with pytest.raises(ValueError): - random.negative_binomial(1, 0) - - -def test_multivariate_normal_basic_stats(random): - # numpy/numpy#15871 - n_s = 1000 - mean = np.array([1, 2]) - cov = np.array([[2, 1], [1, 2]]) - s = random.multivariate_normal(mean, cov, size=(n_s,)) - s_center = s - mean - cov_emp = (s_center.T @ s_center) / (n_s - 1) - # these are pretty loose and are only designed to detect major errors - assert np.all(np.abs(s_center.mean(-2)) < 0.1) - assert np.all(np.abs(cov_emp - cov) < 0.2) - - -# chi2max is the maximum acceptable chi-squared value. -@pytest.mark.parametrize( - "sample_size,high,dtype,chi2max", - [ - (5000000, 5, np.int8, 125.0), # p-value ~4.6e-25 - (5000000, 7, np.uint8, 150.0), # p-value ~7.7e-30 - (10000000, 2500, np.int16, 3300.0), # p-value ~3.0e-25 - (50000000, 5000, np.uint16, 6500.0), # p-value ~3.5e-25 - ], -) -def test_integers_small_dtype_chisquared(random, sample_size, high, dtype, chi2max): - # Regression test for gh-14774. - samples = random.integers(high, size=sample_size, dtype=dtype) - - values, counts = np.unique(samples, return_counts=True) - expected = sample_size / high - chi2 = ((counts - expected) ** 2 / expected).sum() - assert chi2 < chi2max - - -def test_bad_permuation(random): - bad_x_str = "abcd" - with pytest.raises(IndexError): - random.permutation(bad_x_str) - - bad_x_float = 1.2 - with pytest.raises(IndexError): - random.permutation(bad_x_float) diff --git a/randomgen/tests/test_sfc.py b/randomgen/tests/test_sfc.py index 248aac1d5..ed8b3260d 100644 --- a/randomgen/tests/test_sfc.py +++ b/randomgen/tests/test_sfc.py @@ -7,10 +7,10 @@ def test_known(): sfc = SFC64(SeedSequence(0)) weyl = sfc.weyl_increments(2) - expected = np.array([6524879303493105881, 12838133130686035807], dtype=np.uint64) + expected = np.array([6524879303493105881, 17467897594175157085], dtype=np.uint64) np.testing.assert_equal(weyl, expected) weyl = sfc.weyl_increments(2, 48, 16) - expected = np.array([8826510519078708777, 232634225573672851], dtype=np.uint64) + expected = np.array([18331436834911646537, 1349527966119344023], dtype=np.uint64) np.testing.assert_equal(weyl, expected) diff --git a/randomgen/tests/test_smoke.py b/randomgen/tests/test_smoke.py index c72241bb5..df9b80ebf 100644 --- a/randomgen/tests/test_smoke.py +++ b/randomgen/tests/test_smoke.py @@ -29,7 +29,6 @@ SPECK128, AESCounter, ChaCha, - Generator, LCG128Mix, Philox, Romu, @@ -146,7 +145,7 @@ def setup_class(cls): cls.bit_generator = Xoshiro256 cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -161,9 +160,9 @@ def _extra_setup(cls): def init_generator(self, seed=None, mode="sequence"): if seed is not None: - return Generator(self.bit_generator(*seed, mode=mode)) + return np.random.Generator(self.bit_generator(*seed, mode=mode)) else: - return Generator(self.bit_generator(seed=seed, mode=mode)) + return np.random.Generator(self.bit_generator(seed=seed, mode=mode)) def _reset_state(self): self.rg.bit_generator.state = self.initial_state @@ -215,7 +214,7 @@ def test_jumped(self): new_bit_gen = self.rg.bit_generator.jumped() assert isinstance(new_bit_gen, self.rg.bit_generator.__class__) assert_(comp_state(state, self.rg.bit_generator.state)) - Generator(new_bit_gen).random(1000000) + np.random.Generator(new_bit_gen).random(1000000) else: bit_gen_name = self.rg.bit_generator.__class__.__name__ pytest.skip("jumped is not supported by {0}".format(bit_gen_name)) @@ -389,22 +388,6 @@ def test_permutation(self): permuted = self.rg.permutation(original) assert_((original != permuted).any()) - def test_tomaxint(self): - with pytest.deprecated_call(): - vals = self.rg.tomaxint(size=100000) - maxsize = 0 - if os.name == "nt": - maxsize = 2**31 - 1 - else: - try: - maxsize = sys.maxint - except AttributeError: - maxsize = sys.maxsize - if maxsize < 2**32: - assert_((vals < sys.maxsize).all()) - else: - assert_((vals >= 2**32).any()) - def test_beta(self): vals = self.rg.beta(2.0, 2.0, 10) assert_(len(vals) == 10) @@ -464,40 +447,6 @@ def test_negative_binomial(self): vals = self.rg.negative_binomial(10, 0.2, 10) assert_(len(vals) == 10) - def test_rand(self): - state = self.rg.bit_generator.state - with pytest.deprecated_call(): - vals = self.rg.rand(10, 10, 10) - self.rg.bit_generator.state = state - assert_((vals == self.rg.random((10, 10, 10))).all()) - assert_(vals.shape == (10, 10, 10)) - with pytest.deprecated_call(): - vals = self.rg.rand(10, 10, 10, dtype=np.float32) - assert_(vals.shape == (10, 10, 10)) - - def test_randn(self): - state = self.rg.bit_generator.state - with pytest.deprecated_call(): - vals = self.rg.randn(10, 10, 10) - self.rg.bit_generator.state = state - vals2 = self.rg.standard_normal((10, 10, 10)) - assert_equal(vals, vals2) - assert_equal(vals.shape, (10, 10, 10)) - - state = self.rg.bit_generator.state - with pytest.deprecated_call(): - vals = self.rg.randn(10, 10, 10) - self.rg.bit_generator.state = state - assert_equal(vals, self.rg.standard_normal((10, 10, 10))) - - state = self.rg.bit_generator.state - with pytest.deprecated_call(): - self.rg.randn(10, 10, 10) - self.rg.bit_generator.state = state - with pytest.deprecated_call(): - vals = self.rg.randn(10, 10, 10, dtype=np.float32) - assert_(vals.shape == (10, 10, 10)) - def test_noncentral_chisquare(self): vals = self.rg.noncentral_chisquare(10, 2, 10) assert_(len(vals) == 10) @@ -538,7 +487,7 @@ def test_integers(self): def test_random_integers(self): with suppress_warnings() as sup: sup.record(DeprecationWarning) - vals = self.rg.random_integers(10, 20, 10) + vals = self.rg.integers(10, 20, 10) assert_(len(vals) == 10) def test_rayleigh(self): @@ -601,6 +550,7 @@ def test_dirichlet(self): s = self.rg.dirichlet((10, 5, 3), 20) assert_(s.shape == (20, 3)) + @pytest.mark.skip(reason="Doesn't work since can't register bit generators") def test_pickle(self): pick = pickle.dumps(self.rg) unpick = pickle.loads(pick) @@ -921,81 +871,6 @@ def test_integers_broadcast_errors(self, dtype): with pytest.raises(ValueError): self.rg.integers([0], [0], dtype=dtype) - def test_complex_normal(self): - st = self.rg.bit_generator.state - vals = self.rg.complex_normal(2.0 + 7.0j, 10.0, 5.0 - 5.0j, size=10) - assert_(len(vals) == 10) - - self.rg.bit_generator.state = st - vals2 = [ - self.rg.complex_normal(2.0 + 7.0j, 10.0, 5.0 - 5.0j) for _ in range(10) - ] - np.testing.assert_allclose(vals, vals2) - - self.rg.bit_generator.state = st - vals3 = self.rg.complex_normal( - 2.0 + 7.0j * np.ones(10), 10.0 * np.ones(1), 5.0 - 5.0j - ) - np.testing.assert_allclose(vals, vals3) - - self.rg.bit_generator.state = st - norms = self.rg.standard_normal(size=20) - norms = np.reshape(norms, (10, 2)) - cov = 0.5 * (-5.0) - v_real = 7.5 - v_imag = 2.5 - rho = cov / np.sqrt(v_real * v_imag) - imag = 7 + np.sqrt(v_imag) * ( - rho * norms[:, 0] + np.sqrt(1 - rho**2) * norms[:, 1] - ) - real = 2 + np.sqrt(v_real) * norms[:, 0] - vals4 = [re + im * (0 + 1.0j) for re, im in zip(real, imag)] - - np.testing.assert_allclose(vals4, vals) - - def test_complex_normal_bm(self): - st = self.rg.bit_generator.state - vals = self.rg.complex_normal(2.0 + 7.0j, 10.0, 5.0 - 5.0j, size=10) - assert_(len(vals) == 10) - - self.rg.bit_generator.state = st - vals2 = [ - self.rg.complex_normal(2.0 + 7.0j, 10.0, 5.0 - 5.0j) for _ in range(10) - ] - np.testing.assert_allclose(vals, vals2) - - self.rg.bit_generator.state = st - vals3 = self.rg.complex_normal( - 2.0 + 7.0j * np.ones(10), 10.0 * np.ones(1), 5.0 - 5.0j - ) - np.testing.assert_allclose(vals, vals3) - - def test_complex_normal_zero_variance(self): - st = self.rg.bit_generator.state - c = self.rg.complex_normal(0, 1.0, 1.0) - assert_almost_equal(c.imag, 0.0) - self.rg.bit_generator.state = st - n = self.rg.standard_normal() - np.testing.assert_allclose(c, n, atol=1e-8) - - st = self.rg.bit_generator.state - c = self.rg.complex_normal(0, 1.0, -1.0) - assert_almost_equal(c.real, 0.0) - self.rg.bit_generator.state = st - self.rg.standard_normal() - n = self.rg.standard_normal() - assert_almost_equal(c.real, 0.0) - np.testing.assert_allclose(c.imag, n, atol=1e-8) - - def test_random_uintegers(self): - assert len(self.rg.uintegers(10)) == 10 - assert len(self.rg.uintegers(10, bits=32)) == 10 - assert isinstance(self.rg.uintegers(), int) - assert isinstance(self.rg.uintegers(bits=32), int) - with pytest.raises(ValueError): - with pytest.deprecated_call(): - self.rg.random_uintegers(bits=128) - def test_bit_generator_raw_large(self): bg = self.rg.bit_generator state = bg.state @@ -1030,7 +905,7 @@ def setup_class(cls): cls.bit_generator = MT19937 cls.advance = None cls.seed = [2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 32 cls._extra_setup() @@ -1053,7 +928,7 @@ def setup_class(cls): cls.bit_generator = MT64 cls.advance = None cls.seed = [2**43 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1067,7 +942,7 @@ def setup_class(cls): cls.bit_generator = partial(JSF, seed_size=3) cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1080,7 +955,7 @@ def setup_class(cls): cls.bit_generator = partial(JSF, size=32, seed_size=3) cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1096,7 +971,7 @@ def setup_class(cls): 2**96 + 2**48 + 2**21 + 2**16 + 2**5 + 1, 2**21 + 2**16 + 2**5 + 1, ] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = None cls._extra_setup() @@ -1134,7 +1009,7 @@ class TestPCG64VariantDXSM(TestPCG64): def setup_class(cls): super().setup_class() cls.bit_generator = partial(PCG64, variant="dxsm-128") - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls._extra_setup() @@ -1144,7 +1019,7 @@ class TestPCG64CMDXSM(TestPCG64): def setup_class(cls): super().setup_class() cls.bit_generator = partial(PCG64, variant="dxsm") - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls._extra_setup() @@ -1159,7 +1034,7 @@ def setup_class(cls): cls.bit_generator = partial(Philox, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls.max_vector_seed_size = 1 @@ -1185,7 +1060,7 @@ def setup_class(cls): cls.bit_generator = partial(Philox, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls.max_vector_seed_size = 1 @@ -1201,7 +1076,7 @@ def setup_class(cls): cls.bit_generator = partial(Philox, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1216,7 +1091,7 @@ def setup_class(cls): cls.bit_generator = partial(Philox, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1232,7 +1107,7 @@ def setup_class(cls): cls.bit_generator = partial(ThreeFry, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1247,7 +1122,7 @@ def setup_class(cls): cls.bit_generator = partial(ThreeFry, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1262,7 +1137,7 @@ def setup_class(cls): cls.bit_generator = partial(ThreeFry, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1277,7 +1152,7 @@ def setup_class(cls): cls.bit_generator = partial(ThreeFry, number=cls.number, width=cls.width) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1290,7 +1165,7 @@ def setup_class(cls): cls.bit_generator = Xoroshiro128 cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1301,7 +1176,7 @@ class TestXoroshiro128PlusPlus(RNG): def setup_class(cls): super().setup_class() cls.bit_generator = partial(Xoroshiro128, plusplus=True) - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls._extra_setup() @@ -1313,7 +1188,7 @@ def setup_class(cls): cls.bit_generator = Xoshiro256 cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1326,7 +1201,7 @@ def setup_class(cls): cls.bit_generator = Xoshiro512 cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1339,7 +1214,7 @@ def setup_class(cls): cls.bit_generator = Xorshift1024 cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1352,7 +1227,7 @@ def setup_class(cls): cls.bit_generator = DSFMT cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls._extra_setup() cls.seed_vector_bits = 32 @@ -1365,7 +1240,7 @@ def setup_class(cls): cls.bit_generator = SFMT cls.advance = None cls.seed = [12345] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls._extra_setup() cls.seed_vector_bits = 32 @@ -1400,7 +1275,7 @@ def setup_class(cls): 2**48 + 2**21 + 2**16 + 2**5 + 1, 2**21 + 2**16 + 2**5 + 1, ] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = None cls._extra_setup() @@ -1413,7 +1288,7 @@ def setup_class(cls): cls.bit_generator = AESCounter cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1428,7 +1303,7 @@ def setup_class(cls): cls.bit_generator = ChaCha cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.seed = [2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1441,7 +1316,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = HC128 cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 cls._extra_setup() @@ -1454,7 +1329,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = SPECK128 cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed, mode="legacy")) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed, mode="legacy")) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 @@ -1468,7 +1343,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = LXM cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed)) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 @@ -1477,7 +1352,7 @@ def setup_class(cls): cls.out_of_bounds = 2**192 + 1 def init_generator(self, seed=None, mode="sequence"): - return Generator(self.bit_generator(seed=seed)) + return np.random.Generator(self.bit_generator(seed=seed)) class TestLCG128Mix(RNG): @@ -1486,7 +1361,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = LCG128Mix cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed)) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 @@ -1495,7 +1370,7 @@ def setup_class(cls): cls.out_of_bounds = 2**192 + 1 def init_generator(self, seed=None, mode="sequence"): - return Generator(self.bit_generator(seed=seed)) + return np.random.Generator(self.bit_generator(seed=seed)) class TestPCG64DXSM(TestLCG128Mix): @@ -1504,7 +1379,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = PCG64DXSM cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed)) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 @@ -1519,7 +1394,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = EFIIX64 cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed)) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 @@ -1534,7 +1409,7 @@ def setup_class(cls): super().setup_class() cls.bit_generator = Romu cls.seed = [2**231 + 2**21 + 2**16 + 2**5 + 1] - cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.rg = np.random.Generator(cls.bit_generator(*cls.seed)) cls.advance = 2**63 + 2**31 + 2**15 + 1 cls.initial_state = cls.rg.bit_generator.state cls.seed_vector_bits = 64 diff --git a/randomgen/tests/test_wrapper.py b/randomgen/tests/test_wrapper.py index f8d71ea1a..b3308a574 100644 --- a/randomgen/tests/test_wrapper.py +++ b/randomgen/tests/test_wrapper.py @@ -1,7 +1,7 @@ import numpy as np +from numpy.random import Generator import pytest -from randomgen.generator import Generator from randomgen.pcg64 import PCG64 from randomgen.wrapper import UserBitGenerator diff --git a/randomgen/tests/test_wrapper_numba.py b/randomgen/tests/test_wrapper_numba.py index 4a56381f2..d08a07d72 100644 --- a/randomgen/tests/test_wrapper_numba.py +++ b/randomgen/tests/test_wrapper_numba.py @@ -1,9 +1,9 @@ import ctypes import numpy as np +from numpy.random import Generator import pytest -from randomgen.generator import Generator from randomgen.wrapper import UserBitGenerator HAS_NUMBA = False diff --git a/randomgen/threefry.pyx b/randomgen/threefry.pyx index 358430d94..3bc4ff5fd 100644 --- a/randomgen/threefry.pyx +++ b/randomgen/threefry.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np from randomgen.common cimport * diff --git a/randomgen/wrapper.pyx b/randomgen/wrapper.pyx index bcfdb83a1..c63a238a7 100644 --- a/randomgen/wrapper.pyx +++ b/randomgen/wrapper.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + from randomgen.common cimport * from randomgen.distributions cimport next_uint64_t, next_uint32_t, next_double_t import ctypes diff --git a/randomgen/xoroshiro128.pyx b/randomgen/xoroshiro128.pyx index bed9f7df8..cf1e93072 100644 --- a/randomgen/xoroshiro128.pyx +++ b/randomgen/xoroshiro128.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/xorshift1024.pyx b/randomgen/xorshift1024.pyx index b82604772..da189afca 100644 --- a/randomgen/xorshift1024.pyx +++ b/randomgen/xorshift1024.pyx @@ -1,4 +1,6 @@ -# coding=utf-8 +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/xoshiro256.pyx b/randomgen/xoshiro256.pyx index 77998690a..b71fd15ca 100644 --- a/randomgen/xoshiro256.pyx +++ b/randomgen/xoshiro256.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/randomgen/xoshiro512.pyx b/randomgen/xoshiro512.pyx index ed527001b..958eba45b 100644 --- a/randomgen/xoshiro512.pyx +++ b/randomgen/xoshiro512.pyx @@ -1,3 +1,6 @@ +#!python +#cython: binding=True + import numpy as np cimport numpy as np diff --git a/setup.py b/setup.py index b8ecb59e8..b39e3d8d5 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,8 @@ from setuptools import Distribution, find_packages, setup from setuptools.extension import Extension -from distutils.version import LooseVersion +from packaging.version import parse + import glob import io import os @@ -93,7 +94,7 @@ def src_join(*fname): EXTRA_LINK_ARGS += ["-g"] UNDEF_MACROS += ["NDEBUG"] -if Cython.__version__ >= LooseVersion("0.29"): +if parse(Cython.__version__) >= parse("0.29"): DEFS = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")] else: DEFS = [("NPY_NO_DEPRECATED_API", "0")] @@ -102,7 +103,7 @@ def src_join(*fname): DEFS += [("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")] PCG64_DEFS = DEFS[:] -if sys.maxsize < 2 ** 32 or os.name == "nt": +if sys.maxsize < 2**32 or os.name == "nt": # Force emulated mode here PCG64_DEFS += [("PCG_FORCE_EMULATED_128BIT_MATH", "1")] diff --git a/tools/practrand-driver.py b/tools/practrand-driver.py index f077bd7ea..c100c72c0 100644 --- a/tools/practrand-driver.py +++ b/tools/practrand-driver.py @@ -40,7 +40,7 @@ import randomgen as rg -BUFFER_SIZE = 256 * 2 ** 20 +BUFFER_SIZE = 256 * 2**20 DESCRIPTION = """ A driver that simplifies testing bit generators using PractRand. @@ -146,7 +146,7 @@ def jumped_state(bit_generator, n_streams=2, entropy=None): if config["seed"] == 64: entropy = entropy[0] elif config["seed_size"] == 128: - entropy = int(entropy[0]) + int(entropy[1]) * 2 ** 64 + entropy = int(entropy[0]) + int(entropy[1]) * 2**64 elif config["seed_size"] == 256: base = int(0) for i in range(4): @@ -159,13 +159,13 @@ def jumped_state(bit_generator, n_streams=2, entropy=None): if seed_size in (32, 64): _entropy = [] while entropy > 0: - low = entropy % 2 ** seed_size + low = entropy % 2**seed_size _entropy.append(low) entropy = entropy >> seed_size dtype = np.uint32 if seed_size == 32 else np.uint64 entropy = np.array(_entropy, dtype=dtype) elif seed_size in (128, 256): - entropy = entropy % 2 ** seed_size + entropy = entropy % 2**seed_size else: raise NotImplementedError diff --git a/tools/process-prng-tester-results.py b/tools/process-prng-tester-results.py index 1d367a947..9546c4d2e 100644 --- a/tools/process-prng-tester-results.py +++ b/tools/process-prng-tester-results.py @@ -33,18 +33,18 @@ def parse_key(key): def to_bytes(s): if "MB" in s: - return int(s[:-2].strip()) * 2 ** 20 + return int(s[:-2].strip()) * 2**20 elif "GB" in s: - return int(s[:-2].strip()) * 2 ** 30 + return int(s[:-2].strip()) * 2**30 elif "TB" in s: - return int(s[:-2].strip()) * 2 ** 40 + return int(s[:-2].strip()) * 2**40 def from_bytes(b): b = b >> 20 - if b >= 2 ** 20: + if b >= 2**20: return f"{b>>20}TB" - elif b >= 2 ** 10: + elif b >= 2**10: return f"{b>>10}GB" return f"{b}MB" From f1fe2246fc7a6007d7a458a5acc676a76b799816 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 09:16:48 +0100 Subject: [PATCH 02/14] MAINT: Improve typing and restore mypy defs --- randomgen/generator.pyi | 84 +++++++++++++++++++++ randomgen/src/mt19937/generate-jump-test.py | 28 +++---- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/randomgen/generator.pyi b/randomgen/generator.pyi index b254739ba..e93fa3ea8 100644 --- a/randomgen/generator.pyi +++ b/randomgen/generator.pyi @@ -11,6 +11,90 @@ class Generator: ... def __init__(self, bit_generator: Optional[BitGenerator] = ...) -> None: ... +class ExtendedGenerator: + _bit_generator: BitGenerator + lock: Lock + _generator: Generator + def __init__(self, bit_generator: Optional[BitGenerator] = ...) -> None: ... + @property + def bit_generator(self) -> BitGenerator: ... + @property + def state(self) -> Dict[str, Any]: ... + @state.setter + def state(self, value: Dict[str, Any]) -> None: ... + @overload + def uintegers(self, size: None, bits: Literal[32, 64] = ...) -> int: ... + @overload + def uintegers(self, size: RequiredSize, bits: Literal[32, 64] = ...) -> ndarray: ... + @overload + def random(self) -> float: ... # type: ignore[misc] + @overload + def random(self, size: None = ...) -> float: ... # type: ignore[misc] + @overload + def random( + self, size: Size = ..., dtype: str = ..., out: Optional[ndarray] = ... + ) -> ndarray: ... + # Multivariate distributions: + def multivariate_normal( + self, + mean: ndarray, + cov: ndarray, + size: Size = ..., + check_valid: Literal["raise", "ignore", "warn"] = ..., + tol: float = ..., + *, + method: Literal["svd", "eigh", "cholesky", "factor"] = ... + ) -> ndarray: ... + @overload + def complex_normal( # type: ignore[misc] + self, + loc: complex = ..., + gamma: complex = ..., + relation: complex = ..., + ) -> complex: ... + @overload + def complex_normal( # type: ignore[misc] + self, + loc: complex = ..., + gamma: complex = ..., + relation: complex = ..., + size: RequiredSize = ..., + ) -> ndarray: ... + @overload + def complex_normal( + self, + loc: Union[complex, ndarray] = ..., + gamma: Union[complex, ndarray] = ..., + relation: Union[complex, ndarray] = ..., + size: Size = ..., + ) -> ndarray: ... + def standard_wishart( + self, df: int, dim: int, size: Size = ..., *, rescale: bool = ... + ) -> ndarray: ... + def wishart( + self, + df: Union[int, ndarray], + scale: ndarray, + size: Size = ..., + *, + check_valid: Literal["raise", "ignore", "warn"] = ..., + tol: float = ..., + rank: Optional[int] = ..., + method: Literal["svd", "eigh", "cholesky", "factor"] = ... + ) -> ndarray: ... + def multivariate_complex_normal( + self, + loc: ndarray, + gamma: Optional[ndarray] = ..., + relation: Optional[ndarray] = ..., + size: Size = ..., + *, + check_valid: Literal["raise", "ignore", "warn"] = ..., + tol: float = ..., + method: Literal["svd", "eigh", "cholesky", "factor"] = ... + ) -> ndarray: ... + + def _raises_not_implemented(*args: Any, **kwargs: Any) -> None: ... beta = _raises_not_implemented diff --git a/randomgen/src/mt19937/generate-jump-test.py b/randomgen/src/mt19937/generate-jump-test.py index 9534a1ff2..910389f9a 100644 --- a/randomgen/src/mt19937/generate-jump-test.py +++ b/randomgen/src/mt19937/generate-jump-test.py @@ -18,7 +18,7 @@ import pprint import shutil import subprocess -from typing import Dict, List, Tuple, Union +from typing import Dict, List, Tuple, Union, cast import black import numpy as np @@ -35,33 +35,33 @@ def save_state(bit_gen: MT19937, file_name: str) -> None: - state = bit_gen.state - key = state["state"]["key"] - pos = state["state"]["pos"] + bit_gen_state = cast(dict[str, Union[int, np.ndarray]], bit_gen.state["state"]) + state_key = cast(np.ndarray, bit_gen_state["key"]) + state_pos = bit_gen_state["pos"] with open(file_name, "w") as f: - for k in key: + for k in state_key: f.write(f"{k}\n") - f.write(f"{pos}\n") + f.write(f"{state_pos}\n") def parse_output(text: str) -> Tuple[List[Dict[str, Union[List, int]]], List[int]]: lines = text.split("\n") - - state = {"key": [], "pos": -1} - states = [state] + key_list : list[int] = [] + output_state = {"key": key_list, "pos": -1} + states = [output_state] pf = [] for line in lines: parts = line.split(":") if "pf[" in parts[0]: pf.append(int(parts[1].strip())) elif "[" in parts[0]: - state["key"].append(int(parts[1].strip())) + output_state["key"].append(int(parts[1].strip())) elif ".ptr" in parts[0]: - state["pos"] = int(parts[1].strip()) + output_state["pos"] = int(parts[1].strip()) elif "=====" in line: - state["key"] = np.asarray(state["key"], dtype="uint32") - state = {"key": [], "pos": -1} - states.append(state) + output_state["key"] = np.asarray(output_state["key"], dtype="uint32") + output_state = {"key": [], "pos": -1} + states.append(output_state) return states[:-1], pf From fc40b6d881c91a4e2386e19d5c1c3c265a79cfe2 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 10:35:40 +0100 Subject: [PATCH 03/14] MAINT: Clean pyi files --- randomgen/_seed_sequence.pyi | 4 ++-- randomgen/aes.pyi | 2 +- randomgen/chacha.pyi | 2 +- randomgen/common.pyi | 2 +- randomgen/dsfmt.pyi | 11 +++-------- randomgen/generator.pyi | 16 +++++----------- randomgen/jsf.pyi | 2 -- randomgen/pcg32.pyi | 2 -- randomgen/pcg64.pyi | 11 +++++------ randomgen/rdrand.pyi | 2 +- randomgen/src/mt19937/generate-jump-test.py | 2 +- randomgen/wrapper.pyi | 4 ++-- randomgen/xoshiro256.pyi | 4 +--- randomgen/xoshiro512.pyi | 4 +--- 14 files changed, 24 insertions(+), 44 deletions(-) diff --git a/randomgen/_seed_sequence.pyi b/randomgen/_seed_sequence.pyi index 020a27b27..7445b277a 100644 --- a/randomgen/_seed_sequence.pyi +++ b/randomgen/_seed_sequence.pyi @@ -11,7 +11,7 @@ class ISeedSequence(metaclass=ABCMeta): self, n_words: int, dtype: Type[unsignedinteger[Any]] = ... ) -> Sequence[int]: ... -class ISpawnableSeedSequence(ISeedSequence): +class ISpawnableSeedSequence(ISeedSequence, metaclass=ABCMeta): @abstractmethod def spawn(self, n_children: int) -> List["SeedSequence"]: ... @@ -20,7 +20,7 @@ class SeedSequence(ISpawnableSeedSequence): self, entropy: Optional[Union[int, Sequence[int]]] = ..., *, - spawn_key: Sequence[int] = (), + spawn_key: Sequence[int] = ..., pool_size: int = ..., n_children_spawned: int = ... ) -> None: ... diff --git a/randomgen/aes.pyi b/randomgen/aes.pyi index 9bc5e0d07..7888f0d20 100644 --- a/randomgen/aes.pyi +++ b/randomgen/aes.pyi @@ -13,7 +13,7 @@ class AESCounter(BitGenerator): *, counter: Optional[Union[int, Sequence[int]]] = ..., key: Optional[Union[int, Sequence[int]]] = ..., - mode: Optional[SeedMode] = ..., + mode: Optional[SeedMode] = ... ) -> None: ... @property def use_aesni(self) -> bool: ... diff --git a/randomgen/chacha.pyi b/randomgen/chacha.pyi index 8ccd33ce0..4264962be 100644 --- a/randomgen/chacha.pyi +++ b/randomgen/chacha.pyi @@ -13,7 +13,7 @@ class ChaCha(BitGenerator): counter: Optional[Union[int, Sequence[int]]] = ..., key: Optional[Union[int, Sequence[int]]] = ..., rounds: int = ..., - mode: Optional[SeedMode] = ..., + mode: Optional[SeedMode] = ... ) -> None: ... @property def use_simd(self) -> bool: ... diff --git a/randomgen/common.pyi b/randomgen/common.pyi index 46482f7e1..4023e99f1 100644 --- a/randomgen/common.pyi +++ b/randomgen/common.pyi @@ -19,7 +19,7 @@ class BitGenerator: self, seed: Union[IntegerSequenceSeed] = ..., mode: Optional[SeedMode] = ..., - ): ... + ) -> None: ... def random_raw( self, size: Optional[int] = ..., output: bool = ... ) -> Union[None, int, ndarray]: ... diff --git a/randomgen/dsfmt.pyi b/randomgen/dsfmt.pyi index 2d9fd77df..0476315e7 100644 --- a/randomgen/dsfmt.pyi +++ b/randomgen/dsfmt.pyi @@ -12,17 +12,12 @@ class DSFMT(BitGenerator): self, seed: Optional[IntegerSequenceSeed] = ..., *, - mode: Optional[SeedMode] = ..., + mode: Optional[SeedMode] = ... ) -> None: ... def seed(self, seed: Union[int, Sequence[int]] = ...) -> None: ... def jump(self, iter: int = ...) -> DSFMT: ... def jumped(self, iter: int = ...) -> DSFMT: ... @property - def state( - self, - ) -> DSFMTState: ... + def state(self) -> DSFMTState: ... @state.setter - def state( - self, - value: DSFMTState, - ) -> None: ... + def state(self, value: DSFMTState) -> None: ... diff --git a/randomgen/generator.pyi b/randomgen/generator.pyi index e93fa3ea8..d7ad1468a 100644 --- a/randomgen/generator.pyi +++ b/randomgen/generator.pyi @@ -1,7 +1,6 @@ from threading import Lock from typing import Any, Dict, Literal, Optional, Sequence, Tuple, Union, overload -import numpy as np from numpy import ndarray from randomgen.common import BitGenerator @@ -27,14 +26,13 @@ class ExtendedGenerator: @overload def uintegers(self, size: RequiredSize, bits: Literal[32, 64] = ...) -> ndarray: ... @overload - def random(self) -> float: ... # type: ignore[misc] + def random(self) -> float: ... @overload - def random(self, size: None = ...) -> float: ... # type: ignore[misc] + def random(self, size: None = ...) -> float: ... @overload def random( self, size: Size = ..., dtype: str = ..., out: Optional[ndarray] = ... ) -> ndarray: ... - # Multivariate distributions: def multivariate_normal( self, mean: ndarray, @@ -46,14 +44,11 @@ class ExtendedGenerator: method: Literal["svd", "eigh", "cholesky", "factor"] = ... ) -> ndarray: ... @overload - def complex_normal( # type: ignore[misc] - self, - loc: complex = ..., - gamma: complex = ..., - relation: complex = ..., + def complex_normal( + self, loc: complex = ..., gamma: complex = ..., relation: complex = ... ) -> complex: ... @overload - def complex_normal( # type: ignore[misc] + def complex_normal( self, loc: complex = ..., gamma: complex = ..., @@ -94,7 +89,6 @@ class ExtendedGenerator: method: Literal["svd", "eigh", "cholesky", "factor"] = ... ) -> ndarray: ... - def _raises_not_implemented(*args: Any, **kwargs: Any) -> None: ... beta = _raises_not_implemented diff --git a/randomgen/jsf.pyi b/randomgen/jsf.pyi index ab7dc5d27..f247ec3a4 100644 --- a/randomgen/jsf.pyi +++ b/randomgen/jsf.pyi @@ -1,7 +1,5 @@ from typing import Dict, Optional, Union -import numpy as np - from randomgen.common import BitGenerator from randomgen.typing import IntegerSequenceSeed, SeedMode diff --git a/randomgen/pcg32.pyi b/randomgen/pcg32.pyi index a967dd591..abd81470e 100644 --- a/randomgen/pcg32.pyi +++ b/randomgen/pcg32.pyi @@ -1,7 +1,5 @@ from typing import Dict, Optional, Union -import numpy as np - from randomgen.common import BitGenerator from randomgen.typing import IntegerSequenceSeed, SeedMode diff --git a/randomgen/pcg64.pyi b/randomgen/pcg64.pyi index 1bd58420b..0831fdb10 100644 --- a/randomgen/pcg64.pyi +++ b/randomgen/pcg64.pyi @@ -1,12 +1,11 @@ -import numpy as np - -DEFAULT_MULTIPLIER: int -DEFAULT_DXSM_MULTIPLIER: int from typing import Dict, Optional, Union from randomgen.common import BitGenerator from randomgen.typing import IntegerSequenceSeed, Literal, SeedMode +DEFAULT_MULTIPLIER: int +DEFAULT_DXSM_MULTIPLIER: int + class PCG64(BitGenerator): def __init__( self, @@ -19,7 +18,7 @@ class PCG64(BitGenerator): mode: Optional[SeedMode] = ... ) -> None: ... def seed( - self, seed: Optional[IntegerSequenceSeed] = ..., inc: Optional[int] = -999999 + self, seed: Optional[IntegerSequenceSeed] = ..., inc: Optional[int] = ... ) -> None: ... @property def state(self) -> Dict[str, Union[str, int, Dict[str, int]]]: ... @@ -55,7 +54,7 @@ class LCG128Mix(BitGenerator): class PCG64DXSM(PCG64): def __init__( self, seed: Optional[IntegerSequenceSeed] = ..., inc: Optional[int] = ... - ): ... + ) -> None: ... @property def state(self) -> Dict[str, Union[str, int, Dict[str, int]]]: ... @state.setter diff --git a/randomgen/rdrand.pyi b/randomgen/rdrand.pyi index 8e52c2e60..7099db6db 100644 --- a/randomgen/rdrand.pyi +++ b/randomgen/rdrand.pyi @@ -8,7 +8,7 @@ from randomgen.common import BitGenerator class RaisingLock: lock: Lock - def acquire(self, blocking: bool = ..., timeout: int = -1) -> None: ... + def acquire(self, blocking: bool = ..., timeout: int = ...) -> None: ... def release(self) -> None: ... def __enter__(self) -> None: ... def __exit__( diff --git a/randomgen/src/mt19937/generate-jump-test.py b/randomgen/src/mt19937/generate-jump-test.py index 910389f9a..a6fff749f 100644 --- a/randomgen/src/mt19937/generate-jump-test.py +++ b/randomgen/src/mt19937/generate-jump-test.py @@ -46,7 +46,7 @@ def save_state(bit_gen: MT19937, file_name: str) -> None: def parse_output(text: str) -> Tuple[List[Dict[str, Union[List, int]]], List[int]]: lines = text.split("\n") - key_list : list[int] = [] + key_list: list[int] = [] output_state = {"key": key_list, "pos": -1} states = [output_state] pf = [] diff --git a/randomgen/wrapper.pyi b/randomgen/wrapper.pyi index d122bbd4b..f998d16e0 100644 --- a/randomgen/wrapper.pyi +++ b/randomgen/wrapper.pyi @@ -1,4 +1,4 @@ -from ctypes import CFUNCTYPE, c_void_p +from ctypes import c_void_p from typing import Any, Callable, Literal, Optional from numba.core.ccallback import CFunc @@ -16,7 +16,7 @@ class UserBitGenerator(BitGenerator): state: Optional[int] = ..., state_getter: Optional[Callable[[], Any]] = ..., state_setter: Optional[Callable[[Any], None]] = ..., - ): ... + ) -> None: ... @property def state(self) -> Any: ... @state.setter diff --git a/randomgen/xoshiro256.pyi b/randomgen/xoshiro256.pyi index 224d87afd..52203b2af 100644 --- a/randomgen/xoshiro256.pyi +++ b/randomgen/xoshiro256.pyi @@ -13,8 +13,6 @@ class Xoshiro256(BitGenerator): def jump(self, iter: int = ...) -> Xoshiro256: ... def jumped(self, iter: int = ...) -> Xoshiro256: ... @property - def state( - self, - ) -> Dict[str, Union[str, np.ndarray, int]]: ... + def state(self) -> Dict[str, Union[str, np.ndarray, int]]: ... @state.setter def state(self, value: Dict[str, Union[str, np.ndarray, int]]) -> None: ... diff --git a/randomgen/xoshiro512.pyi b/randomgen/xoshiro512.pyi index 0de3a93f2..acee72bc4 100644 --- a/randomgen/xoshiro512.pyi +++ b/randomgen/xoshiro512.pyi @@ -13,8 +13,6 @@ class Xoshiro512(BitGenerator): def jump(self, iter: int = ...) -> Xoshiro512: ... def jumped(self, iter: int = ...) -> Xoshiro512: ... @property - def state( - self, - ) -> Dict[str, Union[str, np.ndarray, int]]: ... + def state(self) -> Dict[str, Union[str, np.ndarray, int]]: ... @state.setter def state(self, value: Dict[str, Union[str, np.ndarray, int]]) -> None: ... From 40995aa4064f55a23ee630315a2ea5ce883e12c4 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 10:37:30 +0100 Subject: [PATCH 04/14] MAINT: Further clean ups --- randomgen/tests/test_generator_mt19937.py | 2 +- randomgen/tests/test_smoke.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/randomgen/tests/test_generator_mt19937.py b/randomgen/tests/test_generator_mt19937.py index fe4b9b608..b1e2fa647 100644 --- a/randomgen/tests/test_generator_mt19937.py +++ b/randomgen/tests/test_generator_mt19937.py @@ -2,7 +2,7 @@ import sys import numpy as np -from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_raises +from numpy.testing import assert_raises from packaging.version import parse import pytest diff --git a/randomgen/tests/test_smoke.py b/randomgen/tests/test_smoke.py index df9b80ebf..ac7ca06aa 100644 --- a/randomgen/tests/test_smoke.py +++ b/randomgen/tests/test_smoke.py @@ -1,13 +1,10 @@ from functools import partial -import os import pickle -import sys import time import numpy as np from numpy.testing import ( assert_, - assert_almost_equal, assert_array_equal, assert_equal, suppress_warnings, From 5bf965837b86b9aec0838c5a18893b2a2dda85bc Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 10:49:22 +0100 Subject: [PATCH 05/14] MAINT: Fix imports --- randomgen/tests/test_smoke.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/randomgen/tests/test_smoke.py b/randomgen/tests/test_smoke.py index ac7ca06aa..e1e61708b 100644 --- a/randomgen/tests/test_smoke.py +++ b/randomgen/tests/test_smoke.py @@ -3,12 +3,7 @@ import time import numpy as np -from numpy.testing import ( - assert_, - assert_array_equal, - assert_equal, - suppress_warnings, -) +from numpy.testing import assert_, assert_array_equal, assert_equal, suppress_warnings import pytest from randomgen import ( From 0bbb513f7b18502e9c72b55f5f198986000954be Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 10:55:35 +0100 Subject: [PATCH 06/14] MAINT: Fix imports --- randomgen/generator.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/randomgen/generator.pyx b/randomgen/generator.pyx index 8cfe6166f..d9e38c06c 100644 --- a/randomgen/generator.pyx +++ b/randomgen/generator.pyx @@ -1244,7 +1244,7 @@ and the trailing dimensions must match exactly so that return out.view(complex) -def _removed(name: str) -> Callable[[Any,...],None]: +def _removed(name: str) -> Callable[[Any, Any],None]: def f(*args, **kwargs): raise NotImplementedError( f"{name} has been removed. Use NumPy's Generator" From 6f31521e563aa5e99476c101cca3ef91422e52d2 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 11:12:00 +0100 Subject: [PATCH 07/14] MAINT: Improve typing --- randomgen/generator.pyi | 48 +++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/randomgen/generator.pyi b/randomgen/generator.pyi index d7ad1468a..1db7862c0 100644 --- a/randomgen/generator.pyi +++ b/randomgen/generator.pyi @@ -28,10 +28,10 @@ class ExtendedGenerator: @overload def random(self) -> float: ... @overload - def random(self, size: None = ...) -> float: ... + def random(self, size: None) -> float: ... @overload def random( - self, size: Size = ..., dtype: str = ..., out: Optional[ndarray] = ... + self, size: RequiredSize, dtype: str = ..., out: Optional[ndarray] = ... ) -> ndarray: ... def multivariate_normal( self, @@ -44,24 +44,50 @@ class ExtendedGenerator: method: Literal["svd", "eigh", "cholesky", "factor"] = ... ) -> ndarray: ... @overload + def complex_normal(self, loc: complex) -> complex: ... + @overload + def complex_normal(self, loc: complex, gamma: complex) -> complex: ... + @overload + def complex_normal(self, loc: complex, *, relation: complex) -> complex: ... + @overload + def complex_normal(self, *, gamma: complex, relation: complex) -> complex: ... + @overload def complex_normal( - self, loc: complex = ..., gamma: complex = ..., relation: complex = ... + self, loc: complex, gamma: complex, relation: complex ) -> complex: ... @overload + def complex_normal(self, loc: ndarray) -> ndarray: ... + @overload + def complex_normal(self, *, gamma: ndarray) -> ndarray: ... + @overload + def complex_normal( + self, loc: Union[complex, ndarray], gamma: ndarray + ) -> ndarray: ... + @overload + def complex_normal( + self, *, gamma: ndarray, relation: Union[complex, ndarray] + ) -> ndarray: ... + @overload + def complex_normal( + self, loc: Union[complex, ndarray], *, relation: ndarray + ) -> ndarray: ... + @overload def complex_normal( self, - loc: complex = ..., - gamma: complex = ..., - relation: complex = ..., - size: RequiredSize = ..., + loc: Union[complex, ndarray], + gamma: Union[complex, ndarray], + *, + relation: ndarray ) -> ndarray: ... @overload + def complex_normal(self, *, relation: ndarray) -> ndarray: ... + @overload def complex_normal( self, - loc: Union[complex, ndarray] = ..., - gamma: Union[complex, ndarray] = ..., - relation: Union[complex, ndarray] = ..., - size: Size = ..., + loc: Union[complex, ndarray], + gamma: Union[complex, ndarray], + relation: Union[complex, ndarray], + size: RequiredSize, ) -> ndarray: ... def standard_wishart( self, df: int, dim: int, size: Size = ..., *, rescale: bool = ... From 2fde4918caadfb043eacf234a3d7a722ad386055 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 11:14:38 +0100 Subject: [PATCH 08/14] MAINT: Update mac vmimage --- ci/azure/azure_template_posix.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/azure/azure_template_posix.yml b/ci/azure/azure_template_posix.yml index cd5067bd1..304befc44 100644 --- a/ci/azure/azure_template_posix.yml +++ b/ci/azure/azure_template_posix.yml @@ -54,6 +54,7 @@ jobs: ${{ if eq(parameters.name, 'macOS') }}: python39_latest_macos: python.version: '3.9' + vmImage: macOS-latest maxParallel: 10 From ffaf3ee93d961bdb227669564154e1349c5cfc39 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 11:20:31 +0100 Subject: [PATCH 09/14] MAINT: Update OS to latest --- azure-pipelines.yml | 6 +++--- ci/azure/azure_template_posix.yml | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index c49f9de0f..8cf68d9e1 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -21,14 +21,14 @@ jobs: - template: ci/azure/azure_template_posix.yml parameters: name: macOS - vmImage: macOS-10.15 + vmImage: macOS-latest - template: ci/azure/azure_template_posix.yml parameters: name: Linux - vmImage: ubuntu-20.04 + vmImage: ubuntu-latest - template: ci/azure/azure_template_windows.yml parameters: name: Windows - vmImage: windows-2019 + vmImage: windows-latest diff --git a/ci/azure/azure_template_posix.yml b/ci/azure/azure_template_posix.yml index 304befc44..cd5067bd1 100644 --- a/ci/azure/azure_template_posix.yml +++ b/ci/azure/azure_template_posix.yml @@ -54,7 +54,6 @@ jobs: ${{ if eq(parameters.name, 'macOS') }}: python39_latest_macos: python.version: '3.9' - vmImage: macOS-latest maxParallel: 10 From 4aa511e4919f6feba2f8721a22b85c8dac9b90de Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 11:30:01 +0100 Subject: [PATCH 10/14] TST: Improve test accuracy --- randomgen/tests/test_direct.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/randomgen/tests/test_direct.py b/randomgen/tests/test_direct.py index 3e421a602..9c102e0dd 100644 --- a/randomgen/tests/test_direct.py +++ b/randomgen/tests/test_direct.py @@ -120,19 +120,19 @@ def uniform32_from_uint64(x): lower = np.uint64(0xFFFFFFFF) lower = np.array(x & lower, dtype=np.uint32) joined = np.column_stack([lower, upper]).ravel() - out = (joined >> np.uint32(8)) * (1.0 / 2**24) + out = (joined >> np.uint32(8)) * (np.float32(1.0) / np.float32(2**24)) return out.astype(np.float32) def uniform32_from_uint53(x): x = np.uint64(x) >> np.uint64(16) x = np.uint32(x & np.uint64(0xFFFFFFFF)) - out = (x >> np.uint32(8)) * (1.0 / 2**24) + out = (x >> np.uint32(8)) * (np.float32(1.0) / np.float32(2**24)) return out.astype(np.float32) def uniform32_from_uint32(x): - return (x >> np.uint32(8)) * (1.0 / 2**24) + return (x >> np.uint32(8)) * (np.float32(1.0) / np.float32(2**24)) def uniform32_from_uint(x, bits): From 186fd0c3d88fe205627d7afe701f1b07c707b540 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 11:39:28 +0100 Subject: [PATCH 11/14] TST: Relax tol a bit --- randomgen/tests/test_direct.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/randomgen/tests/test_direct.py b/randomgen/tests/test_direct.py index 9c102e0dd..886e579be 100644 --- a/randomgen/tests/test_direct.py +++ b/randomgen/tests/test_direct.py @@ -285,13 +285,13 @@ def test_uniform_float(self): rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) vals = uniform32_from_uint(self.data1["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) - assert_allclose(uniforms, vals) + assert_allclose(uniforms, vals, atol=1e-7) assert_equal(uniforms.dtype, np.float32) rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) vals = uniform32_from_uint(self.data2["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) - assert_allclose(uniforms, vals) + assert_allclose(uniforms, vals, atol=1e-7) assert_equal(uniforms.dtype, np.float32) def test_seed_float(self): @@ -1270,13 +1270,13 @@ def test_uniform_float(self): rs = np.random.Generator(self.setup_bitgenerator(self.data1["seed"])) vals = uniform32_from_uint(self.data1["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) - assert_allclose(uniforms, vals) + assert_allclose(uniforms, vals, atol=1e-7) assert_equal(uniforms.dtype, np.float32) rs = np.random.Generator(self.setup_bitgenerator(self.data2["seed"])) vals = uniform32_from_uint(self.data2["data"], self.bits) uniforms = rs.random(len(vals), dtype=np.float32) - assert_allclose(uniforms, vals) + assert_allclose(uniforms, vals, atol=1e-7) assert_equal(uniforms.dtype, np.float32) def test_buffer_reset(self): From f312dde6c3cb0462b874be7546a1e122735463f7 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 12:03:52 +0100 Subject: [PATCH 12/14] DOC: Update docs --- appveyor.yml | 4 - benchmark.py | 3 +- doc/source/custom-bit-generators.ipynb | 92 ++++++++++++++---- legacy-travis.yml | 125 ------------------------- randomgen/aes.pyx | 6 +- randomgen/chacha.pyx | 6 +- randomgen/dsfmt.pyx | 3 +- randomgen/efiix64.pyx | 3 +- randomgen/generator.pyx | 12 +-- randomgen/hc128.pyx | 6 +- randomgen/jsf.pyx | 3 +- randomgen/lxm.pyx | 6 +- randomgen/mt19937.pyx | 3 +- randomgen/pcg32.pyx | 2 + randomgen/pcg64.pyx | 5 +- randomgen/philox.pyx | 6 +- randomgen/rdrand.pyx | 6 +- randomgen/speck128.pyx | 6 +- randomgen/threefry.pyx | 6 +- randomgen/xoroshiro128.pyx | 6 +- randomgen/xorshift1024.pyx | 6 +- randomgen/xoshiro256.pyx | 6 +- randomgen/xoshiro512.pyx | 6 +- 23 files changed, 146 insertions(+), 181 deletions(-) delete mode 100644 legacy-travis.yml diff --git a/appveyor.yml b/appveyor.yml index f47e0fe13..7b76a647f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -28,7 +28,3 @@ build_script: test_script: - pytest randomgen - -on_success: - - cd %GIT_DIR%\ - - python benchmark.py diff --git a/benchmark.py b/benchmark.py index 226469c99..d70d626f7 100644 --- a/benchmark.py +++ b/benchmark.py @@ -15,7 +15,8 @@ rg = numpy.random.RandomState() rg.random_sample() else: - from randomgen import Generator, {bitgen} + from randomgen import {bitgen} + from numpy.random import Generator rg = Generator({bitgen}()) rg.random() """ diff --git a/doc/source/custom-bit-generators.ipynb b/doc/source/custom-bit-generators.ipynb index 29dea4457..e883bede1 100644 --- a/doc/source/custom-bit-generators.ipynb +++ b/doc/source/custom-bit-generators.ipynb @@ -252,7 +252,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Accessing `python_pcg.state` would raise `NotImplementedError`. It is possible to\n", "wire up this function by setting `state_setter` and `state_getter` in `UserBitGenerator`.\n", @@ -299,7 +303,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "### Performance\n", "We can time `random_raw` to see how fast (**slow**) the pure python version is. It is about 3 orders-of-magnitude (1000x) slower than the C implementation." @@ -334,7 +342,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "## Using numba\n", "\n", @@ -528,7 +540,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "We start by instantizing the class and taking a look at the initial state." ] @@ -572,7 +588,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "`from_cfunc` is then used to pass the `CFunc`s, state address pointer and the state getter and setter to `UserBitGenerator`. We see that the state changes after calling `random_raw`." ] @@ -619,7 +639,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Some `Generator` function use 32-bit integers to save bits. `random` with `dtype=np.float32` is one. After calling this function we see that `has_uint` is now 1." ] @@ -669,7 +693,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "### Performance \n", "We can use `random_raw` function to assess the performance and compare it to the C-implementation ``JSF``. It is about 6% slower which is an impressive outcome." @@ -703,7 +731,11 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "name": "stdout", @@ -722,7 +754,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Next, we will clone the state of the native ``JSF`` to the numba implementation." ] @@ -730,7 +766,11 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -761,7 +801,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "While the structure of the state is different, the values are the same: ``[a, b, c, d]``." ] @@ -769,7 +813,11 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -798,7 +846,11 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, "source": [ "Finally, we can take a look at the next few values to show that the implementations of the two generators are identical." ] @@ -806,7 +858,11 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -827,7 +883,11 @@ { "cell_type": "code", "execution_count": 15, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -867,4 +927,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/legacy-travis.yml b/legacy-travis.yml deleted file mode 100644 index 782e25c49..000000000 --- a/legacy-travis.yml +++ /dev/null @@ -1,125 +0,0 @@ -dist: bionic -sudo: required -language: python - -addons: - apt: - packages: - - pandoc - -env: - global: - # Doctr deploy key for bashtage/randomgen - - secure: "czwFlflS1lcfbSQ9ktv+pLAPV9/6+wmwiMTyIYyv5xgQVWRL5NRebWH+ZhQ6s2T5x17wFMtlafcAvkdV0CHQZLru34V2UNldCapuEtQ8b32EDHBXHKbs45b7SSkLx4TFXdjiJurleY4ZIKle0gX6BW21zYBwaHJqbN6I8nRv9Rp47XEU1UV1Mdf/PhfTnxY31rFrPYL77xeWJzoFfT8zao39V4gQds+1Ag7FjdNVdSDVKwDduF4kS7tIbKqb4M+jsbc3PIKyP9nyQpEQF5ebJuG7mqXJhVJGEL83rBx8MLFPA/1X3cUzKacgKyp2+Wmlt0EVhwCa1aRf9cSK6I7TbMC7/eGtDnC2ToiRlFJurVRblaEmhzVQS1yQ4Dkooqsj9hNVl6nhu7JfR52GLogns33Ec/yYuRcWcULKSlR5Cerfef/5YijBEhlr9X76SJiOpjvS4lwWFYX+h8xzuVhRLGwIVB9oQNllxYItzcDSGmRx+EOMXWASHmoUDnBOZg4GMVukqOcF5l0ynoepiA1YHLdZlMy6SB3P7BZKF/aNCOn9nXw+N9X4U/yUpkM3Pb7HoGdNrC8RO4SwrNjGrarkdEB6e1lBReK/dqcylaF/mpK9VLpfQszDI8xnR4VCmlEM+le0xOsyHfeGciabdI4KH0i0SfYl4ls5XrN+CaqFWdo=" - - PYPI=false - - COVERAGE=true - - RANDOMGEN_DEBUG=true - -cache: - directories: - - $HOME/.cache/pip - -matrix: - fast_finish: true - include: - - os: osx - language: generic - env: [PYTHON=3.6, NUMPY=1.16.6] - - os: linux - arch: ppc64le - python: 3.6 - env: [PYPI=true, PPC64_LE=1, COVERAGE=true, EXTRA_PYTEST_OPTIONS="-v -s"] - - os: linux - arch: s390x - python: 3.6 - env: [PYPI=true, S390X=1, COVERAGE=true] - - os: linux - python: 3.7 - env: [PYTHON=3.7, DOCBUILD=true, RANDOMGEN_DEBUG=false, CC=clang, NUMPY=1.17] - - os: linux - python: 3.7 - env: [PYPI=true, COVERAGE=true, NUMPY=1.16.6] - - os: linux - env: [PYPI=true, PYTHON=3.6, NUMPY=1.18.5, CYTHON=0.29] - - os: linux - python: 3.8 - env: [PYPI=true, COVERAGE=true, NUMPY=1.19.0rc2] - - os: linux - python: 3.8 - env: [PYPI=true, COVERAGE=true, RANDOMGEN_CYTHON_COVERAGE=false, RANDOMGEN_DEBUG=false] - - os: linux - python: 3.7 - env: [PYPI=true, COVERAGE=false, NUMPY=1.17.5, TEST_INSTALL=true] - - os: linux - python: 3.8 - env: [PYPI=true, COVERAGE=false, RANDOMGEN_CYTHON_COVERAGE=false, RANDOMGEN_DEBUG=false, PYPI_PRE=true] - allow_failures: - - os: linux - arch: ppc64le - python: 3.6 - env: [PYPI=true, PPC64_LE=1, COVERAGE=true, EXTRA_PYTEST_OPTIONS="-v -s"] - - os: linux - arch: s390x - python: 3.6 - env: [PYPI=true, S390X=1, COVERAGE=true] - - os: linux - python: 3.8 - env: [PYPI=true, COVERAGE=false, RANDOMGEN_CYTHON_COVERAGE=false, RANDOMGEN_DEBUG=false, PYPI_PRE=true] - -before_install: - - git fetch --tags - - if [[ $PYPI = true ]]; then source ci/install-gcc-8.sh; fi - - if [[ $PYPI = true ]]; then source ci/pypi-install.sh; else source ci/conda-install.sh; fi - - pip install tempita coverage coveralls pytest-cov codecov coveralls - - pip list - - export BUILD_DIR=${PWD} - - if [[ ${DOCBUILD} == true ]]; then pip install sphinx sphinx_rtd_theme sphinx-material ipython doctr nbsphinx -q; fi - - gcc --version || true - - clang --version || true - - export SRCDIR="$PWD" - - if [[ -z "$RANDOMGEN_CYTHON_COVERAGE" ]]; then export RANDOMGEN_CYTHON_COVERAGE="$COVERAGE"; fi - - if [[ "$RANDOMGEN_CYTHON_COVERAGE" == true ]]; then export EXTRA_PYTEST_OPTIONS="${EXTRA_PYTEST_OPTIONS} --skip-slow"; fi - - if [[ "$COVERAGE" == true ]]; then export COVERAGE_OPTIONS="--cov-config .coveragerc --cov=randomgen"; fi - -install: - - | - if [[ -n ${TEST_INSTALL} && ${TEST_INSTALL} == true ]]; then - pip install . -v --no-build-isolation - else - pip install -e . -v --no-build-isolation - fi - -script: - - | - if [[ -n ${TEST_INSTALL} && ${TEST_INSTALL} == true ]]; then - mkdir test_run - cd test_run - python -c 'import randomgen; randomgen.test(extra_args=["--skip-slow", "-n=2"])' - cd .. - else - echo pytest -r a ${COVERAGE_OPTIONS} ${EXTRA_PYTEST_OPTIONS} randomgen/tests/ - pytest -r a ${COVERAGE_OPTIONS} ${EXTRA_PYTEST_OPTIONS} randomgen/tests/ - fi - - | - if [[ ${DOCBUILD} == true ]]; then - sudo apt-get install -y enchant - pip install sphinxcontrib-spelling - cd ${BUILD_DIR}/doc - make html - make html - cd ${BUILD_DIR} - doctr deploy devel --build-tags - if [[ -z ${TRAVIS_TAG} ]]; then - echo "Not a tagged build." - else - doctr deploy . --build-tags - fi - fi - -after_success: - - | - if [[ ${PYPI} == true && -z ${PPC64_LE} && -z ${RANDOMGEN_DEBUG} ]]; then - cd ${BUILD_DIR} - python benchmark.py; - fi - - if [[ "$COVERAGE" = true ]]; then codecov; coveralls --rcfile="$SRCDIR"/.coveragerc || true; fi diff --git a/randomgen/aes.pyx b/randomgen/aes.pyx index adf15ead4..b1126837e 100644 --- a/randomgen/aes.pyx +++ b/randomgen/aes.pyx @@ -97,7 +97,8 @@ cdef class AESCounter(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, AESCounter + >>> from numpy.random import Generator + >>> from randomgen import AESCounter >>> rg = [Generator(AESCounter(1234)) for _ in range(10)] # Advance each AESCounter instances by i jumps >>> for i in range(10): @@ -116,7 +117,8 @@ cdef class AESCounter(BitGenerator): Examples -------- - >>> from randomgen import Generator, AESCounter + >>> from numpy.random import Generator + >>> from randomgen import AESCounter >>> rg = Generator(AESCounter(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/chacha.pyx b/randomgen/chacha.pyx index a8dc5a700..969f44a7e 100644 --- a/randomgen/chacha.pyx +++ b/randomgen/chacha.pyx @@ -104,7 +104,8 @@ cdef class ChaCha(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, ChaCha + >>> from numpy.random import Generator + >>> from randomgen import ChaCha >>> rg = [Generator(ChaCha(1234)) for _ in range(10)] # Advance each ChaCha instances by i jumps >>> for i in range(10): @@ -123,7 +124,8 @@ cdef class ChaCha(BitGenerator): Examples -------- - >>> from randomgen import Generator, ChaCha + >>> from numpy.random import Generator + >>> from randomgen import ChaCha >>> rg = Generator(ChaCha(1234, rounds=8)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/dsfmt.pyx b/randomgen/dsfmt.pyx index 1bf725506..5973ba56c 100644 --- a/randomgen/dsfmt.pyx +++ b/randomgen/dsfmt.pyx @@ -93,8 +93,9 @@ cdef class DSFMT(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. + >>> from numpy.random import Generator >>> from randomgen.entropy import random_entropy - >>> from randomgen import Generator, DSFMT + >>> from randomgen import DSFMT >>> seed = random_entropy() >>> rs = [Generator(DSFMT(seed)) for _ in range(10)] # Advance each DSFMT instance by i jumps diff --git a/randomgen/efiix64.pyx b/randomgen/efiix64.pyx index 45a02b508..970b917f0 100644 --- a/randomgen/efiix64.pyx +++ b/randomgen/efiix64.pyx @@ -79,7 +79,8 @@ cdef class EFIIX64(BitGenerator): Examples -------- - >>> from randomgen import Generator, EFIIX64 + >>> from numpy.random import Generator + >>> from randomgen import EFIIX64 >>> rg = Generator(EFIIX64(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/generator.pyx b/randomgen/generator.pyx index d9e38c06c..13f4dc68d 100644 --- a/randomgen/generator.pyx +++ b/randomgen/generator.pyx @@ -477,10 +477,10 @@ cdef class ExtendedGenerator: Diagonal covariance means that points are oriented along x or y-axis: - >>> from randomgen import Generator - >>> rg = Generator() + >>> from numpy.random import ExtendedGenerator + >>> erg = ExtendedGenerator() >>> import matplotlib.pyplot as plt - >>> x, y = rg.multivariate_normal(mean, cov, 5000).T + >>> x, y = erg.multivariate_normal(mean, cov, 5000).T >>> plt.plot(x, y, 'x') >>> plt.axis('equal') >>> plt.show() @@ -498,11 +498,11 @@ cdef class ExtendedGenerator: Examples -------- - >>> from randomgen import Generator - >>> rg = Generator() + >>> from randomgen import ExtendedGenerator + >>> erg = ExtendedGenerator() >>> mean = (1, 2) >>> cov = [[1, 0], [0, 1]] - >>> x = rg.multivariate_normal(mean, cov, (3, 3)) + >>> x = erg.multivariate_normal(mean, cov, (3, 3)) >>> x.shape (3, 3, 2) diff --git a/randomgen/hc128.pyx b/randomgen/hc128.pyx index 33a25ef81..6f50a1a43 100644 --- a/randomgen/hc128.pyx +++ b/randomgen/hc128.pyx @@ -88,7 +88,8 @@ cdef class HC128(BitGenerator): ``HC128`` can be used in parallel applications by using distinct keys - >>> from randomgen import Generator, HC128 + >>> from numpy.random import Generator + >>> from randomgen import HC128 >>> rg = [Generator(HC128(key=1234 + i)) for i in range(10)] **Compatibility Guarantee** @@ -98,7 +99,8 @@ cdef class HC128(BitGenerator): Examples -------- - >>> from randomgen import Generator, HC128 + >>> from numpy.random import Generator + >>> from randomgen import HC128 >>> rg = Generator(HC128(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/jsf.pyx b/randomgen/jsf.pyx index d438a307b..519febf76 100644 --- a/randomgen/jsf.pyx +++ b/randomgen/jsf.pyx @@ -175,7 +175,8 @@ cdef class JSF(BitGenerator): Examples -------- - >>> from randomgen import Generator, JSF + >>> from numpy.random import Generator + >>> from randomgen import JSF >>> rg = Generator(JSF(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/lxm.pyx b/randomgen/lxm.pyx index 398949155..7abc371e1 100644 --- a/randomgen/lxm.pyx +++ b/randomgen/lxm.pyx @@ -91,7 +91,8 @@ cdef class LXM(BitGenerator): can be used in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, LXM + >>> from numpy.random import Generator + >>> from randomgen import LXM >>> rg = [Generator(LXM(1234))] # Advance each LXM instance by i jumps >>> for i in range(10): @@ -107,7 +108,8 @@ cdef class LXM(BitGenerator): Examples -------- - >>> from randomgen import Generator, LXM + >>> from numpy.random import Generator + >>> from randomgen import LXM >>> rg = Generator(LXM(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/mt19937.pyx b/randomgen/mt19937.pyx index ff6b395dc..6996ffa02 100644 --- a/randomgen/mt19937.pyx +++ b/randomgen/mt19937.pyx @@ -89,8 +89,9 @@ cdef class MT19937(BitGenerator): process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. + >>> from numpy.random import Generator >>> from randomgen.entropy import random_entropy - >>> from randomgen import Generator, MT19937 + >>> from randomgen import MT19937 >>> seed = random_entropy() >>> rs = [Generator(MT19937(seed)) for _ in range(10)] # Advance each MT19937 instance by i jumps diff --git a/randomgen/pcg32.pyx b/randomgen/pcg32.pyx index 3c073dcd2..c1329ef6d 100644 --- a/randomgen/pcg32.pyx +++ b/randomgen/pcg32.pyx @@ -85,6 +85,8 @@ cdef class PCG32(BitGenerator): with a different value in each instance to produce non-overlapping sequences. + >>> from numpy.random import Generator + >>> from randomgen import PCG32 >>> rg = [Generator(PCG32(1234, i + 1)) for i in range(10)] >>> for i in range(10): ... rg[i].bit_generator.advance(i * 2**32) diff --git a/randomgen/pcg64.pyx b/randomgen/pcg64.pyx index 0178546b4..8200bddbb 100644 --- a/randomgen/pcg64.pyx +++ b/randomgen/pcg64.pyx @@ -134,6 +134,8 @@ cdef class PCG64(BitGenerator): ``PCG64`` can be used in parallel applications by calling ``advance`` with a different value on each instance to produce non-overlapping sequences. + >>> from numpy.random import Generator + >>> from randomgen import PCG64 >>> rg = [Generator(PCG64(1234, i + 1)) for i in range(10)] >>> for i in range(10): ... rg[i].bit_generator.advance(i * 2**64) @@ -1029,7 +1031,8 @@ cdef class PCG64DXSM(PCG64): ``PCG64DXSM`` can be used in parallel applications by calling ``advance`` with a different value on each instance to produce non-overlapping sequences. - >>> from randomgen import Generator, PCG64DXSM + >>> from numpy.random import Generator + >>> from randomgen import PCG64DXSM >>> rg = [Generator(PCG64DXSM(1234, i + 1)) for i in range(10)] >>> for i in range(10): ... rg[i].bit_generator.advance(i * 2**64) diff --git a/randomgen/philox.pyx b/randomgen/philox.pyx index 43f6c5104..927a0bf47 100644 --- a/randomgen/philox.pyx +++ b/randomgen/philox.pyx @@ -135,7 +135,8 @@ cdef class Philox(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, Philox + >>> from numpy.random import Generator + >>> from randomgen import Philox >>> rg = [Generator(Philox(1234)) for _ in range(10)] # Advance each Philox instance by i jumps >>> for i in range(10): @@ -154,7 +155,8 @@ cdef class Philox(BitGenerator): Examples -------- - >>> from randomgen import Generator, Philox + >>> from numpy.random import Generator + >>> from randomgen import Philox >>> rg = Generator(Philox(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/rdrand.pyx b/randomgen/rdrand.pyx index 7265739e3..63d5f92a8 100644 --- a/randomgen/rdrand.pyx +++ b/randomgen/rdrand.pyx @@ -158,7 +158,8 @@ cdef class RDRAND(BitGenerator): ``RDRAND`` is stateless and so multiple instances can be used in parallel. - >>> from randomgen import Generator, RDRAND + >>> from numpy.random import Generator + >>> from randomgen import RDRAND >>> rg = [Generator(RDRAND()) for _ in range(10)] **Exceptions** @@ -228,7 +229,8 @@ cdef class RDRAND(BitGenerator): Examples -------- - >>> from randomgen import Generator, RDRAND + >>> from numpy.random import Generator + >>> from randomgen import RDRAND >>> rg = Generator(RDRAND()) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/speck128.pyx b/randomgen/speck128.pyx index a6529e9c7..9d3abecd4 100644 --- a/randomgen/speck128.pyx +++ b/randomgen/speck128.pyx @@ -106,7 +106,8 @@ cdef class SPECK128(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, SPECK128 + >>> from numpy.random import Generator + >>> from randomgen import SPECK128 >>> rg = [Generator(SPECK128(1234)) for _ in range(10)] # Advance each SPECK128 instances by i jumps >>> for i in range(10): @@ -125,7 +126,8 @@ cdef class SPECK128(BitGenerator): Examples -------- - >>> from randomgen import Generator, SPECK128 + >>> from numpy.random import Generator + >>> from randomgen import SPECK128 >>> rg = Generator(SPECK128(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/threefry.pyx b/randomgen/threefry.pyx index 3bc4ff5fd..3d39006bc 100644 --- a/randomgen/threefry.pyx +++ b/randomgen/threefry.pyx @@ -133,7 +133,8 @@ cdef class ThreeFry(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, ThreeFry + >>> from numpy.random import Generator + >>> from randomgen import ThreeFry >>> rg = [Generator(ThreeFry(1234)) for _ in range(10)] # Advance each ThreeFry instance by i jumps >>> for i in range(10): @@ -152,7 +153,8 @@ cdef class ThreeFry(BitGenerator): Examples -------- - >>> from randomgen import Generator, ThreeFry + >>> from numpy.random import Generator + >>> from randomgen import ThreeFry >>> rg = Generator(ThreeFry(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/xoroshiro128.pyx b/randomgen/xoroshiro128.pyx index cf1e93072..8931d5a87 100644 --- a/randomgen/xoroshiro128.pyx +++ b/randomgen/xoroshiro128.pyx @@ -102,7 +102,8 @@ cdef class Xoroshiro128(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, Xoroshiro128 + >>> from numpy.random import Generator + >>> from randomgen import Xoroshiro128 >>> rg = [Generator(Xoroshiro128(1234)) for _ in range(10)] # Advance each Xoroshiro128 instance by i jumps >>> for i in range(10): @@ -117,7 +118,8 @@ cdef class Xoroshiro128(BitGenerator): -------- Using the preferred version Xoroshiro128++ - >>> from randomgen import Generator, Xoroshiro128 + >>> from numpy.random import Generator + >>> from randomgen import Xoroshiro128 >>> rg = Generator(Xoroshiro128(1234, plusplus=True)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/xorshift1024.pyx b/randomgen/xorshift1024.pyx index da189afca..32a484890 100644 --- a/randomgen/xorshift1024.pyx +++ b/randomgen/xorshift1024.pyx @@ -86,7 +86,8 @@ cdef class Xorshift1024(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, Xorshift1024 + >>> from numpy.random import Generator + >>> from randomgen import Xorshift1024 >>> rg = [Generator(Xorshift1024(1234)) for _ in range(10)] # Advance each Xorshift1024 instance by i jumps >>> for i in range(10): @@ -99,7 +100,8 @@ cdef class Xorshift1024(BitGenerator): Examples -------- - >>> from randomgen import Generator, Xorshift1024 + >>> from numpy.random import Generator + >>> from randomgen import Xorshift1024 >>> rg = Generator(Xorshift1024(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/xoshiro256.pyx b/randomgen/xoshiro256.pyx index b71fd15ca..fc39cc7ca 100644 --- a/randomgen/xoshiro256.pyx +++ b/randomgen/xoshiro256.pyx @@ -89,7 +89,8 @@ cdef class Xoshiro256(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, Xoshiro256 + >>> from numpy.random import Generator + >>> from randomgen import Xoshiro256 >>> rg = [Generator(Xoshiro256(1234)) for _ in range(10)] # Advance each Xoshiro256 instance by i jumps >>> for i in range(10): @@ -102,7 +103,8 @@ cdef class Xoshiro256(BitGenerator): Examples -------- - >>> from randomgen import Generator, Xoshiro256 + >>> from numpy.random import Generator + >>> from randomgen import Xoshiro256 >>> rg = Generator(Xoshiro256(1234)) >>> rg.standard_normal() 0.123 # random diff --git a/randomgen/xoshiro512.pyx b/randomgen/xoshiro512.pyx index 958eba45b..c0f08c595 100644 --- a/randomgen/xoshiro512.pyx +++ b/randomgen/xoshiro512.pyx @@ -88,7 +88,8 @@ cdef class Xoshiro512(BitGenerator): generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import Generator, Xoshiro512 + >>> from numpy.random import Generator + >>> from randomgen import Xoshiro512 >>> rg = [Generator(Xoshiro512(1234)) for _ in range(10)] # Advance each Xoshiro512 instance by i jumps >>> for i in range(10): @@ -101,7 +102,8 @@ cdef class Xoshiro512(BitGenerator): Examples -------- - >>> from randomgen import Generator, Xoshiro512 + >>> from numpy.random import Generator + >>> from randomgen import Xoshiro512 >>> rg = Generator(Xoshiro512(1234)) >>> rg.standard_normal() 0.123 # random From ad90452e4040c0637f2230772ca0e4ff426198a4 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 14:03:09 +0100 Subject: [PATCH 13/14] DOC: Finalize docs --- MANIFEST.in | 1 + README.md | 30 ++++------ ci/azure/azure_template_posix.yml | 2 +- ci/azure/install-posix.sh | 6 +- doc/requirements.txt | 2 +- doc/source/bit_generators/index.rst | 2 +- doc/source/change-log.rst | 81 +++++++++++--------------- doc/source/custom-bit-generators.ipynb | 53 ++++++++--------- doc/source/evolution.rst | 24 ++++---- doc/source/extending.rst | 8 +-- doc/source/future.rst | 6 +- doc/source/generator.rst | 7 ++- doc/source/index.rst | 16 +++-- doc/source/legacy.rst | 10 ++-- doc/source/new-or-different.rst | 75 ++++++++++-------------- randomgen/mt19937.pyx | 2 +- randomgen/rdrand.pyx | 2 +- requirements-dev.txt | 2 +- requirements.txt | 2 +- 19 files changed, 156 insertions(+), 175 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 25ab3f662..7c92dad1b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ recursive-exclude randomgen *.c +include randomgen/py.typed include randomgen/_version.py include requirements.txt include README.md diff --git a/README.md b/README.md index 9139bc62d..ae9774f47 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,13 @@ or the [stable documentation](https://bashtage.github.io/randomgen/devel/change- # WARNINGS -## Changes in v1.19 +## Changes in v1.24 -``Generator`` and ``RandomState`` have been officially deprecated, and will +``Generator`` and ``RandomState`` have been **removed**. + +## Changes from 1.18 to 1.19 + +``Generator`` and ``RandomState`` have been officially deprecated in 1.19, and will warn with a ``FutureWarning`` about their removal. They will also receive virtually no maintenance. It is now time to move to NumPy's ``np.random.Generator`` which has features not in ``randomstate.Generator`` and is maintained more actively. @@ -54,8 +58,7 @@ to ``randomstate.ExtendedGenerator``: There are no plans to remove any of the bit generators, e.g., ``AESCounter``, ``ThreeFry``, or ``PCG64``. -## Changes in v1.18 - +### Changes from 1.16 to 1.18 There are many changes between v1.16.x and v1.18.x. These reflect API decision taken in conjunction with NumPy in preparation of the core of `randomgen` being used as the preferred random number generator in @@ -66,16 +69,6 @@ bit generators (or `BigGenerator`s). ## Future Plans -A substantial portion of randomgen has been merged into NumPy. Revamping NumPy's random -number generation was always the goal of this project (and its predecessor -[NextGen NumPy RandomState](https://github.com/bashtage/ng-numpy-randomstate>)), -and so it has succeeded. - -While I have no immediate plans to remove anything, after a 1.19 release I will: - -* Remove `Generator` and `RandomState`. These duplicate NumPy and will diverge over time. - The versions in NumPy are authoritative. **Deprecated** -* Preserve novel methods of `Generator` in a new class, `ExtendedGenerator`. **Done** * Add some distributions that are not supported in NumPy. _Ongoing_ * Add any interesting bit generators I come across. _Recent additions include the DXSM and CM-DXSM variants of PCG64 and the LXM generator._ @@ -102,7 +95,6 @@ The RNGs include: * Chaotic PRNGS: Small-Fast Chaotic (`SFC64`) and Jenkin's Small-Fast (`JSF`). - ## Status * Builds and passes all tests on: @@ -114,8 +106,8 @@ The RNGs include: ## Version -The package version matches the latest version of NumPy where -`Generator(MT19937())` passes all NumPy test. +The package version matches the latest version of NumPy when the package +is released. ## Documentation @@ -128,8 +120,8 @@ the latest commit (unreleased) is available under ## Requirements Building requires: -* Python (3.6, 3.7, 3.8, 3.9) -* NumPy (1.14, 1.15, 1.16, 1.17, 1.18, 1.19) +* Python (3.6, 3.7, 3.8, 3.9, 3.10) +* NumPy (1.17+) * Cython (0.29+) * tempita (0.5+), if not provided by Cython diff --git a/ci/azure/azure_template_posix.yml b/ci/azure/azure_template_posix.yml index cd5067bd1..2f5b2d925 100644 --- a/ci/azure/azure_template_posix.yml +++ b/ci/azure/azure_template_posix.yml @@ -25,7 +25,7 @@ jobs: python38_legacy: python.version: '3.8' coverage: true - NUMPY: 1.16.6 + NUMPY: 1.17.0 python39_latest: python.version: '3.9' python310_latest: diff --git a/ci/azure/install-posix.sh b/ci/azure/install-posix.sh index 8b89a529e..8fd9a60d6 100644 --- a/ci/azure/install-posix.sh +++ b/ci/azure/install-posix.sh @@ -13,13 +13,13 @@ else fi # Not all available in conda -python -m pip install setuptools "setuptools_scm[toml]<7" "oldest-supported-numpy" wheel pip black==22.3.0 isort flake8 --upgrade +python -m pip install setuptools "setuptools_scm[toml]<7" "oldest-supported-numpy" wheel pip black==22.6.0 isort flake8 --upgrade EXTRA="pytest pytest-xdist coverage pytest-cov" -if [[ -n ${NUMPY} ]]; then CMD="$CMD==${NUMPY}"; fi; +if [[ -n ${NUMPY} ]]; then CMD="$CMD~=${NUMPY}"; fi; CMD="$CMD cython" -if [[ -n ${CYTHON} ]]; then CMD="$CMD==${CYTHON}"; fi; +if [[ -n ${CYTHON} ]]; then CMD="$CMD~=${CYTHON}"; fi; CMD="$CMD pandas" CMD="$CMD $EXTRA" if [[ ${USE_CONDA} == "true" ]]; then CMD="$CMD numba"; fi; diff --git a/doc/requirements.txt b/doc/requirements.txt index 878db4b5e..4b0c8b316 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,7 +1,7 @@ numba>=0.49 nbsphinx sphinx-material -sphinx>=3 +sphinx>=5 ipython>=6 numpydoc pygments>=2.7.4 # not directly required, pinned by Snyk to avoid a vulnerability \ No newline at end of file diff --git a/doc/source/bit_generators/index.rst b/doc/source/bit_generators/index.rst index 03499f85d..9c8f65e96 100644 --- a/doc/source/bit_generators/index.rst +++ b/doc/source/bit_generators/index.rst @@ -2,7 +2,7 @@ Bit Generators -------------- The random values produced by :class:`numpy.random.Generator` -(and also :class:`~randomgen.generator.Generator`) +(and also ``Generator``) are produced by a bit generator. These bit generators do not directly provide random numbers and only contain methods used for seeding, getting or setting the state, jumping or advancing the state, and for accessing diff --git a/doc/source/change-log.rst b/doc/source/change-log.rst index e34259c66..24c815df9 100644 --- a/doc/source/change-log.rst +++ b/doc/source/change-log.rst @@ -9,11 +9,13 @@ Change Log

Deprecated

- :class:`~randomgen.generator.Generator` and :class:`~randomgen.mtrand.RandomState` - are **DEPRECATED**. You should be using :class:`numpy.random.Generator` or - :class:`numpy.random.RandomState` which are better maintained. These will be - maintained until after NumPy 1.21 (or 2 releases after NumPy 1.19) for users who - cannot update NumPy. + ``Generator`` and ``RandomState`` have been **REMOVED** in 1.23. + You should be using :class:`numpy.random.Generator` or + :class:`numpy.random.RandomState` which are maintained. + +v1.23.0 +======= +- Removed ``Generator`` and ``RandomState``. v1.20.2 ======= @@ -44,7 +46,7 @@ v1.20.2 v1.20.1 ======= -- Fixed a bug that affects :func:`~randomgen.generator.Generator.standard_gamma` when +- Fixed a bug that affects ``standard_gamma`` when used with ``out`` and a Fortran contiguous array. - Added :func:`~randomgen.generator.ExtendedGenerator.multivariate_complex_normal`. - Added :func:`~randomgen.generator.ExtendedGenerator.standard_wishart` and @@ -105,20 +107,20 @@ v1.19.0 - Added support for broadcasting inputs in :class:`randomgen.generator.ExtendedGenerator.multivariate_normal`. - Added support for the `++` variant of :class:`randomgen.xoroshiro128.Xoroshiro128`. - Fixed a bug the produced incorrect results in :func:`~randomgen.mt19937.MT19937.jumped`. -- Fixed multiple bugs in :class:`~randomgen.generator.Generator` that were fixed in :class:`numpy.random.Generator`. +- Fixed multiple bugs in ``Generator`` that were fixed in :class:`numpy.random.Generator`. v1.18.0 ======= -- :meth:`~randomgen.generator.Generator.choice` pulled in upstream performance improvement that +- ``choice`` pulled in upstream performance improvement that use a hash set when choosing without replacement and without user-provided probabilities. - Added support for :class:`~randomgen.seed_sequence.SeedSequence` (and NumPy's ``SeedSequence``). - Fixed a bug that affected both :class:`~randomgen.generator.Generator.randint` - in :class:`~randomgen.generator.Generator` and :meth:`~randomgen.mtrand.RandomState.randint` - in :class:`~randomgen.mtrand.RandomState` when ``high=2**32``. This value is inbounds for + in ``Generator`` and ``randint`` + in ``RandomState`` when ``high=2**32``. This value is inbounds for a 32-bit unsigned closed interval generator, and so should have been redirected to a 32-bit generator. It was erroneously sent to the 64-bit path. The random values produced are fully random but inefficient. This fix breaks the stream in :class:`~randomgen.generator.Generator - is the value for ``high`` is used. The fix restores :class:`~randomgen.mtrand.RandomState` to + is the value for ``high`` is used. The fix restores ``RandomState`` to NumPy 1.16 compatibility. only affects the output if ``dtype`` is ``'int64'`` - This release brings many breaking changes. Most of these have been @@ -127,20 +129,16 @@ v1.18.0 going into NumPy. - Two changes that are more abrupt are: - * The ``.generator`` method of the bit generators raise - ``NotImplementedError`` + * The ``.generator`` method of the bit generators raise ``NotImplementedError`` * The internal structures that is used in C have been renamed. The main rename is ``brng_t`` to ``bitgen_t`` - The other key changes are: - * Rename ``RandomGenerator`` to :class:`~randomgen.generator.Generator`. - * Rename :meth:`~randomgen.generator.Generator.randint` to - :meth:`~randomgen.generator.Generator.integers`. - * Rename :meth:`~randomgen.generator.Generator.random_integers` to - :meth:`~randomgen.generator.Generator.integers`. - * Rename :meth:`~randomgen.generator.Generator.random_sample` - to :meth:`~randomgen.generator.Generator.random`. + * Rename ``RandomGenerator`` to ``Generator``. + * Rename ``randint`` to ``integers``. + * Rename ``random_integers`` to ``integers``. + * Rename ``random_sample`` to ``random``. * Change ``jump`` which operated in-place to :meth:`~randomgen.xoshiro256.Xoshiro256.jumped` which returns a new ``BitGenerator``. @@ -163,40 +161,34 @@ v1.16.6 - Improved the performance of :class:`~randomgen.pcg64.PCG64` on Windows. - Improved performance of :func:`~randomgen.dsfmt.DSFMT.jump` and :func:`~randomgen.dsfmt.DSFMT.jumped`. -- Improves backward compatibility of :class:`~randomgen.mtrand.RandomState` +- Improves backward compatibility of ``RandomState`` v1.16.5 ======= -- Fixed bugs in :func:`~randomgen.mtrand.RandomState.laplace`, - :func:`~randomgen.mtrand.RandomState.gumbel`, - :func:`~randomgen.mtrand.RandomState.logseries`, - :func:`~randomgen.mtrand.RandomState.normal`, - :func:`~randomgen.mtrand.RandomState.standard_normal`, - :func:`~randomgen.mtrand.RandomState.standard_exponential`, - :func:`~randomgen.mtrand.RandomState.exponential`, and - :func:`~randomgen.mtrand.RandomState.logistic` that could result in ``nan`` - values in rare circumstances (about 1 in :math:`10^{53}` draws). -- Added keyword ``closed`` to :func:`~randomgen.generator.Generator.randint` +- Fixed bugs in ``laplace``, ``gumbel``, ``logseries``, ``normal``, + ``standard_normal``, ``standard_exponential``, ``exponential``, and ``logistic`` + that could result in ``nan`` values in rare circumstances (about 1 in :math:`10^{53}` draws). +- Added keyword ``closed`` to ``randint`` which changes sampling from the half-open interval ``[low, high)`` to the closed interval ``[low, high]``. -- Fixed a bug in :func:`~randomgen.mtrand.RandomState.random_integers` that +- Fixed a bug in ``random_integers`` that could lead to valid values being treated as invalid. v1.16.4 ======= -- Add a fast path for broadcasting :func:`~randomgen.generator.Generator.randint` +- Add a fast path for broadcasting ``randint`` when using ``uint64`` or ``int64``. - Refactor PCG64 so that it does not rely on Cython conditional compilation. -- Add :func:`~randomgen.generator.Generator.brng` to access the basic RNG. -- Allow multidimensional arrays in :func:`~randomgen.generator.Generator.choice`. -- Speed-up :func:`~randomgen.generator.Generator.choice` when not replacing. +- Add ``brng`` to access the basic RNG. +- Allow multidimensional arrays in ``choice``. +- Speed-up ``choice`` when not replacing. The gains can be very large (1000x or more) when the input array is large but the sample size is small. -- Add parameter checks in :func:`~randomgen.generator.Generator.multinomial`. -- Fix an edge-case bug in :func:`~randomgen.generator.Generator.zipf`. -- Allow 0 for sample in :func:`~randomgen.generator.Generator.hypergeometric`. -- Add broadcasting to :func:`~randomgen.generator.Generator.multinomial` (see +- Add parameter checks in ``multinomial``. +- Fix an edge-case bug in ``zipf``. +- Allow 0 for sample in ``hypergeometric``. +- Add broadcasting to ``multinomial`` (see `NumPy issue 9710 `_) v1.16.3 @@ -210,13 +202,10 @@ v1.16.2 into NumPy, including removing: * ``random_raw``, which have been moved to the individual bit generators - * ``random_uintegers``, which can be replaced with - :func:`~randomgen.generator.Generator.randint`. + * ``random_uintegers``, which can be replaced with ``randint``. -- Added :class:`~randomgen.mtrand.RandomState` as a clone of NumPy's - RandomState. -- Removed :class:`~randomgen.legacy.LegacyGenerator` since this is no - longer needed +- Added ``RandomState`` as a clone of NumPy's RandomState. +- Removed ``LegacyGenerator`` since this is no longer needed - Fixed many small bugs, including in cffi and ctype interfaces v1.16.1 diff --git a/doc/source/custom-bit-generators.ipynb b/doc/source/custom-bit-generators.ipynb index e883bede1..83565110e 100644 --- a/doc/source/custom-bit-generators.ipynb +++ b/doc/source/custom-bit-generators.ipynb @@ -177,15 +177,15 @@ "output_type": "stream", "text": [ "Get the state from a seeded PCG64\n", - "{'state': 168573994392035485979667429847573090803, 'inc': 1}\n", + "{'state': 35399562948360463058890781895381311971, 'inc': 87136372517582989555478159403783844777}\n", "State and increment are identical\n", - "{'state': 168573994392035485979667429847573090803, 'inc': 1}\n", + "{'state': 35399562948360463058890781895381311971, 'inc': 87136372517582989555478159403783844777}\n", "First 5 values from PythonPCG64\n", - "[17589501946320304812 4238265634445818190 2962725298318311686\n", - " 8662843047148884465 14592389037209137702]\n", + "[11749869230777074271 4976686463289251617 755828109848996024\n", + " 304881062738325533 15002187965291974971]\n", "Match official C version\n", - "[17589501946320304812 4238265634445818190 2962725298318311686\n", - " 8662843047148884465 14592389037209137702]\n" + "[11749869230777074271 4976686463289251617 755828109848996024\n", + " 304881062738325533 15002187965291974971]\n" ] } ], @@ -235,9 +235,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "Before: 223347365555069667157438957040342502044\n", - "Std. Normal : -0.32247746250377424\n", - "After: 57149987494923580281468740370759435277\n" + "Before: 133411349017971402732463711865589153492\n", + "Std. Normal : 0.36159505490948474\n", + "After: 9405893610231781608176235507540826829\n" ] } ], @@ -282,7 +282,8 @@ { "data": { "text/plain": [ - "{'state': 57149987494923580281468740370759435277, 'inc': 1}" + "{'state': 9405893610231781608176235507540826829,\n", + " 'inc': 87136372517582989555478159403783844777}" ] }, "execution_count": 4, @@ -330,8 +331,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "2.42 ms ± 24 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n", - "3.96 µs ± 20.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" + "3.08 ms ± 24.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n", + "4.55 µs ± 21.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n" ] } ], @@ -720,7 +721,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "2.91 ms ± 15 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "4.4 ms ± 62.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], @@ -741,7 +742,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "2.73 ms ± 6.94 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" + "4.19 ms ± 31.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], @@ -776,10 +777,10 @@ "data": { "text/plain": [ "{'bit_generator': 'JSF',\n", - " 'state': {'a': 18171450132690946015,\n", - " 'b': 15404671487167129326,\n", - " 'c': 10657586896028597556,\n", - " 'd': 16851287489526651507,\n", + " 'state': {'a': 17190901158427765818,\n", + " 'b': 14501513697102443756,\n", + " 'c': 15715724510248929625,\n", + " 'd': 12712143389959007425,\n", " 'p': 7,\n", " 'q': 13,\n", " 'r': 37},\n", @@ -823,8 +824,8 @@ "data": { "text/plain": [ "{'bit_gen': 'NumbaJSF',\n", - " 'state': array([18171450132690946015, 15404671487167129326, 10657586896028597556,\n", - " 16851287489526651507], dtype=uint64),\n", + " 'state': array([17190901158427765818, 14501513697102443756, 15715724510248929625,\n", + " 12712143389959007425], dtype=uint64),\n", " 'has_uint': 1,\n", " 'uinteger': 0}" ] @@ -867,8 +868,8 @@ { "data": { "text/plain": [ - "array([ 6237172512235144455, 115752806140568818, 6885676076213832469,\n", - " 16217168628111436974, 4020603927364330085], dtype=uint64)" + "array([ 3814417803339974021, 15780814468893899944, 17400468283504521969,\n", + " 17987378307908897868, 18034113569054765009], dtype=uint64)" ] }, "execution_count": 14, @@ -892,8 +893,8 @@ { "data": { "text/plain": [ - "array([ 6237172512235144455, 115752806140568818, 6885676076213832469,\n", - " 16217168628111436974, 4020603927364330085], dtype=uint64)" + "array([ 3814417803339974021, 15780814468893899944, 17400468283504521969,\n", + " 17987378307908897868, 18034113569054765009], dtype=uint64)" ] }, "execution_count": 15, @@ -908,7 +909,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -922,7 +923,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.7" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/doc/source/evolution.rst b/doc/source/evolution.rst index f7d2cd004..1c2bb7631 100644 --- a/doc/source/evolution.rst +++ b/doc/source/evolution.rst @@ -3,15 +3,22 @@ Evolution of randomgen ====================== +Changes in 1.23 +--------------- +``Generator`` and ``RandomState`` have been **removed**. + +Use :class:`numpy.random.Generator` if possible, or :class:`numpy.random.RandomState` +if you face legacy constraints. + Changes in 1.19 --------------- -:class:`~randomgen.generator.Generator` and :class:`~randomgen.mtrand.RandomState` have been +``Generator`` and ``RandomState`` have been officially deprecated, and will warn with a ``FutureWarning`` about their removal. They will also receive virtually no maintenance. It is now time to move to NumPy's :class:`numpy.random.Generator` -which has features not in :class:`~randomgen.generator.Generator` and is maintained more actively. +which has features not in ``Generator`` and is maintained more actively. -A few distributions that are not present in :class:`~randomgen.generator.Generator` have been moved +A few distributions that are not present in ``Generator`` have been moved to :class:`~randomgen.generator.ExtendedGenerator`: * :func:`~randomgen.generator.ExtendedGenerator.multivariate_normal`: which supports broadcasting @@ -34,13 +41,10 @@ bit generators (or ``BitGenerator``). The main changes are -* Rename ``RandomGenerator`` to :class:`~randomgen.generator.Generator`. -* Rename :meth:`~randomgen.generator.Generator.randint` to - :meth:`~randomgen.generator.Generator.integers`. -* Rename :meth:`~randomgen.generator.Generator.random_integers` to - :meth:`~randomgen.generator.Generator.integers`. -* Rename :meth:`~randomgen.generator.Generator.random_sample` to - :meth:`~randomgen.generator.Generator.random`. +* Rename ``RandomGenerator`` to ``Generator``. +* Rename ``randint`` to ``integers``. +* Rename ``random_integers`` to ``integers``. +* Rename ``random_sample`` to ``random``. * Change ``jump`` which operated in-place to ``jumped`` which returns a new ``BitGenerator``. * Rename Basic RNG to bit generator, which impacts the API in multiple places where names like ``brng`` and ``basic_rng`` have been replaced by ``bitgen`` or ``bit_generator``. diff --git a/doc/source/extending.rst b/doc/source/extending.rst index 40ee2665c..9e4847efd 100644 --- a/doc/source/extending.rst +++ b/doc/source/extending.rst @@ -2,7 +2,7 @@ Extending --------- The bit generators have been designed to be extendable using standard tools for high-performance Python -- numba and Cython. -The :class:`randomgen.generator.Generator` object can also be used with +The :class:`numpy.random.Generator` object can also be used with user-provided bit generators as long as these export a small set of required functions. @@ -135,7 +135,7 @@ examples folder. New Bit Generators ================== -:class:`~randomgen.generator.Generator` can be used with other +``Generator`` can be used with other user-provided bit generators. The simplest way to write a new bit generator is to examine the pyx file of one of the existing bit generators. The key structure that must be provided is the ``capsule`` which contains a ``PyCapsule`` to a @@ -156,7 +156,7 @@ used by the bit generator. The next three are function pointers which return th next 64- and 32-bit unsigned integers, the next random double and the next raw value. This final function is used for testing and so can be set to the next 64-bit unsigned integer function if not needed. Functions inside -:class:`~randomgen.generator.Generator` use this structure as in +``Generator`` use this structure as in .. code-block:: c @@ -168,5 +168,5 @@ Python BitGenerators :class:`~randomgen.wrapper.UserBitGenerator` is a utility class that lets users write bit generators in Python. While these are inherently low performance, this interface allows users to rapidly prototype a bit generator and to pass this -bit generator to a :class:`~randomgen.generator.Generator` to generate variates +bit generator to a ``Generator`` to generate variates from the full spectrum of distributions. diff --git a/doc/source/future.rst b/doc/source/future.rst index 523b65844..bedb851a9 100644 --- a/doc/source/future.rst +++ b/doc/source/future.rst @@ -8,10 +8,8 @@ and so it has succeeded. The future plans for randomgen are: -* Remove :class:`~randomgen.generator.Generator` and :class:`~randomgen.mtrand.RandomState`. These - duplicate NumPy and will diverge over time. The versions in NumPy are authoritative. These - have been deprecated as of version 1.19 and will be removed in 1.21. -* Put the novel methods of :class:`~randomgen.generator.Generator` in a +* ``Generator`` and ``RandomState`` have been **removed** in 1.23. +* Put the novel methods of ``Generator`` in a :class:`~randomgen.generator.ExtendedGenerator`. :class:`~randomgen.generator.ExtendedGenerator` will be maintained, although it is possible that some of the methods may migrate to NumPy. diff --git a/doc/source/generator.rst b/doc/source/generator.rst index fd9346e6f..5b85f2ece 100644 --- a/doc/source/generator.rst +++ b/doc/source/generator.rst @@ -5,10 +5,11 @@ Random Generator .. raw:: html -

Deprecated

+

Removed

- :class:`~randomgen.generator.Generator` has been **removed**. You should be using - :class:`numpy.random.Generator`. +.. danger:: + + ``Generator`` has been **removed**. You should be using :class:`numpy.random.Generator`. .. currentmodule:: randomgen.generator diff --git a/doc/source/index.rst b/doc/source/index.rst index 2df2b7156..3487d5d6c 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -1,5 +1,6 @@ .. danger:: + ``Generator`` and ``RandomState`` has been removed from randomgen in 1.23. randomgen has been substantially merged into NumPy as of 1.17. :ref:`evolution` details how randomgen has changed since it was incorporated into NumPy. @@ -123,10 +124,9 @@ Random Generator .. toctree:: :maxdepth: 1 - future - Random Generation extended-generator - legacy + new-or-different + future Bit Generators -------------- @@ -144,7 +144,6 @@ New Features Parallel Applications Multithreaded Generation - new-or-different Quality Assurance Comparing Performance extending @@ -152,6 +151,15 @@ New Features Reading System Entropy references +Removed Features +---------------- +.. toctree:: + :maxdepth: 2 + + Random Generation + legacy + + Changes ~~~~~~~ .. toctree:: diff --git a/doc/source/legacy.rst b/doc/source/legacy.rst index 8586e65b9..92d8755fa 100644 --- a/doc/source/legacy.rst +++ b/doc/source/legacy.rst @@ -1,15 +1,17 @@ Legacy Random Generation ------------------------ + .. container:: admonition danger .. raw:: html -

Deprecated

+

Removed

+ +.. danger:: - :class:`~randomgen.mtrand.RandomState` has been **removed**. You should be using - :class:`numpy.random.Generator`, or if you must have backward compatibility with - NumPy before 1.17, :class:`numpy.random.RandomState`. + ``RandomState`` has been **removed**. You should be using :class:`numpy.random.Generator`, + or if you must have backward compatibility with NumPy before 1.17, :class:`numpy.random.RandomState`. .. currentmodule:: randomgen.mtrand diff --git a/doc/source/new-or-different.rst b/doc/source/new-or-different.rst index 2fadd79b9..6a03e2a80 100644 --- a/doc/source/new-or-different.rst +++ b/doc/source/new-or-different.rst @@ -3,8 +3,8 @@ What's New or Different ----------------------- -Differences from NumPy 1.17+ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Differences from NumPy (1.17+) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * :class:`~randomgen.wrapper.UserBitGenerator` allows bit generators to be written in Python (slow, suitable for experiments and testing) or numba (fast, similar speed to compiled C). See `the demonstration notebook`_ for @@ -69,45 +69,36 @@ Differences from NumPy 1.17+

Deprecated

- :class:`~randomgen.generator.Generator` is **deprecated**. You should be using + ``Generator`` is **deprecated**. You should be using :class:`numpy.random.Generator`. -* randomgen's :class:`~randomgen.generator.Generator` continues to expose legacy - methods :func:`~randomgen.generator.Generator.random_sample` \, - :func:`~randomgen.generator.Generator.randint` \, - :func:`~randomgen.generator.Generator.random_integers` \, - :func:`~randomgen.generator.Generator.rand` \, :func:`~randomgen.generator.Generator.randn` \, - and :func:`~randomgen.generator.Generator.tomaxint`. **Note**: These should - not be used, and their modern replacements are preferred: +* randomgen's ``Generator`` continues to expose legacy + methods ``random_sample``, ``randint``, ``random_integers``, ``rand``, ``randn``, + and ``tomaxint``. **Note**: These should not be used, and their modern replacements are preferred: - * :func:`~randomgen.generator.Generator.random_sample`\, :func:`~randomgen.generator.Generator.rand` → :func:`~randomgen.generator.Generator.random` - * :func:`~randomgen.generator.Generator.random_integers`\, :func:`~randomgen.generator.Generator.randint` → :func:`~randomgen.generator.Generator.integers` - * :func:`~randomgen.generator.Generator.randn` → :func:`~randomgen.generator.Generator.standard_normal` - * :func:`~randomgen.generator.Generator.tomaxint` → :func:`~randomgen.generator.Generator.integers` with ``dtype`` set to ``int`` + * ``random_sample``, ``rand` → ``random`` + * ``random_integers``, ``randint`` → ``integers`` + * ``randn`` → ``standard_normal`` + * ``tomaxint`` → ``integers`` with ``dtype`` set to ``int`` * randomgen's bit generators remain seedable and the convenience function - :func:`~randomgen.generator.Generator.seed` is exposed as part of - :class:`~randomgen.generator.Generator`. Additionally, the convenience - property :func:`~randomgen.generator.Generator.state` is available - to get or set the state of the underlying bit generator. + ``seed` is exposed as part of``Generator``. Additionally, the convenience + property ``state`` is available to get or set the state of the underlying bit generator. * :func:`numpy.random.Generator.multivariate_hypergeometric` was added after - :class:`~randomgen.generator.Generator` was merged into NumPy and will not - be ported over. + ``Generator`` was merged into NumPy and will not be ported over. * :func:`numpy.random.Generator.shuffle` and :func:`numpy.random.Generator.permutation` support ``axis`` keyword to operator along an axis other than 0. -* :func:`~randomgen.generator.Generator.integers` supports the keyword argument ``use_masked`` - to switch between masked generation of bounded integers and Lemire's superior method. +* ``integers`` supports the keyword argument ``use_masked`` to switch between masked + generation of bounded integers and Lemire's superior method. Differences from NumPy before 1.17 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * The normal, exponential and gamma generators use 256-step Ziggurat methods which are 2-10 times faster than NumPy's default implementation in - :meth:`~randomgen.generator.Generator.standard_normal` \, - :meth:`~randomgen.generator.Generator.standard_exponential` or - :meth:`~randomgen.generator.Generator.standard_gamma`. + ``standard_normal``, ``standard_exponential`` or ``standard_gamma``. * The Box-Muller used to produce NumPy's normals is no longer available. * All bit generators functions to produce doubles, uint64s and @@ -119,29 +110,26 @@ Differences from NumPy before 1.17 to produce either single or double prevision uniform random variables for select core distributions - * Uniforms (:meth:`~randomgen.generator.Generator.random` and - :meth:`~randomgen.generator.Generator.rand`) - * Normals (:meth:`~randomgen.generator.Generator.standard_normal` and - :meth:`~randomgen.generator.Generator.randn`) - * Standard Gammas (:meth:`~randomgen.generator.Generator.standard_gamma`) - * Standard Exponentials (:meth:`~randomgen.generator.Generator.standard_exponential`) + * Uniforms (``random`` and ``rand``) + * Normals (``standard_normal`` and ``randn``) + * Standard Gammas (``standard_gamma``) + * Standard Exponentials (``standard_exponential``) * Optional ``out`` argument that allows existing arrays to be filled for select core distributions - * Uniforms (:meth:`~randomgen.generator.Generator.random`) - * Normals (:meth:`~randomgen.generator.Generator.standard_normal`) - * Standard Gammas (:meth:`~randomgen.generator.Generator.standard_gamma`) - * Standard Exponentials (:meth:`~randomgen.generator.Generator.standard_exponential`) + * Uniforms (``random``) + * Normals (``standard_normal``) + * Standard Gammas (``standard_gamma``) + * Standard Exponentials (``standard_exponential``) This allows multithreading to fill large arrays in chunks using suitable PRNGs in parallel. -* :meth:`~randomgen.generator.Generator.integers` supports broadcasting inputs. +* ``integers`` supports broadcasting inputs. -* :meth:`~randomgen.generator.Generator.integers` supports - drawing from open (default, ``[low, high)``) or closed +* ``integers`` supports drawing from open (default, ``[low, high)``) or closed (``[low, high]``) intervals using the keyword argument ``endpoint``. Closed intervals are simpler to use when the distribution may include the maximum value of a given integer type. @@ -153,19 +141,16 @@ Differences from NumPy before 1.17 * Support for Lemire’s method of generating uniform integers on an arbitrary interval by setting ``use_masked=True`` in - (:meth:`~randomgen.generator.Generator.integers`). + (``integers``). -* :meth:`~randomgen.generator.Generator.multinomial` - supports multidimensional values of ``n`` +* ``multinomial`` supports multidimensional values of ``n`` -* :meth:`~randomgen.generator.Generator.choice` - is much faster when sampling small amounts from large arrays +* ``choice`` is much faster when sampling small amounts from large arrays -* :meth:`~randomgen.generator.Generator.choice` - supports the ``axis`` keyword to work with multidimensional arrays. +* ``choice`` supports the ``axis`` keyword to work with multidimensional arrays. * For changes since the previous release, see the :ref:`change-log` diff --git a/randomgen/mt19937.pyx b/randomgen/mt19937.pyx index 6996ffa02..3ca8d230a 100644 --- a/randomgen/mt19937.pyx +++ b/randomgen/mt19937.pyx @@ -285,7 +285,7 @@ cdef class MT19937(BitGenerator): ---------- .. [1] Matsumoto, M, Generating multiple disjoint streams of pseudorandom number sequences. Accessed on: May 6, 2020. - [Online]. Available: + (online). Available: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/JUMP/ .. [2] Hiroshi Haramoto, Makoto Matsumoto, Takuji Nishimura, François diff --git a/randomgen/rdrand.pyx b/randomgen/rdrand.pyx index 63d5f92a8..ec822f5d2 100644 --- a/randomgen/rdrand.pyx +++ b/randomgen/rdrand.pyx @@ -242,7 +242,7 @@ cdef class RDRAND(BitGenerator): [Accessed 10 July 2020]. .. [2] Intel. 2020. Intel® Digital Random Number Generator (DRNG) Software Implementation. - [online] Available at: + (online) Available at: [Accessed 10 July 2020]. """ diff --git a/requirements-dev.txt b/requirements-dev.txt index d36ca8b22..b8b89c689 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ -black==22.3.0 +black==22.6.0 pytest>=6 pytest-cov scipy>=1.3.2 diff --git a/requirements.txt b/requirements.txt index 8b82ee104..9089b4a31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -numpy>=1.14 +numpy>=1.17 cython>=0.29.24 setuptools wheel From be92b9be7d2c5d29c5b16cd7318308ad013a2cba Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Mon, 18 Jul 2022 14:13:47 +0100 Subject: [PATCH 14/14] MAINT: Fix numpy req --- setup.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/setup.py b/setup.py index b39e3d8d5..ccd550d21 100644 --- a/setup.py +++ b/setup.py @@ -23,9 +23,7 @@ except ImportError: raise ImportError("tempita required to install, use pip install tempita") -with open("requirements.txt") as f: - setup_required = f.read().splitlines() -install_required = [pkg for pkg in setup_required if "numpy" in pkg] +install_required = ["numpy >= 1.17"] CYTHON_COVERAGE = os.environ.get("RANDOMGEN_CYTHON_COVERAGE", "0") in ( "true", @@ -386,6 +384,5 @@ def is_pure(self): ], zip_safe=False, install_requires=install_required, - setup_requires=setup_required, python_requires=">=3.6", )