Skip to content

fluxopt.leontief

Cross-effect propagation: the Leontief inverse of contribution_from.

An effect that contributes to another — CO2 priced into cost — makes the effect system a linear one, and (I - C)^-1 is what solves it. Two consumers need it and neither is the other's: the binder folds it into the coefficients it hands the solver (so the model never multiplies it at build time), and the post-solve breakdown applies it to attribute the full priced-in cost to each contributor.

It lives here because it is neither's. Before, the binder imported it from the reader, which had the dependency backwards.

Functions:

Name Description
leontief

Compute Leontief inverse (I - C)^-1 from cross-effect coefficients.

apply_leontief

Apply Leontief inverse to an array with an effect dimension.

leontief

leontief(cf: DataArray) -> DataArray

Compute Leontief inverse (I - C)^-1 from cross-effect coefficients.

Parameters:

Name Type Description Default

cf

DataArray

Cross-effect coefficients with dims (effect, source_effect) and optionally extra batch dims (e.g. time).

required
Source code in src/fluxopt/leontief.py
def leontief(cf: xr.DataArray) -> xr.DataArray:
    """Compute Leontief inverse (I - C)^-1 from cross-effect coefficients.

    Args:
        cf: Cross-effect coefficients with dims ``(effect, source_effect)``
            and optionally extra batch dims (e.g. ``time``).
    """
    n = cf.sizes['effect']
    batch_dims = [d for d in cf.dims if d not in ('effect', 'source_effect')]
    ordered = [*batch_dims, 'effect', 'source_effect']
    mat = np.eye(n) - cf.transpose(*ordered).values  # (..., n, n)
    return xr.DataArray(_inverse_per_batch(mat), dims=ordered, coords=cf.coords).transpose(*cf.dims)

apply_leontief

apply_leontief(leontief: DataArray, arr: DataArray) -> DataArray

Apply Leontief inverse to an array with an effect dimension.

Parameters:

Name Type Description Default

leontief

DataArray

Leontief inverse (effect, source_effect[, ...]).

required

arr

DataArray

Array whose effect dim is contracted over.

required
Source code in src/fluxopt/leontief.py
def apply_leontief(
    leontief: xr.DataArray,
    arr: xr.DataArray,
) -> xr.DataArray:
    """Apply Leontief inverse to an array with an ``effect`` dimension.

    Args:
        leontief: Leontief inverse ``(effect, source_effect[, ...])``.
        arr: Array whose ``effect`` dim is contracted over.
    """
    result: xr.DataArray = xr.dot(leontief, arr.rename({'effect': 'source_effect'}), dim='source_effect', optimize=True)
    return result