Chandra B. Pradhan - Fab Futures - Data Science
Home About

Week 5: Probability¶

Probability Fitting: Histogram¶

In [2]:
import numpy as np
import matplotlib.pyplot as plt

# -----------------------------
# 1. Generate Sample Data
# -----------------------------
# Generate 1000 random data points from a Normal Distribution 
# (Mean=50, Standard Deviation=10)
mu, sigma = 50, 10
data = np.random.normal(mu, sigma, 1000)

# -----------------------------
# 2. Plot Histogram (Probability Density)
# -----------------------------
plt.figure(figsize=(10, 6))

# Plot the histogram. 
# 'density=True' ensures that the area under the histogram sums to 1, 
# making it a probability density function plot.
plt.hist(data, 
         bins=30, 
         density=True, 
         alpha=0.6, 
         color='skyblue',
         edgecolor='black',
         label='Data Histogram (PDF)')

# -----------------------------
# Optional: Overlay Theoretical Probability Density Function (PDF)
# -----------------------------
from scipy.stats import norm
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, sigma) # Calculate PDF values for the theoretical normal distribution
plt.plot(x, p, 'r', linewidth=2, label='Theoretical Normal PDF')

# -----------------------------
# 3. Add Labels and Title
# -----------------------------
plt.title('Probability Density Histogram of Normally Distributed Data')
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.legend()
plt.grid(axis='y', alpha=0.5)

plt.tight_layout()
plt.savefig('probability_density_histogram.png')
No description has been provided for this image
In [ ]: