IndividualUnitAverager

class unit_averaging.averager.IndividualUnitAverager(focus_function, ind_estimates)[source]

Bases: BaseUnitAverager

Unit averaging scheme that assigns all weight to the target unit.

This class implements a unit averaging scheme where all weight is assigned to the target unit, effectively ignoring all other units. This is useful when the focus is solely on the target unit’s estimate and for comparing other averaging schemes with no averaging using the same interface.

Parameters:
  • focus_function (BaseFocusFunction) – Focus function expressing the transformation of interest.

  • ind_estimates (np.ndarray | list | dict[str | int, np.ndarray | list]) – Individual unit estimates. Can be a list, numpy array, or dictionary. Each unit-specific estimate should be a NumPy array or a list. The first dimension of ind_estimates indexes units (rows or dictionary entries).

Attributes:
  • ind_estimates (np.ndarray) – Array of individual unit estimates.

  • keys (np.ndarray) – Array of keys corresponding to the units. The individual estimates are converted to numpy arrays internally. If ind_estimates is a dictionary, the keys are preserved in the keys attribute. If ind_estimates is a list or array, keys defaults to numeric indices (0, 1, 2, …).

  • weights (np.ndarray) – The computed weights for each unit. For this scheme, the weight for the target unit is 1.0, and the weights for all other units are 0.0.

  • estimate (float) – The computed unit averaging estimate, which is simply the target unit’s estimate.

  • focus_function (BaseFocusFunction) – Focus function expressing the transformation of interest.

  • target_id (int | str) – The ID of the target unit. Initialized as None, set by calling fit().

Example

>>> from unit_averaging import IndividualUnitAverager, InlineFocusFunction
>>> import numpy as np
>>> # Define a focus function
>>> focus_function = InlineFocusFunction(lambda x: x[0], lambda x: [1, 0])
>>> # Define individual unit estimates
>>> ind_estimates = {"a": np.array([1, 2]), "b": np.array([3, 4])}
>>> # Create an IndividualUnitAverager instance
>>> averager = IndividualUnitAverager(focus_function, ind_estimates)
>>> # Fit the averager to the target unit
>>> averager.fit(target_id="b")
>>> # Print the estimate
>>> print(averager.weights)     # [0., 1.]
>>> print(averager.estimate)    # 3.0

Methods

average(focus_function=None)[source]

Perform unit averaging with the fitted weights.

This method computes the unit averaging estimate using the fitted weights. It can accept a different focus function and reuse the fitted weights.

Parameters:

focus_function (BaseFocusFunction | None) – Focus function to use in computing the averaging estimator. Expresses the parameter of interest. If None, defaults to the focus function used in fitting.

Returns:

The unit averaging estimate.

Return type:

float

Raises:

TypeError – If weights have not been fitted yet by calling fit()

fit(target_id)[source]

Compute the unit averaging weights and the averaging estimator.

Parameters:

target_id (int | str) – ID of the target unit. This is specified in terms of the keys attribute, which are either numeric indices (if ind_estimates was an array or list) or dictionary keys (if ind_estimates was a dictionary)

Raises:

ValueError – If the target unit is not found in the keys.