-
Notifications
You must be signed in to change notification settings - Fork 15
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
Added Binomial distribution #101
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81c1cbd
initial commit for binomial function
MoritzNeuberger a64376c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 084bdd2
... forgot to add the new files ...
MoritzNeuberger 0fb3930
Merge branch 'binomial-dist' of github.com:MoritzNeuberger/numba-stat…
MoritzNeuberger c8d11cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e655e9f
Merge branch 'main' into binomial-dist
HDembinski e9ce1a2
fixes
HDembinski 11ee806
fixes
HDembinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Binomial distribution. | ||
|
||
See Also | ||
-------- | ||
scipy.stats.binom: Scipy equivalent. | ||
""" | ||
|
||
import numpy as np | ||
from ._special import xlogy as _xlogy | ||
from ._special import xlog1py as _xlog1py | ||
from math import lgamma as _lgamma | ||
from ._util import _jit, _generate_wrappers, _prange, _seed | ||
import numba as nb | ||
|
||
_doc_par = """ | ||
k : int | ||
number of successes. | ||
n : int | ||
number of trails. | ||
p : float | ||
success probability for each trail. | ||
""" | ||
|
||
|
||
@_jit(2, cache=False) | ||
def _logpmf(k, n, p): | ||
T = type(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want the type of p here, not n |
||
r = np.empty(len(k), T) | ||
for i in _prange(len(r)): | ||
combiln = _lgamma(n + T(1)) - (_lgamma(k[i] + T(1)) + _lgamma(n - k[i] + T(1))) | ||
r[i] = combiln + _xlogy(k[i], p) + _xlog1py(n - k[i], -p) | ||
return r | ||
|
||
|
||
@_jit(2, cache=False) | ||
def _pmf(k, n, p): | ||
return np.exp(_logpmf(k, n, p)) | ||
|
||
|
||
@_jit(2, cache=False) | ||
def _cdf(k, n, p): | ||
T = type(n) | ||
r = np.empty(len(k), T) | ||
for i in _prange(len(r)): | ||
r[i] = np.sum(_pmf(np.arange(0, k[i] + 1), n, p)) | ||
return r | ||
|
||
|
||
@nb.njit( | ||
nb.int64[:](nb.uint64, nb.float32, nb.uint64, nb.optional(nb.uint64)), | ||
cache=True, | ||
inline="always", | ||
error_model="numpy", | ||
) | ||
def _rvs(n, p, size, random_state): | ||
_seed(random_state) | ||
return np.random.binomial(n, p, size=size) | ||
|
||
|
||
_generate_wrappers(globals()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import numpy as np | ||
from numba_stats import binom | ||
import scipy.stats as sc | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("n", np.linspace(0, 10, 6)) | ||
@pytest.mark.parametrize("p", np.linspace(0, 1, 5)) | ||
def test_pmf(n, p): | ||
k = np.arange(n + 1) | ||
got = binom.pmf(k, n, p) | ||
expected = sc.binom.pmf(k, n, p) | ||
np.testing.assert_allclose(got, expected) | ||
|
||
|
||
@pytest.mark.parametrize("n", np.linspace(0, 10, 6)) | ||
@pytest.mark.parametrize("p", np.linspace(0, 1, 5)) | ||
def test_cdf(n, p): | ||
k = np.arange(n + 1) | ||
got = binom.cdf(k, n, p) | ||
expected = sc.binom.cdf(k, n, p) | ||
np.testing.assert_allclose(got, expected) | ||
|
||
|
||
@pytest.mark.parametrize("n", np.linspace(0, 10, 6)) | ||
@pytest.mark.parametrize("p", np.linspace(0, 1, 5)) | ||
def test_rvs(n, p): | ||
got = binom.rvs(n, p, size=1000, random_state=1) | ||
|
||
def expected(): | ||
np.random.seed(1) | ||
return np.random.binomial(n, p, 1000) | ||
|
||
np.testing.assert_equal(got, expected()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trial