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 Gamma, Negative Binomial, and Generalized Poisson Distribution #145

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

dachengx
Copy link
Collaborator

@dachengx dachengx commented Mar 3, 2024

The comparisons between the implemented functions in this PR and other packages like scipy are here:

image
image
image

To reproduce:

from xenonnt_plot_style import XENONPlotStyle as xps
xps.use('xenonnt')


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import matplotlib.cm as cm
from scipy import stats, special
import appletree as apt
from appletree.randgen import gamma, negative_binomial, generalized_poisson


key = apt.randgen.get_key(seed=7)
batch_size = int(1e6)


alpha = 1.5
beta = 0.4


fig, ax = plt.subplots(1, 1)

x = np.linspace(0, 15, 101)

key, rvs = gamma(key, alpha, beta, shape=(batch_size,))
h = np.histogram(rvs, bins=x)
ax.hist(
    rvs,
    bins=x,
    histtype='step',
    density=True,
    label='gamma',
)

ax.hist(
    stats.gamma.rvs(alpha, size=batch_size) / beta,
    bins=x,
    histtype='step',
    density=True,
    label='scipy.stats.gamma',
)

ax.legend()
ax.set_xlabel('x')
ax.set_ylabel('PDF')

plt.show()


n = 2.5
p = 0.3


key, rvs = negative_binomial(key, p, n, shape=(batch_size,))

fig, ax = plt.subplots(1, 1)

x = np.arange(25)

b, c = np.unique(rvs, return_counts=True)
m = b <= x.max()
ax.plot(
    b[m], c[m] / c[m].sum(),
    marker='o',
    label='negative_binomial',
)

ax.plot(
    x, stats.nbinom.pmf(x, n, p),
    marker='+',
    label='scipy.stats.nbinom',
)

ax.legend()
ax.set_xlabel('x')
ax.set_ylabel('PMF')

plt.show()


lam = 1.5
eta = 0.5


def pmf(k, lam, eta):
    mu = lam * (1 - eta)
    offset = mu + eta * k
    return mu * (offset ** (k - 1)) / (special.gamma(k + 1) * np.exp(offset))

def logpmf(k, lam, eta):
    mu = lam * (1 - eta)
    offset = mu + eta * k
    return np.log(mu) + (k - 1) * np.log(offset) - special.gammaln(k + 1) - offset


fig, ax = plt.subplots(1, 1)

x = np.arange(10)

eta = 0.5
key, rvs = generalized_poisson(key, lam, eta, shape=(batch_size,))
b, c = np.unique(rvs, return_counts=True)
m = b <= x.max()
ax.plot(
    b[m], c[m] / c[m].sum(),
    marker='o',
    label=f'generalized_poisson, eta={eta:.1f}',
)
ax.plot(
    x, pmf(x, lam, eta),
    marker='+',
    label='analytical GPD',
)

eta = 0.0
key, rvs = generalized_poisson(key, lam, eta, shape=(batch_size,))
b, c = np.unique(rvs, return_counts=True)
m = b <= x.max()
ax.plot(
    b[m], c[m] / c[m].sum(),
    marker='o',
    label=f'generalized_poisson, eta={eta:.1f}',
)
ax.plot(
    x, stats.poisson.pmf(x, lam),
    marker='+',
    label='scipy.stats.poisson',
)

ax.legend(ncol=1)
ax.set_xlabel('x')
ax.set_ylabel('PMF')

plt.show()

@dachengx dachengx marked this pull request as ready for review March 3, 2024 19:11
@coveralls
Copy link

coveralls commented Mar 3, 2024

Pull Request Test Coverage Report for Build 8132432670

Details

  • 9 of 31 (29.03%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.7%) to 83.291%

Changes Missing Coverage Covered Lines Changed/Added Lines %
appletree/randgen.py 9 31 29.03%
Totals Coverage Status
Change from base Build 7932308533: -0.7%
Covered Lines: 1984
Relevant Lines: 2382

💛 - Coveralls

@dachengx dachengx requested a review from zihaoxu98 March 4, 2024 03:37
Copy link
Collaborator

@zihaoxu98 zihaoxu98 left a comment

Choose a reason for hiding this comment

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

Thanks. Looks all good to me

@zihaoxu98 zihaoxu98 merged commit 6807966 into master Mar 4, 2024
7 checks passed
@zihaoxu98 zihaoxu98 deleted the add_more_distributions branch March 4, 2024 14:59
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