Esempio n. 1
0
def test_negative_binomial(rnd, n, p, loc=0):

    # negative bimonial random variate generator
    negative_binomial_dist = RVGs.NegativeBinomial(n, p, loc)

    # obtain samples
    samples = get_samples(negative_binomial_dist, rnd)

    # get theoretical mean and variance
    mean = scipy.nbinom.stats(n, p, loc, moments='m')
    mean = np.asarray(mean).item()
    var = scipy.nbinom.stats(n, p, loc, moments='v')
    var = np.asarray(var).item()

    # report mean and variance
    print_test_results('Negative Binomial',
                       samples,
                       expectation=mean,
                       variance=var)
Esempio n. 2
0
def test_fitting_negbinomial():
    print("\nTesting NegBinomial with n=10, p=0.2, loc=1")
    dist = RVGs.NegativeBinomial(n=10, p=0.2, loc=1)
    print('  percentile interval: ', dist.get_percentile_interval(alpha=0.05))

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

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

    # plot the fitted distributions
    Plot.plot_negbinomial_fit(data=data,
                              fit_results=dict_mm_results,
                              title='Method of Moment')