Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard_t function to numpy frontend #21549

Merged
8 changes: 8 additions & 0 deletions ivy/functional/frontends/numpy/random/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def standard_gamma(shape, size=None):
return ivy.gamma(shape, 1.0, shape=size, dtype="float64")


@to_ivy_arrays_and_back
@from_zero_dim_arrays_to_scalar
def standard_t(df, size=None):
numerator = ivy.random_normal(mean=0.0, std=1.0, shape=size, dtype="float64")
denominator = ivy.gamma(df / 2, 1.0, shape=size, dtype="float64")
return ivy.sqrt(df / 2) * ivy.divide(numerator, ivy.sqrt(denominator))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I be using the division function implemented in ivy here (ivy.divide)? I tried to do that but the gamma function fails because it expects to have integers or floats as inputs rather than ivy.arrays.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gamma accepts both Array and NativeArray so this shouldn't be an issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
denominator = ivy.gamma(df / 2, 1.0, shape=size, dtype="float64")
return ivy.sqrt(df / 2) * ivy.divide(numerator, ivy.sqrt(denominator))
denominator = ivy.gamma(ivy.divide(df, 2), 1.0, shape=size, dtype="float64")
return ivy.sqrt(ivy.divide(df, 2)) * ivy.divide(numerator, ivy.sqrt(denominator))

When I run the tests locally with ivy.divide I get an error saying:

ivy.utils.exceptions.IvyBackendException: numpy: gamma: low and high bounds must be numerics when shape is specified

Copy link
Contributor

@aparajith21 aparajith21 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error on the CI seems to be somewhat different anbd suggest input issues. Please check the same and request a review once done
https://github.com/unifyai/ivy/actions/runs/5811559595/job/15755368193?pr=21549#step:3:543



@to_ivy_arrays_and_back
@from_zero_dim_arrays_to_scalar
def binomial(n, p, size=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,40 @@ def test_numpy_standard_gamma(
)


@handle_frontend_test(
fn_tree="numpy.random.standard_t",
df=st.floats(min_value=1, max_value=20),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aparajith21 The tests failed when df was so small that it was practically 0 even though the exclude_min argument was set to True. I changed the min_value of floats from 0 to 1 and I believe everything passes now 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, thanks for the contribution! LGTM now then. Sorry for the late review, really busy personally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please request review again once you resolve merge conflicts, and will then merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aparajith21 No problem at all. Have resolved the merge conflict but now some tests fail on the jax backend 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues, sorry for the late review, believe it's not directly related to your PR, will go ahead and merge, thanks!

df_dtypes=helpers.get_dtypes("integer", full=False),
size=st.tuples(
st.integers(min_value=2, max_value=5), st.integers(min_value=2, max_value=5)
),
size_dtypes=helpers.get_dtypes("integer", full=False),
test_with_out=st.just(False),
)
def test_numpy_standard_t(
df,
df_dtypes,
size,
size_dtypes,
frontend,
test_flags,
fn_tree,
backend_fw,
on_device,
):
helpers.test_frontend_function(
input_dtypes=df_dtypes + size_dtypes,
backend_to_test=backend_fw,
test_flags=test_flags,
frontend=frontend,
fn_tree=fn_tree,
on_device=on_device,
test_values=False,
df=df,
size=size,
)


# binomial
@handle_frontend_test(
fn_tree="numpy.random.binomial",
Expand Down