<jupyter_start><jupyter_text>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      Solutions for Coin Flip: A Probabilistic Bit  Task 1: Simulating FairCoin in PythonFlip a fair coin 100 times. Calculate the total number of heads and tails, and then check the ratio of the number of heads and the number of tails.Do the same experiment 1000 times.Do the same experiment 10,000 times.Do the same experiment 100,000 times.Do your results get close to the ideal case (the numbers of heads and tails are equal)? Solution<jupyter_code>from random import randrange

for experiment in [100,1000,10000,100000]:
    heads = tails = 0
    for i in range(experiment):
        if randrange(2) == 0: heads = heads + 1
        else: tails = tails + 1
    print("experiment:",experiment)
    print("heads =",heads,"  tails = ",tails)
    print("the ratio of #heads/#tails is",(round(heads/tails,4)))
    print() # empty line<jupyter_output><empty_output><jupyter_text>Task 2: Simulating BiasedCoin in PythonFlip the following biased coin 100 times. Calcuate the total numbers of heads and tails, and then check the ratio of the number of heads and the number of tails.$BiasedCoin = \begin{array}{c|cc} \hookleftarrow & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & 0.6 & 0.6 \\  \mathbf{Tail} & 0.4 & 0.4  \end{array}$Do the same experiment 1000 times.Do the same experiment 10,000 times.Do the same experiment 100,000 times.Do your results get close to the ideal case $ \mypar{ \dfrac{ \mbox{ of heads} }{ \mbox{ of tails} } = \dfrac{0.6}{0.4} = 1.50000000 } $? Solution<jupyter_code>from random import randrange

# let's pick a random number between {0,1,...,99}
# it is expected to be less than 60 with probability 0.6
#     and greater than or equal to 60 with probability 0.4

for experiment in [100,1000,10000,100000]:
    heads = tails = 0
    for i in range(experiment):
        if randrange(100) <60: heads = heads + 1 # with probability 0.6
        else: tails = tails + 1 # with probability 0.4
    print("experiment:",experiment)
    print("heads =",heads,"  tails = ",tails)
    print("the ratio of #heads/#tails is",(round(heads/tails,4)))
    print() # empty line<jupyter_output><empty_output><jupyter_text>Task 3 Write a function to implement the described biased coin,The inputs are integers $N>0$ and $ B \in \{0,\ldots,N\} $.The output is either "Heads" or "Tails". Solution<jupyter_code>def biased_coin(N,B):
    from random import randrange
    random_number = randrange(N)
    if random_number < B:
        return "Heads"
    else:
        return "Tails"<jupyter_output><empty_output><jupyter_text>Task 4We use the biased coin described in Task 3. (You may use the function given in the solution.)We pick $ N $ as 101.Our task is to determine the value of $ B $ experimentially without checking its value directly.Flip the (same) biased coin 500 times, collect the statistics, and then guess the bias.Compare your guess with the actual bias by calculating the relative error in percentage (the absolute value of the difference divided by the real bias). Solution<jupyter_code>def biased_coin(N,B):
    from random import randrange
    random_number = randrange(N)
    if random_number < B:
        return "Heads"
    else:
        return "Tails"
from random import randrange
N = 101
B = randrange(N+1)
total_tosses = 500
the_number_of_heads = 0
for i in range(total_tosses):
    if biased_coin(N,B) == "Heads":
        the_number_of_heads = the_number_of_heads + 1

my_guess =  the_number_of_heads/total_tosses        
real_bias = B/N
error = abs(my_guess-real_bias)/real_bias*100 

print("my guess is",my_guess)
print("real bias is",real_bias)
print("error (%) is",error)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup