Skip to content

Commit

Permalink
feat: Added numpy.random.noncentral_chisquare function to Frontend (i…
Browse files Browse the repository at this point in the history
…vy-llc#23378)

Co-authored-by: Kareem Morsy <[email protected]>
  • Loading branch information
2 people authored and iababio committed Sep 27, 2023
1 parent 2b7d451 commit 59ffc9d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ivy/functional/frontends/numpy/random/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 59ffc9d

Please sign in to comment.