Skip to main content

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

Project description

unit_averaging

Optimal Estimation for Heterogeneous Data

License: MIT Python 3.12+

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. T., & Morozov, V. (2024). Unit Averaging for Heterogeneous Panels:

@misc{Brownlees2024UnitAveragingHeterogeneous,
   title = {Unit Averaging for Heterogeneous Panels},
   author = {Brownlees, Christian and Morozov, Vladislav},
   year = {2024},
   month = may,
   number = {arXiv:2210.14205},
   eprint = {2210.14205},
   primaryclass = {econ},
   publisher = {arXiv},
   doi = {10.48550/arXiv.2210.14205},
   archiveprefix = {arXiv},
}

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.0.0.tar.gz (16.0 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.0.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: unit_averaging-1.0.0.tar.gz
  • Upload date:
  • Size: 16.0 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.0.0.tar.gz
Algorithm Hash digest
SHA256 f0ca7c35c8a56284e82bc2bb379b05b814c52f9e22e751288d50bf1d155bf514
MD5 415db2ebb5b2d30b8913855241153917
BLAKE2b-256 7bb760da05e8398780f63605efc5c3dd63482c32e3b8b6a08d6df7f72adc5aa2

See more details on using hashes here.

Provenance

The following attestation bundles were made for unit_averaging-1.0.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.0.0-py3-none-any.whl.

File metadata

  • Download URL: unit_averaging-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a913ef6863d8c14718531840a75f572dbdc479a5d731d133ba2d4ee6bb857705
MD5 6b849e820fe40267e283560a66ee5c7a
BLAKE2b-256 c463706fa78a4fe2664e96b5abc75b6670ce2d6f421b5d9ace3801af8e22803f

See more details on using hashes here.

Provenance

The following attestation bundles were made for unit_averaging-1.0.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