CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sat Jan 17 20:17:14 2015
4
5
@author: rlabbe
6
"""
7
8
from scipy.stats import t, norm
9
import matplotlib.pyplot as plt
10
import numpy as np
11
import math
12
import random
13
14
15
df =4
16
mu = 10
17
std = 2
18
19
x = np.linspace(-5, 20, 100)
20
21
plt.plot(x, t.pdf(x, df=df, loc=mu, scale=std), 'r-', lw=5, label='t pdf')
22
23
24
x2 = np.linspace(mu-10, mu+10, 100)
25
plt.plot(x, norm.pdf(x, mu, std), 'b-', lw=5, label='gaussian pdf')
26
plt.legend()
27
plt.figure()
28
29
def student_t(df, mu, std): # nu equals number of degrees of freedom
30
x = random.gauss(0, std)
31
y = 2.0*random.gammavariate(0.5*df, 2.0)
32
return x / (math.sqrt(y/df)) + mu
33
34
35
N = 100000
36
ys = [student_t(2.7, 100, 2) for i in range(N)]
37
plt.hist(ys, 10000, histtype='step')
38
39
ys = [random.gauss(100,2) for i in range(N)]
40
plt.hist(ys, 10000, histtype='step',color='r')
41
42
plt.show()
43