How to Use ECDF Analysis to Validate Distribution Fits in Phitter

How to Use ECDF Analysis to Validate Distribution Fits in Phitter
Image by Editor | Midjourney

The Empirical Cumulative Distribution Function (ECDF) provides a powerful visual tool that, when combined with statistical tests, helps validate your distribution choice. In this article, we’ll explore how to use ECDF analysis alongside traditional methods using Phitter, demonstrating with penguin flipper length measurements. 

Understanding Our Data

Let’s start with the Palmer Penguins dataset, specifically focusing on flipper length measurements:

 
import seaborn as sns
import phitter
import numpy as np

# Load Palmer Penguins dataset
penguins = sns.load_dataset('penguins')
flipper_lengths = penguins['flipper_length_mm'].dropna().values

# Initialize Phitter
phi = phitter.PHITTER(
    data=flipper_lengths,
    fit_type="continuous",
    confidence_level=0.95
)
phi.fit()

# Verify data is loaded
print("Sample size:", len(flipper_lengths))
print("Flipper length range: {:.1f}mm to {:.1f}mm".format(
    min(flipper_lengths), max(flipper_lengths)))

Output:

 
Sample size: 342
Flipper length range: 172.0mm to 231.0mm

Our dataset contains 342 penguin flipper measurements spanning from 172mm to 231mm. 

Traditional Distribution Visualization

Before exploring ECDF analysis, let’s visualize our data using Phitter’s traditional histogram and distribution fitting capabilities:

 
# Visualize the distribution of flipper lengths
phi.plot_histogram_distributions()

Histogram showing the distribution of data with various fitted statistical models, marked with a curve.
Image By Author
 

The histogram reveals several interesting characteristics of our data:
– Multiple peaks suggesting different penguin species groups
– Several potential distribution fits, with beta and generalized_gamma_4p distributions showing promising fits (SSE values of 8.5601E-04 and 1.000E-03 respectively)
– A wide range of flipper lengths (172mm to 231mm) with varying densities

While the histogram provides a good initial view of our data’s distribution, it has limitations:
1. Bin width choices can significantly affect the visualization
2. It can be challenging to compare multiple distributions visually
3. The tail behavior of distributions is often hard to assess

These limitations lead us to explore ECDF analysis as a complementary visualization technique. ECDF plots offer several advantages over traditional histograms, particularly when validating distribution fits. 

Introducing ECDF Analysis

The Empirical Cumulative Distribution Function (ECDF) provides a different perspective on our data distribution. Unlike histograms that show density, ECDF plots display the cumulative probability of observing values less than or equal to each point in our dataset.

Let’s create a basic ECDF plot using Phitter:

 
# Generate basic ECDF plot
phi.plot_ecdf()

Empirical Cumulative Distribution Function (ECDF) plot showing the cumulative distribution of data values.
Image By Author
 

The ECDF plot reveals several key insights about our flipper length data:

1. Data Range: The plot starts rising at around 172mm and reaches 1.0 by 231mm, confirming our full range of measurements

2. Distribution Shape:
– The steeper sections (around 190mm and 210mm) indicate clusters of measurements
– The varying slope suggests multiple groups within our data, consistent with different penguin species

3. Key Percentiles:
– The median flipper length occurs where the curve crosses 0.5 on the y-axis
– We can easily read off any percentile by looking at the y-axis value

Advantages of ECDF over histograms:
– No binning decisions required
– Preserves individual data points
– Easier to compare multiple distributions
– Better visualization of tail behavior
– Direct reading of percentiles

However, while ECDF provides an excellent visual tool, it’s important to remember that proper distribution validation requires both visual inspection and statistical testing. In the next section, we’ll explore how to use ECDF to compare our empirical data with theoretical distributions. 

Comparing Distributions with ECDF

After examining our data’s empirical distribution, we can use Phitter’s ECDF comparison capabilities to evaluate how well theoretical distributions fit our data. Let’s compare our data with the best-fitting distribution:

 
# Compare empirical distribution with best theoretical fits
best_dist = phi.best_distribution['id']

# Plot ECDF comparison for best fit
phi.plot_ecdf_distribution(best_dist)

Comparison of the Empirical Cumulative Distribution Function (ECDF) with a fitted Beta distribution.
Image By Author
 

The comparison plot overlays our empirical distribution (shown in gray) with the fitted beta distribution (shown in red). Let’s examine how well they align:

1. Overall Fit:
– The beta distribution closely follows our empirical data
– Areas where the lines overlap indicate excellent fit
– Small deviations appear around the middle range (200-210mm)

2. Tail Behavior:
– Good alignment in both lower tail (170-180mm) and upper tail (220-230mm)
– This suggests the beta distribution captures extreme values well

Let’s look at the statistical summary of our top distributions:

 
# Print summary of top distributions
print("\nTop 3 distributions by fit quality:")
phi.summarize(k=3)

Table listing the top 3 statistical distributions by fit quality, displaying parameters and fit tests.
Image By Author
 

The summary reveals three competitive distributions:
1. Beta: SSE = 0.000856
2. Generalized Gamma 4P: SSE = 0.001000
3. Johnson SB: SSE = 0.000725

While the ECDF plots provide valuable visual validation, notice that our top distributions show different results in statistical tests. This leads us to our next section, where we’ll explore the importance of statistical validation alongside visual inspection. 

Statistical Validation

While ECDF plots provide excellent visual validation, proper distribution fitting requires statistical confirmation. Phitter provides three key goodness-of-fit tests to validate our distribution choices:

Key observations from our statistical tests:

1. SSE (Sum of Squared Errors):
– All three distributions show very low SSE values (< 0.001)
– Johnson SB has the lowest SSE but fails all three statistical tests

2. Test Results:
– Only the Kolmogorov-Smirnov test is passed by the beta and generalized gamma distributions
– No distribution passes all three tests
– Chi-square and Anderson-Darling tests suggest potential issues with all fits

3. Implications:
– Visual ECDF fit (as seen earlier) doesn’t guarantee statistical significance
– Multiple tests provide different perspectives on fit quality
– Trade-offs exist between different measures of fit

Conclusion

Our analysis of penguin flipper lengths demonstrates that effective distribution fitting requires both visual and statistical validation. The ECDF proved to be a powerful visualization tool, revealing nuances in our data that traditional histograms might miss, particularly in tail behavior and cumulative probability distributions. However, our case study also highlighted an important lesson: even distributions with excellent visual fits can fail formal statistical tests.

The beta distribution, despite showing strong visual alignment in ECDF plots and passing the Kolmogorov-Smirnov test, failed other statistical validations. This outcome underscores an important principle in distribution fitting: no single method tells the complete story. Visual inspection through ECDF provides intuitive insights into your data’s behavior, while statistical tests offer rigorous validation from different mathematical perspectives. The most suitable distribution for your analysis may not be the one with the cleanest statistical profile, but rather the one that effectively balances analytical requirements with statistical rigor.

Leave a Reply

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