Skip to content

ENH: added random.gamma #170

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

Merged
merged 13 commits into from
Oct 30, 2020
Merged

ENH: added random.gamma #170

merged 13 commits into from
Oct 30, 2020

Conversation

samir-nasibli
Copy link

@samir-nasibli samir-nasibli commented Oct 22, 2020

Description

gamma(shape[, scale, size]) Draw samples from a Gamma distribution.

TODO:

  • array_like of floats for scale and shape params

Checklist

  • Docstrings for all functions
  • Gallery example in ./doc/examples (new features only)
  • Unit tests:
  • Clean style in the spirit of PEP8

@samir-nasibli samir-nasibli changed the title ENH: added random.gamma ENH: added random.gamma Oct 22, 2020
@samir-nasibli samir-nasibli requested a review from shssf October 22, 2020 03:02
@codecov
Copy link

codecov bot commented Oct 22, 2020

Codecov Report

Merging #170 into master will decrease coverage by 52.36%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #170       +/-   ##
==========================================
- Coverage   52.36%   0.00%   -52.37%     
==========================================
  Files          20      20               
  Lines        1352    1368       +16     
  Branches      384     392        +8     
==========================================
- Hits          708       0      -708     
- Misses        357    1368     +1011     
+ Partials      287       0      -287     
Impacted Files Coverage Δ
dpnp/random/dpnp_iface_random.py 0.00% <0.00%> (-35.88%) ⬇️
dpnp/config.py 0.00% <0.00%> (-100.00%) ⬇️
dpnp/_version.py 0.00% <0.00%> (-100.00%) ⬇️
dpnp/linalg/__init__.py 0.00% <0.00%> (-100.00%) ⬇️
dpnp/random/__init__.py 0.00% <0.00%> (-100.00%) ⬇️
dpnp/dpnp_iface_types.py 0.00% <0.00%> (-93.34%) ⬇️
dpnp/dpnp_iface.py 0.00% <0.00%> (-89.86%) ⬇️
dpnp/dpnp_iface_linearalgebra.py 0.00% <0.00%> (-84.79%) ⬇️
dpnp/dpnp_iface_bitwise.py 0.00% <0.00%> (-70.59%) ⬇️
dpnp/linalg/linalg_iface.py 0.00% <0.00%> (-66.67%) ⬇️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 793f441...5041c9d. Read the comment docs.

@samir-nasibli samir-nasibli added the in progress Please do not merge. Work is in progress. label Oct 22, 2020
@samir-nasibli
Copy link
Author

samir-nasibli commented Oct 22, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@samir-nasibli samir-nasibli removed the in progress Please do not merge. Work is in progress. label Oct 23, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@shssf shssf added the in progress Please do not merge. Work is in progress. label Oct 23, 2020
@oleksandr-pavlyk
Copy link
Contributor

Something is amiss. Basic moments do not match:

In [24]: np.var(np.random.gamma(shape=2.56, scale=0.9, size=10**6))
Out[24]: 2.0689620288819444

