Statistical Distributions
The statsmodels
library provides tools for working with empirical distributions, making it especially useful for non-parametric data analysis. Its Empirical Cumulative Distribution Function (ECDF) implementation estimates the cumulative distribution directly from data, without assuming a specific theoretical distribution. This is valuable for exploratory data analysis and assessing goodness of fit
Syntax
from statsmodels.distributions.empirical_distribution import ECDF
ecdf = ECDF(data, side='right')
data
: The array-like input data points for which the empirical distribution will be calculated.side
(optional): Defines the side of the interval to include. The options are:- ‘right’: Creates a right-continuous step function (default behavior).
- ‘left’: Creates a left-continuous step function.
Example
The following example demonstrates how to create an Empirical Cumulative Distribution Function (ECDF) from exponentially distributed data, visualize it using matplotlib
, and calculate cumulative probabilities for specific values, showing how ECDF can be used for non-parametric estimation of the probability distribution:
import numpy as npfrom statsmodels.distributions.empirical_distribution import ECDFimport matplotlib.pyplot as plt# Generate sample datanp.random.seed(42)data = np.random.exponential(size=200)# Create ECDF objectecdf = ECDF(data)# Generate points for plottingx = np.linspace(min(data), max(data), 100)y = ecdf(x)# Plot ECDFplt.plot(x, y, 'b-', label='ECDF')plt.xlabel('Value')plt.ylabel('Cumulative Probability')plt.title('Empirical Cumulative Distribution Function')plt.grid(True)plt.legend()plt.show()# Calculate probabilities for specific pointspoints = [0.5, 1.0, 1.5]probabilities = ecdf(points)print("\nCumulative probabilities:")for point, prob in zip(points, probabilities):print(f"P(X ≤ {point:.1f}) = {prob:.3f}")
The code above generates the ouput as follows:
Cumulative probabilities:P(X ≤ 0.5) = 0.450P(X ≤ 1.0) = 0.635P(X ≤ 1.5) = 0.780
The plot generated by 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 Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours