diff --git a/ivy/functional/frontends/numpy/random/functions.py b/ivy/functional/frontends/numpy/random/functions.py index aa3dfbc1a7f86..478b50d700128 100644 --- a/ivy/functional/frontends/numpy/random/functions.py +++ b/ivy/functional/frontends/numpy/random/functions.py @@ -5,6 +5,7 @@ to_ivy_arrays_and_back, from_zero_dim_arrays_to_scalar, ) +from ivy import with_supported_dtypes @to_ivy_arrays_and_back @@ -184,6 +185,27 @@ def negative_binomial(n, p, size=None): return ivy.poisson(lam=lambda_, shape=size) +@with_supported_dtypes( + {"1.25.2 and below": ("float16", "float32")}, + "numpy", +) +@to_ivy_arrays_and_back +@from_zero_dim_arrays_to_scalar +def noncentral_chisquare(df, nonc, size=None): + if ivy.any(df <= 0): + raise ValueError("Degree of freedom must be greater than 0") + if ivy.has_nans(nonc): + return ivy.nan + if ivy.any(nonc == 0): + return chisquare(df, size=size) + if ivy.any(df < 1): + n = standard_normal() + ivy.sqrt(nonc) + return chisquare(df - 1, size=size) + n * n + else: + i = poisson(nonc / 2.0, size=size) + return chisquare(df + 2 * i, size=size) + + @to_ivy_arrays_and_back @from_zero_dim_arrays_to_scalar def normal(loc=0.0, scale=1.0, size=None): diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py index fac33261b9cf6..df6e88d78bace 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_random/test_functions.py @@ -672,6 +672,47 @@ def test_numpy_negative_binomial( ) +# noncentral_chisquare +@handle_frontend_test( + fn_tree="numpy.random.noncentral_chisquare", + dtype_and_df=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + min_value=0, + exclude_min=True, + ), + dtype_and_nonc=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + min_value=0, + ), + size=helpers.get_shape(allow_none=True), + test_with_out=st.just(False), +) +def test_numpy_noncentral_chisquare( + dtype_and_df, + dtype_and_nonc, + size, + frontend, + test_flags, + fn_tree, + backend_fw, + on_device, +): + dtype_df, df = dtype_and_df + dtype_nonc, nonc = dtype_and_nonc + helpers.test_frontend_function( + input_dtypes=dtype_df + dtype_nonc, + backend_to_test=backend_fw, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + df=df[0], + nonc=nonc[0], + size=size, + ) + + # normal @handle_frontend_test( fn_tree="numpy.random.normal",