From cf3e99ae1a7dcd32bb3b3669e236e13375ce25a7 Mon Sep 17 00:00:00 2001 From: Abhishek Bagusetty Date: Thu, 3 Dec 2026 12:35:32 -0600 Subject: [PATCH] Add support for ndtri --- CHANGELOG.md | 1 + doc/reference/scipy_special.rst | 9 +++ .../kernels/elementwise_functions/erf.hpp | 6 ++ .../elementwise_functions/erf_funcs.cpp | 5 ++ dpnp/scipy/special/__init__.py | 2 + dpnp/scipy/special/_erf.py | 64 +++++++++++++++++++ dpnp/tests/test_special.py | 60 ++++++++++++++++- dpnp/tests/test_strides.py | 1 + dpnp/tests/test_sycl_queue.py | 1 + dpnp/tests/test_usm_type.py | 1 + 10 files changed, 149 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3957a444ffcf..822356cc2956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi * Added the `dpnp.random.Generator` API with `default_rng`, `BitGenerator`, `SeedSequence` and the `Philox4x3210` bit generator, sampling on the device with the device RNG API of oneMKL or oneMath [#3157](https://github.com/IntelPython/dpnp/pull/3157) * Added `dpnp.argpartition` and `dpnp.ndarray.argpartition` to return the indices that would partition an array [#3138](https://github.com/IntelPython/dpnp/pull/3138) * Added implementation of `dpnp.scipy.linalg.solve_triangular` (SciPy-compatible) [#3200](https://github.com/IntelPython/dpnp/pull/3200) +* Added implementation of `dpnp.scipy.special.ndtri` [#3219](https://github.com/IntelPython/dpnp/pull/3219) ### Changed diff --git a/doc/reference/scipy_special.rst b/doc/reference/scipy_special.rst index ec501b82625b..a4652183ffa3 100644 --- a/doc/reference/scipy_special.rst +++ b/doc/reference/scipy_special.rst @@ -17,3 +17,12 @@ Error function and Fresnel integrals erfcx erfinv erfcinv + +Statistical functions +--------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + + ndtri diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/erf.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/erf.hpp index d73ac7d30b72..eca8d30b07fd 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/erf.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/erf.hpp @@ -97,4 +97,10 @@ MACRO_DEFINE_FUNCTOR(sycl::erfc, Erfc); MACRO_DEFINE_FUNCTOR(erfcx, Erfcx); MACRO_DEFINE_FUNCTOR(erfinv, Erfinv); MACRO_DEFINE_FUNCTOR(erfcinv, Erfcinv); +#if defined(__SYCL_EXT_INTEL_MATH_SUPPORT) +// the Intel math extension names the inverse of the normal CDF normcdfinv +MACRO_DEFINE_FUNCTOR(normcdfinv, Ndtri); +#else +MACRO_DEFINE_FUNCTOR(ndtri, Ndtri); +#endif } // namespace dpnp::tensor::kernels::erfs diff --git a/dpnp/libtensor/source/elementwise_functions/erf_funcs.cpp b/dpnp/libtensor/source/elementwise_functions/erf_funcs.cpp index 513033dbb632..6e0b4aaa0a3c 100644 --- a/dpnp/libtensor/source/elementwise_functions/erf_funcs.cpp +++ b/dpnp/libtensor/source/elementwise_functions/erf_funcs.cpp @@ -213,6 +213,7 @@ MACRO_DEFINE_IMPL(erfc, Erfc); MACRO_DEFINE_IMPL(erfcx, Erfcx); MACRO_DEFINE_IMPL(erfinv, Erfinv); MACRO_DEFINE_IMPL(erfcinv, Erfcinv); +MACRO_DEFINE_IMPL(ndtri, Ndtri); } // namespace impl void init_erf_funcs(py::module_ m) @@ -244,5 +245,9 @@ void init_erf_funcs(py::module_ m) impl::populate( m, "_erfcinv", "", impl::erfcinv_contig_dispatch_vector, impl::erfcinv_strided_dispatch_vector); + + impl::populate( + m, "_ndtri", "", impl::ndtri_contig_dispatch_vector, + impl::ndtri_strided_dispatch_vector); } } // namespace dpnp::tensor::py_internal diff --git a/dpnp/scipy/special/__init__.py b/dpnp/scipy/special/__init__.py index b4ffff1ab87d..4db7c7ec3830 100644 --- a/dpnp/scipy/special/__init__.py +++ b/dpnp/scipy/special/__init__.py @@ -47,6 +47,7 @@ erfcinv, erfcx, erfinv, + ndtri, ) __all__ = [ @@ -55,4 +56,5 @@ "erfcinv", "erfcx", "erfinv", + "ndtri", ] diff --git a/dpnp/scipy/special/_erf.py b/dpnp/scipy/special/_erf.py index 2726f3800bf9..cd5e9493fac1 100644 --- a/dpnp/scipy/special/_erf.py +++ b/dpnp/scipy/special/_erf.py @@ -343,3 +343,67 @@ def __call__(self, x, /, out=None): # pylint: disable=arguments-differ mkl_fn_to_call="_mkl_erf_to_call", mkl_impl_fn="_erfcinv", ) + +_NDTRI_DOCSTRING = r""" +Calculates the inverse of the standard normal cumulative distribution function +of a given input array. + +It is the quantile function of the standard normal distribution, also known as +the probit function. + +For full documentation refer to :obj:`scipy.special.ndtri`. + +Parameters +---------- +x : {dpnp.ndarray, usm_ndarray} + Input array, expected to have a real-valued floating-point data type. + Domain: [0, 1]. +out : {None, dpnp.ndarray, usm_ndarray, tuple of ndarray}, optional + Optional output array for the function values. + A tuple (possible only as a keyword argument) must have length equal to the + number of outputs. + + Default: ``None``. + +Returns +------- +out : dpnp.ndarray + The values of the inverse of the standard normal cumulative distribution + function at the given points `x`. + +See Also +-------- +:obj:`dpnp.scipy.special.erf` : Gauss error function. +:obj:`dpnp.scipy.special.erfinv` : Inverse of the error function. +:obj:`dpnp.scipy.special.erfcinv` : Inverse of the complementary error function. + +Notes +----- +It is related to the inverse of the complementary error function by + +.. math:: + \operatorname{ndtri}(x) = -\sqrt{2} \, + \operatorname{erfcinv}(2x) + +Examples +-------- +>>> import dpnp as np +>>> x = np.linspace(0.0, 1.0, num=5) +>>> y = np.scipy.special.ndtri(x) +>>> y +array([ -inf, -0.67448975, 0. , 0.67448975, inf]) + +Verify that the standard normal cumulative distribution function of ``y`` is +``x``: + +>>> 0.5 * (1 + np.scipy.special.erf(y / np.sqrt(2))) +array([0. , 0.25, 0.5 , 0.75, 1. ]) + +""" + +ndtri = DPNPErf( + "ndtri", + ti._erf_result_type, + ti._ndtri, + _NDTRI_DOCSTRING, +) diff --git a/dpnp/tests/test_special.py b/dpnp/tests/test_special.py index 075bef5aeca3..d8488d861bf8 100644 --- a/dpnp/tests/test_special.py +++ b/dpnp/tests/test_special.py @@ -9,6 +9,7 @@ generate_random_numpy_array, get_all_dtypes, get_complex_dtypes, + has_support_aspect64, ) from .third_party.cupy.testing import installed, with_requires @@ -126,9 +127,18 @@ def test_erfinv(self, inverse): data=dpnp.linspace(-1, 1, 101), ) + def test_ndtri(self, inverse): + self._check_variant_func( + inverse, + dpnp.scipy.special.ndtri, + lambda z: -numpy.sqrt(2) * dpnp.scipy.special.erfcinv(2 * z), + rtol=10 * self.tol, + data=dpnp.linspace(0, 1, 101), + ) + @with_requires("scipy") -@pytest.mark.parametrize("func", ["erfinv", "erfcinv"]) +@pytest.mark.parametrize("func", ["erfinv", "erfcinv", "ndtri"]) class TestInverse: @pytest.mark.parametrize( @@ -173,3 +183,51 @@ def test_complex(self, func, dt): x = dpnp.empty(5, dtype=dt) with pytest.raises(ValueError): getattr(dpnp.scipy.special, func)(x) + + def test_nan_inf(self, func): + import scipy.special + + a = numpy.array([numpy.nan, -numpy.inf, numpy.inf]) + ia = dpnp.array(a) + + result = getattr(dpnp.scipy.special, func)(ia) + expected = getattr(scipy.special, func)(a) + assert_allclose(result, expected) + + +@with_requires("scipy") +class TestNdtri: + tol = 100 * dpnp.finfo(dpnp.default_float_type()).eps + + @pytest.mark.skipif( + not has_support_aspect64(), reason="1e-300 and 1 - 1e-12 need fp64" + ) + def test_tails(self): + import scipy.special + + # 1e-300 and 1 - 1e-12 are only resolved by the tail expansions + a = numpy.array([1e-300, 1e-100, 1e-16, 1e-8, 1 - 1e-8, 1 - 1e-12]) + ia = dpnp.array(a) + + result = dpnp.scipy.special.ndtri(ia) + expected = scipy.special.ndtri(a) + assert_dtype_allclose(result, expected) + + def test_symmetry(self): + a = dpnp.linspace(0.01, 0.49, 25) + result = dpnp.scipy.special.ndtri(1 - a) + expected = -dpnp.scipy.special.ndtri(a) + assert_allclose(result, expected, rtol=self.tol) + + def test_inverse_of_cdf(self): + x = dpnp.linspace(-4, 4, 33) + p = 0.5 * (1 + dpnp.scipy.special.erf(x / numpy.sqrt(2))) + result = dpnp.scipy.special.ndtri(p) + assert_allclose(result, x, rtol=self.tol, atol=self.tol) + + def test_out(self): + a = dpnp.linspace(0.1, 0.9, 5) + out = dpnp.empty_like(a) + result = dpnp.scipy.special.ndtri(a, out=out) + assert result is out + assert_allclose(out, dpnp.scipy.special.ndtri(a)) diff --git a/dpnp/tests/test_strides.py b/dpnp/tests/test_strides.py index ac43374f10a2..0b90c178b90a 100644 --- a/dpnp/tests/test_strides.py +++ b/dpnp/tests/test_strides.py @@ -175,6 +175,7 @@ def test_reduce_hypot(dtype, stride): ("erfcx", -3, 3), ("erfinv", -1, 1), ("erfcinv", 0, 2), + ("ndtri", 0, 1), ], ) @pytest.mark.parametrize("stride", [2, -1, -3]) diff --git a/dpnp/tests/test_sycl_queue.py b/dpnp/tests/test_sycl_queue.py index 35b40f6e598b..a1d6a1ea7bd4 100644 --- a/dpnp/tests/test_sycl_queue.py +++ b/dpnp/tests/test_sycl_queue.py @@ -1696,6 +1696,7 @@ def test_interp(device, left, right, period): ("erfcx", -3, 3), ("erfinv", -1, 1), ("erfcinv", 0, 2), + ("ndtri", 0, 1), ], ) @pytest.mark.parametrize("device", valid_queues, ids=dev_ids) diff --git a/dpnp/tests/test_usm_type.py b/dpnp/tests/test_usm_type.py index 108723b375cb..dbdf22ca45ac 100644 --- a/dpnp/tests/test_usm_type.py +++ b/dpnp/tests/test_usm_type.py @@ -1384,6 +1384,7 @@ def test_choose(usm_type_x, usm_type_ind): ("erfcx", -3, 3), ("erfinv", -1, 1), ("erfcinv", 0, 2), + ("ndtri", 0, 1), ], ) @pytest.mark.parametrize("usm_type", list_of_usm_types)