Path: blob/master/experiments/distributions.py
1700 views
# -*- coding: utf-8 -*-1"""2Created on Sat Jan 17 20:17:14 201534@author: rlabbe5"""67from scipy.stats import t, norm8import matplotlib.pyplot as plt9import numpy as np10import math11import random121314df =415mu = 1016std = 21718x = np.linspace(-5, 20, 100)1920plt.plot(x, t.pdf(x, df=df, loc=mu, scale=std), 'r-', lw=5, label='t pdf')212223x2 = np.linspace(mu-10, mu+10, 100)24plt.plot(x, norm.pdf(x, mu, std), 'b-', lw=5, label='gaussian pdf')25plt.legend()26plt.figure()2728def student_t(df, mu, std): # nu equals number of degrees of freedom29x = random.gauss(0, std)30y = 2.0*random.gammavariate(0.5*df, 2.0)31return x / (math.sqrt(y/df)) + mu323334N = 10000035ys = [student_t(2.7, 100, 2) for i in range(N)]36plt.hist(ys, 10000, histtype='step')3738ys = [random.gauss(100,2) for i in range(N)]39plt.hist(ys, 10000, histtype='step',color='r')4041plt.show()4243