Skip to content

Instantly share code, notes, and snippets.

@terakun
Created December 26, 2018 10:27
Show Gist options
  • Save terakun/33e50b17fda516987bca34c5d561caac to your computer and use it in GitHub Desktop.
Save terakun/33e50b17fda516987bca34c5d561caac to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.stats import t as student_t
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def student(n):
samples = np.random.normal(0,1,n)
mean = np.mean(samples)
unbiased_var = np.var(samples, ddof=1)
return np.sqrt(n/unbiased_var)*mean
n = 100
sample_size = 200000
samples = [student(n) for i in range(0,sample_size)]
fig = plt.figure()
plt.xlim([-10,10])
plt.title("$n="+str(n)+"$")
plt.xlabel("$x$")
plt.ylabel("$p(x)$")
plt.hist(samples,bins=2000,normed=True,range=(-100,100),label="histogram")
dist = student_t(n-1,0)
x = np.linspace(-10,10,200)
plt.plot(x, dist.pdf(x), alpha=0.7,label="p.d.f.")
plt.legend()
plt.show()
with PdfPages('n'+str(n)+'.pdf') as pp:
# save figure
pp.savefig(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment