In [4]:
import ipywidgets as widgets 
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
%matplotlib inline  
In [5]:
# Define a function to create a figure
# The inputs are the parameters for the Binomial Distributions

def Binomial_Distribution(p = 0, n=10):
    #n = 10
    #p = 0.2

    # Range of outcomes
    x = range(n + 1)
    y = binom.pmf(x, n, p)

    # Plot
    fig, ax = plt.subplots(1, 1, figsize=(4,4))
    plt.bar(x, y, color='lightblue')
    plt.xlabel('Number of Successes')
    plt.ylabel('Probability')
    plt.title(f'Binomial Distribution (p={p:.1f}, n={n:.0f})')
    plt.grid(linestyle='--')
    plt.ylim([0,0.4])
    plt.tight_layout()
    plt.show()
In [6]:
widgets.interact(Binomial_Distribution, p = (0,1,0.1),n=(10,100,10))
interactive(children=(FloatSlider(value=0.0, description='p', max=1.0), IntSlider(value=10, description='n', m…
Out[6]:
<function __main__.Binomial_Distribution(p=0, n=10)>
In [12]:
p = 0.9606
n= 100
x = range(n + 1)
y = binom.pmf(x, n, p)

print(binom.pmf(95,n,p))

# Plot
fig, ax = plt.subplots(1, 1, figsize=(4,4))
plt.bar(x, y, color='lightblue')
plt.xlabel('Number of Successes')
plt.ylabel('Probability')
plt.title(f'Binomial Distribution (p={p:.1f}, n={n:.0f})')
plt.grid(linestyle='--')
plt.ylim([0,0.25])
plt.xlim([85,105])
plt.show()
0.15694569929950464
No description has been provided for this image
In [40]:
poisson.pmf(0,10/25)
Out[40]:
0.6703200460356393
In [44]:
# Binomial Distribution

from math import comb
p = 1/25
n = 10
k = 0
combination = comb(n,k)
result = combination * (p)**(k) * (1-p)**(n-k)
print(result)
1
0.6648326359915008
In [43]:
result = (1-p) ** 10
print(result)
0.6648326359915008
In [46]:
px = 0.05*((0.95)**9)
print(px)
0.03151247048623045
In [49]:
from scipy.stats import poisson
print(poisson.pmf(0,1))
print(poisson.pmf(1,1))
print(poisson.pmf(2,1))
print(poisson.pmf(3,1))
print(poisson.pmf(4,1))
print(poisson.pmf(5,1))
print(poisson.pmf(6,1))
mu = 1
x = np.arange(0,10,1)
fig, ax = plt.subplots(1, 1, figsize=(5,4))
bars = plt.bar(x, poisson.pmf(x, mu), label='poisson pmf', color='lightblue')
plt.xlabel('X')
plt.ylabel('Probability')
for bar in bars:
    height = bar.get_height()
    if height > 0.0005:
        ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.3f}', 
                ha='center', va='bottom', fontsize=8)
plt.show()
0.36787944117144233
0.36787944117144233
0.18393972058572114
0.06131324019524039
0.015328310048810101
0.00306566200976202
0.0005109436682936698
No description has been provided for this image
In [21]:
from scipy.stats import geom
p = 0.9  # Probability of success

# Probability of the first success on the 3rd trial
probability = geom.pmf(4, p)
print("PMF at k=2:", "{:.5f}".format(probability))
PMF at k=2: 0.00090
In [47]:
p = 0.6
x = np.arange(0,10,1)
fig, ax = plt.subplots(1, 1, figsize=(4,4))
bars = ax.bar(x, geom.pmf(x, p), label='Geometric pmf', color='pink')
plt.xlabel('X')
plt.ylabel('Probability')
# Add text on top of bars
for bar in bars:
    height = bar.get_height()
    if height > 0.005:
        ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.3f}', 
                ha='center', va='bottom', fontsize=7)
plt.show()
No description has been provided for this image
In [ ]: