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

Added Binomial distribution #101

Merged
merged 8 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/numba_stats/_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ def get(name, signature):

# n-ary functions (double)
voigt_profile = get("voigt_profile", float64(float64, float64, float64))

xlogy = get("xlogy", float64(float64, float64))
xlog1py = get("xlog1py", float64(float64, float64))
61 changes: 61 additions & 0 deletions src/numba_stats/binom.py
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.
Copy link
Owner

Choose a reason for hiding this comment

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

trial

"""


@_jit(2, cache=False)
def _logpmf(k, n, p):
T = type(n)
Copy link
Owner

Choose a reason for hiding this comment

The 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())
34 changes: 34 additions & 0 deletions tests/test_binom.py
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())