Exemplo n.º 1
0
def test_fitting_beta_binomial():

    print("\nTesting Beta-Binomial with n=100, a=2, b=3, loc=1, scale=2:")
    dist = RVGs.BetaBinomial(n=20, a=2, b=3, loc=1)
    print('  percentile interval: ', dist.get_percentile_interval(alpha=0.05))

    data = np.array(get_samples(dist, np.random))
    # method of moment
    dict_mm_results = RVGs.BetaBinomial.fit_mm(mean=np.mean(data),
                                               st_dev=np.std(data),
                                               n=20,
                                               fixed_location=1)
    # maximum likelihood
    dict_ml_results = RVGs.BetaBinomial.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_beta_binomial_fit(data=data,
                                fit_results=dict_mm_results,
                                title='Method of Moment')
    Plot.plot_beta_binomial_fit(data=data,
                                fit_results=dict_ml_results,
                                title='Maximum Likelihood')
Exemplo n.º 2
0
def test_beta_binomial(rnd, n, a, b, loc=0):

    # beta random variate generator
    beta_binomial_dist = RVGs.BetaBinomial(n, a, b, loc)

    # obtain samples
    samples = get_samples(beta_binomial_dist, rnd)

    # report mean and variance
    print_test_results('BetaBinomial',
                       samples,
                       expectation=(a * n / (a + b)) + loc,
                       variance=(n * a * b * (a + b + n)) / ((a + b)**2 *
                                                             (a + b + 1)))