Caleb Fangmeier лет назад: 6
Родитель
Сommit
650d63fb7c
1 измененных файлов с 9 добавлено и 9 удалено
  1. 9 9
      HW2/p1.py

+ 9 - 9
HW2/p1.py

@@ -9,19 +9,15 @@ 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)
+    a = 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)
+    x = poisson(lam=a)
 
-    # 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))
+    # Finally, count for how many experiments the a lies in
+    # the range [x-sqrt(x), x+sqrt(x)]
+    n_pass = sum((x - sqrt(x) < a) & (a < x + sqrt(x)))
     relative_frequency = n_pass / n_experiments
 
     # and print the results
@@ -30,3 +26,7 @@ def find_relative_frequency(n_experiments, b):
 
 find_relative_frequency(10000, 5)
 find_relative_frequency(10000, 10)
+
+# Example output
+# > For b=5, 6012/10000 passed, R=60.12%
+# > For b=10, 6372/10000 passed, R=63.72%