Consider flipping nn times a coin. The probability for heads isgiven by pp where pp is some parameter which can be chosen from theinterval (0,1)(0,1).
Write a Python code to simulate nn coin flips with headsprobability pp and compute the running proportion of heads X¯nX¯nfor nn running from 1 to 1,000 trials. Plot your results. Your plotshould illustrate how the proportion of heads appears to convergeto pp as nn approaches 1,000.
In [ ]:
### Insert your code here for simulating the coin flips and for computing the average
?
?
In [2]:
### Complete the plot commands accordingly for also plotting the computed running averages in the graph below
?
p = 0.25 # just an example
?
plt.figure(figsize=(10,5))
plt.title("Proportion of heads in 1,000 coin flips")
plt.plot(np.arange(1000),p*np.ones(1000),'-',color="red",label="true probability")
plt.xlabel("Number of coin flips")
plt.ylabel("Running average")
plt.legend(loc="upper right")