Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions doc/reference/scipy_special.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ Error function and Fresnel integrals
erfcx
erfinv
erfcinv

Statistical functions
---------------------

.. autosummary::
:toctree: generated/
:nosignatures:

ndtri
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions dpnp/libtensor/source/elementwise_functions/erf_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -244,5 +245,9 @@ void init_erf_funcs(py::module_ m)
impl::populate<impl::ErfcinvContigFactory, impl::ErfcinvStridedFactory>(
m, "_erfcinv", "", impl::erfcinv_contig_dispatch_vector,
impl::erfcinv_strided_dispatch_vector);

impl::populate<impl::NdtriContigFactory, impl::NdtriStridedFactory>(
m, "_ndtri", "", impl::ndtri_contig_dispatch_vector,
impl::ndtri_strided_dispatch_vector);
}
} // namespace dpnp::tensor::py_internal
2 changes: 2 additions & 0 deletions dpnp/scipy/special/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
erfcinv,
erfcx,
erfinv,
ndtri,
)

__all__ = [
Expand All @@ -55,4 +56,5 @@
"erfcinv",
"erfcx",
"erfinv",
"ndtri",
]
64 changes: 64 additions & 0 deletions dpnp/scipy/special/_erf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
60 changes: 59 additions & 1 deletion dpnp/tests/test_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
1 change: 1 addition & 0 deletions dpnp/tests/test_strides.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions dpnp/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions dpnp/tests/test_usm_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading