numpy 2.4.0__cp313-cp313t-musllinux_1_2_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- numpy/__config__.py +170 -0
- numpy/__config__.pyi +108 -0
- numpy/__init__.cython-30.pxd +1242 -0
- numpy/__init__.pxd +1155 -0
- numpy/__init__.py +942 -0
- numpy/__init__.pyi +6202 -0
- numpy/_array_api_info.py +346 -0
- numpy/_array_api_info.pyi +206 -0
- numpy/_configtool.py +39 -0
- numpy/_configtool.pyi +1 -0
- numpy/_core/__init__.py +201 -0
- numpy/_core/__init__.pyi +666 -0
- numpy/_core/_add_newdocs.py +7151 -0
- numpy/_core/_add_newdocs.pyi +2 -0
- numpy/_core/_add_newdocs_scalars.py +381 -0
- numpy/_core/_add_newdocs_scalars.pyi +16 -0
- numpy/_core/_asarray.py +130 -0
- numpy/_core/_asarray.pyi +43 -0
- numpy/_core/_dtype.py +366 -0
- numpy/_core/_dtype.pyi +56 -0
- numpy/_core/_dtype_ctypes.py +120 -0
- numpy/_core/_dtype_ctypes.pyi +83 -0
- numpy/_core/_exceptions.py +162 -0
- numpy/_core/_exceptions.pyi +54 -0
- numpy/_core/_internal.py +968 -0
- numpy/_core/_internal.pyi +61 -0
- numpy/_core/_methods.py +252 -0
- numpy/_core/_methods.pyi +22 -0
- numpy/_core/_multiarray_tests.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_multiarray_umath.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_operand_flag_tests.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_rational_tests.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_simd.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_simd.pyi +35 -0
- numpy/_core/_string_helpers.py +100 -0
- numpy/_core/_string_helpers.pyi +12 -0
- numpy/_core/_struct_ufunc_tests.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_type_aliases.py +131 -0
- numpy/_core/_type_aliases.pyi +86 -0
- numpy/_core/_ufunc_config.py +515 -0
- numpy/_core/_ufunc_config.pyi +69 -0
- numpy/_core/_umath_tests.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/_core/_umath_tests.pyi +47 -0
- numpy/_core/arrayprint.py +1779 -0
- numpy/_core/arrayprint.pyi +158 -0
- numpy/_core/cversions.py +13 -0
- numpy/_core/defchararray.py +1414 -0
- numpy/_core/defchararray.pyi +1150 -0
- numpy/_core/einsumfunc.py +1650 -0
- numpy/_core/einsumfunc.pyi +184 -0
- numpy/_core/fromnumeric.py +4233 -0
- numpy/_core/fromnumeric.pyi +1735 -0
- numpy/_core/function_base.py +547 -0
- numpy/_core/function_base.pyi +276 -0
- numpy/_core/getlimits.py +462 -0
- numpy/_core/getlimits.pyi +124 -0
- numpy/_core/include/numpy/__multiarray_api.c +376 -0
- numpy/_core/include/numpy/__multiarray_api.h +1628 -0
- numpy/_core/include/numpy/__ufunc_api.c +55 -0
- numpy/_core/include/numpy/__ufunc_api.h +349 -0
- numpy/_core/include/numpy/_neighborhood_iterator_imp.h +90 -0
- numpy/_core/include/numpy/_numpyconfig.h +33 -0
- numpy/_core/include/numpy/_public_dtype_api_table.h +86 -0
- numpy/_core/include/numpy/arrayobject.h +7 -0
- numpy/_core/include/numpy/arrayscalars.h +198 -0
- numpy/_core/include/numpy/dtype_api.h +547 -0
- numpy/_core/include/numpy/halffloat.h +70 -0
- numpy/_core/include/numpy/ndarrayobject.h +304 -0
- numpy/_core/include/numpy/ndarraytypes.h +1982 -0
- numpy/_core/include/numpy/npy_2_compat.h +249 -0
- numpy/_core/include/numpy/npy_2_complexcompat.h +28 -0
- numpy/_core/include/numpy/npy_3kcompat.h +374 -0
- numpy/_core/include/numpy/npy_common.h +989 -0
- numpy/_core/include/numpy/npy_cpu.h +126 -0
- numpy/_core/include/numpy/npy_endian.h +79 -0
- numpy/_core/include/numpy/npy_math.h +602 -0
- numpy/_core/include/numpy/npy_no_deprecated_api.h +20 -0
- numpy/_core/include/numpy/npy_os.h +42 -0
- numpy/_core/include/numpy/numpyconfig.h +185 -0
- numpy/_core/include/numpy/random/LICENSE.txt +21 -0
- numpy/_core/include/numpy/random/bitgen.h +20 -0
- numpy/_core/include/numpy/random/distributions.h +209 -0
- numpy/_core/include/numpy/random/libdivide.h +2079 -0
- numpy/_core/include/numpy/ufuncobject.h +343 -0
- numpy/_core/include/numpy/utils.h +37 -0
- numpy/_core/lib/libnpymath.a +0 -0
- numpy/_core/lib/npy-pkg-config/mlib.ini +12 -0
- numpy/_core/lib/npy-pkg-config/npymath.ini +20 -0
- numpy/_core/lib/pkgconfig/numpy.pc +7 -0
- numpy/_core/memmap.py +363 -0
- numpy/_core/memmap.pyi +3 -0
- numpy/_core/multiarray.py +1740 -0
- numpy/_core/multiarray.pyi +1316 -0
- numpy/_core/numeric.py +2758 -0
- numpy/_core/numeric.pyi +1276 -0
- numpy/_core/numerictypes.py +633 -0
- numpy/_core/numerictypes.pyi +196 -0
- numpy/_core/overrides.py +188 -0
- numpy/_core/overrides.pyi +47 -0
- numpy/_core/printoptions.py +32 -0
- numpy/_core/printoptions.pyi +28 -0
- numpy/_core/records.py +1088 -0
- numpy/_core/records.pyi +340 -0
- numpy/_core/shape_base.py +996 -0
- numpy/_core/shape_base.pyi +182 -0
- numpy/_core/strings.py +1813 -0
- numpy/_core/strings.pyi +536 -0
- numpy/_core/tests/_locales.py +72 -0
- numpy/_core/tests/_natype.py +144 -0
- numpy/_core/tests/data/astype_copy.pkl +0 -0
- numpy/_core/tests/data/generate_umath_validation_data.cpp +170 -0
- numpy/_core/tests/data/recarray_from_file.fits +0 -0
- numpy/_core/tests/data/umath-validation-set-README.txt +15 -0
- numpy/_core/tests/data/umath-validation-set-arccos.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arccosh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arcsin.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arcsinh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arctan.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arctanh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-cbrt.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-cos.csv +1375 -0
- numpy/_core/tests/data/umath-validation-set-cosh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-exp.csv +412 -0
- numpy/_core/tests/data/umath-validation-set-exp2.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-expm1.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-log.csv +271 -0
- numpy/_core/tests/data/umath-validation-set-log10.csv +1629 -0
- numpy/_core/tests/data/umath-validation-set-log1p.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-log2.csv +1629 -0
- numpy/_core/tests/data/umath-validation-set-sin.csv +1370 -0
- numpy/_core/tests/data/umath-validation-set-sinh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-tan.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-tanh.csv +1429 -0
- numpy/_core/tests/examples/cython/checks.pyx +373 -0
- numpy/_core/tests/examples/cython/meson.build +43 -0
- numpy/_core/tests/examples/cython/setup.py +39 -0
- numpy/_core/tests/examples/limited_api/limited_api1.c +17 -0
- numpy/_core/tests/examples/limited_api/limited_api2.pyx +11 -0
- numpy/_core/tests/examples/limited_api/limited_api_latest.c +19 -0
- numpy/_core/tests/examples/limited_api/meson.build +59 -0
- numpy/_core/tests/examples/limited_api/setup.py +24 -0
- numpy/_core/tests/test__exceptions.py +90 -0
- numpy/_core/tests/test_abc.py +54 -0
- numpy/_core/tests/test_api.py +655 -0
- numpy/_core/tests/test_argparse.py +90 -0
- numpy/_core/tests/test_array_api_info.py +113 -0
- numpy/_core/tests/test_array_coercion.py +928 -0
- numpy/_core/tests/test_array_interface.py +222 -0
- numpy/_core/tests/test_arraymethod.py +84 -0
- numpy/_core/tests/test_arrayobject.py +75 -0
- numpy/_core/tests/test_arrayprint.py +1324 -0
- numpy/_core/tests/test_casting_floatingpoint_errors.py +154 -0
- numpy/_core/tests/test_casting_unittests.py +955 -0
- numpy/_core/tests/test_conversion_utils.py +209 -0
- numpy/_core/tests/test_cpu_dispatcher.py +48 -0
- numpy/_core/tests/test_cpu_features.py +450 -0
- numpy/_core/tests/test_custom_dtypes.py +393 -0
- numpy/_core/tests/test_cython.py +352 -0
- numpy/_core/tests/test_datetime.py +2792 -0
- numpy/_core/tests/test_defchararray.py +858 -0
- numpy/_core/tests/test_deprecations.py +460 -0
- numpy/_core/tests/test_dlpack.py +190 -0
- numpy/_core/tests/test_dtype.py +2110 -0
- numpy/_core/tests/test_einsum.py +1351 -0
- numpy/_core/tests/test_errstate.py +131 -0
- numpy/_core/tests/test_extint128.py +217 -0
- numpy/_core/tests/test_finfo.py +86 -0
- numpy/_core/tests/test_function_base.py +504 -0
- numpy/_core/tests/test_getlimits.py +171 -0
- numpy/_core/tests/test_half.py +593 -0
- numpy/_core/tests/test_hashtable.py +36 -0
- numpy/_core/tests/test_indexerrors.py +122 -0
- numpy/_core/tests/test_indexing.py +1692 -0
- numpy/_core/tests/test_item_selection.py +167 -0
- numpy/_core/tests/test_limited_api.py +102 -0
- numpy/_core/tests/test_longdouble.py +370 -0
- numpy/_core/tests/test_mem_overlap.py +933 -0
- numpy/_core/tests/test_mem_policy.py +453 -0
- numpy/_core/tests/test_memmap.py +248 -0
- numpy/_core/tests/test_multiarray.py +11008 -0
- numpy/_core/tests/test_multiprocessing.py +55 -0
- numpy/_core/tests/test_multithreading.py +353 -0
- numpy/_core/tests/test_nditer.py +3533 -0
- numpy/_core/tests/test_nep50_promotions.py +287 -0
- numpy/_core/tests/test_numeric.py +4295 -0
- numpy/_core/tests/test_numerictypes.py +650 -0
- numpy/_core/tests/test_overrides.py +800 -0
- numpy/_core/tests/test_print.py +202 -0
- numpy/_core/tests/test_protocols.py +46 -0
- numpy/_core/tests/test_records.py +544 -0
- numpy/_core/tests/test_regression.py +2677 -0
- numpy/_core/tests/test_scalar_ctors.py +203 -0
- numpy/_core/tests/test_scalar_methods.py +328 -0
- numpy/_core/tests/test_scalarbuffer.py +153 -0
- numpy/_core/tests/test_scalarinherit.py +105 -0
- numpy/_core/tests/test_scalarmath.py +1168 -0
- numpy/_core/tests/test_scalarprint.py +403 -0
- numpy/_core/tests/test_shape_base.py +904 -0
- numpy/_core/tests/test_simd.py +1345 -0
- numpy/_core/tests/test_simd_module.py +105 -0
- numpy/_core/tests/test_stringdtype.py +1855 -0
- numpy/_core/tests/test_strings.py +1515 -0
- numpy/_core/tests/test_ufunc.py +3405 -0
- numpy/_core/tests/test_umath.py +4962 -0
- numpy/_core/tests/test_umath_accuracy.py +132 -0
- numpy/_core/tests/test_umath_complex.py +631 -0
- numpy/_core/tests/test_unicode.py +369 -0
- numpy/_core/umath.py +60 -0
- numpy/_core/umath.pyi +232 -0
- numpy/_distributor_init.py +15 -0
- numpy/_distributor_init.pyi +1 -0
- numpy/_expired_attrs_2_0.py +78 -0
- numpy/_expired_attrs_2_0.pyi +61 -0
- numpy/_globals.py +121 -0
- numpy/_globals.pyi +17 -0
- numpy/_pyinstaller/__init__.py +0 -0
- numpy/_pyinstaller/__init__.pyi +0 -0
- numpy/_pyinstaller/hook-numpy.py +36 -0
- numpy/_pyinstaller/hook-numpy.pyi +6 -0
- numpy/_pyinstaller/tests/__init__.py +16 -0
- numpy/_pyinstaller/tests/pyinstaller-smoke.py +32 -0
- numpy/_pyinstaller/tests/test_pyinstaller.py +35 -0
- numpy/_pytesttester.py +201 -0
- numpy/_pytesttester.pyi +18 -0
- numpy/_typing/__init__.py +173 -0
- numpy/_typing/_add_docstring.py +153 -0
- numpy/_typing/_array_like.py +106 -0
- numpy/_typing/_char_codes.py +213 -0
- numpy/_typing/_dtype_like.py +114 -0
- numpy/_typing/_extended_precision.py +15 -0
- numpy/_typing/_nbit.py +19 -0
- numpy/_typing/_nbit_base.py +94 -0
- numpy/_typing/_nbit_base.pyi +39 -0
- numpy/_typing/_nested_sequence.py +79 -0
- numpy/_typing/_scalars.py +20 -0
- numpy/_typing/_shape.py +8 -0
- numpy/_typing/_ufunc.py +7 -0
- numpy/_typing/_ufunc.pyi +975 -0
- numpy/_utils/__init__.py +95 -0
- numpy/_utils/__init__.pyi +28 -0
- numpy/_utils/_convertions.py +18 -0
- numpy/_utils/_convertions.pyi +4 -0
- numpy/_utils/_inspect.py +192 -0
- numpy/_utils/_inspect.pyi +70 -0
- numpy/_utils/_pep440.py +486 -0
- numpy/_utils/_pep440.pyi +118 -0
- numpy/char/__init__.py +2 -0
- numpy/char/__init__.pyi +111 -0
- numpy/conftest.py +248 -0
- numpy/core/__init__.py +33 -0
- numpy/core/__init__.pyi +0 -0
- numpy/core/_dtype.py +10 -0
- numpy/core/_dtype.pyi +0 -0
- numpy/core/_dtype_ctypes.py +10 -0
- numpy/core/_dtype_ctypes.pyi +0 -0
- numpy/core/_internal.py +27 -0
- numpy/core/_multiarray_umath.py +57 -0
- numpy/core/_utils.py +21 -0
- numpy/core/arrayprint.py +10 -0
- numpy/core/defchararray.py +10 -0
- numpy/core/einsumfunc.py +10 -0
- numpy/core/fromnumeric.py +10 -0
- numpy/core/function_base.py +10 -0
- numpy/core/getlimits.py +10 -0
- numpy/core/multiarray.py +25 -0
- numpy/core/numeric.py +12 -0
- numpy/core/numerictypes.py +10 -0
- numpy/core/overrides.py +10 -0
- numpy/core/overrides.pyi +7 -0
- numpy/core/records.py +10 -0
- numpy/core/shape_base.py +10 -0
- numpy/core/umath.py +10 -0
- numpy/ctypeslib/__init__.py +13 -0
- numpy/ctypeslib/__init__.pyi +15 -0
- numpy/ctypeslib/_ctypeslib.py +603 -0
- numpy/ctypeslib/_ctypeslib.pyi +236 -0
- numpy/doc/ufuncs.py +138 -0
- numpy/dtypes.py +41 -0
- numpy/dtypes.pyi +630 -0
- numpy/exceptions.py +246 -0
- numpy/exceptions.pyi +27 -0
- numpy/f2py/__init__.py +86 -0
- numpy/f2py/__init__.pyi +5 -0
- numpy/f2py/__main__.py +5 -0
- numpy/f2py/__version__.py +1 -0
- numpy/f2py/__version__.pyi +1 -0
- numpy/f2py/_backends/__init__.py +9 -0
- numpy/f2py/_backends/__init__.pyi +5 -0
- numpy/f2py/_backends/_backend.py +44 -0
- numpy/f2py/_backends/_backend.pyi +46 -0
- numpy/f2py/_backends/_distutils.py +76 -0
- numpy/f2py/_backends/_distutils.pyi +13 -0
- numpy/f2py/_backends/_meson.py +244 -0
- numpy/f2py/_backends/_meson.pyi +62 -0
- numpy/f2py/_backends/meson.build.template +58 -0
- numpy/f2py/_isocbind.py +62 -0
- numpy/f2py/_isocbind.pyi +13 -0
- numpy/f2py/_src_pyf.py +247 -0
- numpy/f2py/_src_pyf.pyi +28 -0
- numpy/f2py/auxfuncs.py +1004 -0
- numpy/f2py/auxfuncs.pyi +262 -0
- numpy/f2py/capi_maps.py +811 -0
- numpy/f2py/capi_maps.pyi +33 -0
- numpy/f2py/cb_rules.py +665 -0
- numpy/f2py/cb_rules.pyi +17 -0
- numpy/f2py/cfuncs.py +1563 -0
- numpy/f2py/cfuncs.pyi +31 -0
- numpy/f2py/common_rules.py +143 -0
- numpy/f2py/common_rules.pyi +9 -0
- numpy/f2py/crackfortran.py +3725 -0
- numpy/f2py/crackfortran.pyi +266 -0
- numpy/f2py/diagnose.py +149 -0
- numpy/f2py/diagnose.pyi +1 -0
- numpy/f2py/f2py2e.py +788 -0
- numpy/f2py/f2py2e.pyi +74 -0
- numpy/f2py/f90mod_rules.py +269 -0
- numpy/f2py/f90mod_rules.pyi +16 -0
- numpy/f2py/func2subr.py +329 -0
- numpy/f2py/func2subr.pyi +7 -0
- numpy/f2py/rules.py +1629 -0
- numpy/f2py/rules.pyi +41 -0
- numpy/f2py/setup.cfg +3 -0
- numpy/f2py/src/fortranobject.c +1436 -0
- numpy/f2py/src/fortranobject.h +173 -0
- numpy/f2py/symbolic.py +1518 -0
- numpy/f2py/symbolic.pyi +219 -0
- numpy/f2py/tests/__init__.py +16 -0
- numpy/f2py/tests/src/abstract_interface/foo.f90 +34 -0
- numpy/f2py/tests/src/abstract_interface/gh18403_mod.f90 +6 -0
- numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +235 -0
- numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap +1 -0
- numpy/f2py/tests/src/assumed_shape/foo_free.f90 +34 -0
- numpy/f2py/tests/src/assumed_shape/foo_mod.f90 +41 -0
- numpy/f2py/tests/src/assumed_shape/foo_use.f90 +19 -0
- numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
- numpy/f2py/tests/src/block_docstring/foo.f +6 -0
- numpy/f2py/tests/src/callback/foo.f +62 -0
- numpy/f2py/tests/src/callback/gh17797.f90 +7 -0
- numpy/f2py/tests/src/callback/gh18335.f90 +17 -0
- numpy/f2py/tests/src/callback/gh25211.f +10 -0
- numpy/f2py/tests/src/callback/gh25211.pyf +18 -0
- numpy/f2py/tests/src/callback/gh26681.f90 +18 -0
- numpy/f2py/tests/src/cli/gh_22819.pyf +6 -0
- numpy/f2py/tests/src/cli/hi77.f +3 -0
- numpy/f2py/tests/src/cli/hiworld.f90 +3 -0
- numpy/f2py/tests/src/common/block.f +11 -0
- numpy/f2py/tests/src/common/gh19161.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
- numpy/f2py/tests/src/crackfortran/common_with_division.f +17 -0
- numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
- numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
- numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
- numpy/f2py/tests/src/crackfortran/data_with_comments.f +8 -0
- numpy/f2py/tests/src/crackfortran/foo_deps.f90 +6 -0
- numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
- numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
- numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
- numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
- numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
- numpy/f2py/tests/src/crackfortran/gh23598Warn.f90 +11 -0
- numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
- numpy/f2py/tests/src/crackfortran/gh27697.f90 +12 -0
- numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
- numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
- numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
- numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
- numpy/f2py/tests/src/f2cmap/.f2py_f2cmap +1 -0
- numpy/f2py/tests/src/f2cmap/isoFortranEnvMap.f90 +9 -0
- numpy/f2py/tests/src/isocintrin/isoCtests.f90 +34 -0
- numpy/f2py/tests/src/kind/foo.f90 +20 -0
- numpy/f2py/tests/src/mixed/foo.f +5 -0
- numpy/f2py/tests/src/mixed/foo_fixed.f90 +8 -0
- numpy/f2py/tests/src/mixed/foo_free.f90 +8 -0
- numpy/f2py/tests/src/modules/gh25337/data.f90 +8 -0
- numpy/f2py/tests/src/modules/gh25337/use_data.f90 +6 -0
- numpy/f2py/tests/src/modules/gh26920/two_mods_with_no_public_entities.f90 +21 -0
- numpy/f2py/tests/src/modules/gh26920/two_mods_with_one_public_routine.f90 +21 -0
- numpy/f2py/tests/src/modules/module_data_docstring.f90 +12 -0
- numpy/f2py/tests/src/modules/use_modules.f90 +20 -0
- numpy/f2py/tests/src/negative_bounds/issue_20853.f90 +7 -0
- numpy/f2py/tests/src/parameter/constant_array.f90 +45 -0
- numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
- numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
- numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
- numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
- numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
- numpy/f2py/tests/src/quoted_character/foo.f +14 -0
- numpy/f2py/tests/src/regression/AB.inc +1 -0
- numpy/f2py/tests/src/regression/assignOnlyModule.f90 +25 -0
- numpy/f2py/tests/src/regression/datonly.f90 +17 -0
- numpy/f2py/tests/src/regression/f77comments.f +26 -0
- numpy/f2py/tests/src/regression/f77fixedform.f95 +5 -0
- numpy/f2py/tests/src/regression/f90continuation.f90 +9 -0
- numpy/f2py/tests/src/regression/incfile.f90 +5 -0
- numpy/f2py/tests/src/regression/inout.f90 +9 -0
- numpy/f2py/tests/src/regression/lower_f2py_fortran.f90 +5 -0
- numpy/f2py/tests/src/regression/mod_derived_types.f90 +23 -0
- numpy/f2py/tests/src/return_character/foo77.f +45 -0
- numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
- numpy/f2py/tests/src/return_complex/foo77.f +45 -0
- numpy/f2py/tests/src/return_complex/foo90.f90 +48 -0
- numpy/f2py/tests/src/return_integer/foo77.f +56 -0
- numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
- numpy/f2py/tests/src/return_logical/foo77.f +56 -0
- numpy/f2py/tests/src/return_logical/foo90.f90 +59 -0
- numpy/f2py/tests/src/return_real/foo77.f +45 -0
- numpy/f2py/tests/src/return_real/foo90.f90 +48 -0
- numpy/f2py/tests/src/routines/funcfortranname.f +5 -0
- numpy/f2py/tests/src/routines/funcfortranname.pyf +11 -0
- numpy/f2py/tests/src/routines/subrout.f +4 -0
- numpy/f2py/tests/src/routines/subrout.pyf +10 -0
- numpy/f2py/tests/src/size/foo.f90 +44 -0
- numpy/f2py/tests/src/string/char.f90 +29 -0
- numpy/f2py/tests/src/string/fixed_string.f90 +34 -0
- numpy/f2py/tests/src/string/gh24008.f +8 -0
- numpy/f2py/tests/src/string/gh24662.f90 +7 -0
- numpy/f2py/tests/src/string/gh25286.f90 +14 -0
- numpy/f2py/tests/src/string/gh25286.pyf +12 -0
- numpy/f2py/tests/src/string/gh25286_bc.pyf +12 -0
- numpy/f2py/tests/src/string/scalar_string.f90 +9 -0
- numpy/f2py/tests/src/string/string.f +12 -0
- numpy/f2py/tests/src/value_attrspec/gh21665.f90 +9 -0
- numpy/f2py/tests/test_abstract_interface.py +26 -0
- numpy/f2py/tests/test_array_from_pyobj.py +678 -0
- numpy/f2py/tests/test_assumed_shape.py +50 -0
- numpy/f2py/tests/test_block_docstring.py +20 -0
- numpy/f2py/tests/test_callback.py +263 -0
- numpy/f2py/tests/test_character.py +641 -0
- numpy/f2py/tests/test_common.py +23 -0
- numpy/f2py/tests/test_crackfortran.py +421 -0
- numpy/f2py/tests/test_data.py +71 -0
- numpy/f2py/tests/test_docs.py +66 -0
- numpy/f2py/tests/test_f2cmap.py +17 -0
- numpy/f2py/tests/test_f2py2e.py +983 -0
- numpy/f2py/tests/test_isoc.py +56 -0
- numpy/f2py/tests/test_kind.py +52 -0
- numpy/f2py/tests/test_mixed.py +35 -0
- numpy/f2py/tests/test_modules.py +83 -0
- numpy/f2py/tests/test_parameter.py +129 -0
- numpy/f2py/tests/test_pyf_src.py +43 -0
- numpy/f2py/tests/test_quoted_character.py +18 -0
- numpy/f2py/tests/test_regression.py +187 -0
- numpy/f2py/tests/test_return_character.py +48 -0
- numpy/f2py/tests/test_return_complex.py +67 -0
- numpy/f2py/tests/test_return_integer.py +55 -0
- numpy/f2py/tests/test_return_logical.py +65 -0
- numpy/f2py/tests/test_return_real.py +109 -0
- numpy/f2py/tests/test_routines.py +29 -0
- numpy/f2py/tests/test_semicolon_split.py +75 -0
- numpy/f2py/tests/test_size.py +45 -0
- numpy/f2py/tests/test_string.py +100 -0
- numpy/f2py/tests/test_symbolic.py +500 -0
- numpy/f2py/tests/test_value_attrspec.py +15 -0
- numpy/f2py/tests/util.py +442 -0
- numpy/f2py/use_rules.py +99 -0
- numpy/f2py/use_rules.pyi +9 -0
- numpy/fft/__init__.py +213 -0
- numpy/fft/__init__.pyi +38 -0
- numpy/fft/_helper.py +235 -0
- numpy/fft/_helper.pyi +44 -0
- numpy/fft/_pocketfft.py +1693 -0
- numpy/fft/_pocketfft.pyi +137 -0
- numpy/fft/_pocketfft_umath.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/fft/tests/__init__.py +0 -0
- numpy/fft/tests/test_helper.py +167 -0
- numpy/fft/tests/test_pocketfft.py +589 -0
- numpy/lib/__init__.py +97 -0
- numpy/lib/__init__.pyi +52 -0
- numpy/lib/_array_utils_impl.py +62 -0
- numpy/lib/_array_utils_impl.pyi +10 -0
- numpy/lib/_arraypad_impl.py +926 -0
- numpy/lib/_arraypad_impl.pyi +88 -0
- numpy/lib/_arraysetops_impl.py +1158 -0
- numpy/lib/_arraysetops_impl.pyi +462 -0
- numpy/lib/_arrayterator_impl.py +224 -0
- numpy/lib/_arrayterator_impl.pyi +45 -0
- numpy/lib/_datasource.py +700 -0
- numpy/lib/_datasource.pyi +30 -0
- numpy/lib/_format_impl.py +1036 -0
- numpy/lib/_format_impl.pyi +56 -0
- numpy/lib/_function_base_impl.py +5758 -0
- numpy/lib/_function_base_impl.pyi +2324 -0
- numpy/lib/_histograms_impl.py +1085 -0
- numpy/lib/_histograms_impl.pyi +40 -0
- numpy/lib/_index_tricks_impl.py +1048 -0
- numpy/lib/_index_tricks_impl.pyi +267 -0
- numpy/lib/_iotools.py +900 -0
- numpy/lib/_iotools.pyi +116 -0
- numpy/lib/_nanfunctions_impl.py +2001 -0
- numpy/lib/_nanfunctions_impl.pyi +48 -0
- numpy/lib/_npyio_impl.py +2583 -0
- numpy/lib/_npyio_impl.pyi +299 -0
- numpy/lib/_polynomial_impl.py +1465 -0
- numpy/lib/_polynomial_impl.pyi +338 -0
- numpy/lib/_scimath_impl.py +642 -0
- numpy/lib/_scimath_impl.pyi +93 -0
- numpy/lib/_shape_base_impl.py +1289 -0
- numpy/lib/_shape_base_impl.pyi +236 -0
- numpy/lib/_stride_tricks_impl.py +582 -0
- numpy/lib/_stride_tricks_impl.pyi +73 -0
- numpy/lib/_twodim_base_impl.py +1201 -0
- numpy/lib/_twodim_base_impl.pyi +408 -0
- numpy/lib/_type_check_impl.py +710 -0
- numpy/lib/_type_check_impl.pyi +348 -0
- numpy/lib/_ufunclike_impl.py +199 -0
- numpy/lib/_ufunclike_impl.pyi +60 -0
- numpy/lib/_user_array_impl.py +310 -0
- numpy/lib/_user_array_impl.pyi +226 -0
- numpy/lib/_utils_impl.py +784 -0
- numpy/lib/_utils_impl.pyi +22 -0
- numpy/lib/_version.py +153 -0
- numpy/lib/_version.pyi +17 -0
- numpy/lib/array_utils.py +7 -0
- numpy/lib/array_utils.pyi +6 -0
- numpy/lib/format.py +24 -0
- numpy/lib/format.pyi +24 -0
- numpy/lib/introspect.py +94 -0
- numpy/lib/introspect.pyi +3 -0
- numpy/lib/mixins.py +180 -0
- numpy/lib/mixins.pyi +78 -0
- numpy/lib/npyio.py +1 -0
- numpy/lib/npyio.pyi +5 -0
- numpy/lib/recfunctions.py +1681 -0
- numpy/lib/recfunctions.pyi +444 -0
- numpy/lib/scimath.py +13 -0
- numpy/lib/scimath.pyi +12 -0
- numpy/lib/stride_tricks.py +1 -0
- numpy/lib/stride_tricks.pyi +4 -0
- numpy/lib/tests/__init__.py +0 -0
- numpy/lib/tests/data/py2-np0-objarr.npy +0 -0
- numpy/lib/tests/data/py2-objarr.npy +0 -0
- numpy/lib/tests/data/py2-objarr.npz +0 -0
- numpy/lib/tests/data/py3-objarr.npy +0 -0
- numpy/lib/tests/data/py3-objarr.npz +0 -0
- numpy/lib/tests/data/python3.npy +0 -0
- numpy/lib/tests/data/win64python2.npy +0 -0
- numpy/lib/tests/test__datasource.py +328 -0
- numpy/lib/tests/test__iotools.py +358 -0
- numpy/lib/tests/test__version.py +64 -0
- numpy/lib/tests/test_array_utils.py +32 -0
- numpy/lib/tests/test_arraypad.py +1427 -0
- numpy/lib/tests/test_arraysetops.py +1302 -0
- numpy/lib/tests/test_arrayterator.py +45 -0
- numpy/lib/tests/test_format.py +1054 -0
- numpy/lib/tests/test_function_base.py +4705 -0
- numpy/lib/tests/test_histograms.py +855 -0
- numpy/lib/tests/test_index_tricks.py +693 -0
- numpy/lib/tests/test_io.py +2857 -0
- numpy/lib/tests/test_loadtxt.py +1099 -0
- numpy/lib/tests/test_mixins.py +215 -0
- numpy/lib/tests/test_nanfunctions.py +1438 -0
- numpy/lib/tests/test_packbits.py +376 -0
- numpy/lib/tests/test_polynomial.py +325 -0
- numpy/lib/tests/test_recfunctions.py +1042 -0
- numpy/lib/tests/test_regression.py +231 -0
- numpy/lib/tests/test_shape_base.py +813 -0
- numpy/lib/tests/test_stride_tricks.py +655 -0
- numpy/lib/tests/test_twodim_base.py +559 -0
- numpy/lib/tests/test_type_check.py +473 -0
- numpy/lib/tests/test_ufunclike.py +97 -0
- numpy/lib/tests/test_utils.py +80 -0
- numpy/lib/user_array.py +1 -0
- numpy/lib/user_array.pyi +1 -0
- numpy/linalg/__init__.py +95 -0
- numpy/linalg/__init__.pyi +71 -0
- numpy/linalg/_linalg.py +3657 -0
- numpy/linalg/_linalg.pyi +548 -0
- numpy/linalg/_umath_linalg.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/linalg/_umath_linalg.pyi +60 -0
- numpy/linalg/lapack_lite.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/linalg/lapack_lite.pyi +143 -0
- numpy/linalg/tests/__init__.py +0 -0
- numpy/linalg/tests/test_deprecations.py +21 -0
- numpy/linalg/tests/test_linalg.py +2442 -0
- numpy/linalg/tests/test_regression.py +182 -0
- numpy/ma/API_CHANGES.txt +135 -0
- numpy/ma/LICENSE +24 -0
- numpy/ma/README.rst +236 -0
- numpy/ma/__init__.py +53 -0
- numpy/ma/__init__.pyi +458 -0
- numpy/ma/core.py +8929 -0
- numpy/ma/core.pyi +3720 -0
- numpy/ma/extras.py +2266 -0
- numpy/ma/extras.pyi +297 -0
- numpy/ma/mrecords.py +762 -0
- numpy/ma/mrecords.pyi +96 -0
- numpy/ma/tests/__init__.py +0 -0
- numpy/ma/tests/test_arrayobject.py +40 -0
- numpy/ma/tests/test_core.py +6008 -0
- numpy/ma/tests/test_deprecations.py +65 -0
- numpy/ma/tests/test_extras.py +1945 -0
- numpy/ma/tests/test_mrecords.py +495 -0
- numpy/ma/tests/test_old_ma.py +939 -0
- numpy/ma/tests/test_regression.py +83 -0
- numpy/ma/tests/test_subclassing.py +469 -0
- numpy/ma/testutils.py +294 -0
- numpy/ma/testutils.pyi +69 -0
- numpy/matlib.py +380 -0
- numpy/matlib.pyi +580 -0
- numpy/matrixlib/__init__.py +12 -0
- numpy/matrixlib/__init__.pyi +3 -0
- numpy/matrixlib/defmatrix.py +1119 -0
- numpy/matrixlib/defmatrix.pyi +218 -0
- numpy/matrixlib/tests/__init__.py +0 -0
- numpy/matrixlib/tests/test_defmatrix.py +455 -0
- numpy/matrixlib/tests/test_interaction.py +360 -0
- numpy/matrixlib/tests/test_masked_matrix.py +240 -0
- numpy/matrixlib/tests/test_matrix_linalg.py +110 -0
- numpy/matrixlib/tests/test_multiarray.py +17 -0
- numpy/matrixlib/tests/test_numeric.py +18 -0
- numpy/matrixlib/tests/test_regression.py +31 -0
- numpy/polynomial/__init__.py +187 -0
- numpy/polynomial/__init__.pyi +31 -0
- numpy/polynomial/_polybase.py +1191 -0
- numpy/polynomial/_polybase.pyi +262 -0
- numpy/polynomial/_polytypes.pyi +501 -0
- numpy/polynomial/chebyshev.py +2001 -0
- numpy/polynomial/chebyshev.pyi +180 -0
- numpy/polynomial/hermite.py +1738 -0
- numpy/polynomial/hermite.pyi +106 -0
- numpy/polynomial/hermite_e.py +1640 -0
- numpy/polynomial/hermite_e.pyi +106 -0
- numpy/polynomial/laguerre.py +1673 -0
- numpy/polynomial/laguerre.pyi +100 -0
- numpy/polynomial/legendre.py +1603 -0
- numpy/polynomial/legendre.pyi +100 -0
- numpy/polynomial/polynomial.py +1625 -0
- numpy/polynomial/polynomial.pyi +109 -0
- numpy/polynomial/polyutils.py +759 -0
- numpy/polynomial/polyutils.pyi +307 -0
- numpy/polynomial/tests/__init__.py +0 -0
- numpy/polynomial/tests/test_chebyshev.py +618 -0
- numpy/polynomial/tests/test_classes.py +613 -0
- numpy/polynomial/tests/test_hermite.py +553 -0
- numpy/polynomial/tests/test_hermite_e.py +554 -0
- numpy/polynomial/tests/test_laguerre.py +535 -0
- numpy/polynomial/tests/test_legendre.py +566 -0
- numpy/polynomial/tests/test_polynomial.py +691 -0
- numpy/polynomial/tests/test_polyutils.py +123 -0
- numpy/polynomial/tests/test_printing.py +557 -0
- numpy/polynomial/tests/test_symbol.py +217 -0
- numpy/py.typed +0 -0
- numpy/random/LICENSE.md +71 -0
- numpy/random/__init__.pxd +14 -0
- numpy/random/__init__.py +213 -0
- numpy/random/__init__.pyi +124 -0
- numpy/random/_bounded_integers.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_bounded_integers.pxd +29 -0
- numpy/random/_bounded_integers.pyi +1 -0
- numpy/random/_common.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_common.pxd +107 -0
- numpy/random/_common.pyi +16 -0
- numpy/random/_examples/cffi/extending.py +44 -0
- numpy/random/_examples/cffi/parse.py +53 -0
- numpy/random/_examples/cython/extending.pyx +77 -0
- numpy/random/_examples/cython/extending_distributions.pyx +117 -0
- numpy/random/_examples/cython/meson.build +53 -0
- numpy/random/_examples/numba/extending.py +86 -0
- numpy/random/_examples/numba/extending_distributions.py +67 -0
- numpy/random/_generator.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_generator.pyi +862 -0
- numpy/random/_mt19937.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_mt19937.pyi +27 -0
- numpy/random/_pcg64.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_pcg64.pyi +41 -0
- numpy/random/_philox.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_philox.pyi +36 -0
- numpy/random/_pickle.py +88 -0
- numpy/random/_pickle.pyi +43 -0
- numpy/random/_sfc64.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/_sfc64.pyi +25 -0
- numpy/random/bit_generator.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/bit_generator.pxd +35 -0
- numpy/random/bit_generator.pyi +123 -0
- numpy/random/c_distributions.pxd +119 -0
- numpy/random/lib/libnpyrandom.a +0 -0
- numpy/random/mtrand.cpython-313t-aarch64-linux-musl.so +0 -0
- numpy/random/mtrand.pyi +759 -0
- numpy/random/tests/__init__.py +0 -0
- numpy/random/tests/data/__init__.py +0 -0
- numpy/random/tests/data/generator_pcg64_np121.pkl.gz +0 -0
- numpy/random/tests/data/generator_pcg64_np126.pkl.gz +0 -0
- numpy/random/tests/data/mt19937-testset-1.csv +1001 -0
- numpy/random/tests/data/mt19937-testset-2.csv +1001 -0
- numpy/random/tests/data/pcg64-testset-1.csv +1001 -0
- numpy/random/tests/data/pcg64-testset-2.csv +1001 -0
- numpy/random/tests/data/pcg64dxsm-testset-1.csv +1001 -0
- numpy/random/tests/data/pcg64dxsm-testset-2.csv +1001 -0
- numpy/random/tests/data/philox-testset-1.csv +1001 -0
- numpy/random/tests/data/philox-testset-2.csv +1001 -0
- numpy/random/tests/data/sfc64-testset-1.csv +1001 -0
- numpy/random/tests/data/sfc64-testset-2.csv +1001 -0
- numpy/random/tests/data/sfc64_np126.pkl.gz +0 -0
- numpy/random/tests/test_direct.py +595 -0
- numpy/random/tests/test_extending.py +131 -0
- numpy/random/tests/test_generator_mt19937.py +2825 -0
- numpy/random/tests/test_generator_mt19937_regressions.py +221 -0
- numpy/random/tests/test_random.py +1724 -0
- numpy/random/tests/test_randomstate.py +2099 -0
- numpy/random/tests/test_randomstate_regression.py +213 -0
- numpy/random/tests/test_regression.py +175 -0
- numpy/random/tests/test_seed_sequence.py +79 -0
- numpy/random/tests/test_smoke.py +882 -0
- numpy/rec/__init__.py +2 -0
- numpy/rec/__init__.pyi +23 -0
- numpy/strings/__init__.py +2 -0
- numpy/strings/__init__.pyi +97 -0
- numpy/testing/__init__.py +22 -0
- numpy/testing/__init__.pyi +107 -0
- numpy/testing/_private/__init__.py +0 -0
- numpy/testing/_private/__init__.pyi +0 -0
- numpy/testing/_private/extbuild.py +250 -0
- numpy/testing/_private/extbuild.pyi +25 -0
- numpy/testing/_private/utils.py +2830 -0
- numpy/testing/_private/utils.pyi +505 -0
- numpy/testing/overrides.py +84 -0
- numpy/testing/overrides.pyi +10 -0
- numpy/testing/print_coercion_tables.py +207 -0
- numpy/testing/print_coercion_tables.pyi +26 -0
- numpy/testing/tests/__init__.py +0 -0
- numpy/testing/tests/test_utils.py +2123 -0
- numpy/tests/__init__.py +0 -0
- numpy/tests/test__all__.py +10 -0
- numpy/tests/test_configtool.py +51 -0
- numpy/tests/test_ctypeslib.py +383 -0
- numpy/tests/test_lazyloading.py +42 -0
- numpy/tests/test_matlib.py +59 -0
- numpy/tests/test_numpy_config.py +47 -0
- numpy/tests/test_numpy_version.py +54 -0
- numpy/tests/test_public_api.py +804 -0
- numpy/tests/test_reloading.py +76 -0
- numpy/tests/test_scripts.py +48 -0
- numpy/tests/test_warnings.py +79 -0
- numpy/typing/__init__.py +233 -0
- numpy/typing/__init__.pyi +3 -0
- numpy/typing/mypy_plugin.py +200 -0
- numpy/typing/tests/__init__.py +0 -0
- numpy/typing/tests/data/fail/arithmetic.pyi +126 -0
- numpy/typing/tests/data/fail/array_constructors.pyi +34 -0
- numpy/typing/tests/data/fail/array_like.pyi +15 -0
- numpy/typing/tests/data/fail/array_pad.pyi +6 -0
- numpy/typing/tests/data/fail/arrayprint.pyi +15 -0
- numpy/typing/tests/data/fail/arrayterator.pyi +14 -0
- numpy/typing/tests/data/fail/bitwise_ops.pyi +17 -0
- numpy/typing/tests/data/fail/char.pyi +63 -0
- numpy/typing/tests/data/fail/chararray.pyi +61 -0
- numpy/typing/tests/data/fail/comparisons.pyi +27 -0
- numpy/typing/tests/data/fail/constants.pyi +3 -0
- numpy/typing/tests/data/fail/datasource.pyi +16 -0
- numpy/typing/tests/data/fail/dtype.pyi +17 -0
- numpy/typing/tests/data/fail/einsumfunc.pyi +12 -0
- numpy/typing/tests/data/fail/flatiter.pyi +38 -0
- numpy/typing/tests/data/fail/fromnumeric.pyi +148 -0
- numpy/typing/tests/data/fail/histograms.pyi +12 -0
- numpy/typing/tests/data/fail/index_tricks.pyi +14 -0
- numpy/typing/tests/data/fail/lib_function_base.pyi +60 -0
- numpy/typing/tests/data/fail/lib_polynomial.pyi +29 -0
- numpy/typing/tests/data/fail/lib_utils.pyi +3 -0
- numpy/typing/tests/data/fail/lib_version.pyi +6 -0
- numpy/typing/tests/data/fail/linalg.pyi +52 -0
- numpy/typing/tests/data/fail/ma.pyi +155 -0
- numpy/typing/tests/data/fail/memmap.pyi +5 -0
- numpy/typing/tests/data/fail/modules.pyi +17 -0
- numpy/typing/tests/data/fail/multiarray.pyi +52 -0
- numpy/typing/tests/data/fail/ndarray.pyi +11 -0
- numpy/typing/tests/data/fail/ndarray_misc.pyi +49 -0
- numpy/typing/tests/data/fail/nditer.pyi +8 -0
- numpy/typing/tests/data/fail/nested_sequence.pyi +17 -0
- numpy/typing/tests/data/fail/npyio.pyi +24 -0
- numpy/typing/tests/data/fail/numerictypes.pyi +5 -0
- numpy/typing/tests/data/fail/random.pyi +62 -0
- numpy/typing/tests/data/fail/rec.pyi +17 -0
- numpy/typing/tests/data/fail/scalars.pyi +86 -0
- numpy/typing/tests/data/fail/shape.pyi +7 -0
- numpy/typing/tests/data/fail/shape_base.pyi +8 -0
- numpy/typing/tests/data/fail/stride_tricks.pyi +9 -0
- numpy/typing/tests/data/fail/strings.pyi +52 -0
- numpy/typing/tests/data/fail/testing.pyi +28 -0
- numpy/typing/tests/data/fail/twodim_base.pyi +39 -0
- numpy/typing/tests/data/fail/type_check.pyi +12 -0
- numpy/typing/tests/data/fail/ufunc_config.pyi +21 -0
- numpy/typing/tests/data/fail/ufunclike.pyi +21 -0
- numpy/typing/tests/data/fail/ufuncs.pyi +17 -0
- numpy/typing/tests/data/fail/warnings_and_errors.pyi +5 -0
- numpy/typing/tests/data/misc/extended_precision.pyi +9 -0
- numpy/typing/tests/data/mypy.ini +8 -0
- numpy/typing/tests/data/pass/arithmetic.py +614 -0
- numpy/typing/tests/data/pass/array_constructors.py +138 -0
- numpy/typing/tests/data/pass/array_like.py +43 -0
- numpy/typing/tests/data/pass/arrayprint.py +37 -0
- numpy/typing/tests/data/pass/arrayterator.py +28 -0
- numpy/typing/tests/data/pass/bitwise_ops.py +131 -0
- numpy/typing/tests/data/pass/comparisons.py +316 -0
- numpy/typing/tests/data/pass/dtype.py +57 -0
- numpy/typing/tests/data/pass/einsumfunc.py +36 -0
- numpy/typing/tests/data/pass/flatiter.py +26 -0
- numpy/typing/tests/data/pass/fromnumeric.py +272 -0
- numpy/typing/tests/data/pass/index_tricks.py +62 -0
- numpy/typing/tests/data/pass/lib_user_array.py +22 -0
- numpy/typing/tests/data/pass/lib_utils.py +19 -0
- numpy/typing/tests/data/pass/lib_version.py +18 -0
- numpy/typing/tests/data/pass/literal.py +52 -0
- numpy/typing/tests/data/pass/ma.py +199 -0
- numpy/typing/tests/data/pass/mod.py +149 -0
- numpy/typing/tests/data/pass/modules.py +45 -0
- numpy/typing/tests/data/pass/multiarray.py +77 -0
- numpy/typing/tests/data/pass/ndarray_conversion.py +81 -0
- numpy/typing/tests/data/pass/ndarray_misc.py +199 -0
- numpy/typing/tests/data/pass/ndarray_shape_manipulation.py +47 -0
- numpy/typing/tests/data/pass/nditer.py +4 -0
- numpy/typing/tests/data/pass/numeric.py +90 -0
- numpy/typing/tests/data/pass/numerictypes.py +17 -0
- numpy/typing/tests/data/pass/random.py +1498 -0
- numpy/typing/tests/data/pass/recfunctions.py +164 -0
- numpy/typing/tests/data/pass/scalars.py +249 -0
- numpy/typing/tests/data/pass/shape.py +19 -0
- numpy/typing/tests/data/pass/simple.py +170 -0
- numpy/typing/tests/data/pass/ufunc_config.py +64 -0
- numpy/typing/tests/data/pass/ufunclike.py +52 -0
- numpy/typing/tests/data/pass/ufuncs.py +16 -0
- numpy/typing/tests/data/pass/warnings_and_errors.py +6 -0
- numpy/typing/tests/data/reveal/arithmetic.pyi +719 -0
- numpy/typing/tests/data/reveal/array_api_info.pyi +70 -0
- numpy/typing/tests/data/reveal/array_constructors.pyi +277 -0
- numpy/typing/tests/data/reveal/arraypad.pyi +27 -0
- numpy/typing/tests/data/reveal/arrayprint.pyi +25 -0
- numpy/typing/tests/data/reveal/arraysetops.pyi +74 -0
- numpy/typing/tests/data/reveal/arrayterator.pyi +27 -0
- numpy/typing/tests/data/reveal/bitwise_ops.pyi +166 -0
- numpy/typing/tests/data/reveal/char.pyi +225 -0
- numpy/typing/tests/data/reveal/chararray.pyi +138 -0
- numpy/typing/tests/data/reveal/comparisons.pyi +264 -0
- numpy/typing/tests/data/reveal/constants.pyi +14 -0
- numpy/typing/tests/data/reveal/ctypeslib.pyi +81 -0
- numpy/typing/tests/data/reveal/datasource.pyi +23 -0
- numpy/typing/tests/data/reveal/dtype.pyi +132 -0
- numpy/typing/tests/data/reveal/einsumfunc.pyi +39 -0
- numpy/typing/tests/data/reveal/emath.pyi +54 -0
- numpy/typing/tests/data/reveal/fft.pyi +37 -0
- numpy/typing/tests/data/reveal/flatiter.pyi +86 -0
- numpy/typing/tests/data/reveal/fromnumeric.pyi +347 -0
- numpy/typing/tests/data/reveal/getlimits.pyi +53 -0
- numpy/typing/tests/data/reveal/histograms.pyi +25 -0
- numpy/typing/tests/data/reveal/index_tricks.pyi +70 -0
- numpy/typing/tests/data/reveal/lib_function_base.pyi +409 -0
- numpy/typing/tests/data/reveal/lib_polynomial.pyi +147 -0
- numpy/typing/tests/data/reveal/lib_utils.pyi +17 -0
- numpy/typing/tests/data/reveal/lib_version.pyi +20 -0
- numpy/typing/tests/data/reveal/linalg.pyi +154 -0
- numpy/typing/tests/data/reveal/ma.pyi +1098 -0
- numpy/typing/tests/data/reveal/matrix.pyi +73 -0
- numpy/typing/tests/data/reveal/memmap.pyi +19 -0
- numpy/typing/tests/data/reveal/mod.pyi +178 -0
- numpy/typing/tests/data/reveal/modules.pyi +51 -0
- numpy/typing/tests/data/reveal/multiarray.pyi +197 -0
- numpy/typing/tests/data/reveal/nbit_base_example.pyi +20 -0
- numpy/typing/tests/data/reveal/ndarray_assignability.pyi +82 -0
- numpy/typing/tests/data/reveal/ndarray_conversion.pyi +83 -0
- numpy/typing/tests/data/reveal/ndarray_misc.pyi +246 -0
- numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +47 -0
- numpy/typing/tests/data/reveal/nditer.pyi +49 -0
- numpy/typing/tests/data/reveal/nested_sequence.pyi +25 -0
- numpy/typing/tests/data/reveal/npyio.pyi +83 -0
- numpy/typing/tests/data/reveal/numeric.pyi +170 -0
- numpy/typing/tests/data/reveal/numerictypes.pyi +16 -0
- numpy/typing/tests/data/reveal/polynomial_polybase.pyi +217 -0
- numpy/typing/tests/data/reveal/polynomial_polyutils.pyi +218 -0
- numpy/typing/tests/data/reveal/polynomial_series.pyi +138 -0
- numpy/typing/tests/data/reveal/random.pyi +1546 -0
- numpy/typing/tests/data/reveal/rec.pyi +171 -0
- numpy/typing/tests/data/reveal/scalars.pyi +191 -0
- numpy/typing/tests/data/reveal/shape.pyi +13 -0
- numpy/typing/tests/data/reveal/shape_base.pyi +52 -0
- numpy/typing/tests/data/reveal/stride_tricks.pyi +27 -0
- numpy/typing/tests/data/reveal/strings.pyi +196 -0
- numpy/typing/tests/data/reveal/testing.pyi +198 -0
- numpy/typing/tests/data/reveal/twodim_base.pyi +225 -0
- numpy/typing/tests/data/reveal/type_check.pyi +67 -0
- numpy/typing/tests/data/reveal/ufunc_config.pyi +29 -0
- numpy/typing/tests/data/reveal/ufunclike.pyi +31 -0
- numpy/typing/tests/data/reveal/ufuncs.pyi +142 -0
- numpy/typing/tests/data/reveal/warnings_and_errors.pyi +11 -0
- numpy/typing/tests/test_isfile.py +38 -0
- numpy/typing/tests/test_runtime.py +110 -0
- numpy/typing/tests/test_typing.py +205 -0
- numpy/version.py +11 -0
- numpy/version.pyi +9 -0
- numpy-2.4.0.dist-info/METADATA +139 -0
- numpy-2.4.0.dist-info/RECORD +915 -0
- numpy-2.4.0.dist-info/WHEEL +5 -0
- numpy-2.4.0.dist-info/entry_points.txt +13 -0
- numpy-2.4.0.dist-info/licenses/LICENSE.txt +935 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/include/numpy/libdivide/LICENSE.txt +21 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/src/common/pythoncapi-compat/COPYING +14 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/src/highway/LICENSE +371 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/src/multiarray/dragon4_LICENSE.txt +27 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/src/npysort/x86-simd-sort/LICENSE.md +28 -0
- numpy-2.4.0.dist-info/licenses/numpy/_core/src/umath/svml/LICENSE +30 -0
- numpy-2.4.0.dist-info/licenses/numpy/fft/pocketfft/LICENSE.md +25 -0
- numpy-2.4.0.dist-info/licenses/numpy/linalg/lapack_lite/LICENSE.txt +48 -0
- numpy-2.4.0.dist-info/licenses/numpy/ma/LICENSE +24 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/LICENSE.md +71 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/distributions/LICENSE.md +61 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/mt19937/LICENSE.md +61 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/pcg64/LICENSE.md +22 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/philox/LICENSE.md +31 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/sfc64/LICENSE.md +27 -0
- numpy-2.4.0.dist-info/licenses/numpy/random/src/splitmix64/LICENSE.md +9 -0
- numpy.libs/libgcc_s-2d945d6c-767fb991.so.1 +0 -0
- numpy.libs/libgcc_s-2d945d6c.so.1 +0 -0
- numpy.libs/libgfortran-67378ab2-e7e7cfab.so.5.0.0 +0 -0
- numpy.libs/libscipy_openblas64_-1fc386ee.so +0 -0
- numpy.libs/libstdc++-85f2cd6d.so.6.0.33 +0 -0
|
@@ -0,0 +1,2825 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import os.path
|
|
3
|
+
import sys
|
|
4
|
+
import warnings
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from numpy.exceptions import AxisError
|
|
10
|
+
from numpy.linalg import LinAlgError
|
|
11
|
+
from numpy.random import MT19937, Generator, RandomState, SeedSequence
|
|
12
|
+
from numpy.testing import (
|
|
13
|
+
IS_WASM,
|
|
14
|
+
assert_,
|
|
15
|
+
assert_allclose,
|
|
16
|
+
assert_array_almost_equal,
|
|
17
|
+
assert_array_equal,
|
|
18
|
+
assert_equal,
|
|
19
|
+
assert_no_warnings,
|
|
20
|
+
assert_raises,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
random = Generator(MT19937())
|
|
24
|
+
|
|
25
|
+
JUMP_TEST_DATA = [
|
|
26
|
+
{
|
|
27
|
+
"seed": 0,
|
|
28
|
+
"steps": 10,
|
|
29
|
+
"initial": {"key_sha256": "bb1636883c2707b51c5b7fc26c6927af4430f2e0785a8c7bc886337f919f9edf", "pos": 9}, # noqa: E501
|
|
30
|
+
"jumped": {"key_sha256": "ff682ac12bb140f2d72fba8d3506cf4e46817a0db27aae1683867629031d8d55", "pos": 598}, # noqa: E501
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"seed": 384908324,
|
|
34
|
+
"steps": 312,
|
|
35
|
+
"initial": {"key_sha256": "16b791a1e04886ccbbb4d448d6ff791267dc458ae599475d08d5cced29d11614", "pos": 311}, # noqa: E501
|
|
36
|
+
"jumped": {"key_sha256": "a0110a2cf23b56be0feaed8f787a7fc84bef0cb5623003d75b26bdfa1c18002c", "pos": 276}, # noqa: E501
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"seed": [839438204, 980239840, 859048019, 821],
|
|
40
|
+
"steps": 511,
|
|
41
|
+
"initial": {"key_sha256": "d306cf01314d51bd37892d874308200951a35265ede54d200f1e065004c3e9ea", "pos": 510}, # noqa: E501
|
|
42
|
+
"jumped": {"key_sha256": "0e00ab449f01a5195a83b4aee0dfbc2ce8d46466a640b92e33977d2e42f777f8", "pos": 475}, # noqa: E501
|
|
43
|
+
},
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.fixture(scope='module', params=[True, False])
|
|
48
|
+
def endpoint(request):
|
|
49
|
+
return request.param
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class TestSeed:
|
|
53
|
+
def test_scalar(self):
|
|
54
|
+
s = Generator(MT19937(0))
|
|
55
|
+
assert_equal(s.integers(1000), 479)
|
|
56
|
+
s = Generator(MT19937(4294967295))
|
|
57
|
+
assert_equal(s.integers(1000), 324)
|
|
58
|
+
|
|
59
|
+
def test_array(self):
|
|
60
|
+
s = Generator(MT19937(range(10)))
|
|
61
|
+
assert_equal(s.integers(1000), 465)
|
|
62
|
+
s = Generator(MT19937(np.arange(10)))
|
|
63
|
+
assert_equal(s.integers(1000), 465)
|
|
64
|
+
s = Generator(MT19937([0]))
|
|
65
|
+
assert_equal(s.integers(1000), 479)
|
|
66
|
+
s = Generator(MT19937([4294967295]))
|
|
67
|
+
assert_equal(s.integers(1000), 324)
|
|
68
|
+
|
|
69
|
+
def test_seedsequence(self):
|
|
70
|
+
s = MT19937(SeedSequence(0))
|
|
71
|
+
assert_equal(s.random_raw(1), 2058676884)
|
|
72
|
+
|
|
73
|
+
def test_invalid_scalar(self):
|
|
74
|
+
# seed must be an unsigned 32 bit integer
|
|
75
|
+
assert_raises(TypeError, MT19937, -0.5)
|
|
76
|
+
assert_raises(ValueError, MT19937, -1)
|
|
77
|
+
|
|
78
|
+
def test_invalid_array(self):
|
|
79
|
+
# seed must be an unsigned integer
|
|
80
|
+
assert_raises(TypeError, MT19937, [-0.5])
|
|
81
|
+
assert_raises(ValueError, MT19937, [-1])
|
|
82
|
+
assert_raises(ValueError, MT19937, [1, -2, 4294967296])
|
|
83
|
+
|
|
84
|
+
def test_noninstantized_bitgen(self):
|
|
85
|
+
assert_raises(ValueError, Generator, MT19937)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TestBinomial:
|
|
89
|
+
def test_n_zero(self):
|
|
90
|
+
# Tests the corner case of n == 0 for the binomial distribution.
|
|
91
|
+
# binomial(0, p) should be zero for any p in [0, 1].
|
|
92
|
+
# This test addresses issue #3480.
|
|
93
|
+
zeros = np.zeros(2, dtype='int')
|
|
94
|
+
for p in [0, .5, 1]:
|
|
95
|
+
assert_(random.binomial(0, p) == 0)
|
|
96
|
+
assert_array_equal(random.binomial(zeros, p), zeros)
|
|
97
|
+
|
|
98
|
+
def test_p_is_nan(self):
|
|
99
|
+
# Issue #4571.
|
|
100
|
+
assert_raises(ValueError, random.binomial, 1, np.nan)
|
|
101
|
+
|
|
102
|
+
def test_p_extremely_small(self):
|
|
103
|
+
n = 50000000000
|
|
104
|
+
p = 5e-17
|
|
105
|
+
sample_size = 20000000
|
|
106
|
+
x = random.binomial(n, p, size=sample_size)
|
|
107
|
+
sample_mean = x.mean()
|
|
108
|
+
expected_mean = n * p
|
|
109
|
+
sigma = np.sqrt(n * p * (1 - p) / sample_size)
|
|
110
|
+
# Note: the parameters were chosen so that expected_mean - 6*sigma
|
|
111
|
+
# is a positive value. The first `assert` below validates that
|
|
112
|
+
# assumption (in case someone edits the parameters in the future).
|
|
113
|
+
# The second `assert` is the actual test.
|
|
114
|
+
low_bound = expected_mean - 6 * sigma
|
|
115
|
+
assert low_bound > 0, "bad test params: 6-sigma lower bound is negative"
|
|
116
|
+
test_msg = (f"sample mean {sample_mean} deviates from the expected mean "
|
|
117
|
+
f"{expected_mean} by more than 6*sigma")
|
|
118
|
+
assert abs(expected_mean - sample_mean) < 6 * sigma, test_msg
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class TestMultinomial:
|
|
122
|
+
def test_basic(self):
|
|
123
|
+
random.multinomial(100, [0.2, 0.8])
|
|
124
|
+
|
|
125
|
+
def test_zero_probability(self):
|
|
126
|
+
random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0])
|
|
127
|
+
|
|
128
|
+
def test_int_negative_interval(self):
|
|
129
|
+
assert_(-5 <= random.integers(-5, -1) < -1)
|
|
130
|
+
x = random.integers(-5, -1, 5)
|
|
131
|
+
assert_(np.all(-5 <= x))
|
|
132
|
+
assert_(np.all(x < -1))
|
|
133
|
+
|
|
134
|
+
def test_size(self):
|
|
135
|
+
# gh-3173
|
|
136
|
+
p = [0.5, 0.5]
|
|
137
|
+
assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
|
|
138
|
+
assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
|
|
139
|
+
assert_equal(random.multinomial(1, p, np.uint32(1)).shape, (1, 2))
|
|
140
|
+
assert_equal(random.multinomial(1, p, [2, 2]).shape, (2, 2, 2))
|
|
141
|
+
assert_equal(random.multinomial(1, p, (2, 2)).shape, (2, 2, 2))
|
|
142
|
+
assert_equal(random.multinomial(1, p, np.array((2, 2))).shape,
|
|
143
|
+
(2, 2, 2))
|
|
144
|
+
|
|
145
|
+
assert_raises(TypeError, random.multinomial, 1, p,
|
|
146
|
+
float(1))
|
|
147
|
+
|
|
148
|
+
def test_invalid_prob(self):
|
|
149
|
+
assert_raises(ValueError, random.multinomial, 100, [1.1, 0.2])
|
|
150
|
+
assert_raises(ValueError, random.multinomial, 100, [-.1, 0.9])
|
|
151
|
+
|
|
152
|
+
def test_invalid_n(self):
|
|
153
|
+
assert_raises(ValueError, random.multinomial, -1, [0.8, 0.2])
|
|
154
|
+
assert_raises(ValueError, random.multinomial, [-1] * 10, [0.8, 0.2])
|
|
155
|
+
|
|
156
|
+
def test_p_non_contiguous(self):
|
|
157
|
+
p = np.arange(15.)
|
|
158
|
+
p /= np.sum(p[1::3])
|
|
159
|
+
pvals = p[1::3]
|
|
160
|
+
random = Generator(MT19937(1432985819))
|
|
161
|
+
non_contig = random.multinomial(100, pvals=pvals)
|
|
162
|
+
random = Generator(MT19937(1432985819))
|
|
163
|
+
contig = random.multinomial(100, pvals=np.ascontiguousarray(pvals))
|
|
164
|
+
assert_array_equal(non_contig, contig)
|
|
165
|
+
|
|
166
|
+
def test_multinomial_pvals_float32(self):
|
|
167
|
+
x = np.array([9.9e-01, 9.9e-01, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09,
|
|
168
|
+
1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09], dtype=np.float32)
|
|
169
|
+
pvals = x / x.sum()
|
|
170
|
+
random = Generator(MT19937(1432985819))
|
|
171
|
+
match = r"[\w\s]*pvals array is cast to 64-bit floating"
|
|
172
|
+
with pytest.raises(ValueError, match=match):
|
|
173
|
+
random.multinomial(1, pvals)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class TestMultivariateHypergeometric:
|
|
177
|
+
|
|
178
|
+
seed = 8675309
|
|
179
|
+
|
|
180
|
+
def test_argument_validation(self):
|
|
181
|
+
# Error cases...
|
|
182
|
+
|
|
183
|
+
# `colors` must be a 1-d sequence
|
|
184
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
185
|
+
10, 4)
|
|
186
|
+
|
|
187
|
+
# Negative nsample
|
|
188
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
189
|
+
[2, 3, 4], -1)
|
|
190
|
+
|
|
191
|
+
# Negative color
|
|
192
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
193
|
+
[-1, 2, 3], 2)
|
|
194
|
+
|
|
195
|
+
# nsample exceeds sum(colors)
|
|
196
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
197
|
+
[2, 3, 4], 10)
|
|
198
|
+
|
|
199
|
+
# nsample exceeds sum(colors) (edge case of empty colors)
|
|
200
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
201
|
+
[], 1)
|
|
202
|
+
|
|
203
|
+
# Validation errors associated with very large values in colors.
|
|
204
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
205
|
+
[999999999, 101], 5, 1, 'marginals')
|
|
206
|
+
|
|
207
|
+
int64_info = np.iinfo(np.int64)
|
|
208
|
+
max_int64 = int64_info.max
|
|
209
|
+
max_int64_index = max_int64 // int64_info.dtype.itemsize
|
|
210
|
+
assert_raises(ValueError, random.multivariate_hypergeometric,
|
|
211
|
+
[max_int64_index - 100, 101], 5, 1, 'count')
|
|
212
|
+
|
|
213
|
+
@pytest.mark.parametrize('method', ['count', 'marginals'])
|
|
214
|
+
def test_edge_cases(self, method):
|
|
215
|
+
# Set the seed, but in fact, all the results in this test are
|
|
216
|
+
# deterministic, so we don't really need this.
|
|
217
|
+
random = Generator(MT19937(self.seed))
|
|
218
|
+
|
|
219
|
+
x = random.multivariate_hypergeometric([0, 0, 0], 0, method=method)
|
|
220
|
+
assert_array_equal(x, [0, 0, 0])
|
|
221
|
+
|
|
222
|
+
x = random.multivariate_hypergeometric([], 0, method=method)
|
|
223
|
+
assert_array_equal(x, [])
|
|
224
|
+
|
|
225
|
+
x = random.multivariate_hypergeometric([], 0, size=1, method=method)
|
|
226
|
+
assert_array_equal(x, np.empty((1, 0), dtype=np.int64))
|
|
227
|
+
|
|
228
|
+
x = random.multivariate_hypergeometric([1, 2, 3], 0, method=method)
|
|
229
|
+
assert_array_equal(x, [0, 0, 0])
|
|
230
|
+
|
|
231
|
+
x = random.multivariate_hypergeometric([9, 0, 0], 3, method=method)
|
|
232
|
+
assert_array_equal(x, [3, 0, 0])
|
|
233
|
+
|
|
234
|
+
colors = [1, 1, 0, 1, 1]
|
|
235
|
+
x = random.multivariate_hypergeometric(colors, sum(colors),
|
|
236
|
+
method=method)
|
|
237
|
+
assert_array_equal(x, colors)
|
|
238
|
+
|
|
239
|
+
x = random.multivariate_hypergeometric([3, 4, 5], 12, size=3,
|
|
240
|
+
method=method)
|
|
241
|
+
assert_array_equal(x, [[3, 4, 5]] * 3)
|
|
242
|
+
|
|
243
|
+
# Cases for nsample:
|
|
244
|
+
# nsample < 10
|
|
245
|
+
# 10 <= nsample < colors.sum()/2
|
|
246
|
+
# colors.sum()/2 < nsample < colors.sum() - 10
|
|
247
|
+
# colors.sum() - 10 < nsample < colors.sum()
|
|
248
|
+
@pytest.mark.parametrize('nsample', [8, 25, 45, 55])
|
|
249
|
+
@pytest.mark.parametrize('method', ['count', 'marginals'])
|
|
250
|
+
@pytest.mark.parametrize('size', [5, (2, 3), 150000])
|
|
251
|
+
def test_typical_cases(self, nsample, method, size):
|
|
252
|
+
random = Generator(MT19937(self.seed))
|
|
253
|
+
|
|
254
|
+
colors = np.array([10, 5, 20, 25])
|
|
255
|
+
sample = random.multivariate_hypergeometric(colors, nsample, size,
|
|
256
|
+
method=method)
|
|
257
|
+
if isinstance(size, int):
|
|
258
|
+
expected_shape = (size,) + colors.shape
|
|
259
|
+
else:
|
|
260
|
+
expected_shape = size + colors.shape
|
|
261
|
+
assert_equal(sample.shape, expected_shape)
|
|
262
|
+
assert_((sample >= 0).all())
|
|
263
|
+
assert_((sample <= colors).all())
|
|
264
|
+
assert_array_equal(sample.sum(axis=-1),
|
|
265
|
+
np.full(size, fill_value=nsample, dtype=int))
|
|
266
|
+
if isinstance(size, int) and size >= 100000:
|
|
267
|
+
# This sample is large enough to compare its mean to
|
|
268
|
+
# the expected values.
|
|
269
|
+
assert_allclose(sample.mean(axis=0),
|
|
270
|
+
nsample * colors / colors.sum(),
|
|
271
|
+
rtol=1e-3, atol=0.005)
|
|
272
|
+
|
|
273
|
+
def test_repeatability1(self):
|
|
274
|
+
random = Generator(MT19937(self.seed))
|
|
275
|
+
sample = random.multivariate_hypergeometric([3, 4, 5], 5, size=5,
|
|
276
|
+
method='count')
|
|
277
|
+
expected = np.array([[2, 1, 2],
|
|
278
|
+
[2, 1, 2],
|
|
279
|
+
[1, 1, 3],
|
|
280
|
+
[2, 0, 3],
|
|
281
|
+
[2, 1, 2]])
|
|
282
|
+
assert_array_equal(sample, expected)
|
|
283
|
+
|
|
284
|
+
def test_repeatability2(self):
|
|
285
|
+
random = Generator(MT19937(self.seed))
|
|
286
|
+
sample = random.multivariate_hypergeometric([20, 30, 50], 50,
|
|
287
|
+
size=5,
|
|
288
|
+
method='marginals')
|
|
289
|
+
expected = np.array([[ 9, 17, 24],
|
|
290
|
+
[ 7, 13, 30],
|
|
291
|
+
[ 9, 15, 26],
|
|
292
|
+
[ 9, 17, 24],
|
|
293
|
+
[12, 14, 24]])
|
|
294
|
+
assert_array_equal(sample, expected)
|
|
295
|
+
|
|
296
|
+
def test_repeatability3(self):
|
|
297
|
+
random = Generator(MT19937(self.seed))
|
|
298
|
+
sample = random.multivariate_hypergeometric([20, 30, 50], 12,
|
|
299
|
+
size=5,
|
|
300
|
+
method='marginals')
|
|
301
|
+
expected = np.array([[2, 3, 7],
|
|
302
|
+
[5, 3, 4],
|
|
303
|
+
[2, 5, 5],
|
|
304
|
+
[5, 3, 4],
|
|
305
|
+
[1, 5, 6]])
|
|
306
|
+
assert_array_equal(sample, expected)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
class TestSetState:
|
|
310
|
+
def _create_rng(self):
|
|
311
|
+
seed = 1234567890
|
|
312
|
+
rg = Generator(MT19937(seed))
|
|
313
|
+
bit_generator = rg.bit_generator
|
|
314
|
+
state = bit_generator.state
|
|
315
|
+
legacy_state = (state['bit_generator'],
|
|
316
|
+
state['state']['key'],
|
|
317
|
+
state['state']['pos'])
|
|
318
|
+
return rg, bit_generator, state
|
|
319
|
+
|
|
320
|
+
def test_gaussian_reset(self):
|
|
321
|
+
# Make sure the cached every-other-Gaussian is reset.
|
|
322
|
+
rg, bit_generator, state = self._create_rng()
|
|
323
|
+
old = rg.standard_normal(size=3)
|
|
324
|
+
bit_generator.state = state
|
|
325
|
+
new = rg.standard_normal(size=3)
|
|
326
|
+
assert_(np.all(old == new))
|
|
327
|
+
|
|
328
|
+
def test_gaussian_reset_in_media_res(self):
|
|
329
|
+
# When the state is saved with a cached Gaussian, make sure the
|
|
330
|
+
# cached Gaussian is restored.
|
|
331
|
+
rg, bit_generator, state = self._create_rng()
|
|
332
|
+
rg.standard_normal()
|
|
333
|
+
state = bit_generator.state
|
|
334
|
+
old = rg.standard_normal(size=3)
|
|
335
|
+
bit_generator.state = state
|
|
336
|
+
new = rg.standard_normal(size=3)
|
|
337
|
+
assert_(np.all(old == new))
|
|
338
|
+
|
|
339
|
+
def test_negative_binomial(self):
|
|
340
|
+
# Ensure that the negative binomial results take floating point
|
|
341
|
+
# arguments without truncation.
|
|
342
|
+
rg, _, _ = self._create_rng()
|
|
343
|
+
rg.negative_binomial(0.5, 0.5)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class TestIntegers:
|
|
347
|
+
rfunc = random.integers
|
|
348
|
+
|
|
349
|
+
# valid integer/boolean types
|
|
350
|
+
itype = [bool, np.int8, np.uint8, np.int16, np.uint16,
|
|
351
|
+
np.int32, np.uint32, np.int64, np.uint64]
|
|
352
|
+
|
|
353
|
+
def test_unsupported_type(self, endpoint):
|
|
354
|
+
assert_raises(TypeError, self.rfunc, 1, endpoint=endpoint, dtype=float)
|
|
355
|
+
|
|
356
|
+
def test_bounds_checking(self, endpoint):
|
|
357
|
+
for dt in self.itype:
|
|
358
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
359
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
360
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
361
|
+
assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd,
|
|
362
|
+
endpoint=endpoint, dtype=dt)
|
|
363
|
+
assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1,
|
|
364
|
+
endpoint=endpoint, dtype=dt)
|
|
365
|
+
assert_raises(ValueError, self.rfunc, ubnd, lbnd,
|
|
366
|
+
endpoint=endpoint, dtype=dt)
|
|
367
|
+
assert_raises(ValueError, self.rfunc, 1, 0, endpoint=endpoint,
|
|
368
|
+
dtype=dt)
|
|
369
|
+
|
|
370
|
+
assert_raises(ValueError, self.rfunc, [lbnd - 1], ubnd,
|
|
371
|
+
endpoint=endpoint, dtype=dt)
|
|
372
|
+
assert_raises(ValueError, self.rfunc, [lbnd], [ubnd + 1],
|
|
373
|
+
endpoint=endpoint, dtype=dt)
|
|
374
|
+
assert_raises(ValueError, self.rfunc, [ubnd], [lbnd],
|
|
375
|
+
endpoint=endpoint, dtype=dt)
|
|
376
|
+
assert_raises(ValueError, self.rfunc, 1, [0],
|
|
377
|
+
endpoint=endpoint, dtype=dt)
|
|
378
|
+
assert_raises(ValueError, self.rfunc, [ubnd + 1], [ubnd],
|
|
379
|
+
endpoint=endpoint, dtype=dt)
|
|
380
|
+
|
|
381
|
+
def test_bounds_checking_array(self, endpoint):
|
|
382
|
+
for dt in self.itype:
|
|
383
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
384
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + (not endpoint)
|
|
385
|
+
|
|
386
|
+
assert_raises(ValueError, self.rfunc, [lbnd - 1] * 2, [ubnd] * 2,
|
|
387
|
+
endpoint=endpoint, dtype=dt)
|
|
388
|
+
assert_raises(ValueError, self.rfunc, [lbnd] * 2,
|
|
389
|
+
[ubnd + 1] * 2, endpoint=endpoint, dtype=dt)
|
|
390
|
+
assert_raises(ValueError, self.rfunc, ubnd, [lbnd] * 2,
|
|
391
|
+
endpoint=endpoint, dtype=dt)
|
|
392
|
+
assert_raises(ValueError, self.rfunc, [1] * 2, 0,
|
|
393
|
+
endpoint=endpoint, dtype=dt)
|
|
394
|
+
|
|
395
|
+
def test_rng_zero_and_extremes(self, endpoint):
|
|
396
|
+
for dt in self.itype:
|
|
397
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
398
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
399
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
400
|
+
is_open = not endpoint
|
|
401
|
+
|
|
402
|
+
tgt = ubnd - 1
|
|
403
|
+
assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
|
|
404
|
+
endpoint=endpoint, dtype=dt), tgt)
|
|
405
|
+
assert_equal(self.rfunc([tgt], tgt + is_open, size=1000,
|
|
406
|
+
endpoint=endpoint, dtype=dt), tgt)
|
|
407
|
+
|
|
408
|
+
tgt = lbnd
|
|
409
|
+
assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
|
|
410
|
+
endpoint=endpoint, dtype=dt), tgt)
|
|
411
|
+
assert_equal(self.rfunc(tgt, [tgt + is_open], size=1000,
|
|
412
|
+
endpoint=endpoint, dtype=dt), tgt)
|
|
413
|
+
|
|
414
|
+
tgt = (lbnd + ubnd) // 2
|
|
415
|
+
assert_equal(self.rfunc(tgt, tgt + is_open, size=1000,
|
|
416
|
+
endpoint=endpoint, dtype=dt), tgt)
|
|
417
|
+
assert_equal(self.rfunc([tgt], [tgt + is_open],
|
|
418
|
+
size=1000, endpoint=endpoint, dtype=dt),
|
|
419
|
+
tgt)
|
|
420
|
+
|
|
421
|
+
def test_rng_zero_and_extremes_array(self, endpoint):
|
|
422
|
+
size = 1000
|
|
423
|
+
for dt in self.itype:
|
|
424
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
425
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
426
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
427
|
+
|
|
428
|
+
tgt = ubnd - 1
|
|
429
|
+
assert_equal(self.rfunc([tgt], [tgt + 1],
|
|
430
|
+
size=size, dtype=dt), tgt)
|
|
431
|
+
assert_equal(self.rfunc(
|
|
432
|
+
[tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
|
|
433
|
+
assert_equal(self.rfunc(
|
|
434
|
+
[tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
|
|
435
|
+
|
|
436
|
+
tgt = lbnd
|
|
437
|
+
assert_equal(self.rfunc([tgt], [tgt + 1],
|
|
438
|
+
size=size, dtype=dt), tgt)
|
|
439
|
+
assert_equal(self.rfunc(
|
|
440
|
+
[tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
|
|
441
|
+
assert_equal(self.rfunc(
|
|
442
|
+
[tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
|
|
443
|
+
|
|
444
|
+
tgt = (lbnd + ubnd) // 2
|
|
445
|
+
assert_equal(self.rfunc([tgt], [tgt + 1],
|
|
446
|
+
size=size, dtype=dt), tgt)
|
|
447
|
+
assert_equal(self.rfunc(
|
|
448
|
+
[tgt] * size, [tgt + 1] * size, dtype=dt), tgt)
|
|
449
|
+
assert_equal(self.rfunc(
|
|
450
|
+
[tgt] * size, [tgt + 1] * size, size=size, dtype=dt), tgt)
|
|
451
|
+
|
|
452
|
+
def test_full_range(self, endpoint):
|
|
453
|
+
# Test for ticket #1690
|
|
454
|
+
|
|
455
|
+
for dt in self.itype:
|
|
456
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
457
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
458
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
459
|
+
|
|
460
|
+
try:
|
|
461
|
+
self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
|
|
462
|
+
except Exception as e:
|
|
463
|
+
raise AssertionError("No error should have been raised, "
|
|
464
|
+
"but one was with the following "
|
|
465
|
+
"message:\n\n%s" % str(e))
|
|
466
|
+
|
|
467
|
+
def test_full_range_array(self, endpoint):
|
|
468
|
+
# Test for ticket #1690
|
|
469
|
+
|
|
470
|
+
for dt in self.itype:
|
|
471
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
472
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
473
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
474
|
+
|
|
475
|
+
try:
|
|
476
|
+
self.rfunc([lbnd] * 2, [ubnd], endpoint=endpoint, dtype=dt)
|
|
477
|
+
except Exception as e:
|
|
478
|
+
raise AssertionError("No error should have been raised, "
|
|
479
|
+
"but one was with the following "
|
|
480
|
+
"message:\n\n%s" % str(e))
|
|
481
|
+
|
|
482
|
+
def test_in_bounds_fuzz(self, endpoint):
|
|
483
|
+
# Don't use fixed seed
|
|
484
|
+
random = Generator(MT19937())
|
|
485
|
+
|
|
486
|
+
for dt in self.itype[1:]:
|
|
487
|
+
for ubnd in [4, 8, 16]:
|
|
488
|
+
vals = self.rfunc(2, ubnd - endpoint, size=2 ** 16,
|
|
489
|
+
endpoint=endpoint, dtype=dt)
|
|
490
|
+
assert_(vals.max() < ubnd)
|
|
491
|
+
assert_(vals.min() >= 2)
|
|
492
|
+
|
|
493
|
+
vals = self.rfunc(0, 2 - endpoint, size=2 ** 16, endpoint=endpoint,
|
|
494
|
+
dtype=bool)
|
|
495
|
+
assert_(vals.max() < 2)
|
|
496
|
+
assert_(vals.min() >= 0)
|
|
497
|
+
|
|
498
|
+
def test_scalar_array_equiv(self, endpoint):
|
|
499
|
+
for dt in self.itype:
|
|
500
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
501
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
502
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
503
|
+
|
|
504
|
+
size = 1000
|
|
505
|
+
random = Generator(MT19937(1234))
|
|
506
|
+
scalar = random.integers(lbnd, ubnd, size=size, endpoint=endpoint,
|
|
507
|
+
dtype=dt)
|
|
508
|
+
|
|
509
|
+
random = Generator(MT19937(1234))
|
|
510
|
+
scalar_array = random.integers([lbnd], [ubnd], size=size,
|
|
511
|
+
endpoint=endpoint, dtype=dt)
|
|
512
|
+
|
|
513
|
+
random = Generator(MT19937(1234))
|
|
514
|
+
array = random.integers([lbnd] * size, [ubnd] *
|
|
515
|
+
size, size=size, endpoint=endpoint, dtype=dt)
|
|
516
|
+
assert_array_equal(scalar, scalar_array)
|
|
517
|
+
assert_array_equal(scalar, array)
|
|
518
|
+
|
|
519
|
+
def test_repeatability(self, endpoint):
|
|
520
|
+
# We use a sha256 hash of generated sequences of 1000 samples
|
|
521
|
+
# in the range [0, 6) for all but bool, where the range
|
|
522
|
+
# is [0, 2). Hashes are for little endian numbers.
|
|
523
|
+
tgt = {'bool': '053594a9b82d656f967c54869bc6970aa0358cf94ad469c81478459c6a90eee3', # noqa: E501
|
|
524
|
+
'int16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4', # noqa: E501
|
|
525
|
+
'int32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b', # noqa: E501
|
|
526
|
+
'int64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1', # noqa: E501
|
|
527
|
+
'int8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1', # noqa: E501
|
|
528
|
+
'uint16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4', # noqa: E501
|
|
529
|
+
'uint32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b', # noqa: E501
|
|
530
|
+
'uint64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1', # noqa: E501
|
|
531
|
+
'uint8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1'} # noqa: E501
|
|
532
|
+
|
|
533
|
+
for dt in self.itype[1:]:
|
|
534
|
+
random = Generator(MT19937(1234))
|
|
535
|
+
|
|
536
|
+
# view as little endian for hash
|
|
537
|
+
if sys.byteorder == 'little':
|
|
538
|
+
val = random.integers(0, 6 - endpoint, size=1000, endpoint=endpoint,
|
|
539
|
+
dtype=dt)
|
|
540
|
+
else:
|
|
541
|
+
val = random.integers(0, 6 - endpoint, size=1000, endpoint=endpoint,
|
|
542
|
+
dtype=dt).byteswap()
|
|
543
|
+
|
|
544
|
+
res = hashlib.sha256(val).hexdigest()
|
|
545
|
+
assert_(tgt[np.dtype(dt).name] == res)
|
|
546
|
+
|
|
547
|
+
# bools do not depend on endianness
|
|
548
|
+
random = Generator(MT19937(1234))
|
|
549
|
+
val = random.integers(0, 2 - endpoint, size=1000, endpoint=endpoint,
|
|
550
|
+
dtype=bool).view(np.int8)
|
|
551
|
+
res = hashlib.sha256(val).hexdigest()
|
|
552
|
+
assert_(tgt[np.dtype(bool).name] == res)
|
|
553
|
+
|
|
554
|
+
def test_repeatability_broadcasting(self, endpoint):
|
|
555
|
+
for dt in self.itype:
|
|
556
|
+
lbnd = 0 if dt in (bool, np.bool) else np.iinfo(dt).min
|
|
557
|
+
ubnd = 2 if dt in (bool, np.bool) else np.iinfo(dt).max + 1
|
|
558
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
559
|
+
|
|
560
|
+
# view as little endian for hash
|
|
561
|
+
random = Generator(MT19937(1234))
|
|
562
|
+
val = random.integers(lbnd, ubnd, size=1000, endpoint=endpoint,
|
|
563
|
+
dtype=dt)
|
|
564
|
+
|
|
565
|
+
random = Generator(MT19937(1234))
|
|
566
|
+
val_bc = random.integers([lbnd] * 1000, ubnd, endpoint=endpoint,
|
|
567
|
+
dtype=dt)
|
|
568
|
+
|
|
569
|
+
assert_array_equal(val, val_bc)
|
|
570
|
+
|
|
571
|
+
random = Generator(MT19937(1234))
|
|
572
|
+
val_bc = random.integers([lbnd] * 1000, [ubnd] * 1000,
|
|
573
|
+
endpoint=endpoint, dtype=dt)
|
|
574
|
+
|
|
575
|
+
assert_array_equal(val, val_bc)
|
|
576
|
+
|
|
577
|
+
@pytest.mark.parametrize(
|
|
578
|
+
'bound, expected',
|
|
579
|
+
[(2**32 - 1, np.array([517043486, 1364798665, 1733884389, 1353720612,
|
|
580
|
+
3769704066, 1170797179, 4108474671])),
|
|
581
|
+
(2**32, np.array([517043487, 1364798666, 1733884390, 1353720613,
|
|
582
|
+
3769704067, 1170797180, 4108474672])),
|
|
583
|
+
(2**32 + 1, np.array([517043487, 1733884390, 3769704068, 4108474673,
|
|
584
|
+
1831631863, 1215661561, 3869512430]))]
|
|
585
|
+
)
|
|
586
|
+
def test_repeatability_32bit_boundary(self, bound, expected):
|
|
587
|
+
for size in [None, len(expected)]:
|
|
588
|
+
random = Generator(MT19937(1234))
|
|
589
|
+
x = random.integers(bound, size=size)
|
|
590
|
+
assert_equal(x, expected if size is not None else expected[0])
|
|
591
|
+
|
|
592
|
+
def test_repeatability_32bit_boundary_broadcasting(self):
|
|
593
|
+
desired = np.array([[[1622936284, 3620788691, 1659384060],
|
|
594
|
+
[1417365545, 760222891, 1909653332],
|
|
595
|
+
[3788118662, 660249498, 4092002593]],
|
|
596
|
+
[[3625610153, 2979601262, 3844162757],
|
|
597
|
+
[ 685800658, 120261497, 2694012896],
|
|
598
|
+
[1207779440, 1586594375, 3854335050]],
|
|
599
|
+
[[3004074748, 2310761796, 3012642217],
|
|
600
|
+
[2067714190, 2786677879, 1363865881],
|
|
601
|
+
[ 791663441, 1867303284, 2169727960]],
|
|
602
|
+
[[1939603804, 1250951100, 298950036],
|
|
603
|
+
[1040128489, 3791912209, 3317053765],
|
|
604
|
+
[3155528714, 61360675, 2305155588]],
|
|
605
|
+
[[ 817688762, 1335621943, 3288952434],
|
|
606
|
+
[1770890872, 1102951817, 1957607470],
|
|
607
|
+
[3099996017, 798043451, 48334215]]])
|
|
608
|
+
for size in [None, (5, 3, 3)]:
|
|
609
|
+
random = Generator(MT19937(12345))
|
|
610
|
+
x = random.integers([[-1], [0], [1]],
|
|
611
|
+
[2**32 - 1, 2**32, 2**32 + 1],
|
|
612
|
+
size=size)
|
|
613
|
+
assert_array_equal(x, desired if size is not None else desired[0])
|
|
614
|
+
|
|
615
|
+
def test_int64_uint64_broadcast_exceptions(self, endpoint):
|
|
616
|
+
configs = {np.uint64: ((0, 2**65), (-1, 2**62), (10, 9), (0, 0)),
|
|
617
|
+
np.int64: ((0, 2**64), (-(2**64), 2**62), (10, 9), (0, 0),
|
|
618
|
+
(-2**63 - 1, -2**63 - 1))}
|
|
619
|
+
for dtype in configs:
|
|
620
|
+
for config in configs[dtype]:
|
|
621
|
+
low, high = config
|
|
622
|
+
high = high - endpoint
|
|
623
|
+
low_a = np.array([[low] * 10])
|
|
624
|
+
high_a = np.array([high] * 10)
|
|
625
|
+
assert_raises(ValueError, random.integers, low, high,
|
|
626
|
+
endpoint=endpoint, dtype=dtype)
|
|
627
|
+
assert_raises(ValueError, random.integers, low_a, high,
|
|
628
|
+
endpoint=endpoint, dtype=dtype)
|
|
629
|
+
assert_raises(ValueError, random.integers, low, high_a,
|
|
630
|
+
endpoint=endpoint, dtype=dtype)
|
|
631
|
+
assert_raises(ValueError, random.integers, low_a, high_a,
|
|
632
|
+
endpoint=endpoint, dtype=dtype)
|
|
633
|
+
|
|
634
|
+
low_o = np.array([[low] * 10], dtype=object)
|
|
635
|
+
high_o = np.array([high] * 10, dtype=object)
|
|
636
|
+
assert_raises(ValueError, random.integers, low_o, high,
|
|
637
|
+
endpoint=endpoint, dtype=dtype)
|
|
638
|
+
assert_raises(ValueError, random.integers, low, high_o,
|
|
639
|
+
endpoint=endpoint, dtype=dtype)
|
|
640
|
+
assert_raises(ValueError, random.integers, low_o, high_o,
|
|
641
|
+
endpoint=endpoint, dtype=dtype)
|
|
642
|
+
|
|
643
|
+
def test_int64_uint64_corner_case(self, endpoint):
|
|
644
|
+
# When stored in Numpy arrays, `lbnd` is casted
|
|
645
|
+
# as np.int64, and `ubnd` is casted as np.uint64.
|
|
646
|
+
# Checking whether `lbnd` >= `ubnd` used to be
|
|
647
|
+
# done solely via direct comparison, which is incorrect
|
|
648
|
+
# because when Numpy tries to compare both numbers,
|
|
649
|
+
# it casts both to np.float64 because there is
|
|
650
|
+
# no integer superset of np.int64 and np.uint64. However,
|
|
651
|
+
# `ubnd` is too large to be represented in np.float64,
|
|
652
|
+
# causing it be round down to np.iinfo(np.int64).max,
|
|
653
|
+
# leading to a ValueError because `lbnd` now equals
|
|
654
|
+
# the new `ubnd`.
|
|
655
|
+
|
|
656
|
+
dt = np.int64
|
|
657
|
+
tgt = np.iinfo(np.int64).max
|
|
658
|
+
lbnd = np.int64(np.iinfo(np.int64).max)
|
|
659
|
+
ubnd = np.uint64(np.iinfo(np.int64).max + 1 - endpoint)
|
|
660
|
+
|
|
661
|
+
# None of these function calls should
|
|
662
|
+
# generate a ValueError now.
|
|
663
|
+
actual = random.integers(lbnd, ubnd, endpoint=endpoint, dtype=dt)
|
|
664
|
+
assert_equal(actual, tgt)
|
|
665
|
+
|
|
666
|
+
def test_respect_dtype_singleton(self, endpoint):
|
|
667
|
+
# See gh-7203
|
|
668
|
+
for dt in self.itype:
|
|
669
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
670
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
671
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
672
|
+
dt = np.bool if dt is bool else dt
|
|
673
|
+
|
|
674
|
+
sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
|
|
675
|
+
assert_equal(sample.dtype, dt)
|
|
676
|
+
|
|
677
|
+
for dt in (bool, int):
|
|
678
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
679
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
680
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
681
|
+
|
|
682
|
+
# gh-7284: Ensure that we get Python data types
|
|
683
|
+
sample = self.rfunc(lbnd, ubnd, endpoint=endpoint, dtype=dt)
|
|
684
|
+
assert not hasattr(sample, 'dtype')
|
|
685
|
+
assert_equal(type(sample), dt)
|
|
686
|
+
|
|
687
|
+
def test_respect_dtype_array(self, endpoint):
|
|
688
|
+
# See gh-7203
|
|
689
|
+
for dt in self.itype:
|
|
690
|
+
lbnd = 0 if dt is bool else np.iinfo(dt).min
|
|
691
|
+
ubnd = 2 if dt is bool else np.iinfo(dt).max + 1
|
|
692
|
+
ubnd = ubnd - 1 if endpoint else ubnd
|
|
693
|
+
dt = np.bool if dt is bool else dt
|
|
694
|
+
|
|
695
|
+
sample = self.rfunc([lbnd], [ubnd], endpoint=endpoint, dtype=dt)
|
|
696
|
+
assert_equal(sample.dtype, dt)
|
|
697
|
+
sample = self.rfunc([lbnd] * 2, [ubnd] * 2, endpoint=endpoint,
|
|
698
|
+
dtype=dt)
|
|
699
|
+
assert_equal(sample.dtype, dt)
|
|
700
|
+
|
|
701
|
+
def test_zero_size(self, endpoint):
|
|
702
|
+
# See gh-7203
|
|
703
|
+
for dt in self.itype:
|
|
704
|
+
sample = self.rfunc(0, 0, (3, 0, 4), endpoint=endpoint, dtype=dt)
|
|
705
|
+
assert sample.shape == (3, 0, 4)
|
|
706
|
+
assert sample.dtype == dt
|
|
707
|
+
assert self.rfunc(0, -10, 0, endpoint=endpoint,
|
|
708
|
+
dtype=dt).shape == (0,)
|
|
709
|
+
assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape,
|
|
710
|
+
(3, 0, 4))
|
|
711
|
+
assert_equal(random.integers(0, -10, size=0).shape, (0,))
|
|
712
|
+
assert_equal(random.integers(10, 10, size=0).shape, (0,))
|
|
713
|
+
|
|
714
|
+
def test_error_byteorder(self):
|
|
715
|
+
other_byteord_dt = '<i4' if sys.byteorder == 'big' else '>i4'
|
|
716
|
+
with pytest.raises(ValueError):
|
|
717
|
+
random.integers(0, 200, size=10, dtype=other_byteord_dt)
|
|
718
|
+
|
|
719
|
+
# chi2max is the maximum acceptable chi-squared value.
|
|
720
|
+
@pytest.mark.slow
|
|
721
|
+
@pytest.mark.parametrize('sample_size,high,dtype,chi2max',
|
|
722
|
+
[(5000000, 5, np.int8, 125.0), # p-value ~4.6e-25
|
|
723
|
+
(5000000, 7, np.uint8, 150.0), # p-value ~7.7e-30
|
|
724
|
+
(10000000, 2500, np.int16, 3300.0), # p-value ~3.0e-25
|
|
725
|
+
(50000000, 5000, np.uint16, 6500.0), # p-value ~3.5e-25
|
|
726
|
+
])
|
|
727
|
+
def test_integers_small_dtype_chisquared(self, sample_size, high,
|
|
728
|
+
dtype, chi2max):
|
|
729
|
+
# Regression test for gh-14774.
|
|
730
|
+
samples = random.integers(high, size=sample_size, dtype=dtype)
|
|
731
|
+
|
|
732
|
+
values, counts = np.unique(samples, return_counts=True)
|
|
733
|
+
expected = sample_size / high
|
|
734
|
+
chi2 = ((counts - expected)**2 / expected).sum()
|
|
735
|
+
assert chi2 < chi2max
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
class TestRandomDist:
|
|
739
|
+
# Make sure the random distribution returns the correct value for a
|
|
740
|
+
# given seed
|
|
741
|
+
seed = 1234567890
|
|
742
|
+
|
|
743
|
+
def test_integers(self):
|
|
744
|
+
random = Generator(MT19937(self.seed))
|
|
745
|
+
actual = random.integers(-99, 99, size=(3, 2))
|
|
746
|
+
desired = np.array([[-80, -56], [41, 37], [-83, -16]])
|
|
747
|
+
assert_array_equal(actual, desired)
|
|
748
|
+
|
|
749
|
+
def test_integers_masked(self):
|
|
750
|
+
# Test masked rejection sampling algorithm to generate array of
|
|
751
|
+
# uint32 in an interval.
|
|
752
|
+
random = Generator(MT19937(self.seed))
|
|
753
|
+
actual = random.integers(0, 99, size=(3, 2), dtype=np.uint32)
|
|
754
|
+
desired = np.array([[9, 21], [70, 68], [8, 41]], dtype=np.uint32)
|
|
755
|
+
assert_array_equal(actual, desired)
|
|
756
|
+
|
|
757
|
+
def test_integers_closed(self):
|
|
758
|
+
random = Generator(MT19937(self.seed))
|
|
759
|
+
actual = random.integers(-99, 99, size=(3, 2), endpoint=True)
|
|
760
|
+
desired = np.array([[-80, -56], [41, 38], [-83, -15]])
|
|
761
|
+
assert_array_equal(actual, desired)
|
|
762
|
+
|
|
763
|
+
def test_integers_max_int(self):
|
|
764
|
+
# Tests whether integers with closed=True can generate the
|
|
765
|
+
# maximum allowed Python int that can be converted
|
|
766
|
+
# into a C long. Previous implementations of this
|
|
767
|
+
# method have thrown an OverflowError when attempting
|
|
768
|
+
# to generate this integer.
|
|
769
|
+
actual = random.integers(np.iinfo('l').max, np.iinfo('l').max,
|
|
770
|
+
endpoint=True)
|
|
771
|
+
|
|
772
|
+
desired = np.iinfo('l').max
|
|
773
|
+
assert_equal(actual, desired)
|
|
774
|
+
|
|
775
|
+
def test_random(self):
|
|
776
|
+
random = Generator(MT19937(self.seed))
|
|
777
|
+
actual = random.random((3, 2))
|
|
778
|
+
desired = np.array([[0.096999199829214, 0.707517457682192],
|
|
779
|
+
[0.084364834598269, 0.767731206553125],
|
|
780
|
+
[0.665069021359413, 0.715487190596693]])
|
|
781
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
782
|
+
|
|
783
|
+
random = Generator(MT19937(self.seed))
|
|
784
|
+
actual = random.random()
|
|
785
|
+
assert_array_almost_equal(actual, desired[0, 0], decimal=15)
|
|
786
|
+
|
|
787
|
+
def test_random_float(self):
|
|
788
|
+
random = Generator(MT19937(self.seed))
|
|
789
|
+
actual = random.random((3, 2))
|
|
790
|
+
desired = np.array([[0.0969992 , 0.70751746], # noqa: E203
|
|
791
|
+
[0.08436483, 0.76773121],
|
|
792
|
+
[0.66506902, 0.71548719]])
|
|
793
|
+
assert_array_almost_equal(actual, desired, decimal=7)
|
|
794
|
+
|
|
795
|
+
def test_random_float_scalar(self):
|
|
796
|
+
random = Generator(MT19937(self.seed))
|
|
797
|
+
actual = random.random(dtype=np.float32)
|
|
798
|
+
desired = 0.0969992
|
|
799
|
+
assert_array_almost_equal(actual, desired, decimal=7)
|
|
800
|
+
|
|
801
|
+
@pytest.mark.parametrize('dtype, uint_view_type',
|
|
802
|
+
[(np.float32, np.uint32),
|
|
803
|
+
(np.float64, np.uint64)])
|
|
804
|
+
def test_random_distribution_of_lsb(self, dtype, uint_view_type):
|
|
805
|
+
random = Generator(MT19937(self.seed))
|
|
806
|
+
sample = random.random(100000, dtype=dtype)
|
|
807
|
+
num_ones_in_lsb = np.count_nonzero(sample.view(uint_view_type) & 1)
|
|
808
|
+
# The probability of a 1 in the least significant bit is 0.25.
|
|
809
|
+
# With a sample size of 100000, the probability that num_ones_in_lsb
|
|
810
|
+
# is outside the following range is less than 5e-11.
|
|
811
|
+
assert 24100 < num_ones_in_lsb < 25900
|
|
812
|
+
|
|
813
|
+
def test_random_unsupported_type(self):
|
|
814
|
+
assert_raises(TypeError, random.random, dtype='int32')
|
|
815
|
+
|
|
816
|
+
def test_choice_uniform_replace(self):
|
|
817
|
+
random = Generator(MT19937(self.seed))
|
|
818
|
+
actual = random.choice(4, 4)
|
|
819
|
+
desired = np.array([0, 0, 2, 2], dtype=np.int64)
|
|
820
|
+
assert_array_equal(actual, desired)
|
|
821
|
+
|
|
822
|
+
def test_choice_nonuniform_replace(self):
|
|
823
|
+
random = Generator(MT19937(self.seed))
|
|
824
|
+
actual = random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1])
|
|
825
|
+
desired = np.array([0, 1, 0, 1], dtype=np.int64)
|
|
826
|
+
assert_array_equal(actual, desired)
|
|
827
|
+
|
|
828
|
+
def test_choice_uniform_noreplace(self):
|
|
829
|
+
random = Generator(MT19937(self.seed))
|
|
830
|
+
actual = random.choice(4, 3, replace=False)
|
|
831
|
+
desired = np.array([2, 0, 3], dtype=np.int64)
|
|
832
|
+
assert_array_equal(actual, desired)
|
|
833
|
+
actual = random.choice(4, 4, replace=False, shuffle=False)
|
|
834
|
+
desired = np.arange(4, dtype=np.int64)
|
|
835
|
+
assert_array_equal(actual, desired)
|
|
836
|
+
|
|
837
|
+
def test_choice_nonuniform_noreplace(self):
|
|
838
|
+
random = Generator(MT19937(self.seed))
|
|
839
|
+
actual = random.choice(4, 3, replace=False, p=[0.1, 0.3, 0.5, 0.1])
|
|
840
|
+
desired = np.array([0, 2, 3], dtype=np.int64)
|
|
841
|
+
assert_array_equal(actual, desired)
|
|
842
|
+
|
|
843
|
+
def test_choice_noninteger(self):
|
|
844
|
+
random = Generator(MT19937(self.seed))
|
|
845
|
+
actual = random.choice(['a', 'b', 'c', 'd'], 4)
|
|
846
|
+
desired = np.array(['a', 'a', 'c', 'c'])
|
|
847
|
+
assert_array_equal(actual, desired)
|
|
848
|
+
|
|
849
|
+
def test_choice_multidimensional_default_axis(self):
|
|
850
|
+
random = Generator(MT19937(self.seed))
|
|
851
|
+
actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 3)
|
|
852
|
+
desired = np.array([[0, 1], [0, 1], [4, 5]])
|
|
853
|
+
assert_array_equal(actual, desired)
|
|
854
|
+
|
|
855
|
+
def test_choice_multidimensional_custom_axis(self):
|
|
856
|
+
random = Generator(MT19937(self.seed))
|
|
857
|
+
actual = random.choice([[0, 1], [2, 3], [4, 5], [6, 7]], 1, axis=1)
|
|
858
|
+
desired = np.array([[0], [2], [4], [6]])
|
|
859
|
+
assert_array_equal(actual, desired)
|
|
860
|
+
|
|
861
|
+
def test_choice_exceptions(self):
|
|
862
|
+
sample = random.choice
|
|
863
|
+
assert_raises(ValueError, sample, -1, 3)
|
|
864
|
+
assert_raises(ValueError, sample, 3., 3)
|
|
865
|
+
assert_raises(ValueError, sample, [], 3)
|
|
866
|
+
assert_raises(ValueError, sample, [1, 2, 3, 4], 3,
|
|
867
|
+
p=[[0.25, 0.25], [0.25, 0.25]])
|
|
868
|
+
assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2])
|
|
869
|
+
assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1])
|
|
870
|
+
assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4])
|
|
871
|
+
assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
|
|
872
|
+
# gh-13087
|
|
873
|
+
assert_raises(ValueError, sample, [1, 2, 3], -2, replace=False)
|
|
874
|
+
assert_raises(ValueError, sample, [1, 2, 3], (-1,), replace=False)
|
|
875
|
+
assert_raises(ValueError, sample, [1, 2, 3], (-1, 1), replace=False)
|
|
876
|
+
assert_raises(ValueError, sample, [1, 2, 3], 2,
|
|
877
|
+
replace=False, p=[1, 0, 0])
|
|
878
|
+
|
|
879
|
+
def test_choice_return_shape(self):
|
|
880
|
+
p = [0.1, 0.9]
|
|
881
|
+
# Check scalar
|
|
882
|
+
assert_(np.isscalar(random.choice(2, replace=True)))
|
|
883
|
+
assert_(np.isscalar(random.choice(2, replace=False)))
|
|
884
|
+
assert_(np.isscalar(random.choice(2, replace=True, p=p)))
|
|
885
|
+
assert_(np.isscalar(random.choice(2, replace=False, p=p)))
|
|
886
|
+
assert_(np.isscalar(random.choice([1, 2], replace=True)))
|
|
887
|
+
assert_(random.choice([None], replace=True) is None)
|
|
888
|
+
a = np.array([1, 2])
|
|
889
|
+
arr = np.empty(1, dtype=object)
|
|
890
|
+
arr[0] = a
|
|
891
|
+
assert_(random.choice(arr, replace=True) is a)
|
|
892
|
+
|
|
893
|
+
# Check 0-d array
|
|
894
|
+
s = ()
|
|
895
|
+
assert_(not np.isscalar(random.choice(2, s, replace=True)))
|
|
896
|
+
assert_(not np.isscalar(random.choice(2, s, replace=False)))
|
|
897
|
+
assert_(not np.isscalar(random.choice(2, s, replace=True, p=p)))
|
|
898
|
+
assert_(not np.isscalar(random.choice(2, s, replace=False, p=p)))
|
|
899
|
+
assert_(not np.isscalar(random.choice([1, 2], s, replace=True)))
|
|
900
|
+
assert_(random.choice([None], s, replace=True).ndim == 0)
|
|
901
|
+
a = np.array([1, 2])
|
|
902
|
+
arr = np.empty(1, dtype=object)
|
|
903
|
+
arr[0] = a
|
|
904
|
+
assert_(random.choice(arr, s, replace=True).item() is a)
|
|
905
|
+
|
|
906
|
+
# Check multi dimensional array
|
|
907
|
+
s = (2, 3)
|
|
908
|
+
p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2]
|
|
909
|
+
assert_equal(random.choice(6, s, replace=True).shape, s)
|
|
910
|
+
assert_equal(random.choice(6, s, replace=False).shape, s)
|
|
911
|
+
assert_equal(random.choice(6, s, replace=True, p=p).shape, s)
|
|
912
|
+
assert_equal(random.choice(6, s, replace=False, p=p).shape, s)
|
|
913
|
+
assert_equal(random.choice(np.arange(6), s, replace=True).shape, s)
|
|
914
|
+
|
|
915
|
+
# Check zero-size
|
|
916
|
+
assert_equal(random.integers(0, 0, size=(3, 0, 4)).shape, (3, 0, 4))
|
|
917
|
+
assert_equal(random.integers(0, -10, size=0).shape, (0,))
|
|
918
|
+
assert_equal(random.integers(10, 10, size=0).shape, (0,))
|
|
919
|
+
assert_equal(random.choice(0, size=0).shape, (0,))
|
|
920
|
+
assert_equal(random.choice([], size=(0,)).shape, (0,))
|
|
921
|
+
assert_equal(random.choice(['a', 'b'], size=(3, 0, 4)).shape,
|
|
922
|
+
(3, 0, 4))
|
|
923
|
+
assert_raises(ValueError, random.choice, [], 10)
|
|
924
|
+
|
|
925
|
+
def test_choice_nan_probabilities(self):
|
|
926
|
+
a = np.array([42, 1, 2])
|
|
927
|
+
p = [None, None, None]
|
|
928
|
+
assert_raises(ValueError, random.choice, a, p=p)
|
|
929
|
+
|
|
930
|
+
def test_choice_p_non_contiguous(self):
|
|
931
|
+
p = np.ones(10) / 5
|
|
932
|
+
p[1::2] = 3.0
|
|
933
|
+
random = Generator(MT19937(self.seed))
|
|
934
|
+
non_contig = random.choice(5, 3, p=p[::2])
|
|
935
|
+
random = Generator(MT19937(self.seed))
|
|
936
|
+
contig = random.choice(5, 3, p=np.ascontiguousarray(p[::2]))
|
|
937
|
+
assert_array_equal(non_contig, contig)
|
|
938
|
+
|
|
939
|
+
def test_choice_return_type(self):
|
|
940
|
+
# gh 9867
|
|
941
|
+
p = np.ones(4) / 4.
|
|
942
|
+
actual = random.choice(4, 2)
|
|
943
|
+
assert actual.dtype == np.int64
|
|
944
|
+
actual = random.choice(4, 2, replace=False)
|
|
945
|
+
assert actual.dtype == np.int64
|
|
946
|
+
actual = random.choice(4, 2, p=p)
|
|
947
|
+
assert actual.dtype == np.int64
|
|
948
|
+
actual = random.choice(4, 2, p=p, replace=False)
|
|
949
|
+
assert actual.dtype == np.int64
|
|
950
|
+
|
|
951
|
+
def test_choice_large_sample(self):
|
|
952
|
+
choice_hash = '4266599d12bfcfb815213303432341c06b4349f5455890446578877bb322e222'
|
|
953
|
+
random = Generator(MT19937(self.seed))
|
|
954
|
+
actual = random.choice(10000, 5000, replace=False)
|
|
955
|
+
if sys.byteorder != 'little':
|
|
956
|
+
actual = actual.byteswap()
|
|
957
|
+
res = hashlib.sha256(actual.view(np.int8)).hexdigest()
|
|
958
|
+
assert_(choice_hash == res)
|
|
959
|
+
|
|
960
|
+
def test_choice_array_size_empty_tuple(self):
|
|
961
|
+
random = Generator(MT19937(self.seed))
|
|
962
|
+
assert_array_equal(random.choice([1, 2, 3], size=()), np.array(1),
|
|
963
|
+
strict=True)
|
|
964
|
+
assert_array_equal(random.choice([[1, 2, 3]], size=()), [1, 2, 3])
|
|
965
|
+
assert_array_equal(random.choice([[1]], size=()), [1], strict=True)
|
|
966
|
+
assert_array_equal(random.choice([[1]], size=(), axis=1), [1],
|
|
967
|
+
strict=True)
|
|
968
|
+
|
|
969
|
+
def test_bytes(self):
|
|
970
|
+
random = Generator(MT19937(self.seed))
|
|
971
|
+
actual = random.bytes(10)
|
|
972
|
+
desired = b'\x86\xf0\xd4\x18\xe1\x81\t8%\xdd'
|
|
973
|
+
assert_equal(actual, desired)
|
|
974
|
+
|
|
975
|
+
def test_shuffle(self):
|
|
976
|
+
# Test lists, arrays (of various dtypes), and multidimensional versions
|
|
977
|
+
# of both, c-contiguous or not:
|
|
978
|
+
for conv in [lambda x: np.array([]),
|
|
979
|
+
lambda x: x,
|
|
980
|
+
lambda x: np.asarray(x).astype(np.int8),
|
|
981
|
+
lambda x: np.asarray(x).astype(np.float32),
|
|
982
|
+
lambda x: np.asarray(x).astype(np.complex64),
|
|
983
|
+
lambda x: np.asarray(x).astype(object),
|
|
984
|
+
lambda x: [(i, i) for i in x],
|
|
985
|
+
lambda x: np.asarray([[i, i] for i in x]),
|
|
986
|
+
lambda x: np.vstack([x, x]).T,
|
|
987
|
+
# gh-11442
|
|
988
|
+
lambda x: (np.asarray([(i, i) for i in x],
|
|
989
|
+
[("a", int), ("b", int)])
|
|
990
|
+
.view(np.recarray)),
|
|
991
|
+
# gh-4270
|
|
992
|
+
lambda x: np.asarray([(i, i) for i in x],
|
|
993
|
+
[("a", object, (1,)),
|
|
994
|
+
("b", np.int32, (1,))])]:
|
|
995
|
+
random = Generator(MT19937(self.seed))
|
|
996
|
+
alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
|
|
997
|
+
random.shuffle(alist)
|
|
998
|
+
actual = alist
|
|
999
|
+
desired = conv([4, 1, 9, 8, 0, 5, 3, 6, 2, 7])
|
|
1000
|
+
assert_array_equal(actual, desired)
|
|
1001
|
+
|
|
1002
|
+
def test_shuffle_custom_axis(self):
|
|
1003
|
+
random = Generator(MT19937(self.seed))
|
|
1004
|
+
actual = np.arange(16).reshape((4, 4))
|
|
1005
|
+
random.shuffle(actual, axis=1)
|
|
1006
|
+
desired = np.array([[ 0, 3, 1, 2],
|
|
1007
|
+
[ 4, 7, 5, 6],
|
|
1008
|
+
[ 8, 11, 9, 10],
|
|
1009
|
+
[12, 15, 13, 14]])
|
|
1010
|
+
assert_array_equal(actual, desired)
|
|
1011
|
+
random = Generator(MT19937(self.seed))
|
|
1012
|
+
actual = np.arange(16).reshape((4, 4))
|
|
1013
|
+
random.shuffle(actual, axis=-1)
|
|
1014
|
+
assert_array_equal(actual, desired)
|
|
1015
|
+
|
|
1016
|
+
def test_shuffle_custom_axis_empty(self):
|
|
1017
|
+
random = Generator(MT19937(self.seed))
|
|
1018
|
+
desired = np.array([]).reshape((0, 6))
|
|
1019
|
+
for axis in (0, 1):
|
|
1020
|
+
actual = np.array([]).reshape((0, 6))
|
|
1021
|
+
random.shuffle(actual, axis=axis)
|
|
1022
|
+
assert_array_equal(actual, desired)
|
|
1023
|
+
|
|
1024
|
+
def test_shuffle_axis_nonsquare(self):
|
|
1025
|
+
y1 = np.arange(20).reshape(2, 10)
|
|
1026
|
+
y2 = y1.copy()
|
|
1027
|
+
random = Generator(MT19937(self.seed))
|
|
1028
|
+
random.shuffle(y1, axis=1)
|
|
1029
|
+
random = Generator(MT19937(self.seed))
|
|
1030
|
+
random.shuffle(y2.T)
|
|
1031
|
+
assert_array_equal(y1, y2)
|
|
1032
|
+
|
|
1033
|
+
def test_shuffle_masked(self):
|
|
1034
|
+
# gh-3263
|
|
1035
|
+
a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1)
|
|
1036
|
+
b = np.ma.masked_values(np.arange(20) % 3 - 1, -1)
|
|
1037
|
+
a_orig = a.copy()
|
|
1038
|
+
b_orig = b.copy()
|
|
1039
|
+
for i in range(50):
|
|
1040
|
+
random.shuffle(a)
|
|
1041
|
+
assert_equal(
|
|
1042
|
+
sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask]))
|
|
1043
|
+
random.shuffle(b)
|
|
1044
|
+
assert_equal(
|
|
1045
|
+
sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))
|
|
1046
|
+
|
|
1047
|
+
def test_shuffle_exceptions(self):
|
|
1048
|
+
random = Generator(MT19937(self.seed))
|
|
1049
|
+
arr = np.arange(10)
|
|
1050
|
+
assert_raises(AxisError, random.shuffle, arr, 1)
|
|
1051
|
+
arr = np.arange(9).reshape((3, 3))
|
|
1052
|
+
assert_raises(AxisError, random.shuffle, arr, 3)
|
|
1053
|
+
assert_raises(TypeError, random.shuffle, arr, slice(1, 2, None))
|
|
1054
|
+
arr = [[1, 2, 3], [4, 5, 6]]
|
|
1055
|
+
assert_raises(NotImplementedError, random.shuffle, arr, 1)
|
|
1056
|
+
|
|
1057
|
+
arr = np.array(3)
|
|
1058
|
+
assert_raises(TypeError, random.shuffle, arr)
|
|
1059
|
+
arr = np.ones((3, 2))
|
|
1060
|
+
assert_raises(AxisError, random.shuffle, arr, 2)
|
|
1061
|
+
|
|
1062
|
+
def test_shuffle_not_writeable(self):
|
|
1063
|
+
random = Generator(MT19937(self.seed))
|
|
1064
|
+
a = np.zeros(5)
|
|
1065
|
+
a.flags.writeable = False
|
|
1066
|
+
with pytest.raises(ValueError, match='read-only'):
|
|
1067
|
+
random.shuffle(a)
|
|
1068
|
+
|
|
1069
|
+
def test_permutation(self):
|
|
1070
|
+
random = Generator(MT19937(self.seed))
|
|
1071
|
+
alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
|
|
1072
|
+
actual = random.permutation(alist)
|
|
1073
|
+
desired = [4, 1, 9, 8, 0, 5, 3, 6, 2, 7]
|
|
1074
|
+
assert_array_equal(actual, desired)
|
|
1075
|
+
|
|
1076
|
+
random = Generator(MT19937(self.seed))
|
|
1077
|
+
arr_2d = np.atleast_2d([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]).T
|
|
1078
|
+
actual = random.permutation(arr_2d)
|
|
1079
|
+
assert_array_equal(actual, np.atleast_2d(desired).T)
|
|
1080
|
+
|
|
1081
|
+
bad_x_str = "abcd"
|
|
1082
|
+
assert_raises(AxisError, random.permutation, bad_x_str)
|
|
1083
|
+
|
|
1084
|
+
bad_x_float = 1.2
|
|
1085
|
+
assert_raises(AxisError, random.permutation, bad_x_float)
|
|
1086
|
+
|
|
1087
|
+
random = Generator(MT19937(self.seed))
|
|
1088
|
+
integer_val = 10
|
|
1089
|
+
desired = [3, 0, 8, 7, 9, 4, 2, 5, 1, 6]
|
|
1090
|
+
|
|
1091
|
+
actual = random.permutation(integer_val)
|
|
1092
|
+
assert_array_equal(actual, desired)
|
|
1093
|
+
|
|
1094
|
+
def test_permutation_custom_axis(self):
|
|
1095
|
+
a = np.arange(16).reshape((4, 4))
|
|
1096
|
+
desired = np.array([[ 0, 3, 1, 2],
|
|
1097
|
+
[ 4, 7, 5, 6],
|
|
1098
|
+
[ 8, 11, 9, 10],
|
|
1099
|
+
[12, 15, 13, 14]])
|
|
1100
|
+
random = Generator(MT19937(self.seed))
|
|
1101
|
+
actual = random.permutation(a, axis=1)
|
|
1102
|
+
assert_array_equal(actual, desired)
|
|
1103
|
+
random = Generator(MT19937(self.seed))
|
|
1104
|
+
actual = random.permutation(a, axis=-1)
|
|
1105
|
+
assert_array_equal(actual, desired)
|
|
1106
|
+
|
|
1107
|
+
def test_permutation_exceptions(self):
|
|
1108
|
+
random = Generator(MT19937(self.seed))
|
|
1109
|
+
arr = np.arange(10)
|
|
1110
|
+
assert_raises(AxisError, random.permutation, arr, 1)
|
|
1111
|
+
arr = np.arange(9).reshape((3, 3))
|
|
1112
|
+
assert_raises(AxisError, random.permutation, arr, 3)
|
|
1113
|
+
assert_raises(TypeError, random.permutation, arr, slice(1, 2, None))
|
|
1114
|
+
|
|
1115
|
+
@pytest.mark.parametrize("dtype", [int, object])
|
|
1116
|
+
@pytest.mark.parametrize("axis, expected",
|
|
1117
|
+
[(None, np.array([[3, 7, 0, 9, 10, 11],
|
|
1118
|
+
[8, 4, 2, 5, 1, 6]])),
|
|
1119
|
+
(0, np.array([[6, 1, 2, 9, 10, 11],
|
|
1120
|
+
[0, 7, 8, 3, 4, 5]])),
|
|
1121
|
+
(1, np.array([[ 5, 3, 4, 0, 2, 1],
|
|
1122
|
+
[11, 9, 10, 6, 8, 7]]))])
|
|
1123
|
+
def test_permuted(self, dtype, axis, expected):
|
|
1124
|
+
random = Generator(MT19937(self.seed))
|
|
1125
|
+
x = np.arange(12).reshape(2, 6).astype(dtype)
|
|
1126
|
+
random.permuted(x, axis=axis, out=x)
|
|
1127
|
+
assert_array_equal(x, expected)
|
|
1128
|
+
|
|
1129
|
+
random = Generator(MT19937(self.seed))
|
|
1130
|
+
x = np.arange(12).reshape(2, 6).astype(dtype)
|
|
1131
|
+
y = random.permuted(x, axis=axis)
|
|
1132
|
+
assert y.dtype == dtype
|
|
1133
|
+
assert_array_equal(y, expected)
|
|
1134
|
+
|
|
1135
|
+
def test_permuted_with_strides(self):
|
|
1136
|
+
random = Generator(MT19937(self.seed))
|
|
1137
|
+
x0 = np.arange(22).reshape(2, 11)
|
|
1138
|
+
x1 = x0.copy()
|
|
1139
|
+
x = x0[:, ::3]
|
|
1140
|
+
y = random.permuted(x, axis=1, out=x)
|
|
1141
|
+
expected = np.array([[0, 9, 3, 6],
|
|
1142
|
+
[14, 20, 11, 17]])
|
|
1143
|
+
assert_array_equal(y, expected)
|
|
1144
|
+
x1[:, ::3] = expected
|
|
1145
|
+
# Verify that the original x0 was modified in-place as expected.
|
|
1146
|
+
assert_array_equal(x1, x0)
|
|
1147
|
+
|
|
1148
|
+
def test_permuted_empty(self):
|
|
1149
|
+
y = random.permuted([])
|
|
1150
|
+
assert_array_equal(y, [])
|
|
1151
|
+
|
|
1152
|
+
@pytest.mark.parametrize('outshape', [(2, 3), 5])
|
|
1153
|
+
def test_permuted_out_with_wrong_shape(self, outshape):
|
|
1154
|
+
a = np.array([1, 2, 3])
|
|
1155
|
+
out = np.zeros(outshape, dtype=a.dtype)
|
|
1156
|
+
with pytest.raises(ValueError, match='same shape'):
|
|
1157
|
+
random.permuted(a, out=out)
|
|
1158
|
+
|
|
1159
|
+
def test_permuted_out_with_wrong_type(self):
|
|
1160
|
+
out = np.zeros((3, 5), dtype=np.int32)
|
|
1161
|
+
x = np.ones((3, 5))
|
|
1162
|
+
with pytest.raises(TypeError, match='Cannot cast'):
|
|
1163
|
+
random.permuted(x, axis=1, out=out)
|
|
1164
|
+
|
|
1165
|
+
def test_permuted_not_writeable(self):
|
|
1166
|
+
x = np.zeros((2, 5))
|
|
1167
|
+
x.flags.writeable = False
|
|
1168
|
+
with pytest.raises(ValueError, match='read-only'):
|
|
1169
|
+
random.permuted(x, axis=1, out=x)
|
|
1170
|
+
|
|
1171
|
+
def test_beta(self):
|
|
1172
|
+
random = Generator(MT19937(self.seed))
|
|
1173
|
+
actual = random.beta(.1, .9, size=(3, 2))
|
|
1174
|
+
desired = np.array(
|
|
1175
|
+
[[1.083029353267698e-10, 2.449965303168024e-11],
|
|
1176
|
+
[2.397085162969853e-02, 3.590779671820755e-08],
|
|
1177
|
+
[2.830254190078299e-04, 1.744709918330393e-01]])
|
|
1178
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1179
|
+
|
|
1180
|
+
def test_binomial(self):
|
|
1181
|
+
random = Generator(MT19937(self.seed))
|
|
1182
|
+
actual = random.binomial(100.123, .456, size=(3, 2))
|
|
1183
|
+
desired = np.array([[42, 41],
|
|
1184
|
+
[42, 48],
|
|
1185
|
+
[44, 50]])
|
|
1186
|
+
assert_array_equal(actual, desired)
|
|
1187
|
+
|
|
1188
|
+
random = Generator(MT19937(self.seed))
|
|
1189
|
+
actual = random.binomial(100.123, .456)
|
|
1190
|
+
desired = 42
|
|
1191
|
+
assert_array_equal(actual, desired)
|
|
1192
|
+
|
|
1193
|
+
def test_chisquare(self):
|
|
1194
|
+
random = Generator(MT19937(self.seed))
|
|
1195
|
+
actual = random.chisquare(50, size=(3, 2))
|
|
1196
|
+
desired = np.array([[32.9850547060149, 39.0219480493301],
|
|
1197
|
+
[56.2006134779419, 57.3474165711485],
|
|
1198
|
+
[55.4243733880198, 55.4209797925213]])
|
|
1199
|
+
assert_array_almost_equal(actual, desired, decimal=13)
|
|
1200
|
+
|
|
1201
|
+
def test_dirichlet(self):
|
|
1202
|
+
random = Generator(MT19937(self.seed))
|
|
1203
|
+
alpha = np.array([51.72840233779265162, 39.74494232180943953])
|
|
1204
|
+
actual = random.dirichlet(alpha, size=(3, 2))
|
|
1205
|
+
desired = np.array([[[0.5439892869558927, 0.45601071304410745],
|
|
1206
|
+
[0.5588917345860708, 0.4411082654139292 ]], # noqa: E202
|
|
1207
|
+
[[0.5632074165063435, 0.43679258349365657],
|
|
1208
|
+
[0.54862581112627, 0.45137418887373015]],
|
|
1209
|
+
[[0.49961831357047226, 0.5003816864295278 ], # noqa: E202
|
|
1210
|
+
[0.52374806183482, 0.47625193816517997]]])
|
|
1211
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1212
|
+
bad_alpha = np.array([5.4e-01, -1.0e-16])
|
|
1213
|
+
assert_raises(ValueError, random.dirichlet, bad_alpha)
|
|
1214
|
+
|
|
1215
|
+
random = Generator(MT19937(self.seed))
|
|
1216
|
+
alpha = np.array([51.72840233779265162, 39.74494232180943953])
|
|
1217
|
+
actual = random.dirichlet(alpha)
|
|
1218
|
+
assert_array_almost_equal(actual, desired[0, 0], decimal=15)
|
|
1219
|
+
|
|
1220
|
+
def test_dirichlet_size(self):
|
|
1221
|
+
# gh-3173
|
|
1222
|
+
p = np.array([51.72840233779265162, 39.74494232180943953])
|
|
1223
|
+
assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
|
|
1224
|
+
assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
|
|
1225
|
+
assert_equal(random.dirichlet(p, np.uint32(1)).shape, (1, 2))
|
|
1226
|
+
assert_equal(random.dirichlet(p, [2, 2]).shape, (2, 2, 2))
|
|
1227
|
+
assert_equal(random.dirichlet(p, (2, 2)).shape, (2, 2, 2))
|
|
1228
|
+
assert_equal(random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2))
|
|
1229
|
+
|
|
1230
|
+
assert_raises(TypeError, random.dirichlet, p, float(1))
|
|
1231
|
+
|
|
1232
|
+
def test_dirichlet_bad_alpha(self):
|
|
1233
|
+
# gh-2089
|
|
1234
|
+
alpha = np.array([5.4e-01, -1.0e-16])
|
|
1235
|
+
assert_raises(ValueError, random.dirichlet, alpha)
|
|
1236
|
+
|
|
1237
|
+
# gh-15876
|
|
1238
|
+
assert_raises(ValueError, random.dirichlet, [[5, 1]])
|
|
1239
|
+
assert_raises(ValueError, random.dirichlet, [[5], [1]])
|
|
1240
|
+
assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
|
|
1241
|
+
assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
|
|
1242
|
+
|
|
1243
|
+
def test_dirichlet_alpha_non_contiguous(self):
|
|
1244
|
+
a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
|
|
1245
|
+
alpha = a[::2]
|
|
1246
|
+
random = Generator(MT19937(self.seed))
|
|
1247
|
+
non_contig = random.dirichlet(alpha, size=(3, 2))
|
|
1248
|
+
random = Generator(MT19937(self.seed))
|
|
1249
|
+
contig = random.dirichlet(np.ascontiguousarray(alpha),
|
|
1250
|
+
size=(3, 2))
|
|
1251
|
+
assert_array_almost_equal(non_contig, contig)
|
|
1252
|
+
|
|
1253
|
+
def test_dirichlet_small_alpha(self):
|
|
1254
|
+
eps = 1.0e-9 # 1.0e-10 -> runtime x 10; 1e-11 -> runtime x 200, etc.
|
|
1255
|
+
alpha = eps * np.array([1., 1.0e-3])
|
|
1256
|
+
random = Generator(MT19937(self.seed))
|
|
1257
|
+
actual = random.dirichlet(alpha, size=(3, 2))
|
|
1258
|
+
expected = np.array([
|
|
1259
|
+
[[1., 0.],
|
|
1260
|
+
[1., 0.]],
|
|
1261
|
+
[[1., 0.],
|
|
1262
|
+
[1., 0.]],
|
|
1263
|
+
[[1., 0.],
|
|
1264
|
+
[1., 0.]]
|
|
1265
|
+
])
|
|
1266
|
+
assert_array_almost_equal(actual, expected, decimal=15)
|
|
1267
|
+
|
|
1268
|
+
@pytest.mark.slow
|
|
1269
|
+
@pytest.mark.thread_unsafe(reason="crashes with low memory")
|
|
1270
|
+
def test_dirichlet_moderately_small_alpha(self):
|
|
1271
|
+
# Use alpha.max() < 0.1 to trigger stick breaking code path
|
|
1272
|
+
alpha = np.array([0.02, 0.04, 0.03])
|
|
1273
|
+
exact_mean = alpha / alpha.sum()
|
|
1274
|
+
random = Generator(MT19937(self.seed))
|
|
1275
|
+
sample = random.dirichlet(alpha, size=20000000)
|
|
1276
|
+
sample_mean = sample.mean(axis=0)
|
|
1277
|
+
assert_allclose(sample_mean, exact_mean, rtol=1e-3)
|
|
1278
|
+
|
|
1279
|
+
# This set of parameters includes inputs with alpha.max() >= 0.1 and
|
|
1280
|
+
# alpha.max() < 0.1 to exercise both generation methods within the
|
|
1281
|
+
# dirichlet code.
|
|
1282
|
+
@pytest.mark.parametrize(
|
|
1283
|
+
'alpha',
|
|
1284
|
+
[[5, 9, 0, 8],
|
|
1285
|
+
[0.5, 0, 0, 0],
|
|
1286
|
+
[1, 5, 0, 0, 1.5, 0, 0, 0],
|
|
1287
|
+
[0.01, 0.03, 0, 0.005],
|
|
1288
|
+
[1e-5, 0, 0, 0],
|
|
1289
|
+
[0.002, 0.015, 0, 0, 0.04, 0, 0, 0],
|
|
1290
|
+
[0.0],
|
|
1291
|
+
[0, 0, 0]],
|
|
1292
|
+
)
|
|
1293
|
+
def test_dirichlet_multiple_zeros_in_alpha(self, alpha):
|
|
1294
|
+
alpha = np.array(alpha)
|
|
1295
|
+
y = random.dirichlet(alpha)
|
|
1296
|
+
assert_equal(y[alpha == 0], 0.0)
|
|
1297
|
+
|
|
1298
|
+
def test_exponential(self):
|
|
1299
|
+
random = Generator(MT19937(self.seed))
|
|
1300
|
+
actual = random.exponential(1.1234, size=(3, 2))
|
|
1301
|
+
desired = np.array([[0.098845481066258, 1.560752510746964],
|
|
1302
|
+
[0.075730916041636, 1.769098974710777],
|
|
1303
|
+
[1.488602544592235, 2.49684815275751 ]]) # noqa: E202
|
|
1304
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1305
|
+
|
|
1306
|
+
def test_exponential_0(self):
|
|
1307
|
+
assert_equal(random.exponential(scale=0), 0)
|
|
1308
|
+
assert_raises(ValueError, random.exponential, scale=-0.)
|
|
1309
|
+
|
|
1310
|
+
def test_f(self):
|
|
1311
|
+
random = Generator(MT19937(self.seed))
|
|
1312
|
+
actual = random.f(12, 77, size=(3, 2))
|
|
1313
|
+
desired = np.array([[0.461720027077085, 1.100441958872451],
|
|
1314
|
+
[1.100337455217484, 0.91421736740018 ], # noqa: E202
|
|
1315
|
+
[0.500811891303113, 0.826802454552058]])
|
|
1316
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1317
|
+
|
|
1318
|
+
def test_gamma(self):
|
|
1319
|
+
random = Generator(MT19937(self.seed))
|
|
1320
|
+
actual = random.gamma(5, 3, size=(3, 2))
|
|
1321
|
+
desired = np.array([[ 5.03850858902096, 7.9228656732049 ], # noqa: E202
|
|
1322
|
+
[18.73983605132985, 19.57961681699238],
|
|
1323
|
+
[18.17897755150825, 18.17653912505234]])
|
|
1324
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1325
|
+
|
|
1326
|
+
def test_gamma_0(self):
|
|
1327
|
+
assert_equal(random.gamma(shape=0, scale=0), 0)
|
|
1328
|
+
assert_raises(ValueError, random.gamma, shape=-0., scale=-0.)
|
|
1329
|
+
|
|
1330
|
+
def test_geometric(self):
|
|
1331
|
+
random = Generator(MT19937(self.seed))
|
|
1332
|
+
actual = random.geometric(.123456789, size=(3, 2))
|
|
1333
|
+
desired = np.array([[1, 11],
|
|
1334
|
+
[1, 12],
|
|
1335
|
+
[11, 17]])
|
|
1336
|
+
assert_array_equal(actual, desired)
|
|
1337
|
+
|
|
1338
|
+
def test_geometric_exceptions(self):
|
|
1339
|
+
assert_raises(ValueError, random.geometric, 1.1)
|
|
1340
|
+
assert_raises(ValueError, random.geometric, [1.1] * 10)
|
|
1341
|
+
assert_raises(ValueError, random.geometric, -0.1)
|
|
1342
|
+
assert_raises(ValueError, random.geometric, [-0.1] * 10)
|
|
1343
|
+
with np.errstate(invalid='ignore'):
|
|
1344
|
+
assert_raises(ValueError, random.geometric, np.nan)
|
|
1345
|
+
assert_raises(ValueError, random.geometric, [np.nan] * 10)
|
|
1346
|
+
|
|
1347
|
+
def test_gumbel(self):
|
|
1348
|
+
random = Generator(MT19937(self.seed))
|
|
1349
|
+
actual = random.gumbel(loc=.123456789, scale=2.0, size=(3, 2))
|
|
1350
|
+
desired = np.array([[ 4.688397515056245, -0.289514845417841],
|
|
1351
|
+
[ 4.981176042584683, -0.633224272589149],
|
|
1352
|
+
[-0.055915275687488, -0.333962478257953]])
|
|
1353
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1354
|
+
|
|
1355
|
+
def test_gumbel_0(self):
|
|
1356
|
+
assert_equal(random.gumbel(scale=0), 0)
|
|
1357
|
+
assert_raises(ValueError, random.gumbel, scale=-0.)
|
|
1358
|
+
|
|
1359
|
+
def test_hypergeometric(self):
|
|
1360
|
+
random = Generator(MT19937(self.seed))
|
|
1361
|
+
actual = random.hypergeometric(10.1, 5.5, 14, size=(3, 2))
|
|
1362
|
+
desired = np.array([[ 9, 9],
|
|
1363
|
+
[ 9, 9],
|
|
1364
|
+
[10, 9]])
|
|
1365
|
+
assert_array_equal(actual, desired)
|
|
1366
|
+
|
|
1367
|
+
# Test nbad = 0
|
|
1368
|
+
actual = random.hypergeometric(5, 0, 3, size=4)
|
|
1369
|
+
desired = np.array([3, 3, 3, 3])
|
|
1370
|
+
assert_array_equal(actual, desired)
|
|
1371
|
+
|
|
1372
|
+
actual = random.hypergeometric(15, 0, 12, size=4)
|
|
1373
|
+
desired = np.array([12, 12, 12, 12])
|
|
1374
|
+
assert_array_equal(actual, desired)
|
|
1375
|
+
|
|
1376
|
+
# Test ngood = 0
|
|
1377
|
+
actual = random.hypergeometric(0, 5, 3, size=4)
|
|
1378
|
+
desired = np.array([0, 0, 0, 0])
|
|
1379
|
+
assert_array_equal(actual, desired)
|
|
1380
|
+
|
|
1381
|
+
actual = random.hypergeometric(0, 15, 12, size=4)
|
|
1382
|
+
desired = np.array([0, 0, 0, 0])
|
|
1383
|
+
assert_array_equal(actual, desired)
|
|
1384
|
+
|
|
1385
|
+
def test_laplace(self):
|
|
1386
|
+
random = Generator(MT19937(self.seed))
|
|
1387
|
+
actual = random.laplace(loc=.123456789, scale=2.0, size=(3, 2))
|
|
1388
|
+
desired = np.array([[-3.156353949272393, 1.195863024830054],
|
|
1389
|
+
[-3.435458081645966, 1.656882398925444],
|
|
1390
|
+
[ 0.924824032467446, 1.251116432209336]])
|
|
1391
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1392
|
+
|
|
1393
|
+
def test_laplace_0(self):
|
|
1394
|
+
assert_equal(random.laplace(scale=0), 0)
|
|
1395
|
+
assert_raises(ValueError, random.laplace, scale=-0.)
|
|
1396
|
+
|
|
1397
|
+
def test_logistic(self):
|
|
1398
|
+
random = Generator(MT19937(self.seed))
|
|
1399
|
+
actual = random.logistic(loc=.123456789, scale=2.0, size=(3, 2))
|
|
1400
|
+
desired = np.array([[-4.338584631510999, 1.890171436749954],
|
|
1401
|
+
[-4.64547787337966 , 2.514545562919217], # noqa: E203
|
|
1402
|
+
[ 1.495389489198666, 1.967827627577474]])
|
|
1403
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1404
|
+
|
|
1405
|
+
def test_lognormal(self):
|
|
1406
|
+
random = Generator(MT19937(self.seed))
|
|
1407
|
+
actual = random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2))
|
|
1408
|
+
desired = np.array([[ 0.0268252166335, 13.9534486483053],
|
|
1409
|
+
[ 0.1204014788936, 2.2422077497792],
|
|
1410
|
+
[ 4.2484199496128, 12.0093343977523]])
|
|
1411
|
+
assert_array_almost_equal(actual, desired, decimal=13)
|
|
1412
|
+
|
|
1413
|
+
def test_lognormal_0(self):
|
|
1414
|
+
assert_equal(random.lognormal(sigma=0), 1)
|
|
1415
|
+
assert_raises(ValueError, random.lognormal, sigma=-0.)
|
|
1416
|
+
|
|
1417
|
+
def test_logseries(self):
|
|
1418
|
+
random = Generator(MT19937(self.seed))
|
|
1419
|
+
actual = random.logseries(p=.923456789, size=(3, 2))
|
|
1420
|
+
desired = np.array([[14, 17],
|
|
1421
|
+
[3, 18],
|
|
1422
|
+
[5, 1]])
|
|
1423
|
+
assert_array_equal(actual, desired)
|
|
1424
|
+
|
|
1425
|
+
def test_logseries_zero(self):
|
|
1426
|
+
random = Generator(MT19937(self.seed))
|
|
1427
|
+
assert random.logseries(0) == 1
|
|
1428
|
+
|
|
1429
|
+
@pytest.mark.parametrize("value", [np.nextafter(0., -1), 1., np.nan, 5.])
|
|
1430
|
+
def test_logseries_exceptions(self, value):
|
|
1431
|
+
random = Generator(MT19937(self.seed))
|
|
1432
|
+
with np.errstate(invalid="ignore"):
|
|
1433
|
+
with pytest.raises(ValueError):
|
|
1434
|
+
random.logseries(value)
|
|
1435
|
+
with pytest.raises(ValueError):
|
|
1436
|
+
# contiguous path:
|
|
1437
|
+
random.logseries(np.array([value] * 10))
|
|
1438
|
+
with pytest.raises(ValueError):
|
|
1439
|
+
# non-contiguous path:
|
|
1440
|
+
random.logseries(np.array([value] * 10)[::2])
|
|
1441
|
+
|
|
1442
|
+
def test_multinomial(self):
|
|
1443
|
+
random = Generator(MT19937(self.seed))
|
|
1444
|
+
actual = random.multinomial(20, [1 / 6.] * 6, size=(3, 2))
|
|
1445
|
+
desired = np.array([[[1, 5, 1, 6, 4, 3],
|
|
1446
|
+
[4, 2, 6, 2, 4, 2]],
|
|
1447
|
+
[[5, 3, 2, 6, 3, 1],
|
|
1448
|
+
[4, 4, 0, 2, 3, 7]],
|
|
1449
|
+
[[6, 3, 1, 5, 3, 2],
|
|
1450
|
+
[5, 5, 3, 1, 2, 4]]])
|
|
1451
|
+
assert_array_equal(actual, desired)
|
|
1452
|
+
|
|
1453
|
+
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
|
|
1454
|
+
@pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"])
|
|
1455
|
+
def test_multivariate_normal(self, method):
|
|
1456
|
+
random = Generator(MT19937(self.seed))
|
|
1457
|
+
mean = (.123456789, 10)
|
|
1458
|
+
cov = [[1, 0], [0, 1]]
|
|
1459
|
+
size = (3, 2)
|
|
1460
|
+
actual = random.multivariate_normal(mean, cov, size, method=method)
|
|
1461
|
+
desired = np.array([[[-1.747478062846581, 11.25613495182354 ], # noqa: E202
|
|
1462
|
+
[-0.9967333370066214, 10.342002097029821]],
|
|
1463
|
+
[[ 0.7850019631242964, 11.181113712443013],
|
|
1464
|
+
[ 0.8901349653255224, 8.873825399642492]],
|
|
1465
|
+
[[ 0.7130260107430003, 9.551628690083056],
|
|
1466
|
+
[ 0.7127098726541128, 11.991709234143173]]])
|
|
1467
|
+
|
|
1468
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1469
|
+
|
|
1470
|
+
# Check for default size, was raising deprecation warning
|
|
1471
|
+
actual = random.multivariate_normal(mean, cov, method=method)
|
|
1472
|
+
desired = np.array([0.233278563284287, 9.424140804347195])
|
|
1473
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1474
|
+
# Check that non symmetric covariance input raises exception when
|
|
1475
|
+
# check_valid='raises' if using default svd method.
|
|
1476
|
+
mean = [0, 0]
|
|
1477
|
+
cov = [[1, 2], [1, 2]]
|
|
1478
|
+
assert_raises(ValueError, random.multivariate_normal, mean, cov,
|
|
1479
|
+
check_valid='raise')
|
|
1480
|
+
|
|
1481
|
+
# Check that non positive-semidefinite covariance warns with
|
|
1482
|
+
# RuntimeWarning
|
|
1483
|
+
cov = [[1, 2], [2, 1]]
|
|
1484
|
+
pytest.warns(RuntimeWarning, random.multivariate_normal, mean, cov)
|
|
1485
|
+
pytest.warns(RuntimeWarning, random.multivariate_normal, mean, cov,
|
|
1486
|
+
method='eigh')
|
|
1487
|
+
assert_raises(LinAlgError, random.multivariate_normal, mean, cov,
|
|
1488
|
+
method='cholesky')
|
|
1489
|
+
|
|
1490
|
+
# and that it doesn't warn with RuntimeWarning check_valid='ignore'
|
|
1491
|
+
assert_no_warnings(random.multivariate_normal, mean, cov,
|
|
1492
|
+
check_valid='ignore')
|
|
1493
|
+
|
|
1494
|
+
# and that it raises with RuntimeWarning check_valid='raises'
|
|
1495
|
+
assert_raises(ValueError, random.multivariate_normal, mean, cov,
|
|
1496
|
+
check_valid='raise')
|
|
1497
|
+
assert_raises(ValueError, random.multivariate_normal, mean, cov,
|
|
1498
|
+
check_valid='raise', method='eigh')
|
|
1499
|
+
|
|
1500
|
+
# check degenerate samples from singular covariance matrix
|
|
1501
|
+
cov = [[1, 1], [1, 1]]
|
|
1502
|
+
if method in ('svd', 'eigh'):
|
|
1503
|
+
samples = random.multivariate_normal(mean, cov, size=(3, 2),
|
|
1504
|
+
method=method)
|
|
1505
|
+
assert_array_almost_equal(samples[..., 0], samples[..., 1],
|
|
1506
|
+
decimal=6)
|
|
1507
|
+
else:
|
|
1508
|
+
assert_raises(LinAlgError, random.multivariate_normal, mean, cov,
|
|
1509
|
+
method='cholesky')
|
|
1510
|
+
|
|
1511
|
+
cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32)
|
|
1512
|
+
with warnings.catch_warnings():
|
|
1513
|
+
warnings.simplefilter("error")
|
|
1514
|
+
random.multivariate_normal(mean, cov, method=method)
|
|
1515
|
+
|
|
1516
|
+
mu = np.zeros(2)
|
|
1517
|
+
cov = np.eye(2)
|
|
1518
|
+
assert_raises(ValueError, random.multivariate_normal, mean, cov,
|
|
1519
|
+
check_valid='other')
|
|
1520
|
+
assert_raises(ValueError, random.multivariate_normal,
|
|
1521
|
+
np.zeros((2, 1, 1)), cov)
|
|
1522
|
+
assert_raises(ValueError, random.multivariate_normal,
|
|
1523
|
+
mu, np.empty((3, 2)))
|
|
1524
|
+
assert_raises(ValueError, random.multivariate_normal,
|
|
1525
|
+
mu, np.eye(3))
|
|
1526
|
+
|
|
1527
|
+
@pytest.mark.parametrize('mean, cov', [([0], [[1 + 1j]]), ([0j], [[1]])])
|
|
1528
|
+
def test_multivariate_normal_disallow_complex(self, mean, cov):
|
|
1529
|
+
random = Generator(MT19937(self.seed))
|
|
1530
|
+
with pytest.raises(TypeError, match="must not be complex"):
|
|
1531
|
+
random.multivariate_normal(mean, cov)
|
|
1532
|
+
|
|
1533
|
+
@pytest.mark.parametrize("method", ["svd", "eigh", "cholesky"])
|
|
1534
|
+
def test_multivariate_normal_basic_stats(self, method):
|
|
1535
|
+
random = Generator(MT19937(self.seed))
|
|
1536
|
+
n_s = 1000
|
|
1537
|
+
mean = np.array([1, 2])
|
|
1538
|
+
cov = np.array([[2, 1], [1, 2]])
|
|
1539
|
+
s = random.multivariate_normal(mean, cov, size=(n_s,), method=method)
|
|
1540
|
+
s_center = s - mean
|
|
1541
|
+
cov_emp = (s_center.T @ s_center) / (n_s - 1)
|
|
1542
|
+
# these are pretty loose and are only designed to detect major errors
|
|
1543
|
+
assert np.all(np.abs(s_center.mean(-2)) < 0.1)
|
|
1544
|
+
assert np.all(np.abs(cov_emp - cov) < 0.2)
|
|
1545
|
+
|
|
1546
|
+
def test_negative_binomial(self):
|
|
1547
|
+
random = Generator(MT19937(self.seed))
|
|
1548
|
+
actual = random.negative_binomial(n=100, p=.12345, size=(3, 2))
|
|
1549
|
+
desired = np.array([[543, 727],
|
|
1550
|
+
[775, 760],
|
|
1551
|
+
[600, 674]])
|
|
1552
|
+
assert_array_equal(actual, desired)
|
|
1553
|
+
|
|
1554
|
+
def test_negative_binomial_exceptions(self):
|
|
1555
|
+
with np.errstate(invalid='ignore'):
|
|
1556
|
+
assert_raises(ValueError, random.negative_binomial, 100, np.nan)
|
|
1557
|
+
assert_raises(ValueError, random.negative_binomial, 100,
|
|
1558
|
+
[np.nan] * 10)
|
|
1559
|
+
|
|
1560
|
+
def test_negative_binomial_p0_exception(self):
|
|
1561
|
+
# Verify that p=0 raises an exception.
|
|
1562
|
+
with assert_raises(ValueError):
|
|
1563
|
+
x = random.negative_binomial(1, 0)
|
|
1564
|
+
|
|
1565
|
+
def test_negative_binomial_invalid_p_n_combination(self):
|
|
1566
|
+
# Verify that values of p and n that would result in an overflow
|
|
1567
|
+
# or infinite loop raise an exception.
|
|
1568
|
+
with np.errstate(invalid='ignore'):
|
|
1569
|
+
assert_raises(ValueError, random.negative_binomial, 2**62, 0.1)
|
|
1570
|
+
assert_raises(ValueError, random.negative_binomial, [2**62], [0.1])
|
|
1571
|
+
|
|
1572
|
+
def test_noncentral_chisquare(self):
|
|
1573
|
+
random = Generator(MT19937(self.seed))
|
|
1574
|
+
actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))
|
|
1575
|
+
desired = np.array([[ 1.70561552362133, 15.97378184942111],
|
|
1576
|
+
[13.71483425173724, 20.17859633310629],
|
|
1577
|
+
[11.3615477156643 , 3.67891108738029]]) # noqa: E203
|
|
1578
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1579
|
+
|
|
1580
|
+
actual = random.noncentral_chisquare(df=.5, nonc=.2, size=(3, 2))
|
|
1581
|
+
desired = np.array([[9.41427665607629e-04, 1.70473157518850e-04],
|
|
1582
|
+
[1.14554372041263e+00, 1.38187755933435e-03],
|
|
1583
|
+
[1.90659181905387e+00, 1.21772577941822e+00]])
|
|
1584
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1585
|
+
|
|
1586
|
+
random = Generator(MT19937(self.seed))
|
|
1587
|
+
actual = random.noncentral_chisquare(df=5, nonc=0, size=(3, 2))
|
|
1588
|
+
desired = np.array([[0.82947954590419, 1.80139670767078],
|
|
1589
|
+
[6.58720057417794, 7.00491463609814],
|
|
1590
|
+
[6.31101879073157, 6.30982307753005]])
|
|
1591
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1592
|
+
|
|
1593
|
+
def test_noncentral_f(self):
|
|
1594
|
+
random = Generator(MT19937(self.seed))
|
|
1595
|
+
actual = random.noncentral_f(dfnum=5, dfden=2, nonc=1,
|
|
1596
|
+
size=(3, 2))
|
|
1597
|
+
desired = np.array([[0.060310671139 , 0.23866058175939], # noqa: E203
|
|
1598
|
+
[0.86860246709073, 0.2668510459738 ], # noqa: E202
|
|
1599
|
+
[0.23375780078364, 1.88922102885943]])
|
|
1600
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1601
|
+
|
|
1602
|
+
def test_noncentral_f_nan(self):
|
|
1603
|
+
random = Generator(MT19937(self.seed))
|
|
1604
|
+
actual = random.noncentral_f(dfnum=5, dfden=2, nonc=np.nan)
|
|
1605
|
+
assert np.isnan(actual)
|
|
1606
|
+
|
|
1607
|
+
def test_normal(self):
|
|
1608
|
+
random = Generator(MT19937(self.seed))
|
|
1609
|
+
actual = random.normal(loc=.123456789, scale=2.0, size=(3, 2))
|
|
1610
|
+
desired = np.array([[-3.618412914693162, 2.635726692647081],
|
|
1611
|
+
[-2.116923463013243, 0.807460983059643],
|
|
1612
|
+
[ 1.446547137248593, 2.485684213886024]])
|
|
1613
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1614
|
+
|
|
1615
|
+
def test_normal_0(self):
|
|
1616
|
+
assert_equal(random.normal(scale=0), 0)
|
|
1617
|
+
assert_raises(ValueError, random.normal, scale=-0.)
|
|
1618
|
+
|
|
1619
|
+
def test_pareto(self):
|
|
1620
|
+
random = Generator(MT19937(self.seed))
|
|
1621
|
+
actual = random.pareto(a=.123456789, size=(3, 2))
|
|
1622
|
+
desired = np.array([[1.0394926776069018e+00, 7.7142534343505773e+04],
|
|
1623
|
+
[7.2640150889064703e-01, 3.4650454783825594e+05],
|
|
1624
|
+
[4.5852344481994740e+04, 6.5851383009539105e+07]])
|
|
1625
|
+
# For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this
|
|
1626
|
+
# matrix differs by 24 nulps. Discussion:
|
|
1627
|
+
# https://mail.python.org/pipermail/numpy-discussion/2012-September/063801.html
|
|
1628
|
+
# Consensus is that this is probably some gcc quirk that affects
|
|
1629
|
+
# rounding but not in any important way, so we just use a looser
|
|
1630
|
+
# tolerance on this test:
|
|
1631
|
+
np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30)
|
|
1632
|
+
|
|
1633
|
+
def test_poisson(self):
|
|
1634
|
+
random = Generator(MT19937(self.seed))
|
|
1635
|
+
actual = random.poisson(lam=.123456789, size=(3, 2))
|
|
1636
|
+
desired = np.array([[0, 0],
|
|
1637
|
+
[0, 0],
|
|
1638
|
+
[0, 0]])
|
|
1639
|
+
assert_array_equal(actual, desired)
|
|
1640
|
+
|
|
1641
|
+
def test_poisson_exceptions(self):
|
|
1642
|
+
lambig = np.iinfo('int64').max
|
|
1643
|
+
lamneg = -1
|
|
1644
|
+
assert_raises(ValueError, random.poisson, lamneg)
|
|
1645
|
+
assert_raises(ValueError, random.poisson, [lamneg] * 10)
|
|
1646
|
+
assert_raises(ValueError, random.poisson, lambig)
|
|
1647
|
+
assert_raises(ValueError, random.poisson, [lambig] * 10)
|
|
1648
|
+
with np.errstate(invalid='ignore'):
|
|
1649
|
+
assert_raises(ValueError, random.poisson, np.nan)
|
|
1650
|
+
assert_raises(ValueError, random.poisson, [np.nan] * 10)
|
|
1651
|
+
|
|
1652
|
+
def test_power(self):
|
|
1653
|
+
random = Generator(MT19937(self.seed))
|
|
1654
|
+
actual = random.power(a=.123456789, size=(3, 2))
|
|
1655
|
+
desired = np.array([[1.977857368842754e-09, 9.806792196620341e-02],
|
|
1656
|
+
[2.482442984543471e-10, 1.527108843266079e-01],
|
|
1657
|
+
[8.188283434244285e-02, 3.950547209346948e-01]])
|
|
1658
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1659
|
+
|
|
1660
|
+
def test_rayleigh(self):
|
|
1661
|
+
random = Generator(MT19937(self.seed))
|
|
1662
|
+
actual = random.rayleigh(scale=10, size=(3, 2))
|
|
1663
|
+
desired = np.array([[4.19494429102666, 16.66920198906598],
|
|
1664
|
+
[3.67184544902662, 17.74695521962917],
|
|
1665
|
+
[16.27935397855501, 21.08355560691792]])
|
|
1666
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1667
|
+
|
|
1668
|
+
def test_rayleigh_0(self):
|
|
1669
|
+
assert_equal(random.rayleigh(scale=0), 0)
|
|
1670
|
+
assert_raises(ValueError, random.rayleigh, scale=-0.)
|
|
1671
|
+
|
|
1672
|
+
def test_standard_cauchy(self):
|
|
1673
|
+
random = Generator(MT19937(self.seed))
|
|
1674
|
+
actual = random.standard_cauchy(size=(3, 2))
|
|
1675
|
+
desired = np.array([[-1.489437778266206, -3.275389641569784],
|
|
1676
|
+
[ 0.560102864910406, -0.680780916282552],
|
|
1677
|
+
[-1.314912905226277, 0.295852965660225]])
|
|
1678
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1679
|
+
|
|
1680
|
+
def test_standard_exponential(self):
|
|
1681
|
+
random = Generator(MT19937(self.seed))
|
|
1682
|
+
actual = random.standard_exponential(size=(3, 2), method='inv')
|
|
1683
|
+
desired = np.array([[0.102031839440643, 1.229350298474972],
|
|
1684
|
+
[0.088137284693098, 1.459859985522667],
|
|
1685
|
+
[1.093830802293668, 1.256977002164613]])
|
|
1686
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1687
|
+
|
|
1688
|
+
def test_standard_expoential_type_error(self):
|
|
1689
|
+
assert_raises(TypeError, random.standard_exponential, dtype=np.int32)
|
|
1690
|
+
|
|
1691
|
+
def test_standard_gamma(self):
|
|
1692
|
+
random = Generator(MT19937(self.seed))
|
|
1693
|
+
actual = random.standard_gamma(shape=3, size=(3, 2))
|
|
1694
|
+
desired = np.array([[0.62970724056362, 1.22379851271008],
|
|
1695
|
+
[3.899412530884 , 4.12479964250139], # noqa: E203
|
|
1696
|
+
[3.74994102464584, 3.74929307690815]])
|
|
1697
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1698
|
+
|
|
1699
|
+
def test_standard_gammma_scalar_float(self):
|
|
1700
|
+
random = Generator(MT19937(self.seed))
|
|
1701
|
+
actual = random.standard_gamma(3, dtype=np.float32)
|
|
1702
|
+
desired = 2.9242148399353027
|
|
1703
|
+
assert_array_almost_equal(actual, desired, decimal=6)
|
|
1704
|
+
|
|
1705
|
+
def test_standard_gamma_float(self):
|
|
1706
|
+
random = Generator(MT19937(self.seed))
|
|
1707
|
+
actual = random.standard_gamma(shape=3, size=(3, 2))
|
|
1708
|
+
desired = np.array([[0.62971, 1.2238],
|
|
1709
|
+
[3.89941, 4.1248],
|
|
1710
|
+
[3.74994, 3.74929]])
|
|
1711
|
+
assert_array_almost_equal(actual, desired, decimal=5)
|
|
1712
|
+
|
|
1713
|
+
def test_standard_gammma_float_out(self):
|
|
1714
|
+
actual = np.zeros((3, 2), dtype=np.float32)
|
|
1715
|
+
random = Generator(MT19937(self.seed))
|
|
1716
|
+
random.standard_gamma(10.0, out=actual, dtype=np.float32)
|
|
1717
|
+
desired = np.array([[10.14987, 7.87012],
|
|
1718
|
+
[ 9.46284, 12.56832],
|
|
1719
|
+
[13.82495, 7.81533]], dtype=np.float32)
|
|
1720
|
+
assert_array_almost_equal(actual, desired, decimal=5)
|
|
1721
|
+
|
|
1722
|
+
random = Generator(MT19937(self.seed))
|
|
1723
|
+
random.standard_gamma(10.0, out=actual, size=(3, 2), dtype=np.float32)
|
|
1724
|
+
assert_array_almost_equal(actual, desired, decimal=5)
|
|
1725
|
+
|
|
1726
|
+
def test_standard_gamma_unknown_type(self):
|
|
1727
|
+
assert_raises(TypeError, random.standard_gamma, 1.,
|
|
1728
|
+
dtype='int32')
|
|
1729
|
+
|
|
1730
|
+
def test_out_size_mismatch(self):
|
|
1731
|
+
out = np.zeros(10)
|
|
1732
|
+
assert_raises(ValueError, random.standard_gamma, 10.0, size=20,
|
|
1733
|
+
out=out)
|
|
1734
|
+
assert_raises(ValueError, random.standard_gamma, 10.0, size=(10, 1),
|
|
1735
|
+
out=out)
|
|
1736
|
+
|
|
1737
|
+
def test_standard_gamma_0(self):
|
|
1738
|
+
assert_equal(random.standard_gamma(shape=0), 0)
|
|
1739
|
+
assert_raises(ValueError, random.standard_gamma, shape=-0.)
|
|
1740
|
+
|
|
1741
|
+
def test_standard_normal(self):
|
|
1742
|
+
random = Generator(MT19937(self.seed))
|
|
1743
|
+
actual = random.standard_normal(size=(3, 2))
|
|
1744
|
+
desired = np.array([[-1.870934851846581, 1.25613495182354 ], # noqa: E202
|
|
1745
|
+
[-1.120190126006621, 0.342002097029821],
|
|
1746
|
+
[ 0.661545174124296, 1.181113712443012]])
|
|
1747
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1748
|
+
|
|
1749
|
+
def test_standard_normal_unsupported_type(self):
|
|
1750
|
+
assert_raises(TypeError, random.standard_normal, dtype=np.int32)
|
|
1751
|
+
|
|
1752
|
+
def test_standard_t(self):
|
|
1753
|
+
random = Generator(MT19937(self.seed))
|
|
1754
|
+
actual = random.standard_t(df=10, size=(3, 2))
|
|
1755
|
+
desired = np.array([[-1.484666193042647, 0.30597891831161],
|
|
1756
|
+
[ 1.056684299648085, -0.407312602088507],
|
|
1757
|
+
[ 0.130704414281157, -2.038053410490321]])
|
|
1758
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1759
|
+
|
|
1760
|
+
def test_triangular(self):
|
|
1761
|
+
random = Generator(MT19937(self.seed))
|
|
1762
|
+
actual = random.triangular(left=5.12, mode=10.23, right=20.34,
|
|
1763
|
+
size=(3, 2))
|
|
1764
|
+
desired = np.array([[ 7.86664070590917, 13.6313848513185 ], # noqa: E202
|
|
1765
|
+
[ 7.68152445215983, 14.36169131136546],
|
|
1766
|
+
[13.16105603911429, 13.72341621856971]])
|
|
1767
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1768
|
+
|
|
1769
|
+
def test_uniform(self):
|
|
1770
|
+
random = Generator(MT19937(self.seed))
|
|
1771
|
+
actual = random.uniform(low=1.23, high=10.54, size=(3, 2))
|
|
1772
|
+
desired = np.array([[2.13306255040998 , 7.816987531021207], # noqa: E203
|
|
1773
|
+
[2.015436610109887, 8.377577533009589],
|
|
1774
|
+
[7.421792588856135, 7.891185744455209]])
|
|
1775
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1776
|
+
|
|
1777
|
+
def test_uniform_range_bounds(self):
|
|
1778
|
+
fmin = np.finfo('float').min
|
|
1779
|
+
fmax = np.finfo('float').max
|
|
1780
|
+
|
|
1781
|
+
func = random.uniform
|
|
1782
|
+
assert_raises(OverflowError, func, -np.inf, 0)
|
|
1783
|
+
assert_raises(OverflowError, func, 0, np.inf)
|
|
1784
|
+
assert_raises(OverflowError, func, fmin, fmax)
|
|
1785
|
+
assert_raises(OverflowError, func, [-np.inf], [0])
|
|
1786
|
+
assert_raises(OverflowError, func, [0], [np.inf])
|
|
1787
|
+
|
|
1788
|
+
# (fmax / 1e17) - fmin is within range, so this should not throw
|
|
1789
|
+
# account for i386 extended precision DBL_MAX / 1e17 + DBL_MAX >
|
|
1790
|
+
# DBL_MAX by increasing fmin a bit
|
|
1791
|
+
random.uniform(low=np.nextafter(fmin, 1), high=fmax / 1e17)
|
|
1792
|
+
|
|
1793
|
+
def test_uniform_zero_range(self):
|
|
1794
|
+
func = random.uniform
|
|
1795
|
+
result = func(1.5, 1.5)
|
|
1796
|
+
assert_allclose(result, 1.5)
|
|
1797
|
+
result = func([0.0, np.pi], [0.0, np.pi])
|
|
1798
|
+
assert_allclose(result, [0.0, np.pi])
|
|
1799
|
+
result = func([[2145.12], [2145.12]], [2145.12, 2145.12])
|
|
1800
|
+
assert_allclose(result, 2145.12 + np.zeros((2, 2)))
|
|
1801
|
+
|
|
1802
|
+
def test_uniform_neg_range(self):
|
|
1803
|
+
func = random.uniform
|
|
1804
|
+
assert_raises(ValueError, func, 2, 1)
|
|
1805
|
+
assert_raises(ValueError, func, [1, 2], [1, 1])
|
|
1806
|
+
assert_raises(ValueError, func, [[0, 1], [2, 3]], 2)
|
|
1807
|
+
|
|
1808
|
+
def test_scalar_exception_propagation(self):
|
|
1809
|
+
# Tests that exceptions are correctly propagated in distributions
|
|
1810
|
+
# when called with objects that throw exceptions when converted to
|
|
1811
|
+
# scalars.
|
|
1812
|
+
#
|
|
1813
|
+
# Regression test for gh: 8865
|
|
1814
|
+
|
|
1815
|
+
class ThrowingFloat(np.ndarray):
|
|
1816
|
+
def __float__(self):
|
|
1817
|
+
raise TypeError
|
|
1818
|
+
|
|
1819
|
+
throwing_float = np.array(1.0).view(ThrowingFloat)
|
|
1820
|
+
assert_raises(TypeError, random.uniform, throwing_float,
|
|
1821
|
+
throwing_float)
|
|
1822
|
+
|
|
1823
|
+
class ThrowingInteger(np.ndarray):
|
|
1824
|
+
def __int__(self):
|
|
1825
|
+
raise TypeError
|
|
1826
|
+
|
|
1827
|
+
throwing_int = np.array(1).view(ThrowingInteger)
|
|
1828
|
+
assert_raises(TypeError, random.hypergeometric, throwing_int, 1, 1)
|
|
1829
|
+
|
|
1830
|
+
def test_vonmises(self):
|
|
1831
|
+
random = Generator(MT19937(self.seed))
|
|
1832
|
+
actual = random.vonmises(mu=1.23, kappa=1.54, size=(3, 2))
|
|
1833
|
+
desired = np.array([[ 1.107972248690106, 2.841536476232361],
|
|
1834
|
+
[ 1.832602376042457, 1.945511926976032],
|
|
1835
|
+
[-0.260147475776542, 2.058047492231698]])
|
|
1836
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1837
|
+
|
|
1838
|
+
def test_vonmises_small(self):
|
|
1839
|
+
# check infinite loop, gh-4720
|
|
1840
|
+
random = Generator(MT19937(self.seed))
|
|
1841
|
+
r = random.vonmises(mu=0., kappa=1.1e-8, size=10**6)
|
|
1842
|
+
assert_(np.isfinite(r).all())
|
|
1843
|
+
|
|
1844
|
+
def test_vonmises_nan(self):
|
|
1845
|
+
random = Generator(MT19937(self.seed))
|
|
1846
|
+
r = random.vonmises(mu=0., kappa=np.nan)
|
|
1847
|
+
assert_(np.isnan(r))
|
|
1848
|
+
|
|
1849
|
+
@pytest.mark.parametrize("kappa", [1e4, 1e15])
|
|
1850
|
+
def test_vonmises_large_kappa(self, kappa):
|
|
1851
|
+
random = Generator(MT19937(self.seed))
|
|
1852
|
+
rs = RandomState(random.bit_generator)
|
|
1853
|
+
state = random.bit_generator.state
|
|
1854
|
+
|
|
1855
|
+
random_state_vals = rs.vonmises(0, kappa, size=10)
|
|
1856
|
+
random.bit_generator.state = state
|
|
1857
|
+
gen_vals = random.vonmises(0, kappa, size=10)
|
|
1858
|
+
if kappa < 1e6:
|
|
1859
|
+
assert_allclose(random_state_vals, gen_vals)
|
|
1860
|
+
else:
|
|
1861
|
+
assert np.all(random_state_vals != gen_vals)
|
|
1862
|
+
|
|
1863
|
+
@pytest.mark.parametrize("mu", [-7., -np.pi, -3.1, np.pi, 3.2])
|
|
1864
|
+
@pytest.mark.parametrize("kappa", [1e-9, 1e-6, 1, 1e3, 1e15])
|
|
1865
|
+
def test_vonmises_large_kappa_range(self, mu, kappa):
|
|
1866
|
+
random = Generator(MT19937(self.seed))
|
|
1867
|
+
r = random.vonmises(mu, kappa, 50)
|
|
1868
|
+
assert_(np.all(r > -np.pi) and np.all(r <= np.pi))
|
|
1869
|
+
|
|
1870
|
+
def test_wald(self):
|
|
1871
|
+
random = Generator(MT19937(self.seed))
|
|
1872
|
+
actual = random.wald(mean=1.23, scale=1.54, size=(3, 2))
|
|
1873
|
+
desired = np.array([[0.26871721804551, 3.2233942732115 ], # noqa: E202
|
|
1874
|
+
[2.20328374987066, 2.40958405189353],
|
|
1875
|
+
[2.07093587449261, 0.73073890064369]])
|
|
1876
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1877
|
+
|
|
1878
|
+
def test_wald_nonnegative(self):
|
|
1879
|
+
random = Generator(MT19937(self.seed))
|
|
1880
|
+
samples = random.wald(mean=1e9, scale=2.25, size=1000)
|
|
1881
|
+
assert_(np.all(samples >= 0.0))
|
|
1882
|
+
|
|
1883
|
+
def test_weibull(self):
|
|
1884
|
+
random = Generator(MT19937(self.seed))
|
|
1885
|
+
actual = random.weibull(a=1.23, size=(3, 2))
|
|
1886
|
+
desired = np.array([[0.138613914769468, 1.306463419753191],
|
|
1887
|
+
[0.111623365934763, 1.446570494646721],
|
|
1888
|
+
[1.257145775276011, 1.914247725027957]])
|
|
1889
|
+
assert_array_almost_equal(actual, desired, decimal=15)
|
|
1890
|
+
|
|
1891
|
+
def test_weibull_0(self):
|
|
1892
|
+
random = Generator(MT19937(self.seed))
|
|
1893
|
+
assert_equal(random.weibull(a=0, size=12), np.zeros(12))
|
|
1894
|
+
assert_raises(ValueError, random.weibull, a=-0.)
|
|
1895
|
+
|
|
1896
|
+
def test_zipf(self):
|
|
1897
|
+
random = Generator(MT19937(self.seed))
|
|
1898
|
+
actual = random.zipf(a=1.23, size=(3, 2))
|
|
1899
|
+
desired = np.array([[ 1, 1],
|
|
1900
|
+
[ 10, 867],
|
|
1901
|
+
[354, 2]])
|
|
1902
|
+
assert_array_equal(actual, desired)
|
|
1903
|
+
|
|
1904
|
+
|
|
1905
|
+
class TestBroadcast:
|
|
1906
|
+
# tests that functions that broadcast behave
|
|
1907
|
+
# correctly when presented with non-scalar arguments
|
|
1908
|
+
seed = 123456789
|
|
1909
|
+
|
|
1910
|
+
def test_uniform(self):
|
|
1911
|
+
random = Generator(MT19937(self.seed))
|
|
1912
|
+
low = [0]
|
|
1913
|
+
high = [1]
|
|
1914
|
+
uniform = random.uniform
|
|
1915
|
+
desired = np.array([0.16693771389729, 0.19635129550675, 0.75563050964095])
|
|
1916
|
+
|
|
1917
|
+
random = Generator(MT19937(self.seed))
|
|
1918
|
+
actual = random.uniform(low * 3, high)
|
|
1919
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1920
|
+
|
|
1921
|
+
random = Generator(MT19937(self.seed))
|
|
1922
|
+
actual = random.uniform(low, high * 3)
|
|
1923
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1924
|
+
|
|
1925
|
+
def test_normal(self):
|
|
1926
|
+
loc = [0]
|
|
1927
|
+
scale = [1]
|
|
1928
|
+
bad_scale = [-1]
|
|
1929
|
+
random = Generator(MT19937(self.seed))
|
|
1930
|
+
desired = np.array([-0.38736406738527, 0.79594375042255, 0.0197076236097])
|
|
1931
|
+
|
|
1932
|
+
random = Generator(MT19937(self.seed))
|
|
1933
|
+
actual = random.normal(loc * 3, scale)
|
|
1934
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1935
|
+
assert_raises(ValueError, random.normal, loc * 3, bad_scale)
|
|
1936
|
+
|
|
1937
|
+
random = Generator(MT19937(self.seed))
|
|
1938
|
+
normal = random.normal
|
|
1939
|
+
actual = normal(loc, scale * 3)
|
|
1940
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1941
|
+
assert_raises(ValueError, normal, loc, bad_scale * 3)
|
|
1942
|
+
|
|
1943
|
+
def test_beta(self):
|
|
1944
|
+
a = [1]
|
|
1945
|
+
b = [2]
|
|
1946
|
+
bad_a = [-1]
|
|
1947
|
+
bad_b = [-2]
|
|
1948
|
+
desired = np.array([0.18719338682602, 0.73234824491364, 0.17928615186455])
|
|
1949
|
+
|
|
1950
|
+
random = Generator(MT19937(self.seed))
|
|
1951
|
+
beta = random.beta
|
|
1952
|
+
actual = beta(a * 3, b)
|
|
1953
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1954
|
+
assert_raises(ValueError, beta, bad_a * 3, b)
|
|
1955
|
+
assert_raises(ValueError, beta, a * 3, bad_b)
|
|
1956
|
+
|
|
1957
|
+
random = Generator(MT19937(self.seed))
|
|
1958
|
+
actual = random.beta(a, b * 3)
|
|
1959
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1960
|
+
|
|
1961
|
+
def test_exponential(self):
|
|
1962
|
+
scale = [1]
|
|
1963
|
+
bad_scale = [-1]
|
|
1964
|
+
desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
|
|
1965
|
+
|
|
1966
|
+
random = Generator(MT19937(self.seed))
|
|
1967
|
+
actual = random.exponential(scale * 3)
|
|
1968
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1969
|
+
assert_raises(ValueError, random.exponential, bad_scale * 3)
|
|
1970
|
+
|
|
1971
|
+
def test_standard_gamma(self):
|
|
1972
|
+
shape = [1]
|
|
1973
|
+
bad_shape = [-1]
|
|
1974
|
+
desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
|
|
1975
|
+
|
|
1976
|
+
random = Generator(MT19937(self.seed))
|
|
1977
|
+
std_gamma = random.standard_gamma
|
|
1978
|
+
actual = std_gamma(shape * 3)
|
|
1979
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1980
|
+
assert_raises(ValueError, std_gamma, bad_shape * 3)
|
|
1981
|
+
|
|
1982
|
+
def test_gamma(self):
|
|
1983
|
+
shape = [1]
|
|
1984
|
+
scale = [2]
|
|
1985
|
+
bad_shape = [-1]
|
|
1986
|
+
bad_scale = [-2]
|
|
1987
|
+
desired = np.array([1.34491986425611, 0.42760990636187, 1.4355697857258])
|
|
1988
|
+
|
|
1989
|
+
random = Generator(MT19937(self.seed))
|
|
1990
|
+
gamma = random.gamma
|
|
1991
|
+
actual = gamma(shape * 3, scale)
|
|
1992
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
1993
|
+
assert_raises(ValueError, gamma, bad_shape * 3, scale)
|
|
1994
|
+
assert_raises(ValueError, gamma, shape * 3, bad_scale)
|
|
1995
|
+
|
|
1996
|
+
random = Generator(MT19937(self.seed))
|
|
1997
|
+
gamma = random.gamma
|
|
1998
|
+
actual = gamma(shape, scale * 3)
|
|
1999
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2000
|
+
assert_raises(ValueError, gamma, bad_shape, scale * 3)
|
|
2001
|
+
assert_raises(ValueError, gamma, shape, bad_scale * 3)
|
|
2002
|
+
|
|
2003
|
+
def test_f(self):
|
|
2004
|
+
dfnum = [1]
|
|
2005
|
+
dfden = [2]
|
|
2006
|
+
bad_dfnum = [-1]
|
|
2007
|
+
bad_dfden = [-2]
|
|
2008
|
+
desired = np.array([0.07765056244107, 7.72951397913186, 0.05786093891763])
|
|
2009
|
+
|
|
2010
|
+
random = Generator(MT19937(self.seed))
|
|
2011
|
+
f = random.f
|
|
2012
|
+
actual = f(dfnum * 3, dfden)
|
|
2013
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2014
|
+
assert_raises(ValueError, f, bad_dfnum * 3, dfden)
|
|
2015
|
+
assert_raises(ValueError, f, dfnum * 3, bad_dfden)
|
|
2016
|
+
|
|
2017
|
+
random = Generator(MT19937(self.seed))
|
|
2018
|
+
f = random.f
|
|
2019
|
+
actual = f(dfnum, dfden * 3)
|
|
2020
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2021
|
+
assert_raises(ValueError, f, bad_dfnum, dfden * 3)
|
|
2022
|
+
assert_raises(ValueError, f, dfnum, bad_dfden * 3)
|
|
2023
|
+
|
|
2024
|
+
def test_noncentral_f(self):
|
|
2025
|
+
dfnum = [2]
|
|
2026
|
+
dfden = [3]
|
|
2027
|
+
nonc = [4]
|
|
2028
|
+
bad_dfnum = [0]
|
|
2029
|
+
bad_dfden = [-1]
|
|
2030
|
+
bad_nonc = [-2]
|
|
2031
|
+
desired = np.array([2.02434240411421, 12.91838601070124, 1.24395160354629])
|
|
2032
|
+
|
|
2033
|
+
random = Generator(MT19937(self.seed))
|
|
2034
|
+
nonc_f = random.noncentral_f
|
|
2035
|
+
actual = nonc_f(dfnum * 3, dfden, nonc)
|
|
2036
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2037
|
+
assert np.all(np.isnan(nonc_f(dfnum, dfden, [np.nan] * 3)))
|
|
2038
|
+
|
|
2039
|
+
assert_raises(ValueError, nonc_f, bad_dfnum * 3, dfden, nonc)
|
|
2040
|
+
assert_raises(ValueError, nonc_f, dfnum * 3, bad_dfden, nonc)
|
|
2041
|
+
assert_raises(ValueError, nonc_f, dfnum * 3, dfden, bad_nonc)
|
|
2042
|
+
|
|
2043
|
+
random = Generator(MT19937(self.seed))
|
|
2044
|
+
nonc_f = random.noncentral_f
|
|
2045
|
+
actual = nonc_f(dfnum, dfden * 3, nonc)
|
|
2046
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2047
|
+
assert_raises(ValueError, nonc_f, bad_dfnum, dfden * 3, nonc)
|
|
2048
|
+
assert_raises(ValueError, nonc_f, dfnum, bad_dfden * 3, nonc)
|
|
2049
|
+
assert_raises(ValueError, nonc_f, dfnum, dfden * 3, bad_nonc)
|
|
2050
|
+
|
|
2051
|
+
random = Generator(MT19937(self.seed))
|
|
2052
|
+
nonc_f = random.noncentral_f
|
|
2053
|
+
actual = nonc_f(dfnum, dfden, nonc * 3)
|
|
2054
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2055
|
+
assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3)
|
|
2056
|
+
assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3)
|
|
2057
|
+
assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3)
|
|
2058
|
+
|
|
2059
|
+
def test_noncentral_f_small_df(self):
|
|
2060
|
+
random = Generator(MT19937(self.seed))
|
|
2061
|
+
desired = np.array([0.04714867120827, 0.1239390327694])
|
|
2062
|
+
actual = random.noncentral_f(0.9, 0.9, 2, size=2)
|
|
2063
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2064
|
+
|
|
2065
|
+
def test_chisquare(self):
|
|
2066
|
+
df = [1]
|
|
2067
|
+
bad_df = [-1]
|
|
2068
|
+
desired = np.array([0.05573640064251, 1.47220224353539, 2.9469379318589])
|
|
2069
|
+
|
|
2070
|
+
random = Generator(MT19937(self.seed))
|
|
2071
|
+
actual = random.chisquare(df * 3)
|
|
2072
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2073
|
+
assert_raises(ValueError, random.chisquare, bad_df * 3)
|
|
2074
|
+
|
|
2075
|
+
def test_noncentral_chisquare(self):
|
|
2076
|
+
df = [1]
|
|
2077
|
+
nonc = [2]
|
|
2078
|
+
bad_df = [-1]
|
|
2079
|
+
bad_nonc = [-2]
|
|
2080
|
+
desired = np.array([0.07710766249436, 5.27829115110304, 0.630732147399])
|
|
2081
|
+
|
|
2082
|
+
random = Generator(MT19937(self.seed))
|
|
2083
|
+
nonc_chi = random.noncentral_chisquare
|
|
2084
|
+
actual = nonc_chi(df * 3, nonc)
|
|
2085
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2086
|
+
assert_raises(ValueError, nonc_chi, bad_df * 3, nonc)
|
|
2087
|
+
assert_raises(ValueError, nonc_chi, df * 3, bad_nonc)
|
|
2088
|
+
|
|
2089
|
+
random = Generator(MT19937(self.seed))
|
|
2090
|
+
nonc_chi = random.noncentral_chisquare
|
|
2091
|
+
actual = nonc_chi(df, nonc * 3)
|
|
2092
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2093
|
+
assert_raises(ValueError, nonc_chi, bad_df, nonc * 3)
|
|
2094
|
+
assert_raises(ValueError, nonc_chi, df, bad_nonc * 3)
|
|
2095
|
+
|
|
2096
|
+
def test_standard_t(self):
|
|
2097
|
+
df = [1]
|
|
2098
|
+
bad_df = [-1]
|
|
2099
|
+
desired = np.array([-1.39498829447098, -1.23058658835223, 0.17207021065983])
|
|
2100
|
+
|
|
2101
|
+
random = Generator(MT19937(self.seed))
|
|
2102
|
+
actual = random.standard_t(df * 3)
|
|
2103
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2104
|
+
assert_raises(ValueError, random.standard_t, bad_df * 3)
|
|
2105
|
+
|
|
2106
|
+
def test_vonmises(self):
|
|
2107
|
+
mu = [2]
|
|
2108
|
+
kappa = [1]
|
|
2109
|
+
bad_kappa = [-1]
|
|
2110
|
+
desired = np.array([2.25935584988528, 2.23326261461399, -2.84152146503326])
|
|
2111
|
+
|
|
2112
|
+
random = Generator(MT19937(self.seed))
|
|
2113
|
+
actual = random.vonmises(mu * 3, kappa)
|
|
2114
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2115
|
+
assert_raises(ValueError, random.vonmises, mu * 3, bad_kappa)
|
|
2116
|
+
|
|
2117
|
+
random = Generator(MT19937(self.seed))
|
|
2118
|
+
actual = random.vonmises(mu, kappa * 3)
|
|
2119
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2120
|
+
assert_raises(ValueError, random.vonmises, mu, bad_kappa * 3)
|
|
2121
|
+
|
|
2122
|
+
def test_pareto(self):
|
|
2123
|
+
a = [1]
|
|
2124
|
+
bad_a = [-1]
|
|
2125
|
+
desired = np.array([0.95905052946317, 0.2383810889437, 1.04988745750013])
|
|
2126
|
+
|
|
2127
|
+
random = Generator(MT19937(self.seed))
|
|
2128
|
+
actual = random.pareto(a * 3)
|
|
2129
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2130
|
+
assert_raises(ValueError, random.pareto, bad_a * 3)
|
|
2131
|
+
|
|
2132
|
+
def test_weibull(self):
|
|
2133
|
+
a = [1]
|
|
2134
|
+
bad_a = [-1]
|
|
2135
|
+
desired = np.array([0.67245993212806, 0.21380495318094, 0.7177848928629])
|
|
2136
|
+
|
|
2137
|
+
random = Generator(MT19937(self.seed))
|
|
2138
|
+
actual = random.weibull(a * 3)
|
|
2139
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2140
|
+
assert_raises(ValueError, random.weibull, bad_a * 3)
|
|
2141
|
+
|
|
2142
|
+
def test_power(self):
|
|
2143
|
+
a = [1]
|
|
2144
|
+
bad_a = [-1]
|
|
2145
|
+
desired = np.array([0.48954864361052, 0.19249412888486, 0.51216834058807])
|
|
2146
|
+
|
|
2147
|
+
random = Generator(MT19937(self.seed))
|
|
2148
|
+
actual = random.power(a * 3)
|
|
2149
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2150
|
+
assert_raises(ValueError, random.power, bad_a * 3)
|
|
2151
|
+
|
|
2152
|
+
def test_laplace(self):
|
|
2153
|
+
loc = [0]
|
|
2154
|
+
scale = [1]
|
|
2155
|
+
bad_scale = [-1]
|
|
2156
|
+
desired = np.array([-1.09698732625119, -0.93470271947368, 0.71592671378202])
|
|
2157
|
+
|
|
2158
|
+
random = Generator(MT19937(self.seed))
|
|
2159
|
+
laplace = random.laplace
|
|
2160
|
+
actual = laplace(loc * 3, scale)
|
|
2161
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2162
|
+
assert_raises(ValueError, laplace, loc * 3, bad_scale)
|
|
2163
|
+
|
|
2164
|
+
random = Generator(MT19937(self.seed))
|
|
2165
|
+
laplace = random.laplace
|
|
2166
|
+
actual = laplace(loc, scale * 3)
|
|
2167
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2168
|
+
assert_raises(ValueError, laplace, loc, bad_scale * 3)
|
|
2169
|
+
|
|
2170
|
+
def test_gumbel(self):
|
|
2171
|
+
loc = [0]
|
|
2172
|
+
scale = [1]
|
|
2173
|
+
bad_scale = [-1]
|
|
2174
|
+
desired = np.array([1.70020068231762, 1.52054354273631, -0.34293267607081])
|
|
2175
|
+
|
|
2176
|
+
random = Generator(MT19937(self.seed))
|
|
2177
|
+
gumbel = random.gumbel
|
|
2178
|
+
actual = gumbel(loc * 3, scale)
|
|
2179
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2180
|
+
assert_raises(ValueError, gumbel, loc * 3, bad_scale)
|
|
2181
|
+
|
|
2182
|
+
random = Generator(MT19937(self.seed))
|
|
2183
|
+
gumbel = random.gumbel
|
|
2184
|
+
actual = gumbel(loc, scale * 3)
|
|
2185
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2186
|
+
assert_raises(ValueError, gumbel, loc, bad_scale * 3)
|
|
2187
|
+
|
|
2188
|
+
def test_logistic(self):
|
|
2189
|
+
loc = [0]
|
|
2190
|
+
scale = [1]
|
|
2191
|
+
bad_scale = [-1]
|
|
2192
|
+
desired = np.array([-1.607487640433, -1.40925686003678, 1.12887112820397])
|
|
2193
|
+
|
|
2194
|
+
random = Generator(MT19937(self.seed))
|
|
2195
|
+
actual = random.logistic(loc * 3, scale)
|
|
2196
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2197
|
+
assert_raises(ValueError, random.logistic, loc * 3, bad_scale)
|
|
2198
|
+
|
|
2199
|
+
random = Generator(MT19937(self.seed))
|
|
2200
|
+
actual = random.logistic(loc, scale * 3)
|
|
2201
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2202
|
+
assert_raises(ValueError, random.logistic, loc, bad_scale * 3)
|
|
2203
|
+
assert_equal(random.logistic(1.0, 0.0), 1.0)
|
|
2204
|
+
|
|
2205
|
+
def test_lognormal(self):
|
|
2206
|
+
mean = [0]
|
|
2207
|
+
sigma = [1]
|
|
2208
|
+
bad_sigma = [-1]
|
|
2209
|
+
desired = np.array([0.67884390500697, 2.21653186290321, 1.01990310084276])
|
|
2210
|
+
|
|
2211
|
+
random = Generator(MT19937(self.seed))
|
|
2212
|
+
lognormal = random.lognormal
|
|
2213
|
+
actual = lognormal(mean * 3, sigma)
|
|
2214
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2215
|
+
assert_raises(ValueError, lognormal, mean * 3, bad_sigma)
|
|
2216
|
+
|
|
2217
|
+
random = Generator(MT19937(self.seed))
|
|
2218
|
+
actual = random.lognormal(mean, sigma * 3)
|
|
2219
|
+
assert_raises(ValueError, random.lognormal, mean, bad_sigma * 3)
|
|
2220
|
+
|
|
2221
|
+
def test_rayleigh(self):
|
|
2222
|
+
scale = [1]
|
|
2223
|
+
bad_scale = [-1]
|
|
2224
|
+
desired = np.array(
|
|
2225
|
+
[1.1597068009872629,
|
|
2226
|
+
0.6539188836253857,
|
|
2227
|
+
1.1981526554349398]
|
|
2228
|
+
)
|
|
2229
|
+
|
|
2230
|
+
random = Generator(MT19937(self.seed))
|
|
2231
|
+
actual = random.rayleigh(scale * 3)
|
|
2232
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2233
|
+
assert_raises(ValueError, random.rayleigh, bad_scale * 3)
|
|
2234
|
+
|
|
2235
|
+
def test_wald(self):
|
|
2236
|
+
mean = [0.5]
|
|
2237
|
+
scale = [1]
|
|
2238
|
+
bad_mean = [0]
|
|
2239
|
+
bad_scale = [-2]
|
|
2240
|
+
desired = np.array([0.38052407392905, 0.50701641508592, 0.484935249864])
|
|
2241
|
+
|
|
2242
|
+
random = Generator(MT19937(self.seed))
|
|
2243
|
+
actual = random.wald(mean * 3, scale)
|
|
2244
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2245
|
+
assert_raises(ValueError, random.wald, bad_mean * 3, scale)
|
|
2246
|
+
assert_raises(ValueError, random.wald, mean * 3, bad_scale)
|
|
2247
|
+
|
|
2248
|
+
random = Generator(MT19937(self.seed))
|
|
2249
|
+
actual = random.wald(mean, scale * 3)
|
|
2250
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2251
|
+
assert_raises(ValueError, random.wald, bad_mean, scale * 3)
|
|
2252
|
+
assert_raises(ValueError, random.wald, mean, bad_scale * 3)
|
|
2253
|
+
|
|
2254
|
+
def test_triangular(self):
|
|
2255
|
+
left = [1]
|
|
2256
|
+
right = [3]
|
|
2257
|
+
mode = [2]
|
|
2258
|
+
bad_left_one = [3]
|
|
2259
|
+
bad_mode_one = [4]
|
|
2260
|
+
bad_left_two, bad_mode_two = right * 2
|
|
2261
|
+
desired = np.array([1.57781954604754, 1.62665986867957, 2.30090130831326])
|
|
2262
|
+
|
|
2263
|
+
random = Generator(MT19937(self.seed))
|
|
2264
|
+
triangular = random.triangular
|
|
2265
|
+
actual = triangular(left * 3, mode, right)
|
|
2266
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2267
|
+
assert_raises(ValueError, triangular, bad_left_one * 3, mode, right)
|
|
2268
|
+
assert_raises(ValueError, triangular, left * 3, bad_mode_one, right)
|
|
2269
|
+
assert_raises(ValueError, triangular, bad_left_two * 3, bad_mode_two,
|
|
2270
|
+
right)
|
|
2271
|
+
|
|
2272
|
+
random = Generator(MT19937(self.seed))
|
|
2273
|
+
triangular = random.triangular
|
|
2274
|
+
actual = triangular(left, mode * 3, right)
|
|
2275
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2276
|
+
assert_raises(ValueError, triangular, bad_left_one, mode * 3, right)
|
|
2277
|
+
assert_raises(ValueError, triangular, left, bad_mode_one * 3, right)
|
|
2278
|
+
assert_raises(ValueError, triangular, bad_left_two, bad_mode_two * 3,
|
|
2279
|
+
right)
|
|
2280
|
+
|
|
2281
|
+
random = Generator(MT19937(self.seed))
|
|
2282
|
+
triangular = random.triangular
|
|
2283
|
+
actual = triangular(left, mode, right * 3)
|
|
2284
|
+
assert_array_almost_equal(actual, desired, decimal=14)
|
|
2285
|
+
assert_raises(ValueError, triangular, bad_left_one, mode, right * 3)
|
|
2286
|
+
assert_raises(ValueError, triangular, left, bad_mode_one, right * 3)
|
|
2287
|
+
assert_raises(ValueError, triangular, bad_left_two, bad_mode_two,
|
|
2288
|
+
right * 3)
|
|
2289
|
+
|
|
2290
|
+
assert_raises(ValueError, triangular, 10., 0., 20.)
|
|
2291
|
+
assert_raises(ValueError, triangular, 10., 25., 20.)
|
|
2292
|
+
assert_raises(ValueError, triangular, 10., 10., 10.)
|
|
2293
|
+
|
|
2294
|
+
def test_binomial(self):
|
|
2295
|
+
n = [1]
|
|
2296
|
+
p = [0.5]
|
|
2297
|
+
bad_n = [-1]
|
|
2298
|
+
bad_p_one = [-1]
|
|
2299
|
+
bad_p_two = [1.5]
|
|
2300
|
+
desired = np.array([0, 0, 1])
|
|
2301
|
+
|
|
2302
|
+
random = Generator(MT19937(self.seed))
|
|
2303
|
+
binom = random.binomial
|
|
2304
|
+
actual = binom(n * 3, p)
|
|
2305
|
+
assert_array_equal(actual, desired)
|
|
2306
|
+
assert_raises(ValueError, binom, bad_n * 3, p)
|
|
2307
|
+
assert_raises(ValueError, binom, n * 3, bad_p_one)
|
|
2308
|
+
assert_raises(ValueError, binom, n * 3, bad_p_two)
|
|
2309
|
+
|
|
2310
|
+
random = Generator(MT19937(self.seed))
|
|
2311
|
+
actual = random.binomial(n, p * 3)
|
|
2312
|
+
assert_array_equal(actual, desired)
|
|
2313
|
+
assert_raises(ValueError, binom, bad_n, p * 3)
|
|
2314
|
+
assert_raises(ValueError, binom, n, bad_p_one * 3)
|
|
2315
|
+
assert_raises(ValueError, binom, n, bad_p_two * 3)
|
|
2316
|
+
|
|
2317
|
+
def test_negative_binomial(self):
|
|
2318
|
+
n = [1]
|
|
2319
|
+
p = [0.5]
|
|
2320
|
+
bad_n = [-1]
|
|
2321
|
+
bad_p_one = [-1]
|
|
2322
|
+
bad_p_two = [1.5]
|
|
2323
|
+
desired = np.array([0, 2, 1], dtype=np.int64)
|
|
2324
|
+
|
|
2325
|
+
random = Generator(MT19937(self.seed))
|
|
2326
|
+
neg_binom = random.negative_binomial
|
|
2327
|
+
actual = neg_binom(n * 3, p)
|
|
2328
|
+
assert_array_equal(actual, desired)
|
|
2329
|
+
assert_raises(ValueError, neg_binom, bad_n * 3, p)
|
|
2330
|
+
assert_raises(ValueError, neg_binom, n * 3, bad_p_one)
|
|
2331
|
+
assert_raises(ValueError, neg_binom, n * 3, bad_p_two)
|
|
2332
|
+
|
|
2333
|
+
random = Generator(MT19937(self.seed))
|
|
2334
|
+
neg_binom = random.negative_binomial
|
|
2335
|
+
actual = neg_binom(n, p * 3)
|
|
2336
|
+
assert_array_equal(actual, desired)
|
|
2337
|
+
assert_raises(ValueError, neg_binom, bad_n, p * 3)
|
|
2338
|
+
assert_raises(ValueError, neg_binom, n, bad_p_one * 3)
|
|
2339
|
+
assert_raises(ValueError, neg_binom, n, bad_p_two * 3)
|
|
2340
|
+
|
|
2341
|
+
def test_poisson(self):
|
|
2342
|
+
|
|
2343
|
+
lam = [1]
|
|
2344
|
+
bad_lam_one = [-1]
|
|
2345
|
+
desired = np.array([0, 0, 3])
|
|
2346
|
+
|
|
2347
|
+
random = Generator(MT19937(self.seed))
|
|
2348
|
+
max_lam = random._poisson_lam_max
|
|
2349
|
+
bad_lam_two = [max_lam * 2]
|
|
2350
|
+
poisson = random.poisson
|
|
2351
|
+
actual = poisson(lam * 3)
|
|
2352
|
+
assert_array_equal(actual, desired)
|
|
2353
|
+
assert_raises(ValueError, poisson, bad_lam_one * 3)
|
|
2354
|
+
assert_raises(ValueError, poisson, bad_lam_two * 3)
|
|
2355
|
+
|
|
2356
|
+
def test_zipf(self):
|
|
2357
|
+
a = [2]
|
|
2358
|
+
bad_a = [0]
|
|
2359
|
+
desired = np.array([1, 8, 1])
|
|
2360
|
+
|
|
2361
|
+
random = Generator(MT19937(self.seed))
|
|
2362
|
+
zipf = random.zipf
|
|
2363
|
+
actual = zipf(a * 3)
|
|
2364
|
+
assert_array_equal(actual, desired)
|
|
2365
|
+
assert_raises(ValueError, zipf, bad_a * 3)
|
|
2366
|
+
with np.errstate(invalid='ignore'):
|
|
2367
|
+
assert_raises(ValueError, zipf, np.nan)
|
|
2368
|
+
assert_raises(ValueError, zipf, [0, 0, np.nan])
|
|
2369
|
+
|
|
2370
|
+
def test_geometric(self):
|
|
2371
|
+
p = [0.5]
|
|
2372
|
+
bad_p_one = [-1]
|
|
2373
|
+
bad_p_two = [1.5]
|
|
2374
|
+
desired = np.array([1, 1, 3])
|
|
2375
|
+
|
|
2376
|
+
random = Generator(MT19937(self.seed))
|
|
2377
|
+
geometric = random.geometric
|
|
2378
|
+
actual = geometric(p * 3)
|
|
2379
|
+
assert_array_equal(actual, desired)
|
|
2380
|
+
assert_raises(ValueError, geometric, bad_p_one * 3)
|
|
2381
|
+
assert_raises(ValueError, geometric, bad_p_two * 3)
|
|
2382
|
+
|
|
2383
|
+
def test_hypergeometric(self):
|
|
2384
|
+
ngood = [1]
|
|
2385
|
+
nbad = [2]
|
|
2386
|
+
nsample = [2]
|
|
2387
|
+
bad_ngood = [-1]
|
|
2388
|
+
bad_nbad = [-2]
|
|
2389
|
+
bad_nsample_one = [-1]
|
|
2390
|
+
bad_nsample_two = [4]
|
|
2391
|
+
desired = np.array([0, 0, 1])
|
|
2392
|
+
|
|
2393
|
+
random = Generator(MT19937(self.seed))
|
|
2394
|
+
actual = random.hypergeometric(ngood * 3, nbad, nsample)
|
|
2395
|
+
assert_array_equal(actual, desired)
|
|
2396
|
+
assert_raises(ValueError, random.hypergeometric, bad_ngood * 3, nbad, nsample)
|
|
2397
|
+
assert_raises(ValueError, random.hypergeometric, ngood * 3, bad_nbad, nsample)
|
|
2398
|
+
assert_raises(ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_one) # noqa: E501
|
|
2399
|
+
assert_raises(ValueError, random.hypergeometric, ngood * 3, nbad, bad_nsample_two) # noqa: E501
|
|
2400
|
+
|
|
2401
|
+
random = Generator(MT19937(self.seed))
|
|
2402
|
+
actual = random.hypergeometric(ngood, nbad * 3, nsample)
|
|
2403
|
+
assert_array_equal(actual, desired)
|
|
2404
|
+
assert_raises(ValueError, random.hypergeometric, bad_ngood, nbad * 3, nsample)
|
|
2405
|
+
assert_raises(ValueError, random.hypergeometric, ngood, bad_nbad * 3, nsample)
|
|
2406
|
+
assert_raises(ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_one) # noqa: E501
|
|
2407
|
+
assert_raises(ValueError, random.hypergeometric, ngood, nbad * 3, bad_nsample_two) # noqa: E501
|
|
2408
|
+
|
|
2409
|
+
random = Generator(MT19937(self.seed))
|
|
2410
|
+
hypergeom = random.hypergeometric
|
|
2411
|
+
actual = hypergeom(ngood, nbad, nsample * 3)
|
|
2412
|
+
assert_array_equal(actual, desired)
|
|
2413
|
+
assert_raises(ValueError, hypergeom, bad_ngood, nbad, nsample * 3)
|
|
2414
|
+
assert_raises(ValueError, hypergeom, ngood, bad_nbad, nsample * 3)
|
|
2415
|
+
assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_one * 3)
|
|
2416
|
+
assert_raises(ValueError, hypergeom, ngood, nbad, bad_nsample_two * 3)
|
|
2417
|
+
|
|
2418
|
+
assert_raises(ValueError, hypergeom, -1, 10, 20)
|
|
2419
|
+
assert_raises(ValueError, hypergeom, 10, -1, 20)
|
|
2420
|
+
assert_raises(ValueError, hypergeom, 10, 10, -1)
|
|
2421
|
+
assert_raises(ValueError, hypergeom, 10, 10, 25)
|
|
2422
|
+
|
|
2423
|
+
# ValueError for arguments that are too big.
|
|
2424
|
+
assert_raises(ValueError, hypergeom, 2**30, 10, 20)
|
|
2425
|
+
assert_raises(ValueError, hypergeom, 999, 2**31, 50)
|
|
2426
|
+
assert_raises(ValueError, hypergeom, 999, [2**29, 2**30], 1000)
|
|
2427
|
+
|
|
2428
|
+
def test_logseries(self):
|
|
2429
|
+
p = [0.5]
|
|
2430
|
+
bad_p_one = [2]
|
|
2431
|
+
bad_p_two = [-1]
|
|
2432
|
+
desired = np.array([1, 1, 1])
|
|
2433
|
+
|
|
2434
|
+
random = Generator(MT19937(self.seed))
|
|
2435
|
+
logseries = random.logseries
|
|
2436
|
+
actual = logseries(p * 3)
|
|
2437
|
+
assert_array_equal(actual, desired)
|
|
2438
|
+
assert_raises(ValueError, logseries, bad_p_one * 3)
|
|
2439
|
+
assert_raises(ValueError, logseries, bad_p_two * 3)
|
|
2440
|
+
|
|
2441
|
+
def test_multinomial(self):
|
|
2442
|
+
random = Generator(MT19937(self.seed))
|
|
2443
|
+
actual = random.multinomial([5, 20], [1 / 6.] * 6, size=(3, 2))
|
|
2444
|
+
desired = np.array([[[0, 0, 2, 1, 2, 0],
|
|
2445
|
+
[2, 3, 6, 4, 2, 3]],
|
|
2446
|
+
[[1, 0, 1, 0, 2, 1],
|
|
2447
|
+
[7, 2, 2, 1, 4, 4]],
|
|
2448
|
+
[[0, 2, 0, 1, 2, 0],
|
|
2449
|
+
[3, 2, 3, 3, 4, 5]]], dtype=np.int64)
|
|
2450
|
+
assert_array_equal(actual, desired)
|
|
2451
|
+
|
|
2452
|
+
random = Generator(MT19937(self.seed))
|
|
2453
|
+
actual = random.multinomial([5, 20], [1 / 6.] * 6)
|
|
2454
|
+
desired = np.array([[0, 0, 2, 1, 2, 0],
|
|
2455
|
+
[2, 3, 6, 4, 2, 3]], dtype=np.int64)
|
|
2456
|
+
assert_array_equal(actual, desired)
|
|
2457
|
+
|
|
2458
|
+
random = Generator(MT19937(self.seed))
|
|
2459
|
+
actual = random.multinomial([5, 20], [[1 / 6.] * 6] * 2)
|
|
2460
|
+
desired = np.array([[0, 0, 2, 1, 2, 0],
|
|
2461
|
+
[2, 3, 6, 4, 2, 3]], dtype=np.int64)
|
|
2462
|
+
assert_array_equal(actual, desired)
|
|
2463
|
+
|
|
2464
|
+
random = Generator(MT19937(self.seed))
|
|
2465
|
+
actual = random.multinomial([[5], [20]], [[1 / 6.] * 6] * 2)
|
|
2466
|
+
desired = np.array([[[0, 0, 2, 1, 2, 0],
|
|
2467
|
+
[0, 0, 2, 1, 1, 1]],
|
|
2468
|
+
[[4, 2, 3, 3, 5, 3],
|
|
2469
|
+
[7, 2, 2, 1, 4, 4]]], dtype=np.int64)
|
|
2470
|
+
assert_array_equal(actual, desired)
|
|
2471
|
+
|
|
2472
|
+
@pytest.mark.parametrize("n", [10,
|
|
2473
|
+
np.array([10, 10]),
|
|
2474
|
+
np.array([[[10]], [[10]]])
|
|
2475
|
+
]
|
|
2476
|
+
)
|
|
2477
|
+
def test_multinomial_pval_broadcast(self, n):
|
|
2478
|
+
random = Generator(MT19937(self.seed))
|
|
2479
|
+
pvals = np.array([1 / 4] * 4)
|
|
2480
|
+
actual = random.multinomial(n, pvals)
|
|
2481
|
+
n_shape = () if isinstance(n, int) else n.shape
|
|
2482
|
+
expected_shape = n_shape + (4,)
|
|
2483
|
+
assert actual.shape == expected_shape
|
|
2484
|
+
pvals = np.vstack([pvals, pvals])
|
|
2485
|
+
actual = random.multinomial(n, pvals)
|
|
2486
|
+
expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1]) + (4,)
|
|
2487
|
+
assert actual.shape == expected_shape
|
|
2488
|
+
|
|
2489
|
+
pvals = np.vstack([[pvals], [pvals]])
|
|
2490
|
+
actual = random.multinomial(n, pvals)
|
|
2491
|
+
expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1])
|
|
2492
|
+
assert actual.shape == expected_shape + (4,)
|
|
2493
|
+
actual = random.multinomial(n, pvals, size=(3, 2) + expected_shape)
|
|
2494
|
+
assert actual.shape == (3, 2) + expected_shape + (4,)
|
|
2495
|
+
|
|
2496
|
+
with pytest.raises(ValueError):
|
|
2497
|
+
# Ensure that size is not broadcast
|
|
2498
|
+
actual = random.multinomial(n, pvals, size=(1,) * 6)
|
|
2499
|
+
|
|
2500
|
+
def test_invalid_pvals_broadcast(self):
|
|
2501
|
+
random = Generator(MT19937(self.seed))
|
|
2502
|
+
pvals = [[1 / 6] * 6, [1 / 4] * 6]
|
|
2503
|
+
assert_raises(ValueError, random.multinomial, 1, pvals)
|
|
2504
|
+
assert_raises(ValueError, random.multinomial, 6, 0.5)
|
|
2505
|
+
|
|
2506
|
+
def test_empty_outputs(self):
|
|
2507
|
+
random = Generator(MT19937(self.seed))
|
|
2508
|
+
actual = random.multinomial(np.empty((10, 0, 6), "i8"), [1 / 6] * 6)
|
|
2509
|
+
assert actual.shape == (10, 0, 6, 6)
|
|
2510
|
+
actual = random.multinomial(12, np.empty((10, 0, 10)))
|
|
2511
|
+
assert actual.shape == (10, 0, 10)
|
|
2512
|
+
actual = random.multinomial(np.empty((3, 0, 7), "i8"),
|
|
2513
|
+
np.empty((3, 0, 7, 4)))
|
|
2514
|
+
assert actual.shape == (3, 0, 7, 4)
|
|
2515
|
+
|
|
2516
|
+
|
|
2517
|
+
@pytest.mark.skipif(IS_WASM, reason="can't start thread")
|
|
2518
|
+
class TestThread:
|
|
2519
|
+
# make sure each state produces the same sequence even in threads
|
|
2520
|
+
seeds = range(4)
|
|
2521
|
+
|
|
2522
|
+
def check_function(self, function, sz):
|
|
2523
|
+
from threading import Thread
|
|
2524
|
+
|
|
2525
|
+
out1 = np.empty((len(self.seeds),) + sz)
|
|
2526
|
+
out2 = np.empty((len(self.seeds),) + sz)
|
|
2527
|
+
|
|
2528
|
+
# threaded generation
|
|
2529
|
+
t = [Thread(target=function, args=(Generator(MT19937(s)), o))
|
|
2530
|
+
for s, o in zip(self.seeds, out1)]
|
|
2531
|
+
[x.start() for x in t]
|
|
2532
|
+
[x.join() for x in t]
|
|
2533
|
+
|
|
2534
|
+
# the same serial
|
|
2535
|
+
for s, o in zip(self.seeds, out2):
|
|
2536
|
+
function(Generator(MT19937(s)), o)
|
|
2537
|
+
|
|
2538
|
+
# these platforms change x87 fpu precision mode in threads
|
|
2539
|
+
if np.intp().dtype.itemsize == 4 and sys.platform == "win32":
|
|
2540
|
+
assert_array_almost_equal(out1, out2)
|
|
2541
|
+
else:
|
|
2542
|
+
assert_array_equal(out1, out2)
|
|
2543
|
+
|
|
2544
|
+
def test_normal(self):
|
|
2545
|
+
def gen_random(state, out):
|
|
2546
|
+
out[...] = state.normal(size=10000)
|
|
2547
|
+
|
|
2548
|
+
self.check_function(gen_random, sz=(10000,))
|
|
2549
|
+
|
|
2550
|
+
def test_exp(self):
|
|
2551
|
+
def gen_random(state, out):
|
|
2552
|
+
out[...] = state.exponential(scale=np.ones((100, 1000)))
|
|
2553
|
+
|
|
2554
|
+
self.check_function(gen_random, sz=(100, 1000))
|
|
2555
|
+
|
|
2556
|
+
def test_multinomial(self):
|
|
2557
|
+
def gen_random(state, out):
|
|
2558
|
+
out[...] = state.multinomial(10, [1 / 6.] * 6, size=10000)
|
|
2559
|
+
|
|
2560
|
+
self.check_function(gen_random, sz=(10000, 6))
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
# See Issue #4263
|
|
2564
|
+
class TestSingleEltArrayInput:
|
|
2565
|
+
def _create_arrays(self):
|
|
2566
|
+
return np.array([2]), np.array([3]), np.array([4]), (1,)
|
|
2567
|
+
|
|
2568
|
+
def test_one_arg_funcs(self):
|
|
2569
|
+
argOne, _, _, tgtShape = self._create_arrays()
|
|
2570
|
+
funcs = (random.exponential, random.standard_gamma,
|
|
2571
|
+
random.chisquare, random.standard_t,
|
|
2572
|
+
random.pareto, random.weibull,
|
|
2573
|
+
random.power, random.rayleigh,
|
|
2574
|
+
random.poisson, random.zipf,
|
|
2575
|
+
random.geometric, random.logseries)
|
|
2576
|
+
|
|
2577
|
+
probfuncs = (random.geometric, random.logseries)
|
|
2578
|
+
|
|
2579
|
+
for func in funcs:
|
|
2580
|
+
if func in probfuncs: # p < 1.0
|
|
2581
|
+
out = func(np.array([0.5]))
|
|
2582
|
+
|
|
2583
|
+
else:
|
|
2584
|
+
out = func(argOne)
|
|
2585
|
+
|
|
2586
|
+
assert_equal(out.shape, tgtShape)
|
|
2587
|
+
|
|
2588
|
+
def test_two_arg_funcs(self):
|
|
2589
|
+
argOne, argTwo, _, tgtShape = self._create_arrays()
|
|
2590
|
+
funcs = (random.uniform, random.normal,
|
|
2591
|
+
random.beta, random.gamma,
|
|
2592
|
+
random.f, random.noncentral_chisquare,
|
|
2593
|
+
random.vonmises, random.laplace,
|
|
2594
|
+
random.gumbel, random.logistic,
|
|
2595
|
+
random.lognormal, random.wald,
|
|
2596
|
+
random.binomial, random.negative_binomial)
|
|
2597
|
+
|
|
2598
|
+
probfuncs = (random.binomial, random.negative_binomial)
|
|
2599
|
+
|
|
2600
|
+
for func in funcs:
|
|
2601
|
+
if func in probfuncs: # p <= 1
|
|
2602
|
+
argTwo = np.array([0.5])
|
|
2603
|
+
|
|
2604
|
+
else:
|
|
2605
|
+
argTwo = argTwo
|
|
2606
|
+
|
|
2607
|
+
out = func(argOne, argTwo)
|
|
2608
|
+
assert_equal(out.shape, tgtShape)
|
|
2609
|
+
|
|
2610
|
+
out = func(argOne[0], argTwo)
|
|
2611
|
+
assert_equal(out.shape, tgtShape)
|
|
2612
|
+
|
|
2613
|
+
out = func(argOne, argTwo[0])
|
|
2614
|
+
assert_equal(out.shape, tgtShape)
|
|
2615
|
+
|
|
2616
|
+
def test_integers(self, endpoint):
|
|
2617
|
+
_, _, _, tgtShape = self._create_arrays()
|
|
2618
|
+
itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
|
|
2619
|
+
np.int32, np.uint32, np.int64, np.uint64]
|
|
2620
|
+
func = random.integers
|
|
2621
|
+
high = np.array([1])
|
|
2622
|
+
low = np.array([0])
|
|
2623
|
+
|
|
2624
|
+
for dt in itype:
|
|
2625
|
+
out = func(low, high, endpoint=endpoint, dtype=dt)
|
|
2626
|
+
assert_equal(out.shape, tgtShape)
|
|
2627
|
+
|
|
2628
|
+
out = func(low[0], high, endpoint=endpoint, dtype=dt)
|
|
2629
|
+
assert_equal(out.shape, tgtShape)
|
|
2630
|
+
|
|
2631
|
+
out = func(low, high[0], endpoint=endpoint, dtype=dt)
|
|
2632
|
+
assert_equal(out.shape, tgtShape)
|
|
2633
|
+
|
|
2634
|
+
def test_three_arg_funcs(self):
|
|
2635
|
+
argOne, argTwo, argThree, tgtShape = self._create_arrays()
|
|
2636
|
+
funcs = [random.noncentral_f, random.triangular,
|
|
2637
|
+
random.hypergeometric]
|
|
2638
|
+
|
|
2639
|
+
for func in funcs:
|
|
2640
|
+
out = func(argOne, argTwo, argThree)
|
|
2641
|
+
assert_equal(out.shape, tgtShape)
|
|
2642
|
+
|
|
2643
|
+
out = func(argOne[0], argTwo, argThree)
|
|
2644
|
+
assert_equal(out.shape, tgtShape)
|
|
2645
|
+
|
|
2646
|
+
out = func(argOne, argTwo[0], argThree)
|
|
2647
|
+
assert_equal(out.shape, tgtShape)
|
|
2648
|
+
|
|
2649
|
+
|
|
2650
|
+
@pytest.mark.parametrize("config", JUMP_TEST_DATA)
|
|
2651
|
+
def test_jumped(config):
|
|
2652
|
+
# Each config contains the initial seed, a number of raw steps
|
|
2653
|
+
# the sha256 hashes of the initial and the final states' keys and
|
|
2654
|
+
# the position of the initial and the final state.
|
|
2655
|
+
# These were produced using the original C implementation.
|
|
2656
|
+
seed = config["seed"]
|
|
2657
|
+
steps = config["steps"]
|
|
2658
|
+
|
|
2659
|
+
mt19937 = MT19937(seed)
|
|
2660
|
+
# Burn step
|
|
2661
|
+
mt19937.random_raw(steps)
|
|
2662
|
+
key = mt19937.state["state"]["key"]
|
|
2663
|
+
if sys.byteorder == 'big':
|
|
2664
|
+
key = key.byteswap()
|
|
2665
|
+
sha256 = hashlib.sha256(key)
|
|
2666
|
+
assert mt19937.state["state"]["pos"] == config["initial"]["pos"]
|
|
2667
|
+
assert sha256.hexdigest() == config["initial"]["key_sha256"]
|
|
2668
|
+
|
|
2669
|
+
jumped = mt19937.jumped()
|
|
2670
|
+
key = jumped.state["state"]["key"]
|
|
2671
|
+
if sys.byteorder == 'big':
|
|
2672
|
+
key = key.byteswap()
|
|
2673
|
+
sha256 = hashlib.sha256(key)
|
|
2674
|
+
assert jumped.state["state"]["pos"] == config["jumped"]["pos"]
|
|
2675
|
+
assert sha256.hexdigest() == config["jumped"]["key_sha256"]
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
def test_broadcast_size_error():
|
|
2679
|
+
mu = np.ones(3)
|
|
2680
|
+
sigma = np.ones((4, 3))
|
|
2681
|
+
size = (10, 4, 2)
|
|
2682
|
+
assert random.normal(mu, sigma, size=(5, 4, 3)).shape == (5, 4, 3)
|
|
2683
|
+
with pytest.raises(ValueError):
|
|
2684
|
+
random.normal(mu, sigma, size=size)
|
|
2685
|
+
with pytest.raises(ValueError):
|
|
2686
|
+
random.normal(mu, sigma, size=(1, 3))
|
|
2687
|
+
with pytest.raises(ValueError):
|
|
2688
|
+
random.normal(mu, sigma, size=(4, 1, 1))
|
|
2689
|
+
# 1 arg
|
|
2690
|
+
shape = np.ones((4, 3))
|
|
2691
|
+
with pytest.raises(ValueError):
|
|
2692
|
+
random.standard_gamma(shape, size=size)
|
|
2693
|
+
with pytest.raises(ValueError):
|
|
2694
|
+
random.standard_gamma(shape, size=(3,))
|
|
2695
|
+
with pytest.raises(ValueError):
|
|
2696
|
+
random.standard_gamma(shape, size=3)
|
|
2697
|
+
# Check out
|
|
2698
|
+
out = np.empty(size)
|
|
2699
|
+
with pytest.raises(ValueError):
|
|
2700
|
+
random.standard_gamma(shape, out=out)
|
|
2701
|
+
|
|
2702
|
+
# 2 arg
|
|
2703
|
+
with pytest.raises(ValueError):
|
|
2704
|
+
random.binomial(1, [0.3, 0.7], size=(2, 1))
|
|
2705
|
+
with pytest.raises(ValueError):
|
|
2706
|
+
random.binomial([1, 2], 0.3, size=(2, 1))
|
|
2707
|
+
with pytest.raises(ValueError):
|
|
2708
|
+
random.binomial([1, 2], [0.3, 0.7], size=(2, 1))
|
|
2709
|
+
with pytest.raises(ValueError):
|
|
2710
|
+
random.multinomial([2, 2], [.3, .7], size=(2, 1))
|
|
2711
|
+
|
|
2712
|
+
# 3 arg
|
|
2713
|
+
a = random.chisquare(5, size=3)
|
|
2714
|
+
b = random.chisquare(5, size=(4, 3))
|
|
2715
|
+
c = random.chisquare(5, size=(5, 4, 3))
|
|
2716
|
+
assert random.noncentral_f(a, b, c).shape == (5, 4, 3)
|
|
2717
|
+
with pytest.raises(ValueError, match=r"Output size \(6, 5, 1, 1\) is"):
|
|
2718
|
+
random.noncentral_f(a, b, c, size=(6, 5, 1, 1))
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
def test_broadcast_size_scalar():
|
|
2722
|
+
mu = np.ones(3)
|
|
2723
|
+
sigma = np.ones(3)
|
|
2724
|
+
random.normal(mu, sigma, size=3)
|
|
2725
|
+
with pytest.raises(ValueError):
|
|
2726
|
+
random.normal(mu, sigma, size=2)
|
|
2727
|
+
|
|
2728
|
+
|
|
2729
|
+
def test_ragged_shuffle():
|
|
2730
|
+
# GH 18142
|
|
2731
|
+
seq = [[], [], 1]
|
|
2732
|
+
gen = Generator(MT19937(0))
|
|
2733
|
+
assert_no_warnings(gen.shuffle, seq)
|
|
2734
|
+
assert seq == [1, [], []]
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
@pytest.mark.parametrize("high", [-2, [-2]])
|
|
2738
|
+
@pytest.mark.parametrize("endpoint", [True, False])
|
|
2739
|
+
def test_single_arg_integer_exception(high, endpoint):
|
|
2740
|
+
# GH 14333
|
|
2741
|
+
gen = Generator(MT19937(0))
|
|
2742
|
+
msg = 'high < 0' if endpoint else 'high <= 0'
|
|
2743
|
+
with pytest.raises(ValueError, match=msg):
|
|
2744
|
+
gen.integers(high, endpoint=endpoint)
|
|
2745
|
+
msg = 'low > high' if endpoint else 'low >= high'
|
|
2746
|
+
with pytest.raises(ValueError, match=msg):
|
|
2747
|
+
gen.integers(-1, high, endpoint=endpoint)
|
|
2748
|
+
with pytest.raises(ValueError, match=msg):
|
|
2749
|
+
gen.integers([-1], high, endpoint=endpoint)
|
|
2750
|
+
|
|
2751
|
+
|
|
2752
|
+
@pytest.mark.parametrize("dtype", ["f4", "f8"])
|
|
2753
|
+
def test_c_contig_req_out(dtype):
|
|
2754
|
+
# GH 18704
|
|
2755
|
+
out = np.empty((2, 3), order="F", dtype=dtype)
|
|
2756
|
+
shape = [1, 2, 3]
|
|
2757
|
+
with pytest.raises(ValueError, match="Supplied output array"):
|
|
2758
|
+
random.standard_gamma(shape, out=out, dtype=dtype)
|
|
2759
|
+
with pytest.raises(ValueError, match="Supplied output array"):
|
|
2760
|
+
random.standard_gamma(shape, out=out, size=out.shape, dtype=dtype)
|
|
2761
|
+
|
|
2762
|
+
|
|
2763
|
+
@pytest.mark.parametrize("dtype", ["f4", "f8"])
|
|
2764
|
+
@pytest.mark.parametrize("order", ["F", "C"])
|
|
2765
|
+
@pytest.mark.parametrize("dist", [random.standard_normal, random.random])
|
|
2766
|
+
def test_contig_req_out(dist, order, dtype):
|
|
2767
|
+
# GH 18704
|
|
2768
|
+
out = np.empty((2, 3), dtype=dtype, order=order)
|
|
2769
|
+
variates = dist(out=out, dtype=dtype)
|
|
2770
|
+
assert variates is out
|
|
2771
|
+
variates = dist(out=out, dtype=dtype, size=out.shape)
|
|
2772
|
+
assert variates is out
|
|
2773
|
+
|
|
2774
|
+
|
|
2775
|
+
def test_generator_ctor_old_style_pickle():
|
|
2776
|
+
rg = np.random.Generator(np.random.PCG64DXSM(0))
|
|
2777
|
+
rg.standard_normal(1)
|
|
2778
|
+
# Directly call reduce which is used in pickling
|
|
2779
|
+
ctor, (bit_gen, ), _ = rg.__reduce__()
|
|
2780
|
+
# Simulate unpickling an old pickle that only has the name
|
|
2781
|
+
assert bit_gen.__class__.__name__ == "PCG64DXSM"
|
|
2782
|
+
print(ctor)
|
|
2783
|
+
b = ctor(*("PCG64DXSM",))
|
|
2784
|
+
print(b)
|
|
2785
|
+
b.bit_generator.state = bit_gen.state
|
|
2786
|
+
state_b = b.bit_generator.state
|
|
2787
|
+
assert bit_gen.state == state_b
|
|
2788
|
+
|
|
2789
|
+
|
|
2790
|
+
def test_pickle_preserves_seed_sequence():
|
|
2791
|
+
# GH 26234
|
|
2792
|
+
# Add explicit test that bit generators preserve seed sequences
|
|
2793
|
+
import pickle
|
|
2794
|
+
|
|
2795
|
+
rg = np.random.Generator(np.random.PCG64DXSM(20240411))
|
|
2796
|
+
ss = rg.bit_generator.seed_seq
|
|
2797
|
+
rg_plk = pickle.loads(pickle.dumps(rg))
|
|
2798
|
+
ss_plk = rg_plk.bit_generator.seed_seq
|
|
2799
|
+
assert_equal(ss.state, ss_plk.state)
|
|
2800
|
+
assert_equal(ss.pool, ss_plk.pool)
|
|
2801
|
+
|
|
2802
|
+
rg.bit_generator.seed_seq.spawn(10)
|
|
2803
|
+
rg_plk = pickle.loads(pickle.dumps(rg))
|
|
2804
|
+
ss_plk = rg_plk.bit_generator.seed_seq
|
|
2805
|
+
assert_equal(ss.state, ss_plk.state)
|
|
2806
|
+
|
|
2807
|
+
|
|
2808
|
+
@pytest.mark.parametrize("version", [121, 126])
|
|
2809
|
+
def test_legacy_pickle(version):
|
|
2810
|
+
# Pickling format was changes in 1.22.x and in 2.0.x
|
|
2811
|
+
import gzip
|
|
2812
|
+
import pickle
|
|
2813
|
+
|
|
2814
|
+
base_path = os.path.split(os.path.abspath(__file__))[0]
|
|
2815
|
+
pkl_file = os.path.join(
|
|
2816
|
+
base_path, "data", f"generator_pcg64_np{version}.pkl.gz"
|
|
2817
|
+
)
|
|
2818
|
+
with gzip.open(pkl_file) as gz:
|
|
2819
|
+
rg = pickle.load(gz)
|
|
2820
|
+
state = rg.bit_generator.state['state']
|
|
2821
|
+
|
|
2822
|
+
assert isinstance(rg, Generator)
|
|
2823
|
+
assert isinstance(rg.bit_generator, np.random.PCG64)
|
|
2824
|
+
assert state['state'] == 35399562948360463058890781895381311971
|
|
2825
|
+
assert state['inc'] == 87136372517582989555478159403783844777
|