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