Forráskód Böngészése

Adds HW2 P1 solution

Caleb Fangmeier 6 éve
commit
b082335ff6
3 módosított fájl, 36 hozzáadás és 0 törlés
  1. 3 0
      .gitignore
  2. 32 0
      HW2/p1.py
  3. 1 0
      requirements.txt

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.pyc
+
+env/

+ 32 - 0
HW2/p1.py

@@ -0,0 +1,32 @@
+#!/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)

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+numpy