I'm reading the Statistics (Freeman, Pisani, Purves) book and I'm trying to reproduce an example where a coin is tossed say 50 times, the number of heads counted and this is repeated say 1,000 times.
First, I kept the number of tosses (sample size) at 1000 and increased the repetitions. The more repetitions, the better the data fit the normal curve.
So next, I tried keeping the number of repetitions fixed at 1,000 and increased the sample size. The larger the sample size, the worst the normal curved seemed to fit the data. This seems to be contradictory to the book example which better approximates the normal curve as the sample size increases.
I wanted to see what would happen if I increased the sample size, but with a larger number of repetitions which were fixed at 10,000. This seems to also contradict the book.
Any ideas what I'm doing wrong?
Code and graphs below.
%matplotlib inline
def plot_hist(num_repetitions, num_tosses):
tosses = np.random.randint(0, 2, size=[num_repetitions, num_tosses])
sums = np.apply_along_axis(lambda a: np.sum(a == 1), 1, tosses)
xmin, xmax = min(sums), max(sums)
lnspc = np.linspace(xmin, xmax, len(sums))
m, s = stats.norm.fit(sums) # get mean and standard deviation
pdf_g = stats.norm.pdf(lnspc, m, s) # now get theoretical values in our interval
bins = np.arange(xmin, xmax) - 0.5
step = int((xmax - xmin)/5)
fig, ax = plt.subplots()
_ = ax.hist(sums, bins, edgecolor='black', linewidth=1.2, density=True)
_ = ax.plot(lnspc, pdf_g, label="Norm", color='red')
_ = ax.set_xticks(bins[::step] + 0.5)
_ = ax.set_title('{:,} tosses - {:,} repetitions'.format(num_tosses, num_repetitions))
1. Experiment with increasing number of repetitions (fixed sample size of 1000)
plot_hist(1000, 1000)
plot_hist(10000, 1000)
plot_hist(100000, 1000)
2. Experiment with increasing sample size (fixed at 1000 repetitions)
plot_hist(1000, 100)
plot_hist(1000, 1000)
plot_hist(1000, 10000)
3. Experiment with increasing sample size (fixed at 10,000 repetitions)
plot_hist(10000, 100)
plot_hist(10000, 1000)
plot_hist(10000, 10000)
plot_hist(10000, 100000)