forked from graykode/distribution-is-all-you-need
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent-t.py
33 lines (25 loc) · 816 Bytes
/
student-t.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
"""
Code by Tae-Hwan Hung(@graykode)
https://en.wikipedia.org/wiki/Student%27s_t-distribution
"""
import numpy as np
from matplotlib import pyplot as plt
def gamma_function(n):
cal = 1
for i in range(2, n):
cal *= i
return cal
def student_t(x, freedom, n):
# divide [x.min(), x.max()] by n
x = np.linspace(x.min(), x.max(), n)
c = gamma_function((freedom + 1) // 2) \
/ np.sqrt(freedom * np.pi) * gamma_function(freedom // 2)
y = c * (1 + x**2 / freedom) ** (-((freedom + 1) / 2))
return x, y, np.mean(y), np.std(y)
for freedom in [1, 2, 5]:
x = np.arange(-10, 10) # define range of x
x, y, _, _ = student_t(x, freedom=freedom, n=10000)
plt.plot(x, y, label=r'$v=%d$' % (freedom))
plt.legend()
plt.savefig('graph/student_t.png')
plt.show()