p1.py 1.1 KB

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