#%% import numpy as np import matplotlib.pyplot as plt p, trials = 0.70, 30 # probability of success & number of trials heads, laplace, mean = [], [], [] for n in range(1, trials+1): h = (np.random.rand(n) < p).sum() # no. of success in n trials lp = (h+1)/(n+2) laplace.append(lp) # record laplace's rule m = h/n # mean number of heads in the n trials mean.append(m) # record the mean no. of success in n trials # plotting the results using this guide: # https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html width, location = 0.35, np.arange(len(mean)) plt.rcParams.update({'font.size': 6}) plt.figure(figsize=(32,18)) fig, ax = plt.subplots() rect1 = ax.bar(location - width/2, laplace, width, label='laplace') rect2 = ax.bar(location + width/2, mean, width, label='mean') # configurate how the graph looks ax.set_xticks(location) ax.set_xticklabels(location+1) ax.set_xlabel('number of trials') ax.set_ylabel('estimate of p') ax.set_title('true p = 0.70') ax.legend() # uncomment to save the plot # plt.savefig('fig.png', facecolor="w", bbox_inches='tight', dpi=300) plt.show() # %%