leanfe is designed for speed and memory efficiency. It automatically selects the optimal algorithm based on your data:
YOCO compression + sparse matrices for low/medium-cardinality FEs (< 10K levels per FE, < 20K total)
FWL iterative demeaning for high-cardinality FEs (> 10K levels per FE or > 20K total)
Both approaches produce identical results to traditional methods, just faster.
TipComprehensive Benchmarks
For detailed benchmarks across all 4 backend/language combinations (Python/Polars, Python/DuckDB, R/Polars, R/DuckDB) and all dataset sizes (100K to 100M), see Comprehensive Benchmarks.
How It Works
Smart Strategy Selection
leanfe analyzes your fixed effects and automatically chooses the fastest approach:
FE Cardinality
Strategy
Why
Low (< 10K levels)
YOCO + Sparse
Compression reduces data dramatically; sparse matrices are efficient
DuckDB can process datasets larger than available RAM by streaming from parquet files. The memory measurements above show peak Python heap usage during regression.
When to Use Each Backend
Use Polars (default) when:
Speed is the priority
Data fits comfortably in memory
Running many regressions on the same data
Use DuckDB when:
Dataset is larger than available RAM
Memory is constrained
Reading directly from parquet files
Compression Ratio
The compression ratio depends on the number of unique (regressor, FE) combinations. With discrete regressors (binary treatment + categorical x1), YOCO achieves significant compression:
# Show compression across different dataset sizesprint("Compression Ratio by Dataset Size")