Unit Averaging

Optimal Estimation for Heterogeneous Data

unit_averaging logo

unit_averaging is a Python package for estimating unit-specific parameters in heterogeneous data settings (panel data and meta-analysis). It implements unit averaging: an ensemble method that efficiently combines information across multiple units (e.g., countries, firms, or studies) while accounting for their individual differences.

Key Features

  • Optimal weighting: Automatically computes weights that balance bias and variance

  • Flexible workflow: Works with panel data, meta-analysis, and other heterogeneous datasets

  • Versatility: Can be used on top of various standard estimation packages

  • Customizable: Implement your own weighting schemes by subclassing base classes

  • Theoretically grounded: Based on statistical theory with proven performance

Installation

Install the package with pip:

pip install unit_averaging

Getting Started

New to unit averaging? Take a look at the tutorials or check out the theory section for a background on unit averaging!

Or take a look at the core workflow in a small synthetic example:

import numpy as np
from unit_averaging import OptimalUnitAverager, InlineFocusFunction

# Example: Forecasting with a linear focus function
# Replace these with your actual data!
x_value = 1.0  # Covariate (e.g., lagged value)
estimates = {   # Dict of unit-specific coefficient estimates
    "unit1": np.array([0.5, 0.3]),
    "unit2": np.array([0.7, 0.1])
}
covariances = { # Dict of unit-specific covariance matrices
    "unit1": np.array([[0.1, 0.0], [0.0, 0.1]]),
    "unit2": np.array([[0.1, 0.0], [0.0, 0.1]])
}

# Define focus function: e.g., mu(θ) = θ₀ + θ₁ * x
focus = InlineFocusFunction(
    focus_function=lambda coef: coef[0] + coef[1] * x_value,
    gradient=lambda coef: np.array([1, x_value])
)

# Create and fit averager
averager = OptimalUnitAverager(
    focus_function=focus,
    ind_estimates=estimates,
    ind_covar_ests=covariances
)
averager.fit(target_id="unit1")

Documentation

Citation

If you use unit_averaging in your research, please cite:

Brownlees C. and Morozov V. (2025). Unit Averaging for Heterogeneous Panels. Journal of Business & Economic Statistics. doi: 10.1080/07350015.2025.2584579.

@article{Brownlees2024UnitAveragingHeterogeneous,
   author = {Brownlees, Christian and Morozov, Vladislav},
   title = {{Unit Averaging for Heterogeneous Panels}},
   journal = {Journal of Business \& Economic Statistics},
   year = {2025},
   doi = {10.1080/07350015.2025.2584579},
}

Support

For questions, issues, or contributions: