-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
70 lines (58 loc) · 1.56 KB
/
example.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
69
70
import tensorflow as tf
import numpy as np
from truncatedDistribution import TruncatedDistribution as TD
tf.InteractiveSession()
concentration=40.
rate=4.
gamma=tf.distributions.Gamma(concentration,rate)
left=9.
right=30.
tG=TD(gamma,left,right)
samples=tG.sample(1000).eval()
samples_org=gamma.sample(1000).eval()
import matplotlib.pyplot as plt
import seaborn as sn
sn.set()
f,(ax1,ax2)=plt.subplots(1,2)
ax1.hist(samples)
ax1.set_xlim(left=3,right=16)
ax1.set_title("$\Gamma(40,4)$ truncated at $9$.")
ax2.hist(samples_org)
ax2.set_xlim(left=3,right=16)
ax2.set_title("$\Gamma(40,4)$")
plt.show()
print(tG.mean().eval())
print(tG.variance().eval())
print(gamma.mean().eval())
print(gamma.variance().eval())
a=2.
b=5.
beta=tf.distributions.Beta(a,b)
left=0.35
right=1.
tB=TD(beta,left,right)
X=np.linspace(0,1,100,dtype=np.float32)
Y1=tB.cdf(X).eval()
Y2=beta.cdf(X).eval()
f,(ax1,ax2)=plt.subplots(1,2)
ax1.plot(X,Y1)
ax1.set_xlim(left=0,right=1)
ax1.set_title("$Beta(2,5)$ truncated at $0.35$.")
ax2.plot(X,Y2)
ax2.set_xlim(left=0,right=1)
ax2.set_title("$Beta(2,5)$")
plt.show()
print(tB.mean().eval())
print(tB.variance().eval())
print(beta.mean().eval())
print(beta.variance().eval())
concentration=np.array([10.,11.],dtype=np.float32)
rate=np.array([4.],dtype=np.float32)
gamma=tf.distributions.Gamma(concentration,rate)
right=np.array([1.,0.5,0.7],dtype=np.float32).reshape(3,1)
left=np.array([0.1,0.2,0.3],dtype=np.float32).reshape(3,1)
tG=TD(gamma,left,right)
single_sample=tG.sample().eval()
print(single_sample.shape)
samples=tG.sample(sample_shape=(5,4))
print(samples.shape)