#!/usr/bin/env python3 """ LPC stats HW2, Problem 1 Author: Caleb Fangmeier Created: Sep. 25, 2017 """ from numpy import sum, sqrt from numpy.random import exponential, poisson def find_relative_frequency(n_experiments, b): # First get the mean counts for each experiment mean_counts = exponential(scale=b, size=n_experiments) # Since this is a counting experiment, sample from a Poisson distribution with the # previously generated means. single_counts = poisson(lam=mean_counts) # Generate the per-experiment bounds based on the observed single_counts bound_low = single_counts - sqrt(single_counts) bound_high = single_counts + sqrt(single_counts) # Finally, count for how many experiments the mean_count lies in the range # calculated above. n_pass = sum((bound_low < mean_counts) & (mean_counts < bound_high)) relative_frequency = n_pass / n_experiments # and print the results print(f"For b={b}, {n_pass}/{n_experiments} passed, R={relative_frequency*100:4.2f}%") find_relative_frequency(10000, 5) find_relative_frequency(10000, 10)