InlineFocusFunction

class unit_averaging.focus_function.InlineFocusFunction(focus_function, gradient)[source]

Bases: BaseFocusFunction

Convenience class for creating focus functions using callable objects.

This class allows you to create a focus function by directly passing callable objects for both the focus function and its gradient. It’s useful when the focus function and its gradient are simple enough to be defined as lambda functions or other callable objects, eliminating the need to create a full subclass of BaseFocusFunction.

Parameters:
  • focus_function (Callable) –

    A callable that implements the focus function This function should take an individual estimate (scalar or array) and return a scalar result representing the parameter of interest.

    Signature: (float | np.ndarray) -> (float | np.ndarray).

  • gradient (Callable) –

    A callable that implements the gradient of the focus function with respect to the parameters.

    Signature: (float | np.ndarray) -> np.ndarray.

Attributes:
  • _focus_function (Callable) – The stored callable implementing the focus function.

  • _gradient (Callable) – The stored callable implementing the gradient of the focus function.

Example

>>> from unit_averaging import InlineFocusFunction
>>> import numpy as np
>>> # Estimate
>>> estimate = np.array([2.0, 3.0])
>>> linear_focus = InlineFocusFunction(
...     lambda x: 2*x[0] + 3*x[1],  # Focus function: 2x0 + 3x1
...     lambda x: np.array([2.0, 3.0])  # Gradient: [2, 3]
... )
>>> result = linear_focus.focus_function(np.array([1.0, 1.0]))
>>> print(result)  # Output: 5.0
>>> grad = linear_focus.gradient(np.array([1.0, 1.0]))
>>> print(grad)  # Output: [2. 3.]

Methods

focus_function(ind_estimate)[source]

Compute the focus function for a given estimate.

This method applies the focus function transformation to an individual estimate. The focus function defines the parameter of interest that the unit averaging process aims to estimate.

Parameters:

ind_estimate (float | np.floating[Any] | np.ndarray) – Individual specific estimate. Can be a scalar or an array-like object.

Returns:

Scalar result of applying the focus function to the input estimate.

Return type:

float | np.floating[Any]

gradient(ind_estimate)[source]

Compute the gradient of the focus function for a given estimate.

This method calculates the gradient (vector of first derivatives) of the focus function with respect to the input estimate. The gradient is used in the optimization process to determine the optimal weights for unit averaging.

Parameters:

ind_estimate (float | np.floating[Any] | np.ndarray) – Individual specific estimate. Can be a scalar or an array-like object.

Returns:

Vector of first derivatives of the focus function with respect to the input estimate. The shape of the array should match the shape of the input estimate.

Return type:

np.ndarray