
import sys, math, random
import HypoTesting

print("1 sided hypothesis testing:\n")

n = int(input("Number of (Bernoulli) experiments = "))
p = float(input("Probability of success = "))


mu_0, sigma_0 = HypoTesting.normal_approximation_to_binomial(n, p)
print(f"Mu and sigma to approximate bin({n},{p}) = ", mu_0, sigma_0)

conf = float(input("\nConfidence (0.95 or 0.99) = "))

lessTest = input("Enter L for H0: p <= {p} and R for H0: p >= {p}: ")



# Imagine instead that our null hypothesis was that the coin is 
# not biased toward heads, or that  p <= 0.5

# In that case we want a **one-sided test** that rejects 
# the null hypothesis when X is much larger than 50

# A 5%-significance test involves using normal_probability_below 
# to find the cutoff below which 95% of the probability lies:

if lessTest == 'L':
   hi = HypoTesting.normal_upper_bound(conf, mu_0, sigma_0)
   
   print(f">>>> normal_upper_bound({conf}) = {hi}")
   print( ">>>>> ", f"{conf*100}% of the time, values will fall in this range")

   # Perform the experiment 100000 times:
   count = 0
   for _ in range(100000):
       num_heads = sum(1 if random.random() < p else 0    # Count # of heads
                       for _ in range(n))                # in 1000 flips

       if num_heads <= hi:                       # Count how often
           count += 1                            # the experiment fall in range

   print(f"\nExperimental prob Count <= {hi} = ", count / 100000)  
else:
   lo = HypoTesting.normal_lower_bound(conf, mu_0, sigma_0)
   
   print(f">>>> normal_lower_bound({conf}) = {lo}")
   print( ">>>>> ", f"{conf*100}% of the time, values will fall in this range")

   # Perform the experiment 100000 times:
   count = 0
   for _ in range(100000):
       num_heads = sum(1 if random.random() < p else 0    # Count # of heads
                       for _ in range(n))                # in 1000 flips

       if num_heads >= lo:                       # Count how often
           count += 1                            # the experiment fall in range

   print(f"\nExperimental prob Count >= {lo} = ", count / 100000)  
