Пример #1
0
def plot_gamma_poisson_fit(data,
                           fit_results,
                           title=None,
                           x_label=None,
                           x_range=None,
                           y_range=None,
                           fig_size=(6, 5),
                           bin_width=1,
                           filename=None):
    """
    :param data: (numpy.array) observations
    :param fit_results: dictionary with keys "a", "gamma_scale", "loc"
    :param title: title of the figure
    :param x_label: label to show on the x-axis of the histogram
    :param x_range: (tuple) x range
    :param y_range: (tuple) y range
        (the histogram shows the probability density so the upper value of y_range should be 1).
    :param fig_size: int, specify the figure size
    :param bin_width: bin width
    :param filename: filename to save the figure as
    """

    plot_fit_discrete(data=data,
                      dist=RVGs.GammaPoisson(
                          a=fit_results['a'],
                          gamma_scale=fit_results['gamma_scale'],
                          loc=fit_results['loc']),
                      label='Gamma-Poisson',
                      bin_width=bin_width,
                      title=title,
                      x_label=x_label,
                      x_range=x_range,
                      y_range=y_range,
                      fig_size=fig_size,
                      filename=filename)
Пример #2
0
def test_gamma_poisson(rnd, a, gamma_scale, loc=0):
    # gamma-poisson random variate generator
    gamma_poisson_dist = RVGs.GammaPoisson(a=a,
                                           gamma_scale=gamma_scale,
                                           loc=loc)

    # obtain samples
    samples = get_samples(gamma_poisson_dist, rnd)

    # report mean and variance
    print_test_results('GammaPoisson',
                       samples,
                       expectation=(a * gamma_scale) + loc,
                       variance=a * gamma_scale + a * (gamma_scale**2))
Пример #3
0
def test_fitting_gamma_poisson():

    print("\nTesting Gamma Poisson with a=2, gamma_scale=4, loc=2")
    dist = RVGs.GammaPoisson(a=2, gamma_scale=4, loc=2)
    print('  percentile interval: ', dist.get_percentile_interval(alpha=0.05))

    data = np.array(get_samples(dist, np.random))
    dict_mm_results = RVGs.GammaPoisson.fit_mm(mean=np.average(data),
                                               st_dev=np.std(data),
                                               fixed_location=2)
    dict_ml_results = RVGs.GammaPoisson.fit_ml(data=data, fixed_location=2)

    print("  Fit:")
    print("    MM:", dict_mm_results)
    print("    ML:", dict_ml_results)

    # plot the fitted distributions
    Plot.plot_gamma_poisson_fit(data=data,
                                fit_results=dict_mm_results,
                                title='Method of Moment')
    Plot.plot_gamma_poisson_fit(data=data,
                                fit_results=dict_ml_results,
                                title='Maximum Likelihood')
import numpy as np

import SimPy.RandomVariateGenerators as RVGs

# a random number generator
rng = np.random.RandomState(seed=0)

# gamma
gamma = RVGs.Gamma(a=0.9835458832943496, loc=0, scale=1.9199027077975956)
# generate 10 realizations from gamma
print('Realizations from gamma:')
for i in range(10):
    print(gamma.sample(rng))

# gamma-Poisson
gammaPoisson = RVGs.GammaPoisson(a=0.3693765965041004,
                                 gamma_scale=9.154827879319111,
                                 loc=0)
# generate 10 realizations from gamma-Poisson
print('Realizations from gamma-Poisson:')
for i in range(10):
    print(gammaPoisson.sample(rng))