Lognormal distribution
The lognormal distribution is a probability distribution where the logarithm of a random variable follows a normal distribution, characterized by a right-skewed shape with a positive tail.
This distribution is useful for modeling data that grows multiplicatively rather than additively, such as stock prices, income distributions, and biological growth. Unlike the normal distribution, which emerges from additive random effects (as described by the Central Limit Theorem), the lognormal distribution arises when random effects multiply rather than add.
Example
The following code is a simple demonstration of generating and visualizing lognormal data:
import numpy as npimport matplotlib.pyplot as pltimport seaborn as sns# Set random seed for reproducibilitynp.random.seed(42)# Generate lognormal datamu = 0 # Mean of the underlying normal distributionsigma = 0.5 # Standard deviationsample_size = 1000lognormal_data = np.random.lognormal(mean=mu, sigma=sigma, size=sample_size)# Calculate some descriptive statisticsmean_value = np.mean(lognormal_data)median_value = np.median(lognormal_data)mode_approx = np.exp(mu - sigma**2)# Create a histogram with KDE plotplt.figure(figsize=(10, 6))sns.histplot(lognormal_data, kde=True, bins=50)plt.axvline(mean_value, color='r', linestyle='--', label=f'Mean: {mean_value:.2f}')plt.axvline(median_value, color='g', linestyle='--', label=f'Median: {median_value:.2f}')plt.axvline(mode_approx, color='b', linestyle='--', label=f'Mode (approx): {mode_approx:.2f}')plt.title('Lognormal Distribution (μ=0, σ=0.5)')plt.xlabel('Value')plt.ylabel('Frequency')plt.legend()plt.grid(True, alpha=0.3)# Limit x-axis to better visualize the distributionplt.xlim(0, 5)plt.show()# Print key statisticsprint(f"Mean: {mean_value:.4f}")print(f"Median: {median_value:.4f}")print(f"Mode (approximate): {mode_approx:.4f}")print(f"Variance: {np.var(lognormal_data):.4f}")print(f"Skewness: {((np.exp(sigma**2) + 2) * np.sqrt(np.exp(sigma**2) - 1)):.4f}")
This code generates a sample of 1000 lognormal random values and visualizes their distribution. The output shows a right-skewed distribution with a long tail where mean > median > mode, typical of lognormal data.
The output of the above code will be:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Data Science on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Free course
R for Programmers
An introduction to the fundamentals of the R programming language for experienced programmers.Beginner Friendly1 hour