Esempio n. 1
0
def binomial_lln(sample_size, p):

    ######
    ## Step 1 - create sample of independent uniform random variables

    lower_bound = 0.
    upper_bound = 1.
    uni_sample = np.random.uniform(lower_bound, upper_bound, sample_size)

    ######
    ## Step 2 - transform them to $B(1,p)$ distribution
    sample = [dist.binomial_inverse_cdf(p, u) for u in uni_sample]

    x_ax = [k for k in range(sample_size)]  # values on the x axis
    n_plots = 2
    y_ax = [[0.] * sample_size for j in range(n_plots)]

    # y_values (1) - actual average
    y_ax[1] = [p for x in range(sample_size)]

    # y_values (0) - cumulative average of all the samples
    y_ax[0] = [sum(sample[0:k + 1]) / (k + 1) for k in range(sample_size)]

    mp = pu.PlotUtilities("Cumulative Average", 'x', 'Average')
    mp.multiPlot(x_ax, y_ax)
Esempio n. 2
0
def binomial_lln_hist(sample_size, repeats, p):

    ### plot histogram of Average value of normalised Binomial Distributions

    sample_value = [0.] * repeats
    num_bins = 35
    for i in range(repeats):

        ######
        ## Step 1 - create sample of independent uniform random variables

        lower_bound = 0.
        upper_bound = 1.
        uni_sample = np.random.uniform(lower_bound, upper_bound, sample_size)

        ######
        ## Step 2 - transform them to $B(1,p)$ distribution

        sample = [dist.binomial_inverse_cdf(p, u) for u in uni_sample]
        sample_value[i] = (sum(sample) - sample_size * p) / np.sqrt(
            p * (1 - p)) / sample_size

    mp = pu.PlotUtilities(
        "Histogram of Normalised Binomial Average For Sample of Size={0}".
        format(sample_size), 'Outcome', 'Rel. Occurrence')
    mp.plotHistogram(sample_value, num_bins)
Esempio n. 3
0
def binomial_histogram(p, sz):

    sample = [dist.binomial_inverse_cdf(p, u) for u in getUniformSample(sz)]
    num_bins = 100

    hp = pu.PlotUtilities(
        "Histogram of Binomial Sample with Success Probability={0}".format(p),
        'Outcome', 'Rel. Occurrence')
    hp.plotHistogram(sample, num_bins)
Esempio n. 4
0
def binomial_histogram(p, sz):

    lower_bound = 0.
    upper_bound = 1.

    uni_sample = np.random.uniform(lower_bound, upper_bound, sz)

    #######
    ### transform the uniform sample
    #######
    sample = range(sz)
    for j in range(sz):
        sample[j] = dist.binomial_inverse_cdf(p, uni_sample[j])

    num_bins = 100

    hp = pu.PlotUtilities(
        "Histogram of Binomial Sample with Success Probability={0}".format(p),
        'Outcome', 'Rel. Occurrence')
    hp.plotHistogram(sample, num_bins)
Esempio n. 5
0
def binomial_clt_hist(sample_size, repeats, p):

    ### plot histogram of Average value of normalised Binomial Distributions

    sample_value = [0.] * repeats
    num_bins = 35
    for i in range(repeats):

        ######
        ## Step 1 - create sample of independent uniform random variables

        lower_bound = 0.
        upper_bound = 1.
        uni_sample = np.random.uniform(lower_bound, upper_bound, sample_size)

        ######
        ## Step 2 - transform them to $B(1,p)$ distribution

        sample = [dist.binomial_inverse_cdf(p, u) for u in uni_sample]
        sample_value[i] = (sum(sample) - sample_size * p) / np.sqrt(
            sample_size * p * (1 - p))

    plt.xlabel('Outcome')
    plt.ylabel('Relative Occurrence')
    plt.title(
        "Histogram of Normalised Binomial Average For Sample of Size={0}".
        format(sample_size))

    _n, bins, _hist = plt.hist(sample_value,
                               num_bins,
                               normed=True,
                               facecolor='green',
                               alpha=0.75)

    y = [dist.standard_normal_pdf(b) for b in bins]
    plt.plot(bins, y, 'r--')
    plt.show()
    plt.close()