forked from vischia/statex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centralllimit.py
68 lines (50 loc) · 2.04 KB
/
centralllimit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Author: Jake VanderPlas
# License: BSD
# The figure produced by this code is published in the textbook
# "Statistics, Data Mining, and Machine Learning in Astronomy" (2013)
# For more information, see http://astroML.github.com
# To report a bug or issue, use the following forum:
# https://groups.google.com/forum/#!forum/astroml-general
# Modified by Pietro Vischia for use in a Statistics Course (to make the number of generated data configurable, and to hide the predefined values in the M array, and to make nomenclature consistent with the slides (N-->M) )
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
#------------------------------------------------------------
# Generate the uniform samples
# N is the number of random variables that are averaged
# i.e. Q = \sum_{i=1}^{M} x_i
M = [..., ..., ...]
# Number of data to generate
Ndata = int(...)
np.random.seed(42)
x = np.random.random((max(M), Ndata))
#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(5, 5))
fig.subplots_adjust(hspace=0.05)
for i in range(len(M)):
ax = fig.add_subplot(3, 1, i + 1)
# take the mean of the first N[i] samples
x_i = x[:M[i], :].mean(0)
# histogram the data
ax.hist(x_i, bins=np.linspace(0, 1, 101),
histtype='stepfilled', alpha=0.5, normed=True)
# plot the expected gaussian pdf
mu = 0.5
sigma = 1. / np.sqrt(12 * M[i])
dist = norm(mu, sigma)
x_pdf = np.linspace(-0.5, 1.5, 1000)
ax.plot(x_pdf, dist.pdf(x_pdf), '-k')
ax.set_xlim(0.0, 1.0)
ax.set_ylim(0.001, None)
ax.xaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.yaxis.set_major_locator(plt.MaxNLocator(5))
ax.text(0.99, 0.95, r"$M = %i$" % M[i],
ha='right', va='top', transform=ax.transAxes)
if i == len(M) - 1:
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.4f'))
ax.set_xlabel(r'$x$')
else:
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.set_ylabel('$p(x)$')
plt.show()