In [25]: np.mean(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[25]: 2.0468521330146694

In [26]: np.var(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[26]: 1.6367525135977068

In [27]: dpnp.mean(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[27]: 2.559302218900502

In [28]: dpnp.var(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[28]: 6.545711234562073

@samir-nasibli
Copy link
Author

samir-nasibli commented Oct 24, 2020

Something is amiss. Basic moments do not match:

In [24]: np.var(np.random.gamma(shape=2.56, scale=0.9, size=10**6))
Out[24]: 2.0689620288819444

In [25]: np.mean(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[25]: 2.0468521330146694

In [26]: np.var(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[26]: 1.6367525135977068

In [27]: dpnp.mean(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[27]: 2.559302218900502

In [28]: dpnp.var(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
Out[28]: 6.545711234562073

@oleksandr-pavlyk Thank you for the report. I fixed it.
Reproduce:

>>> np.var(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
1.6414887360289188
>>> np.mean(np.random.gamma(shape=2.56, scale=0.8, size=10**6))
2.0480613902279794
>>> np.var(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
1.6337041157687837
>>> np.mean(dpnp.random.gamma(shape=2.56, scale=0.8, size=10**6))
2.049404203568917

@oleksandr-pavlyk You set a great example to test. Is it worth checking the basic moments based on this sample? I can add test suit.

@samir-nasibli samir-nasibli removed the in progress Please do not merge. Work is in progress. label Oct 26, 2020
@oleksandr-pavlyk
Copy link
Contributor

Expectations of bounded functions, or moments for light-tailed distributions is appropriate.

An appropriate check is that sample expectation is within several numbers (say 5) of standard deviations of the estimator from its mean. The mean needs to be known analytically, while the standard deviation may be estimated from the sample

@oleksandr-pavlyk
Copy link
Contributor

Having built dpnp with oneAPI 2020.8.0.1005:

In [1]: import dpnp
Running on: Intel(R) Gen9
DPCtrl SYCL queue used
SYCL kernels link time: 0.182214 (sec.)


In [2]: import dpctl

In [3]: dpctl.set_default_queue('opencl', 'cpu', 0)

In [5]: X = dpnp.random.gamma(shape=125.4, scale=0.02, size=10**5)
terminate called after throwing an instance of 'oneapi::mkl::unsupported_device'
  what():  oneMKL: rng/generate: Intel(R) Gen9 is not supported
Aborted (core dumped) 

I was expecting dpnp to use queue from dpctl which was just set to be CPU.

It would greatly enhance user's experience if these C++ exceptions where intercepted and rethrown as Python exceptions.

@oleksandr-pavlyk
Copy link
Contributor

Having built dpnp with oneAPI 2020.8.0.1005 (beta 10):

In [1]: import dpnp
Running on: Intel(R) Gen9
DPCtrl SYCL queue used
SYCL kernels link time: 0.182214 (sec.)


In [2]: import dpctl

In [3]: dpctl.set_default_queue('opencl', 'cpu', 0)

In [5]: X = dpnp.random.gamma(shape=125.4, scale=0.02, size=10**5)
terminate called after throwing an instance of 'oneapi::mkl::unsupported_device'
  what():  oneMKL: rng/generate: Intel(R) Gen9 is not supported
Aborted (core dumped) 

I was expecting dpnp to use queue from dpctl which was just set to be CPU.

Random number generation is not honoring dpctl supplied queue because it is using the cached random engine, which does have a queue retrieved at the module initialization time stored in the instance, so while DPNP_CURRENT_QUEUE gives the queue associated with OpenCL CPU device, the engine still uses the Level-0 GPU device.

@shssf I think this is a problem.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@samir-nasibli
Copy link
Author

It would greatly enhance user's experience if these C++ exceptions where intercepted and rethrown as Python exceptions.

Added issue #201 for this. Will be fixed in other PR

@oleksandr-pavlyk
Copy link
Contributor

shape=0 parameter causes oneMKL exception. These can be either disallowed, or handled as a special case using dpnp.zeros.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@samir-nasibli
Copy link
Author

samir-nasibli commented Oct 30, 2020

shape=0 parameter causes oneMKL exception. These can be either disallowed, or handled as a special case using dpnp.zeros.

AttributeError: type object 'dpnp.dparray' has no attribute 'zeros'.
But I have fixed case, when shape=0 via dparray.fill

@oleksandr-pavlyk
Copy link
Contributor

shape=0 parameter causes oneMKL exception. These can be either disallowed, or handled as a special case using dpnp.zeros.

AttributeError: type object 'dpnp.dparray' has no attribute 'zeros'.
But I have fixed case, when shape=0 via dparray.fill

Fill works. zeros is not a method of dparray, it is function in module, dpnp.zeros. It will produce a new array, so instead of

result = dpnp.array(size, dtype=dtype)
result.fill(0.0)

you could simply write

result = dpnp.zeros(size, dtype=dtype)

Copy link
Contributor

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Choose a reason for hiding this comment

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

Further changes must be done outside of this PR.

@shssf shssf merged commit 812b7e0 into master Oct 30, 2020
@shssf shssf deleted the samir-nasibli/feature/random_gamma branch October 30, 2020 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants