mannjain commited on
Commit
985786b
·
1 Parent(s): 2f33cb0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +1140 -0
app.py ADDED
@@ -0,0 +1,1140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ import tensorflow as tf
5
+ import tensorflow_probability as tfp
6
+ from math import sqrt
7
+ from scipy import stats
8
+ import pandas as pd
9
+
10
+ tfd = tfp.distributions
11
+ tfl = tfp.layers
12
+
13
+ st.title("Probability Distributions")
14
+ add_selectbox = st.sidebar.selectbox(
15
+ 'Choose an Option',
16
+ ('Discrete Univariate', 'Continuous Univariate')
17
+ )
18
+
19
+
20
+
21
+ # st.title("1 dimensional normal distribution")
22
+
23
+ def Sum(p1val,p2val,zval):
24
+ l = int(max(max(zval), len(zval)))
25
+ l=l*2
26
+ s=[0]*l
27
+ for i in range(len(zval)):
28
+ for j in range(len(zval)):
29
+ s[int(zval[i]+zval[j])]+=p1val[i]*p2val[j]
30
+ return s
31
+ def Product(p1val,p2val,zval):
32
+ l=int(max(max(zval),len(zval)))
33
+ l=l**2
34
+ s=[0]*l
35
+ for i in range(len(zval)):
36
+ for j in range(len(zval)):
37
+ s[int(zval[i]*zval[j])]+=p1val[i]*p2val[j]
38
+ return s
39
+
40
+ def Normal():
41
+ st.header("Normal distribution")
42
+ p = tfd.Normal(2, 1)
43
+ mean = st.slider('Mean', -5, 5, 0)
44
+ std = st.slider('Scale', 0, 5, 1)
45
+ z = f"""\\begin{{array}}{{cc}}
46
+ \mu &= {mean} \\\\
47
+ \sigma &= {std}
48
+ \\end{{array}}
49
+ """
50
+ st.latex(z)
51
+ st1 = r'''
52
+ mean= \[\mu\]
53
+
54
+ Variance = \[ \sigma ^2\]
55
+
56
+ Entropy = \[ \frac{1}{2}\log (2 \pi \sigma ^2) + \frac{1}{2} \]
57
+ '''
58
+
59
+ st.latex(st1)
60
+ q=tfd.Normal(mean,std)
61
+ z_values = tf.linspace(-5, 5, 200)
62
+ z_values = tf.cast(z_values, tf.float32)
63
+ prob_values_p = p.prob(z_values)
64
+ prob_values_q = q.prob(z_values)
65
+
66
+ fig, ax = plt.subplots()
67
+ ax.plot(z_values, prob_values_p, label=r'Distribution with unknown parameter', linestyle='--', lw=5, alpha=0.5)
68
+ ax.plot(z_values, prob_values_q, label=r'Distribution with given parameters')
69
+
70
+ ax.set_xlabel("x")
71
+ ax.set_ylabel("PDF(x)")
72
+ ax.legend()
73
+ ax.set_ylim((0, 1))
74
+
75
+ st.pyplot(fig)
76
+ kl = tfd.kl_divergence(q, p)
77
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
78
+
79
+ st.subheader("Sum of two Normal Distributions")
80
+ z_values1 = tf.linspace(-10, 10, 21)
81
+ z_values1 = tf.cast(z_values1, tf.float32)
82
+
83
+ mean1 = st.slider('Mean1', -5, 5, 0)
84
+ std1 = st.slider('Std1', 0, 5, 1)
85
+ mean2 = st.slider('Mean2', -5, 5, 0)
86
+ std2 = st.slider('Std2', 0, 5, 1)
87
+ q1=tfd.Normal(mean1,std1)
88
+ q2=tfd.Normal(mean2,std2)
89
+ prob_values_q1 = list(q1.prob(z_values1))
90
+ prob_values_q2 = list(q2.prob(z_values1))
91
+
92
+ fig2, (ax2,ax3) = plt.subplots(1,2)
93
+ ax2.plot(z_values1, prob_values_q1, label=r'Normal(mean1,std1)')
94
+ ax3.plot(z_values1, prob_values_q2, label=r'Normal(mean2,std2)')
95
+
96
+ ax2.set_xlabel("x")
97
+ ax2.set_ylabel("PDF(x)")
98
+ ax2.set_title("Normal(mean1,std1)")
99
+ ax2.set_ylim((0, 1))
100
+
101
+ ax3.set_xlabel("x")
102
+ ax3.set_ylabel("PDF(x)")
103
+ ax3.set_title("Normal(mean2,std2)")
104
+ ax3.set_ylim((0, 1))
105
+
106
+ st.pyplot(fig2)
107
+
108
+ prob_values_sum = Sum(prob_values_q1, prob_values_q2, z_values1)
109
+ q3 = tfd.Normal(mean1+mean2, sqrt(((std1)**2 + (std2)**2)))
110
+ prob_values_q3 = q3.prob(range(len(prob_values_sum)))
111
+
112
+ fig3, ax4 = plt.subplots()
113
+ ax4.plot(range(len(prob_values_sum)), prob_values_sum, label=r'Normal(mean1,std1)+Normal(mean2,std2)', linestyle='--', lw=5, alpha=0.5)
114
+ ax4.plot(range(len(prob_values_sum)), prob_values_q3, label=r'Normal(mean1+mean2, sqrt((std1^2 + std2^2)')
115
+
116
+ ax4.set_xlabel("x")
117
+ ax4.set_ylabel("PDF(x)")
118
+ ax4.legend()
119
+ ax4.set_ylim((0, 1))
120
+
121
+ st.pyplot(fig3)
122
+ st.markdown("Sum of two Normal distributions yields a Normal distribution")
123
+
124
+ st.subheader("Relationship between Poisson and Normal Distribution")
125
+ rate3 = st.slider('lambda1', 100, 500, 250, 50)
126
+ q4 = tfd.Poisson(rate=rate3)
127
+ z_values1 = tf.linspace(0, 600, 601)
128
+ z_values1 = tf.cast(z_values1, tf.float32)
129
+ prob_values_q4 = list(q4.prob(z_values1))
130
+ q5 = tfd.Normal(rate3, sqrt(rate3))
131
+ prob_values_q5 = list(q5.prob(z_values1))
132
+
133
+ fig4, ax5 = plt.subplots()
134
+ ax5.stem(z_values1, prob_values_q4, label=r'Poisson(lambda1)]', linefmt='r', markerfmt='ro')
135
+ ax5.plot(z_values1, prob_values_q5, label=r'Normal(lamda1,sqrt(lambda1)', lw=3)
136
+ ax5.set_xlabel("x")
137
+ ax5.set_ylabel("PDF(x)")
138
+ ax5.legend()
139
+ ax5.set_ylim((0, 0.1))
140
+ st.pyplot(fig4)
141
+ st.markdown("For large values of lambda, Poisson(lambda) becomes approximately a normal distribution having mean= lambda and variance= lambda")
142
+
143
+ def Exponential():
144
+ st.subheader("Exponential distribution")
145
+ p = tfd.Exponential(2)
146
+ rate = st.slider('Lambda', 1, 5, 1)
147
+ cdf = r'''
148
+ cdf = $\int_a^b f(x)dx$
149
+ '''
150
+
151
+ st.latex(cdf)
152
+ q = tfd.Exponential(rate)
153
+ z_values = tf.linspace(-5, 5, 200)
154
+ z_values = tf.cast(z_values, tf.float32)
155
+ prob_values_p = p.prob(z_values)
156
+ prob_values_q = q.prob(z_values)
157
+
158
+ fig, ax = plt.subplots()
159
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
160
+ ax.plot(z_values, prob_values_q, label=r'q')
161
+
162
+ ax.set_xlabel("x")
163
+ ax.set_ylabel("PDF(x)")
164
+ ax.legend()
165
+ ax.set_ylim((0, 1))
166
+
167
+ st.pyplot(fig)
168
+ kl = tfd.kl_divergence(q, p)
169
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
170
+
171
+ st.subheader("Relationship between Gamma and Exponential Distribution")
172
+ alpha1 = 1
173
+ st.write("alpha1=1")
174
+ beta1 = st.slider('beta1', 0.0, 10.0, 5.0, 0.5)
175
+ q2 = tfd.Gamma(concentration=alpha1, rate=beta1)
176
+ prob_values_q2 = list(q2.prob(z_values))
177
+ q3 = tfd.Exponential(beta1)
178
+ prob_values_q3 = list(q3.prob(z_values))
179
+ fig3, ax3 = plt.subplots()
180
+ ax3.plot(z_values, prob_values_q2, linestyle='--', lw=3, alpha=0.5, label=r'Gamma(1,beta)')
181
+ ax3.plot(z_values, prob_values_q3, label=r'Exponential(beta)')
182
+
183
+ ax3.set_xlabel("x")
184
+ ax3.set_ylabel("PDF(x)")
185
+ ax3.legend()
186
+ # ax3.set_ylim((0, 1))
187
+ st.pyplot(fig3)
188
+
189
+
190
+ def Uniform():
191
+ st.subheader("Uniform distribution")
192
+ p = tfd.Uniform(0,1)
193
+ low = st.slider('low', 0, 5, 1)
194
+ high = st.slider('high', 1, 6, 1)
195
+
196
+ cdf = r'''
197
+ cdf = $\int_a^b f(x)dx$
198
+ '''
199
+
200
+ st.latex(cdf)
201
+ q = tfd.Uniform(low,high)
202
+ z_values = tf.linspace(-5, 5, 200)
203
+ z_values = tf.cast(z_values, tf.float32)
204
+ prob_values_p = p.prob(z_values)
205
+ prob_values_q = q.prob(z_values)
206
+
207
+ fig, ax = plt.subplots()
208
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
209
+ ax.plot(z_values, prob_values_q, label=r'q')
210
+
211
+ ax.set_xlabel("x")
212
+ ax.set_ylabel("PDF(x)")
213
+ ax.legend()
214
+ ax.set_ylim((0, 1))
215
+
216
+ st.pyplot(fig)
217
+ kl = tfd.kl_divergence(q, p)
218
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
219
+ st.subheader("Relationship between Beta Distribution and Uniform Distribution")
220
+
221
+ st.write("beta = alpha= 1")
222
+ q4 = tfd.Beta(1, 1)
223
+ q5 = tfd.Uniform(0, 1)
224
+ z_values1 = tf.linspace(0, 1, 200)
225
+ z_values1 = tf.cast(z_values1, tf.float32)
226
+ prob_values_q4 = list(q4.prob(z_values1))
227
+ prob_values_q5 = list(q5.prob(z_values1))
228
+
229
+ fig3, ax3 = plt.subplots()
230
+ ax3.plot(z_values1, prob_values_q4, label=r'Beta(1,1)', linestyle='--', lw=3)
231
+ ax3.plot(z_values1, prob_values_q5, label=r'Uniform(0,1)')
232
+
233
+ ax3.set_xlabel("x")
234
+ ax3.set_ylabel("PDF(x)")
235
+ ax3.legend()
236
+ # ax3.set_ylim((0, 1))
237
+
238
+ st.pyplot(fig3)
239
+
240
+
241
+ def Cauchy():
242
+ st.subheader("Cauchy distribution")
243
+ p = tfd.Cauchy(0, 0.5)
244
+ loc = st.slider('location', 0.0, 5.0, 1.0, 0.5)
245
+ sc = st.slider('scale', 0.0, 5.0, 1.0, 0.5)
246
+
247
+ cdf = r'''
248
+ cdf = $\int_a^b f(x)dx$
249
+ '''
250
+
251
+ st.latex(cdf)
252
+ q = tfd.Cauchy(loc, sc)
253
+ z_values = tf.linspace(-5, 5, 200)
254
+ z_values = tf.cast(z_values, tf.float32)
255
+ prob_values_p = p.prob(z_values)
256
+ prob_values_q = q.prob(z_values)
257
+
258
+ fig, ax = plt.subplots()
259
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
260
+ ax.plot(z_values, prob_values_q, label=r'q')
261
+
262
+ ax.set_xlabel("x")
263
+ ax.set_ylabel("PDF(x)")
264
+ ax.legend()
265
+ ax.set_ylim((0, 1))
266
+
267
+ st.pyplot(fig)
268
+ kl = tfd.kl_divergence(q, p)
269
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
270
+
271
+ st.subheader("Relationship between Cauchy and StudentT Distribution")
272
+ q2 = tfd.Cauchy(0,1)
273
+ q3 = tfd.StudentT(1,0,1)
274
+ prob_values_q2 = list(q2.prob(z_values))
275
+ prob_values_q3 = list(q3.prob(z_values))
276
+
277
+ fig2, ax2 = plt.subplots()
278
+ ax2.plot(z_values, prob_values_q2, label=r'Cauchy(0,1)', linestyle='--', lw=3)
279
+ ax2.plot(z_values, prob_values_q3, label=r'StudentT(1,0,1)')
280
+
281
+ ax2.set_xlabel("x")
282
+ ax2.set_ylabel("PDF(x)")
283
+ ax2.legend()
284
+ ax2.set_ylim((0, 1))
285
+
286
+ st.pyplot(fig2)
287
+
288
+
289
+ def Chi():
290
+ st.subheader("Chi distribution")
291
+ p = tfd.Chi(3)
292
+ d = st.slider('dof', 0.0, 5.0, 1.0, 0.5)
293
+
294
+ cdf = r'''
295
+ cdf = $\int_a^b f(x)dx$
296
+ '''
297
+
298
+ st.latex(cdf)
299
+ q = tfd.Chi(d)
300
+ z_values = tf.linspace(-5, 5, 200)
301
+ z_values = tf.cast(z_values, tf.float32)
302
+ prob_values_p = p.prob(z_values)
303
+ prob_values_q = q.prob(z_values)
304
+
305
+ fig, ax = plt.subplots()
306
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
307
+ ax.plot(z_values, prob_values_q, label=r'q')
308
+
309
+ ax.set_xlabel("x")
310
+ ax.set_ylabel("PDF(x)")
311
+ ax.legend()
312
+ ax.set_ylim((0, 1))
313
+
314
+ st.pyplot(fig)
315
+ kl = tfd.kl_divergence(q, p)
316
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
317
+
318
+ #st.subheader("Transformation of Chi Distribution")
319
+ #d2 = st.slider('dof2', 0, 5, 1, 1)
320
+ #p1 = tfd.Chi(d2)
321
+ #prob_values_new = list(p1.prob(z_values))
322
+ #z_values2 = np.zeros(len(z_values))
323
+ #for i in range(len(z_values2)):
324
+ # z_values2[i] = z_values[i]**2
325
+
326
+ #q1 = tfd.Chi2(d2)
327
+ #new = list(q1.prob(z_values))
328
+ # fig2, ax2 = plt.subplots()
329
+ # ax2.plot(z_values, prob_values_new, label=r'X->Chi(d)', lw=3)
330
+ # ax2.plot(z_values2, prob_values_new, label=r'X transformed to X^2', lw=2)
331
+ # ax2.plot(z_values2, new, label=r'Chi2(d)', linestyle='--', lw=2)
332
+
333
+ #ax2.set_xlabel("x")
334
+ #ax2.set_ylabel("PDF(x)")
335
+ #ax2.legend()
336
+ #st.pyplot(fig2)
337
+
338
+ def Chi_squared():
339
+ st.subheader("Chi-squared distribution")
340
+ p = tfd.Chi2(4)
341
+ dof = st.slider('dof', 0.0, 10.0, 2.0,0.5)
342
+
343
+ cdf = r'''
344
+ cdf = $\int_a^b f(x)dx$
345
+ '''
346
+
347
+ st.latex(cdf)
348
+ q = tfd.Chi2(dof)
349
+ z_values = tf.linspace(0, 10, 200)
350
+ z_values = tf.cast(z_values, tf.float32)
351
+ prob_values_p = p.prob(z_values)
352
+ prob_values_q = q.prob(z_values)
353
+
354
+ fig, ax = plt.subplots()
355
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
356
+ ax.plot(z_values, prob_values_q, label=r'q')
357
+
358
+ ax.set_xlabel("x")
359
+ ax.set_ylabel("PDF(x)")
360
+ ax.legend()
361
+ ax.set_ylim((0, 1))
362
+
363
+ st.pyplot(fig)
364
+ kl = tfd.kl_divergence(q, p)
365
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
366
+
367
+ st.subheader("Relationship between Chi-Squared and Exponential Distribution")
368
+ q2 =tfd.Chi2(2)
369
+ q3 = tfd.Exponential(0.5)
370
+ prob_values_q2 = list(q2.prob(z_values))
371
+ prob_values_q3 = list(q3.prob(z_values))
372
+
373
+ fig2, ax2 = plt.subplots()
374
+ ax2.plot(z_values, prob_values_q2, label=r'Chi-Squared(2)', linestyle='--', lw=3)
375
+ ax2.plot(z_values, prob_values_q3, label=r'Exponential(0.5)')
376
+
377
+ ax2.set_xlabel("x")
378
+ ax2.set_ylabel("PDF(x)")
379
+ ax2.legend()
380
+ ax2.set_ylim((0, 1))
381
+
382
+ st.pyplot(fig2)
383
+
384
+
385
+ def Laplace():
386
+ st.subheader("Laplace distribution")
387
+ p = tfd.Laplace(0, 3)
388
+ m = st.slider('mu', 0, 5, 1)
389
+ s = st.slider('sigma', 0, 5, 1)
390
+
391
+ cdf = r'''
392
+ cdf = $\int_a^b f(x)dx$
393
+ '''
394
+
395
+ st.latex(cdf)
396
+ q = tfd.Laplace(m, s)
397
+ z_values = tf.linspace(-5, 5, 200)
398
+ z_values = tf.cast(z_values, tf.float32)
399
+ prob_values_p = p.prob(z_values)
400
+ prob_values_q = q.prob(z_values)
401
+
402
+ fig, ax = plt.subplots()
403
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
404
+ ax.plot(z_values, prob_values_q, label=r'q')
405
+
406
+ ax.set_xlabel("x")
407
+ ax.set_ylabel("PDF(x)")
408
+ ax.legend()
409
+ ax.set_ylim((0, 1))
410
+
411
+ st.pyplot(fig)
412
+ kl = tfd.kl_divergence(q, p)
413
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
414
+
415
+
416
+ def Pareto():
417
+ st.subheader("Pareto distribution")
418
+ p = tfd.Pareto(2, 1)
419
+ a = st.slider('alpha', 0.0, 5.0, 2.5, 0.5)
420
+ s = st.slider('scale', 0.0, 5.0, 2.5, 0.5)
421
+
422
+ cdf = r'''
423
+ cdf = $\int_a^b f(x)dx$
424
+ '''
425
+
426
+ st.latex(cdf)
427
+ q = tfd.Pareto(a, s)
428
+ z_values = tf.linspace(0, 5, 200)
429
+ z_values = tf.cast(z_values, tf.float32)
430
+ prob_values_p = p.prob(z_values)
431
+ prob_values_q = q.prob(z_values)
432
+
433
+ fig, ax = plt.subplots()
434
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=2, alpha=0.5)
435
+ ax.plot(z_values, prob_values_q, label=r'q')
436
+
437
+ ax.set_xlabel("x")
438
+ ax.set_ylabel("PDF(x)")
439
+ ax.legend()
440
+
441
+ st.pyplot(fig)
442
+ kl = tfd.kl_divergence(q, p)
443
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
444
+
445
+ def Weibull():
446
+ st.header("Weibull distribution")
447
+ p = tfd.Weibull(1,2)
448
+ k = st.slider('k', 0.0, 5.0, 0.5, 0.5)
449
+ l = st.slider('lambda', 0.0, 5.0, 0.5, 0.5)
450
+
451
+ cdf = r'''
452
+ cdf = $\int_a^b f(x)dx$
453
+ '''
454
+
455
+ st.latex(cdf)
456
+ q = tfd.Weibull(k, l)
457
+ z_values = tf.linspace(0, 10, 200)
458
+ z_values = tf.cast(z_values, tf.float32)
459
+ prob_values_p = p.prob(z_values)
460
+ prob_values_q = q.prob(z_values)
461
+
462
+ fig, ax = plt.subplots()
463
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
464
+ ax.plot(z_values, prob_values_q, label=r'q')
465
+
466
+ ax.set_xlabel("x")
467
+ ax.set_ylabel("PDF(x)")
468
+ ax.legend()
469
+ ax.set_ylim((0, 2))
470
+
471
+ st.pyplot(fig)
472
+ kl = tfd.kl_divergence(q, p)
473
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
474
+
475
+ st.subheader("Relationship between Weibull and Exponential Distribution")
476
+ l1 = st.slider('l', 0.0, 5.0, 1.0, 0.5)
477
+ k1 = 1
478
+ st.write("k=1")
479
+ q2 = tfd.Weibull(k1, l1)
480
+ prob_values_q2 = list(q2.prob(z_values))
481
+ q3 = tfd.Exponential(1/l1)
482
+ prob_values_q3 = list(q3.prob(z_values))
483
+ fig3, ax3 = plt.subplots()
484
+ ax3.plot(z_values, prob_values_q2, linestyle='--', lw=3, alpha=0.5, label=r'Weibull(1,l)')
485
+ ax3.plot(z_values, prob_values_q3, label=r'Exponential(1/l)')
486
+
487
+ ax3.set_xlabel("x")
488
+ ax3.set_ylabel("PDF(x)")
489
+ ax3.legend()
490
+ ax3.set_ylim((0, 2))
491
+ st.pyplot(fig3)
492
+
493
+ def StudentT():
494
+ st.subheader("StudentT Distribution")
495
+ p = tfd.StudentT(1,0,1)
496
+ df = st.slider('df',0,5,2,1)
497
+ loc = st.slider('loc', 0, 5, 2, 1)
498
+ scale = st.slider('scale', 0, 5, 2, 1)
499
+
500
+ cdf = r'''
501
+ cdf = $\int_a^b f(x)dx$
502
+ '''
503
+
504
+ st.latex(cdf)
505
+ q = tfd.StudentT(df,loc,scale)
506
+ z_values = tf.linspace(-10, 10 , 200)
507
+ z_values = tf.cast(z_values, tf.float32)
508
+ prob_values_p = p.prob(z_values)
509
+ prob_values_q = q.prob(z_values)
510
+
511
+ fig, ax = plt.subplots()
512
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=3, alpha=0.5)
513
+ ax.plot(z_values, prob_values_q, label=r'q')
514
+
515
+ ax.set_xlabel("x")
516
+ ax.set_ylabel("PDF(x)")
517
+ ax.legend()
518
+ ax.set_ylim((0, 1))
519
+
520
+ st.pyplot(fig)
521
+
522
+ st.subheader("Relationship between Cauchy and StudentT Distribution")
523
+ q2 = tfd.Cauchy(0, 1)
524
+ q3 = tfd.StudentT(1, 0, 1)
525
+ prob_values_q2 = list(q2.prob(z_values))
526
+ prob_values_q3 = list(q3.prob(z_values))
527
+
528
+ fig2, ax2 = plt.subplots()
529
+ ax2.plot(z_values, prob_values_q2, label=r'Cauchy(0,1)', linestyle='--', lw=3)
530
+ ax2.plot(z_values, prob_values_q3, label=r'StudentT(1,0,1)')
531
+
532
+ ax2.set_xlabel("x")
533
+ ax2.set_ylabel("PDF(x)")
534
+ ax2.legend()
535
+ ax2.set_ylim((0, 1))
536
+
537
+ st.pyplot(fig2)
538
+
539
+ def Beta():
540
+ st.subheader("Beta distribution")
541
+ p = tfd.Beta(1.5,1.1)
542
+ alpha = st.slider('alpha', 0.0, 2.0, 1.2,0.1)
543
+ beta = st.slider('beta', 0.0, 2.0, 1.2,0.1)
544
+
545
+ cdf = r'''
546
+ cdf = $\int_a^b f(x)dx$
547
+ '''
548
+
549
+ st.latex(cdf)
550
+ q = tfd.Beta(alpha, beta)
551
+ z_values = tf.linspace(0, 1, 200)
552
+ z_values = tf.cast(z_values, tf.float32)
553
+ prob_values_p = p.prob(z_values)
554
+ prob_values_q = q.prob(z_values)
555
+
556
+ fig, ax = plt.subplots()
557
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
558
+ ax.plot(z_values, prob_values_q, label=r'q')
559
+
560
+ ax.set_xlabel("x")
561
+ ax.set_ylabel("PDF(x)")
562
+ ax.legend()
563
+ #ax.set_ylim((0, 1))
564
+
565
+ st.pyplot(fig)
566
+ kl = tfd.kl_divergence(q, p)
567
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
568
+
569
+ st.subheader("Relationship between Beta Distribution and Normal Distribution")
570
+ alpha1 = st.slider('beta=alpha', 50, 500,250, 50)
571
+ q2 = tfd.Beta(alpha1, alpha1)
572
+ q3 = tfd.Normal(0.5,sqrt(0.25/(2*alpha1+1)))
573
+ z_values1 = tf.linspace(0, 1, 200)
574
+ z_values1 = tf.cast(z_values1, tf.float32)
575
+ prob_values_q2 = q2.prob(z_values1)
576
+ prob_values_q3 = q3.prob(z_values1)
577
+
578
+ fig2, ax2 = plt.subplots()
579
+ ax2.plot(z_values1, prob_values_q2, label=r'Beta(alpha,Beta)', linestyle='--', lw=3)
580
+ ax2.plot(z_values1, prob_values_q3, label=r'Normal')
581
+
582
+ ax2.set_xlabel("x")
583
+ ax2.set_ylabel("PDF(x)")
584
+ ax2.legend()
585
+ #ax2.set_ylim((0, 1))
586
+
587
+ st.pyplot(fig2)
588
+
589
+ st.subheader("Relationship between Beta Distribution and Uniform Distribution")
590
+
591
+ st.write("beta = alpha= 1")
592
+ q4 = tfd.Beta(1,1)
593
+ q5 = tfd.Uniform(0,1)
594
+ z_values1 = tf.linspace(0, 1, 200)
595
+ z_values1 = tf.cast(z_values1, tf.float32)
596
+ prob_values_q4 = list(q4.prob(z_values1))
597
+ prob_values_q5 = list(q5.prob(z_values1))
598
+
599
+ fig3, ax3 = plt.subplots()
600
+ ax3.plot(z_values1, prob_values_q4, label=r'Beta(1,1)', linestyle='--', lw=3)
601
+ ax3.plot(z_values1, prob_values_q5, label=r'Uniform(0,1)')
602
+
603
+ ax3.set_xlabel("x")
604
+ ax3.set_ylabel("PDF(x)")
605
+ ax3.legend()
606
+ #ax3.set_ylim((0, 1))
607
+
608
+ st.pyplot(fig3)
609
+
610
+ st.subheader("Transformation of Beta Distribution")
611
+ alpha2 = st.slider('alpha2', 0.0, 2.0, 1.2,0.1)
612
+ beta2 = st.slider('beta2', 0.0, 2.0, 1.8,0.1)
613
+ p1 = tfd.Beta(alpha2, beta2)
614
+ prob_values_new = list(p1.prob(z_values))
615
+ z_values2 = np.zeros(len(z_values))
616
+ for i in range(len(z_values2)):
617
+ z_values2[i] = 1 - z_values[i]
618
+
619
+ q1 = tfd.Beta(beta2,alpha2)
620
+ new = list(q1.prob(z_values))
621
+ fig2, ax2 = plt.subplots()
622
+ ax2.plot(z_values, prob_values_new, label=r'X->Beta(alpha,beta)', lw=3)
623
+ ax2.plot(z_values2, prob_values_new, label=r'X transformed to 1-X', lw=2)
624
+ ax2.plot(z_values, new, label=r'X->Beta(beta,alpha)', linestyle='--', lw=2)
625
+
626
+ ax2.set_xlabel("x")
627
+ ax2.set_ylabel("PDF(x)")
628
+ ax2.legend()
629
+ st.pyplot(fig2)
630
+
631
+
632
+ def Poisson():
633
+ st.subheader("Poisson distribution")
634
+ p = tfd.Poisson(5)
635
+ rate = st.slider('lambda', 0, 10, 1)
636
+
637
+ cdf = r'''
638
+ cdf = $\int_a^b f(x)dx$
639
+ '''
640
+
641
+ st.latex(cdf)
642
+ q = tfd.Poisson(rate)
643
+ z_values = tf.linspace(-2, 10, 13)
644
+ z_values = tf.cast(z_values, tf.float32)
645
+ prob_values_p = p.prob(z_values)
646
+ prob_values_q = q.prob(z_values)
647
+
648
+ fig, ax = plt.subplots()
649
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt='r', markerfmt='ro')
650
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt='--', markerfmt='bo')
651
+
652
+ ax.set_xlabel("x")
653
+ ax.set_ylabel("PDF(x)")
654
+ ax.legend()
655
+ ax.set_ylim((0, 1))
656
+ st.pyplot(fig)
657
+ st.subheader("Addition of two Poisson distributions")
658
+ rate1 = st.slider('lambda1', 0, 10, 1)
659
+ rate2 = st.slider('lambda2', 0, 10, 1)
660
+ q1 = tfd.Poisson(rate1)
661
+ q2 = tfd.Poisson(rate2)
662
+ prob_values_q1 = list(q1.prob(z_values))
663
+ prob_values_q2 = list(q2.prob(z_values))
664
+ fig2, (ax2,ax3) = plt.subplots(1,2)
665
+ ax2.stem(z_values, prob_values_q1, linefmt='r', markerfmt='ro')
666
+ ax3.stem(z_values, prob_values_q2, linefmt='--', markerfmt='bo')
667
+
668
+ ax2.set_xlabel("x")
669
+ ax2.set_title("Poisson(lambda1)")
670
+ ax2.set_ylim((0, 1))
671
+
672
+ ax3.set_xlabel("x")
673
+ ax3.set_title("Poisson(lambda2)")
674
+ ax3.set_ylim((0, 1))
675
+
676
+ st.pyplot(fig2)
677
+
678
+ prob_values_sum =Sum(prob_values_q1, prob_values_q2, z_values)
679
+ q3 = tfd.Poisson(rate1+rate2)
680
+ prob_values_q3 = list(q3.prob(range(len(prob_values_sum))))
681
+ fig3, ax4 = plt.subplots()
682
+ ax4.stem(range(len(prob_values_sum)), prob_values_sum, label=r'Poisson(lambda1)+Poisson(lambda2)', linefmt='r', markerfmt='ro')
683
+ ax4.stem(range(len(prob_values_sum)), prob_values_q3, linefmt='--', label=r'Poisson(lambda1+lambda2)', markerfmt='bo')
684
+ ax4.set_xlabel("x")
685
+ ax4.set_ylabel("PDF(x)")
686
+ ax4.legend()
687
+ ax4.set_ylim((0, 1))
688
+ st.pyplot(fig3)
689
+ st.markdown("Summation of two poisson distribution with parameter lamba1 and lambda2 yields Poisson distribution with parameter (lambda1+lambda2)")
690
+
691
+ st.subheader("Relationship between Poisson and Normal Distribution")
692
+ rate3 = st.slider('lambda1', 100, 500,250,50)
693
+ q4 = tfd.Poisson(rate=rate3)
694
+ z_values1 = tf.linspace(0, 600, 601)
695
+ z_values1 = tf.cast(z_values1, tf.float32)
696
+ prob_values_q4 = list(q4.prob(z_values1))
697
+ q5 = tfd.Normal(rate3, sqrt(rate3))
698
+ prob_values_q5 = list(q5.prob(z_values1))
699
+
700
+ fig4, ax5 = plt.subplots()
701
+ ax5.stem(z_values1, prob_values_q4, label=r'Poisson(lambda1)]', linefmt='r', markerfmt='ro')
702
+ ax5.plot(z_values1, prob_values_q5, label=r'Normal(lamda1,sqrt(lambda1)', lw=3)
703
+ ax5.set_xlabel("x")
704
+ ax5.set_ylabel("PDF(x)")
705
+ ax5.legend()
706
+ ax5.set_ylim((0, 0.1))
707
+ st.pyplot(fig4)
708
+ st.markdown("for large values of lambda, Poisson(lambda) becomes approximately a normal distribution having mean= lambda and variance= lambda")
709
+
710
+
711
+ def Binomial():
712
+ st.subheader("Binomial distribution")
713
+ p = tfd.Binomial(total_count=5, probs=.5)
714
+ count = st.slider('n', 1, 10, 4, 1)
715
+ prob = st.slider('prob', 0.0, 1.0, 0.5,0.1)
716
+ st1 = r'''
717
+ Mean = \[n p \]
718
+ Variance = \[n p q\]
719
+ Entropy = \[\frac{1}{2} \log_2 (2 \pi n e p q)\ + O ( \frac {1}{n}) \]
720
+ '''
721
+
722
+ st.latex(st1)
723
+ q = tfd.Binomial(total_count=count, probs=prob )
724
+ z_values = tf.linspace(0, 10, 11)
725
+ z_values = tf.cast(z_values, tf.float32)
726
+ prob_values_p = list(p.prob(z_values))
727
+ prob_values_q = list(q.prob(z_values))
728
+
729
+ fig, ax = plt.subplots()
730
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt = 'r', markerfmt = 'ro')
731
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt ='--', markerfmt = 'bo')
732
+
733
+ ax.set_xlabel("x")
734
+ ax.set_ylabel("PDF(x)")
735
+ ax.legend()
736
+ ax.set_ylim((0, 1))
737
+
738
+ st.pyplot(fig)
739
+
740
+ st.markdown("Relationship between Poisson and Binomial Distribution")
741
+ count1 = st.slider('n', 750, 1000, 800, 25)
742
+ prob1 = st.slider('prob', 0.0, 0.010, 0.001, 0.001)
743
+
744
+ t = tfd.Binomial(total_count=count1, probs=prob1)
745
+ r = tfd.Poisson(count1*prob1)
746
+
747
+ z_values1 = tf.linspace(0, 50, 51)
748
+ z_values1 = tf.cast(z_values1, tf.float32)
749
+ prob_values_t = t.prob(z_values1)
750
+ prob_values_r = r.prob(z_values1)
751
+
752
+ fig1, ax = plt.subplots()
753
+ ax.stem(z_values1, prob_values_t, label=r'binomial', linefmt='r', markerfmt='ro')
754
+ ax.stem(z_values1, prob_values_r, label=r'poisson', linefmt='--', markerfmt='bo')
755
+
756
+ ax.set_xlabel("x")
757
+ ax.set_ylabel("PDF(x)")
758
+ ax.legend()
759
+ ax.set_ylim((0, 0.5))
760
+
761
+ st.pyplot(fig1)
762
+ st.markdown("")
763
+ st.markdown("It is clear from the graph, that for large values of n and small values of p, the binomial distribution approximates to poisson distribution.")
764
+ st.markdown("")
765
+
766
+ st.markdown("Transformation of Binomial Distribution")
767
+ count2 = st.slider('n1', 1, 10, 4, 1)
768
+ prob2 = st.slider('p', 0.0, 1.0, 0.5, 0.1)
769
+ p1 = tfd.Binomial(total_count=count2, probs=prob2)
770
+ prob_values_n_p = list(p1.prob(z_values))
771
+ z_values2=np.zeros(len(z_values))
772
+ for i in range(len(z_values2)):
773
+ z_values2[i]=count2-z_values[i]
774
+
775
+ q1 = tfd.Binomial(total_count=count2, probs=1-prob2)
776
+ new = list(q1.prob(z_values))
777
+ fig2, ax = plt.subplots()
778
+ ax.stem(z_values, prob_values_n_p, label=r'X->Binomial(n,p)', linefmt='r', markerfmt='ro')
779
+ ax.stem(z_values2, prob_values_n_p, label=r'X transformed to N-X', linefmt='g', markerfmt='go')
780
+ ax.stem(z_values, new, label=r'X->Binomial(n,1-p)', linefmt='--', markerfmt='bo')
781
+
782
+ ax.set_xlabel("x")
783
+ ax.set_ylabel("PDF(x)")
784
+ ax.legend()
785
+ ax.set_ylim((0, 1))
786
+ ax.set_xlim((-0.5,count2+0.5))
787
+ st.pyplot(fig2)
788
+ st.markdown("When a r.v. X with the binomial(n,p) distribution is transformed to n-X, we get a binomial(n,1-p) distribution.")
789
+
790
+ st.markdown("Relationship between Normal and Binomial Distribution")
791
+ count3 = st.slider('n3', 750, 1000, 800, 25)
792
+ prob3 = st.slider('prob3', 0.0, 1.0, 0.5, 0.1)
793
+
794
+ t = tfd.Binomial(total_count=count3, probs=prob3)
795
+ r = tfd.Normal(count3 * prob3, sqrt((count3*prob3*(1-prob3))))
796
+
797
+ z_values1 = tf.linspace(0, 1000, 1001)
798
+ z_values1 = tf.cast(z_values1, tf.float32)
799
+ prob_values_t = t.prob(z_values1)
800
+ prob_values_r = r.prob(z_values1)
801
+
802
+ fig2, ax2 = plt.subplots()
803
+ ax2.stem(z_values1, prob_values_t, label=r'Binomial', linefmt='r', markerfmt='ro')
804
+ ax2.plot(z_values1, prob_values_r, label=r'Normal', lw=3)
805
+
806
+ ax2.set_xlabel("x")
807
+ ax2.set_ylabel("PDF(x)")
808
+ ax2.legend()
809
+ ax2.set_ylim((0, 0.1))
810
+
811
+ st.pyplot(fig2)
812
+ st.markdown("")
813
+ st.markdown("For large values of n, the binomial distribution approximates to the normal distribution with mean = n*p and variance = n*p*(1-p)")
814
+ st.markdown("")
815
+
816
+
817
+ def Bernoulli_dist():
818
+ st.header("Bernoulli distribution")
819
+ p = tfd.Bernoulli(probs=0.5)
820
+ suc = st.slider('p', 0.0, 1.0, 0.8, 0.1)
821
+
822
+ pmf = r'''
823
+
824
+ pmf = f(x) = p \quad \qquad if x=1
825
+ \\ \qquad \qquad \quad \,\,\,= 1-p \quad \,\, if x=0
826
+ \\ \,\,\,\quad\qquad= 0 \quad\qquad else
827
+ '''
828
+ mean = suc
829
+ variance = suc*(1-suc)
830
+
831
+ st1 = f'''
832
+ mean = p = {mean}\\\\
833
+ variance = p*(1-p) = {variance:0.3f}\\\\
834
+ Entropy = -q lnq - p ln p
835
+ '''
836
+ st.latex(pmf)
837
+ st.latex(st1)
838
+ q = tfd.Bernoulli(probs = suc)
839
+ z_values = tf.linspace(0, 1, 2)
840
+ z_values = tf.cast(z_values, tf.float32)
841
+ prob_values_p = p.prob(z_values)
842
+ prob_values_q = q.prob(z_values)
843
+ fig, ax = plt.subplots()
844
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt='b', markerfmt='bo')
845
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt='g', markerfmt='go')
846
+
847
+ ax.set_xlabel("x")
848
+ ax.set_ylabel("PDF(x)")
849
+ ax.legend()
850
+ ax.set_ylim((0, 1))
851
+
852
+ st.pyplot(fig)
853
+ st.subheader("Multiplication of two Bernoulli distributions")
854
+ p1 = st.slider('p1', 0.0, 1.0, 0.2, 0.1)
855
+ p2 = st.slider('p2', 0.0, 1.0, 0.7, 0.1)
856
+
857
+ rv_p1 = tfd.Bernoulli(probs=p1)
858
+ rv_p2 = tfd.Bernoulli(probs=p2)
859
+ prob_values_p1 = rv_p1.prob(z_values)
860
+ prob_values_p2 = rv_p2.prob(z_values)
861
+ prob_values_pone = list(prob_values_p1)
862
+ prob_values_ptwo = list(prob_values_p2)
863
+ prob_values_pdt = Product(prob_values_pone, prob_values_ptwo, z_values)
864
+
865
+ fig1,(ax1,ax2)=plt.subplots(1,2)
866
+ ax1.stem(z_values, prob_values_p1, label=r'p1', linefmt='b', markerfmt='bo')
867
+ ax2.stem(z_values, prob_values_p2, label=r'p2', linefmt='g', markerfmt='go')
868
+
869
+ ax1.set_title("Bernoulli(p1)")
870
+ ax1.set_xlabel("x")
871
+ ax1.set_ylabel("PDF(x)")
872
+ ax1.set_ylim((0, 1))
873
+
874
+ ax2.set_title("Bernoulli(p2)")
875
+ ax2.set_xlabel("x")
876
+ ax2.set_ylim((0, 1))
877
+
878
+ st.pyplot(fig1)
879
+
880
+ rv_p1p2 = tfd.Bernoulli(probs=p1*p2)
881
+ prob_values_p1p2 = rv_p1p2.prob(range(len(prob_values_pdt)))
882
+
883
+ fig2, ax3 = plt.subplots()
884
+ ax3.stem(range(len(prob_values_pdt)), prob_values_pdt, label=r'Product of Bernoulli(p1) and Bernoulli(p2)', linefmt='r', markerfmt='ro')
885
+ ax3.stem(range(len(prob_values_pdt)), prob_values_p1p2, label=r'Bernoulli(p1*p2)', linefmt='--', markerfmt='bo')
886
+ ax3.set_xlabel("x")
887
+ ax3.set_ylabel("PDF(x)")
888
+ ax3.legend()
889
+ ax3.set_ylim((0, 1))
890
+ ax3.set_xlim((-0.5, 1.5))
891
+
892
+ st.pyplot(fig2)
893
+ st.markdown("Multiplication of a Bernoulli(p1) distribution with Bernouli(p2) gives a Bernoulli distribution with parameter=p1*p2")
894
+ st.subheader("Addition of two Bernoulli distributions")
895
+ p3 = st.slider('p3', 0.0, 1.0, 0.6, 0.1)
896
+ rv_p3 = tfd.Bernoulli(probs=p3)
897
+ prob_values_p3 = list(rv_p3.prob(z_values))
898
+ prob_values_sum=Sum(prob_values_p3, prob_values_p3, z_values)
899
+ fig3, ax4 = plt.subplots()
900
+ ax4.stem(range(len(prob_values_sum)), prob_values_sum, label=r'Sum of 2 Bernoulli(p3) distributions', linefmt='r', markerfmt='ro')
901
+ b = tfd.Binomial(total_count=2, probs=p3)
902
+ prob_values_bin = list(b.prob(range(len(prob_values_sum))))
903
+ ax4.stem(range(len(prob_values_sum)), prob_values_bin, label=r'Binomial(2,p3)', linefmt='--', markerfmt='bo')
904
+ ax4.set_xlabel("x")
905
+ ax4.set_ylabel("PDF(x)")
906
+ ax4.legend()
907
+ ax4.set_ylim((0, 1))
908
+
909
+ st.pyplot(fig3)
910
+ st.markdown("The sum of n Bernoulli(p) distributions is a binomial(n,p) distribution")
911
+
912
+ def BetaBinomial():
913
+ st.markdown("Beta Binomial distribution")
914
+ p = tfd.BetaBinomial(8,0.5,1.2)
915
+ num = st.slider('n', 0, 10, 5, 1)
916
+ al = st.slider('alpha', 0.0, 5.0, 0.2, 0.1)
917
+ be = st.slider('beta', 0.0, 5.0, 0.2, 0.1)
918
+
919
+
920
+ st1 = r'''
921
+ Mean = \[ \frac {n \alpha}{\alpha + \beta} \]
922
+ Variance = \[ \frac {(n \alpha \beta)(\alpha + \beta + n)}{(\alpha + \beta + 1) (\alpha + \beta)^2} \]
923
+ '''
924
+
925
+ st.latex(st1)
926
+ q = tfd.BetaBinomial(num, al, be)
927
+ z_values = tf.linspace(0, 10, 11)
928
+ z_values = tf.cast(z_values, tf.float32)
929
+ prob_values_p = p.prob(z_values)
930
+ prob_values_q = q.prob(z_values)
931
+
932
+ fig, ax = plt.subplots()
933
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt='r', markerfmt='ro')
934
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt='--', markerfmt='bo')
935
+
936
+ ax.set_xlabel("x")
937
+ ax.set_ylabel("PDF(x)")
938
+ ax.legend()
939
+ ax.set_ylim((0, 1))
940
+
941
+ st.pyplot(fig)
942
+
943
+ st.markdown("")
944
+ st.markdown("Relarionship between BetaBinomial and Uniform-Discrete Distribution")
945
+ st.markdown("")
946
+ num2 = st.slider('n2', 0, 10, 5, 1)
947
+ q2 = tfd.BetaBinomial(num2, 1, 1)
948
+ prob_values_q2 = q2.prob(z_values)
949
+
950
+
951
+ fig2, ax2 = plt.subplots()
952
+ ax2.stem(z_values, prob_values_q2, label=r'BetaBinomial(n,1,1)', linefmt='r', markerfmt='ro')
953
+
954
+ ax2.set_xlabel("x")
955
+ ax2.set_ylabel("PDF(x)")
956
+ ax2.legend()
957
+ ax2.set_ylim((0, 1))
958
+
959
+ st.pyplot(fig2)
960
+ st.markdown("A BetaBinomial(n,alpha,beta) with alpha=beta=1 becomes a uniform-discrete distribution from 0 to n(n2 here)")
961
+ st.markdown("")
962
+
963
+ st.markdown("Relationship between Binomial and BetaBinomial Distribution")
964
+ num3 = st.slider('_n', 0, 10, 5, 1)
965
+ al2 = st.slider('_alpha', 100, 500, 200, 50)
966
+ be2 = st.slider('_beta', 100, 500, 200, 50)
967
+
968
+ q3 = tfd.BetaBinomial(num3, al2, be2)
969
+ prob_values_q3 = q3.prob(z_values)
970
+
971
+ success = al2/(al2+be2)
972
+ q4 = tfd.Binomial(total_count=num3, probs=success)
973
+ prob_values_q4 = q4.prob(z_values)
974
+
975
+ fig3, ax3 = plt.subplots()
976
+ ax3.stem(z_values, prob_values_q3, label=r'BetaBinomial', linefmt='r', markerfmt='ro')
977
+ ax3.stem(z_values, prob_values_q4, label=r'Binomial', linefmt='--', markerfmt='bo')
978
+ ax3.set_xlabel("x")
979
+ ax3.set_ylabel("PDF(x)")
980
+ ax3.legend()
981
+ ax3.set_ylim((0, 1))
982
+
983
+ st.pyplot(fig3)
984
+ st.markdown("For large values of (alpha+beta), the BetaBinomial approximates to the Binomial Distribution with the probanility of success = alpha/(alpha+beta)")
985
+ st.markdown("")
986
+
987
+ def Geometric():
988
+ st.subheader("Geometric distribution")
989
+ p = tfd.Geometric(probs=0.2)
990
+ prob = st.slider('prob', 0.0, 1.0, 0.4, 0.1)
991
+
992
+ st1 = r'''
993
+ Mean=\[ \frac {1}{p} \]
994
+ Variance= \[ \frac {1-p}{p^2} \]
995
+ Entropy = \[ \frac { -(1-p) \log_2 {(1-p)} - p \log_2 p} {p}\]
996
+ '''
997
+
998
+ st.latex(st1)
999
+ q = tfd.Geometric(probs=prob)
1000
+ z_values = tf.linspace(0, 10, 11)
1001
+ z_values = tf.cast(z_values, tf.float32)
1002
+ prob_values_p = p.prob(z_values)
1003
+ prob_values_q = q.prob(z_values)
1004
+
1005
+ fig, ax = plt.subplots()
1006
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt='b', markerfmt='bo')
1007
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt='g', markerfmt='go')
1008
+ ax.set_xlabel("x")
1009
+ ax.set_ylabel("PMF(x)")
1010
+ ax.legend()
1011
+ ax.set_ylim((0, 1))
1012
+
1013
+ st.pyplot(fig)
1014
+
1015
+ def Gamma():
1016
+ st.subheader("Gamma distribution")
1017
+ p = tfd.Gamma(concentration = 3.5, rate = 3 )
1018
+ concn = st.slider('concentration', 0.0, 10.0, 5.0, 0.5)
1019
+ rat = st.slider('rate', 0.0, 10.0, 2.0, 0.5)
1020
+
1021
+ cdf = r'''
1022
+ cdf = $\int_a^b f(x)dx$
1023
+ '''
1024
+
1025
+ st.latex(cdf)
1026
+ q = tfd.Gamma(concentration = concn, rate = rat )
1027
+ z_values = tf.linspace(0, 20, 200)
1028
+ z_values = tf.cast(z_values, tf.float32)
1029
+ prob_values_p = p.prob(z_values)
1030
+ prob_values_q = q.prob(z_values)
1031
+
1032
+ fig, ax = plt.subplots()
1033
+ ax.plot(z_values, prob_values_p, label=r'p', linestyle='--', lw=5, alpha=0.5)
1034
+ ax.plot(z_values, prob_values_q, label=r'q')
1035
+
1036
+ ax.set_xlabel("x")
1037
+ ax.set_ylabel("PDF(x)")
1038
+ ax.legend()
1039
+ #ax.set_ylim((0, 1))
1040
+
1041
+ st.pyplot(fig)
1042
+ kl = tfd.kl_divergence(q, p)
1043
+ st.latex(f"D_{{KL}}(q||p) \\text{{ is : }}{kl:0.2f}")
1044
+
1045
+ st.subheader("Relationship between Gamma and Exponential Distribution")
1046
+ alpha1=1
1047
+ st.write("alpha1=1")
1048
+ beta1 = st.slider('beta1', 0.0, 10.0, 5.0, 0.5)
1049
+ q2 = tfd.Gamma(concentration=alpha1, rate=beta1)
1050
+ prob_values_q2 = list(q2.prob(z_values))
1051
+ q3 = tfd.Exponential(beta1)
1052
+ prob_values_q3 = list(q3.prob(z_values))
1053
+ fig3, ax3 = plt.subplots()
1054
+ ax3.plot(z_values, prob_values_q2, linestyle='--', lw=3, alpha=0.5, label=r'Gamma(1,beta)')
1055
+ ax3.plot(z_values, prob_values_q3, label=r'Exponential(beta)')
1056
+
1057
+ ax3.set_xlabel("x")
1058
+ ax3.set_ylabel("PDF(x)")
1059
+ ax3.legend()
1060
+ #ax3.set_ylim((0, 1))
1061
+ st.pyplot(fig3)
1062
+
1063
+ def NegBin():
1064
+ st.markdown("Negative Binomial distribution")
1065
+ p = tfd.NegativeBinomial(total_count=5, probs=.5)
1066
+ count = st.slider('n', 1, 10, 1)
1067
+ prob = st.slider('prob', 0.0, 1.0, 0.1)
1068
+ cdf = r'''
1069
+ cdf = $\int_a^b f(x)dx$
1070
+ '''
1071
+
1072
+ st.latex(cdf)
1073
+ q = tfd.NegativeBinomial(total_count=count, probs=prob )
1074
+ z_values = tf.linspace(0, 100, 100)
1075
+ z_values = tf.cast(z_values, tf.float32)
1076
+ prob_values_p = p.prob(z_values)
1077
+ prob_values_q = q.prob(z_values)
1078
+
1079
+ fig, ax = plt.subplots()
1080
+ ax.stem(z_values, prob_values_p, label=r'Distribution with unknown parameters', linefmt='b', markerfmt='bo')
1081
+ ax.stem(z_values, prob_values_q, label=r'Distribution with given parameters', linefmt='g', markerfmt='go')
1082
+
1083
+ ax.set_xlabel("x")
1084
+ ax.set_ylabel("PMF(x)")
1085
+ ax.legend()
1086
+ ax.set_ylim((0, 1))
1087
+
1088
+ st.pyplot(fig)
1089
+
1090
+
1091
+
1092
+ if (add_selectbox == "Continuous Univariate"):
1093
+ selection1 = st.sidebar.selectbox(
1094
+ 'Choose an Option',
1095
+ ('Beta','Cauchy', 'Chi', 'Chi-Squared', 'Exponential', 'Gamma', 'Laplace', 'Normal',
1096
+ 'Pareto', 'StudentT', 'Uniform', 'Weibull')
1097
+ )
1098
+ if (selection1 == "Normal"):
1099
+ Normal()
1100
+ elif (selection1 == "Exponential"):
1101
+ Exponential()
1102
+ elif(selection1 == "Uniform"):
1103
+ Uniform()
1104
+ elif(selection1 == "Error"):
1105
+ Error()
1106
+ elif (selection1 == "Cauchy"):
1107
+ Cauchy()
1108
+ elif (selection1 == "Chi"):
1109
+ Chi()
1110
+ elif (selection1 == "Chi-Squared"):
1111
+ Chi_squared()
1112
+ elif (selection1 == "Laplace"):
1113
+ Laplace()
1114
+ elif(selection1=="Pareto"):
1115
+ Pareto()
1116
+ elif(selection1 == "Weibull"):
1117
+ Weibull()
1118
+ elif (selection1 == "Gamma"):
1119
+ Gamma()
1120
+ elif (selection1 == "StudentT"):
1121
+ StudentT()
1122
+ else:
1123
+ Beta()
1124
+ elif (add_selectbox == "Discrete Univariate"):
1125
+ selection1 = st.sidebar.selectbox(
1126
+ 'Choose an Option',
1127
+ ('Bernoulli', 'Beta-Binomial','Binomial', 'Geometric', 'Negative-Binomial', 'Poisson')
1128
+ )
1129
+ if (selection1 == "Poisson"):
1130
+ Poisson()
1131
+ elif (selection1 == "Bernoulli"):
1132
+ Bernoulli_dist()
1133
+ elif (selection1 == "Binomial"):
1134
+ Binomial()
1135
+ elif (selection1 == "Beta-Binomial"):
1136
+ BetaBinomial()
1137
+ elif (selection1 == "Geometric"):
1138
+ Geometric()
1139
+ elif (selection1 == "Negative-Binomial"):
1140
+ NegBin()