Statistical Distribution Examples
# ---------Poisson Distribution Example
import seaborn as sns
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import poisson
# -----------------------------
# Step 1: Load Titanic dataset
# -----------------------------
titanic = sns.load_dataset("titanic")
# Drop missing values in 'survived'
df = titanic[["survived"]].dropna()
# -----------------------------
# Step 2: Count total survivors
# -----------------------------
total_survivors = df["survived"].sum()
total_passengers = len(df)
print(f"Total passengers: {total_passengers}")
print(f"Total survivors: {total_survivors}")
# -----------------------------
# Step 3: Poisson λ (average survivors per passenger)
# -----------------------------
lambda_val = df["survived"].mean()
print(f"Average survivors per passenger (λ): {lambda_val:.4f}")
# -----------------------------
# Step 4: Poisson PMF for k = 0 or 1
# -----------------------------
k_values = [Link]([0, 1])
poisson_probs = [Link](k_values, lambda_val)
# -----------------------------
# Step 5: Visualization
# -----------------------------
[Link](figsize=(6, 4))
bars = [Link](k_values, poisson_probs, color=['salmon', 'skyblue'], edgecolor='black', alpha=0.8)
# Add probability labels on top of bars
for bar, prob in zip(bars, poisson_probs):
[Link](bar.get_x() + bar.get_width()/2, bar.get_height() + 0.01,
f"{prob:.3f}", ha='center', fontsize=10)
[Link](f"Poisson Distribution for Titanic 'Survived' (λ={lambda_val:.4f})")
[Link]("Survived (0 = No, 1 = Yes)")
[Link]("Probability")
[Link]([0, 1], ["Did Not Survive", "Survived"])
[Link](0, 1)
[Link](axis='y', linestyle='--', alpha=0.7)
[Link]()
# -----------------------------
# Step 6: Example probability calculation
# -----------------------------
prob_survive = [Link](1, lambda_val)
print(f"Probability of survival (Poisson model): {prob_survive:.4f}")
#----------Binomial Distribution Example
import seaborn as sns
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import binom
# -----------------------------
# Step 1: Load Titanic dataset
# -----------------------------
titanic = sns.load_dataset("titanic")
# Drop missing values in 'survived'
df = titanic[["survived"]].dropna()
# -----------------------------
# Step 2: Estimate probability of survival (p)
# -----------------------------
p_survival = df["survived"].mean()
print(f"Estimated probability of survival (p): {p_survival:.4f}")
# -----------------------------
# Step 3: Choose group size (n)
# -----------------------------
n = 10 # Example: group of 10 passengers
# -----------------------------
# Step 4: Calculate Binomial PMF for k = 0 to n
# -----------------------------
k_values = [Link](0, n + 1)
binomial_probs = [Link](k_values, n, p_survival)
# -----------------------------
# Step 5: Visualization
# -----------------------------
[Link](figsize=(8, 5))
bars = [Link](k_values, binomial_probs, color='skyblue', edgecolor='black', alpha=0.8)
# Add probability labels
for bar, prob in zip(bars, binomial_probs):
[Link](bar.get_x() + bar.get_width()/2, bar.get_height() + 0.005,
f"{prob:.3f}", ha='center', fontsize=9)
[Link](f"Binomial Distribution (n={n}, p={p_survival:.2f}) - Titanic Survival")
[Link]("Number of Survivors in Group of 10")
[Link]("Probability")
[Link](axis='y', linestyle='--', alpha=0.7)
[Link]()
# -----------------------------
# Step 6: Example probability calculation
# -----------------------------
# Probability of exactly 5 survivors in a group of 10
prob_5 = [Link](5, n, p_survival)
print(f"Probability of exactly 5 survivors in a group of {n}: {prob_5:.4f}")
#--------------Normal Distribution Example
import seaborn as sns
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import norm
# -----------------------------
# Step 1: Load Titanic dataset
# -----------------------------
titanic = sns.load_dataset("titanic")
# -----------------------------
# Step 2: Extract 'age' column and drop NaN
# -----------------------------
ages = titanic["age"].dropna()
# -----------------------------
# Step 3: Calculate mean and standard deviation
# -----------------------------
mu = [Link]()
sigma = [Link]()
print(f"Mean age (μ): {mu:.2f}")
print(f"Standard deviation (σ): {sigma:.2f}")
# -----------------------------
# Step 4: Create histogram and normal curve
# -----------------------------
[Link](figsize=(8, 5))
# Histogram of ages
count, bins, ignored = [Link](ages, bins=20, density=True, alpha=0.6, color='skyblue',
edgecolor='black')
# Normal distribution curve
x = [Link](min(ages), max(ages), 100)
[Link](x, [Link](x, mu, sigma), 'r-', lw=2, label="Normal PDF")
[Link]("Normal Distribution Fit - Titanic Ages")
[Link]("Age")
[Link]("Density")
[Link]()
[Link](axis='y', linestyle='--', alpha=0.7)
[Link]()
# -----------------------------
# Step 5: Example probability calculation
# -----------------------------
# Probability of a passenger being between 20 and 30 years old
prob_20_30 = [Link](30, mu, sigma) - [Link](20, mu, sigma)
print(f"Probability of age between 20 and 30: {prob_20_30:.4f}")
Comparison
Main difference in results:
Binomial → Probability of a fixed number of successes in a fixed group.
Poisson → Probability of a fixed number of events in a fixed space/time, often for rare
events.
Normal → Probability of a continuous value falling in a range.
Feature Binomial Poisson Normal
Type Discrete Discrete Continuous
Data type Counts of Counts of events Continuous
successes in fixed in fixed interval measurements
trials
Parameters n, p λ meu, σ
Titanic Example Survival in group Survivors per Passenger ages
of passengers passenger record
Example Result P(5 survivors in P(1 survivor) ≈ P(20 ≤ age ≤ 30) ≈
10) ≈ 13.78% 26.19% 25.84%
Shape Varies (can be Skewed for small λ Symmetrical bell
skewed) curve
How Statistical Distribution Helps us to understand dataset
1. Binomial Distribution in Data Science
Purpose:
Models binary outcomes (success/failure, yes/no, survived/died) over a fixed number of
trials.
How it helps:
Predict probabilities of certain outcomes in experiments or business processes.
Risk assessment: Probability of a certain number of failures in a batch.
Quality control: Detect if defect rates are within acceptable limits.
Example in Titanic:
Predict the probability of exactly 5 survivors in a group of 10 passengers.
Helps in survival rate estimation for safety planning.
2. Poisson Distribution in Data Science
Purpose:
Models count data — number of events happening in a fixed time or space, given a
known average rate.
How it helps:
Event forecasting: Predict number of customer arrivals, website clicks, or failures in a
system.
Anomaly detection: Identify unusual spikes in events (e.g., fraud detection).
Resource allocation: Estimate staffing needs based on expected event counts.
Example in Titanic:
Model the number of survivors per passenger class.
Could be extended to predict rescue resource needs based on expected survivor counts.
3. Normal Distribution in Data Science
Purpose:
Models continuous variables that cluster around a mean.
How it helps:
Data modeling: Many natural and human measurements follow a normal distribution.
Statistical inference: Basis for confidence intervals, hypothesis testing, and regression
assumptions.
Outlier detection: Identify values far from the mean.
Feature engineering: Normalize data for machine learning models.
Example in Titanic:
Model passenger ages to understand demographics.
Helps in targeted safety measures (e.g., prioritizing certain age groups).