def compare_two_samples_from_beta(sample_1, sample_2, significance_level=0.95, plot=True): """ Compare two samples from Beta distribution """ if plot: hist(sample_1, 100, alpha=0.5); hist(sample_2, 100, alpha=0.5); show() sample_1_lower_bound, sample_1_upper_bound = get_confidence_intervals_using_the_quantiles( sample_1, significance_level) sample_2_lower_bound, sample_2_upper_bound = get_confidence_intervals_using_the_quantiles( sample_2, significance_level) if sample_1_lower_bound > sample_2_upper_bound or sample_2_lower_bound > sample_1_upper_bound: return 'Samples are not equal' else: return 'Samples are equal'
def bootstrap_confidence_intervals(a_list_of_values, bootstrapping_resamples=1000): """ Bootstrapping confidence intervals """ def sample_wr(population, k): """ Chooses k random elements (with replacement) from a population """ n = len(population) _random, _int = random, int result = [None] * k for i in xrange(k): j = _int(_random() * n) result[i] = population[j] return result # Bootstrapping lower_bounds, upper_bounds = [], [] for _ in range(bootstrapping_resamples): # create resample with the same size as the original resample = sample_wr(a_list_of_values, len(a_list_of_values)) # get confidence interval resample_lower_bound, resample_upper_bound = get_confidence_intervals_using_the_quantiles( resample) # append to results lower_bounds.append(resample_lower_bound) upper_bounds.append(resample_upper_bound) mean_lower_bound, mean_upper_bound = mean(lower_bounds), mean(upper_bounds) return mean_lower_bound, mean_upper_bound
def bootstrap_confidence_intervals(a_list_of_values, bootstrapping_resamples=1000): """ Bootstrapping confidence intervals """ def sample_wr(population, k): """ Chooses k random elements (with replacement) from a population """ n = len(population) _random, _int = random, int result = [None] * k for i in xrange(k): j = _int(_random() * n) result[i] = population[j] return result # Bootstrapping lower_bounds, upper_bounds = [], [] for _ in range(bootstrapping_resamples): # create resample with the same size as the original resample = sample_wr(a_list_of_values, len(a_list_of_values)) # get confidence interval resample_lower_bound, resample_upper_bound = get_confidence_intervals_using_the_quantiles(resample) # append to results lower_bounds.append(resample_lower_bound) upper_bounds.append(resample_upper_bound) mean_lower_bound, mean_upper_bound = mean(lower_bounds), mean(upper_bounds) return mean_lower_bound, mean_upper_bound
def compare_two_samples_from_beta(sample_1, sample_2, significance_level=0.95, plot=True): """ Compare two samples from Beta distribution """ if plot: hist(sample_1, 100, alpha=0.5) hist(sample_2, 100, alpha=0.5) show() sample_1_lower_bound, sample_1_upper_bound = get_confidence_intervals_using_the_quantiles( sample_1, significance_level) sample_2_lower_bound, sample_2_upper_bound = get_confidence_intervals_using_the_quantiles( sample_2, significance_level) if sample_1_lower_bound > sample_2_upper_bound or sample_2_lower_bound > sample_1_upper_bound: return 'Samples are not equal' else: return 'Samples are equal'
def main(): """ BayesianStatistics.py """ data = create_random_sample_from_beta(10, 100, plot=True) print 'Confidence Interval:', get_confidence_intervals_using_the_quantiles(data) sample_1 = create_random_sample_from_beta(100, 10000) sample_2 = create_random_sample_from_beta(100, 10000) print 'Comparing equal samples:', compare_two_samples_from_beta(sample_1, sample_2) sample_1 = create_random_sample_from_beta(150, 10000) sample_2 = create_random_sample_from_beta(100, 10000) print 'Comparing unequal samples:', compare_two_samples_from_beta(sample_1, sample_2)
def main(): """ BayesianStatistics.py """ data = create_random_sample_from_beta(10, 100, plot=True) print 'Confidence Interval:', get_confidence_intervals_using_the_quantiles( data) sample_1 = create_random_sample_from_beta(100, 10000) sample_2 = create_random_sample_from_beta(100, 10000) print 'Comparing equal samples:', compare_two_samples_from_beta( sample_1, sample_2) sample_1 = create_random_sample_from_beta(150, 10000) sample_2 = create_random_sample_from_beta(100, 10000) print 'Comparing unequal samples:', compare_two_samples_from_beta( sample_1, sample_2)