Exemplo n.º 1
0
def test_fitting_poisson():

    print("\nTesting Poisson with mean=100 and loc = 10")
    dist = RVGs.Poisson(mu=100, loc=10)
    print('  percentile interval: ', dist.get_percentile_interval(alpha=0.05))

    data = np.array(get_samples(dist, np.random))
    dict_mm_results = RVGs.Poisson.fit_mm(mean=np.average(data),
                                          fixed_location=10)
    dict_ml_results = RVGs.Poisson.fit_ml(data=data, fixed_location=10)

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

    # plot the fitted distributions
    Plot.plot_poisson_fit(data=data,
                          fit_results=dict_mm_results,
                          title='Method of Moment')
    Plot.plot_poisson_fit(data=data,
                          fit_results=dict_ml_results,
                          title='Maximum Likelihood')
# make a histogram
Hist.plot_histogram(data=cols[0], title='Weekly Number of Drinks', bin_width=1)

# mean and standard deviation
stat = Stat.SummaryStat(name='Weekly number of drinks', data=cols[0])
print('Mean = ', stat.get_mean())
print('StDev = ', stat.get_stdev())

# fit a Poisson distribution
fit_results = RVGs.Poisson.fit_ml(data=cols[0])
print('Fitting a Poisson distribution:', fit_results)

# plot the fitted Poisson distribution
Plot.plot_poisson_fit(data=cols[0],
                      fit_results=fit_results,
                      x_label='Weekly number of drinks',
                      x_range=(0, 40),
                      bin_width=1)

# fit a gamma-Poisson distribution
fit_results = RVGs.GammaPoisson.fit_ml(data=cols[0])
print('Fitting a gamma-Poisson distribution:', fit_results)

# plot the fitted gamma-Poisson distribution
Plot.plot_gamma_poisson_fit(data=cols[0],
                            fit_results=fit_results,
                            x_label='Weekly number of drinks',
                            x_range=(0, 40),
                            bin_width=1)

# fit a beta-binomial distribution