Logistic Distribution

📊 Logistic Distribution in Python

The Logistic Distribution is similar to the normal distribution but has heavier tails. It’s often used in logistic regression, population growth models, and machine learning.


✅ 1. Characteristics of Logistic Distribution

  • Probability Density Function (PDF):

f(x)=e−(x−μ)/ss(1+e−(x−μ)/s)2f(x) = \frac{e^{-(x-\mu)/s}}{s (1 + e^{-(x-\mu)/s})^2}

Where:

  • μ (loc) → mean / center

  • s (scale) → scale parameter (similar to standard deviation)

  • Heavy tails compared to normal distribution

  • Symmetric around μ


✅ 2. Generate Logistic Data Using NumPy

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Parameters
mu = 0 # mean / location
scale = 1 # scale
size = 1000 # number of samples

# Generate logistic random numbers
data = np.random.logistic(loc=mu, scale=scale, size=size)

print(data[:10]) # first 10 numbers

Output (example):

[-0.34, 0.12, -1.23, 0.56, 0.78, -0.45, 1.12, -0.89, 0.34, 0.05]

✅ 3. Visualize Logistic Distribution

sns.histplot(data, bins=30, kde=True, color='purple')
plt.title("Logistic Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
  • Histogram is bell-shaped, similar to normal distribution

  • Tails are heavier than normal


✅ 4. Compare Logistic vs Normal

# Normal distribution for comparison
normal_data = np.random.normal(loc=0, scale=1, size=1000)
sns.histplot(data, bins=30, kde=True, color=‘red’, label=‘Logistic’, alpha=0.5)
sns.histplot(normal_data, bins=30, kde=True, color=‘blue’, label=‘Normal’, alpha=0.5)
plt.title(“Logistic vs Normal Distribution”)
plt.legend()
plt.show()

  • Both are symmetric

  • Logistic has fatter tails


✅ 5. 2D Logistic Distribution Array

arr = np.random.logistic(loc=0, scale=1, size=(3, 4))
print(arr)

Output (example):

[[ 0.23 -0.56 1.12 0.45]
[-0.89 0.34 -0.12 0.78]
[ 1.23 -0.45 0.67 -0.34]]


🧠 Summary Table

Function Parameters Description
np.random.logistic() loc, scale, size Generates logistic random numbers
loc Center / mean Similar to μ in normal distribution
scale Scale Similar to standard deviation
size Number of samples Can be integer or tuple for array

🎯 Practice Exercises

  1. Generate 1000 logistic random numbers with loc=5, scale=2 and plot histogram.

  2. Compare logistic (loc=0, scale=1) vs normal (μ=0, σ=1) distribution in one plot.

  3. Generate a 2×5 array of logistic random numbers.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *