Skip to main content

Unit averaging in Python: efficient ensemble estimation of unit-specific parameters

Project description

unit_averaging

Optimal Estimation for Heterogeneous Data

PyPI License: MIT Python 3.12+ Docs codecov

A Python package for estimating unit-specific parameters in heterogeneous data settings (panel data and meta-analysis).
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.

Logo

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

Documentation

Quick Start

The core workflow of the package in a small synthetic example:

import numpy as np
from unit_averaging import OptimalUnitAverager, InlineFocusFunction

# Example: Forecasting with a linear focus function
x_value = 1.0  # Your 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., μ(θ) = θ₀ + θ₁ * 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")

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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unit_averaging-1.1.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unit_averaging-1.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file unit_averaging-1.1.0.tar.gz.

File metadata

  • Download URL: unit_averaging-1.1.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unit_averaging-1.1.0.tar.gz
Algorithm Hash digest
SHA256 bf31fdff557474bdc315927aca6288e6d8ae461c11208d5eb581f29881082070
MD5 1244abd7592b16c271aaefd7406fc5a9
BLAKE2b-256 a581490ac17b6970708ed7a8c1eb4249889092a3a3c76114a1880277e642e4e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for unit_averaging-1.1.0.tar.gz:

Publisher: publish-to-pypi.yml on vladislav-morozov/unit-averaging

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unit_averaging-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: unit_averaging-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unit_averaging-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76f431dd5a85bb91da200b5e149f6de1ee9fb83d02e28d63814758b182b88727
MD5 0f9cb62ecef7787ea48ab99259ea65f5
BLAKE2b-256 7df3e6ac84a267855a6fb0fdce68a684fc50a0d039193875c730cb2d04254522

See more details on using hashes here.

Provenance

The following attestation bundles were made for unit_averaging-1.1.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on vladislav-morozov/unit-averaging

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page