-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Source code is inspired by the scipy implementation. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hans Dembinski <[email protected]>
- Loading branch information
1 parent
7da0422
commit 3302ff0
Showing
12 changed files
with
198 additions
and
41 deletions.
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
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
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,71 @@ | ||
""" | ||
Binomial distribution. | ||
See Also | ||
-------- | ||
scipy.stats.binom: Scipy equivalent. | ||
""" | ||
|
||
import numpy as np | ||
from ._special import xlogy as _xlogy, xlog1py as _xlog1py, betainc as _betainc | ||
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 trials. | ||
p : float | ||
Success probability for each trial. | ||
""" | ||
|
||
|
||
@_jit(1, narg=2, cache=False) | ||
def _logpmf(k, n, p): | ||
T = type(p) | ||
r = np.empty(len(k), T) | ||
one = T(1) | ||
for i in _prange(len(r)): | ||
combiln = _lgamma(n[i] + one) - ( | ||
_lgamma(k[i] + one) + _lgamma(n[i] - k[i] + one) | ||
) | ||
r[i] = combiln + _xlogy(k[i], p) + _xlog1py(n[i] - k[i], -p) | ||
return r | ||
|
||
|
||
@_jit(1, narg=2, cache=False) | ||
def _pmf(k, n, p): | ||
return np.exp(_logpmf(k, n, p)) | ||
|
||
|
||
@_jit(1, narg=2, cache=False) | ||
def _cdf(k, n, p): | ||
T = type(p) | ||
r = np.empty(len(k), T) | ||
one = T(1) | ||
for i in _prange(len(r)): | ||
if k[i] == n[i]: | ||
r[i] = 1 | ||
elif p == 0: | ||
r[i] = 1 | ||
elif p == 1: | ||
r[i] = 0 | ||
else: | ||
r[i] = 1 - _betainc(k[i] + one, n[i] - k[i], 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
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
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
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,43 @@ | ||
import numpy as np | ||
from numba_stats import binom | ||
import scipy.stats as sc | ||
import pytest | ||
|
||
|
||
# NC and KC are all combinations of n and k from 0 to 10 | ||
N = np.arange(10) | ||
NC = [] | ||
KC = [] | ||
for n in N: | ||
for k in range(n + 1): | ||
NC.append(n) | ||
KC.append(k) | ||
NC = np.array(NC, np.float64) | ||
KC = np.array(KC, np.float64) | ||
|
||
|
||
@pytest.mark.parametrize("p", np.linspace(0, 1, 5)) | ||
def test_pmf(p): | ||
print(KC, NC) | ||
got = binom.pmf(KC, NC, p) | ||
expected = sc.binom.pmf(KC, NC, p) | ||
np.testing.assert_allclose(got, expected) | ||
|
||
|
||
@pytest.mark.parametrize("p", np.linspace(0, 1, 5)) | ||
def test_cdf(p): | ||
got = binom.cdf(KC, NC, p) | ||
expected = sc.binom.cdf(KC, NC, 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()) |
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,16 @@ | ||
from numba_stats import _special as sp | ||
from scipy import special as sp_ref | ||
import numba as nb | ||
import pytest | ||
import numpy as np | ||
|
||
|
||
@pytest.mark.parametrize("a", [1, 2, 3]) | ||
@pytest.mark.parametrize("b", [1, 2, 3]) | ||
@pytest.mark.parametrize("x", [0.1, 0.5, 0.9]) | ||
def test_betainc(a, b, x): | ||
@nb.njit | ||
def betainc(a, b, x): | ||
return sp.betainc(a, b, x) | ||
|
||
np.testing.assert_allclose(betainc(a, b, x), sp_ref.betainc(a, b, x)) |