Skip to content

Interpretation API

The interpretation functions are exact and operate through the generic BaseCapacity.event_value() interface.

shapley_index

Computes one exact Shapley importance value. The element may be an integer index or a variable name when the capacity has a named universe.

shapley_index

shapley_index(
    capacity: BaseCapacity, element: ElementLabel
) -> float

Compute one exact Shapley index through event evaluations.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def shapley_index(
    capacity: BaseCapacity,
    element: ElementLabel,
) -> float:
    """Compute one exact Shapley index through event evaluations."""
    index = _element_index(capacity, element)
    return _shapley_index(capacity, index, {})

shapley_indices

Returns all exact Shapley indices as a dictionary keyed by names or indices.

shapley_indices

shapley_indices(
    capacity: BaseCapacity,
) -> dict[ElementLabel, float]

Return exact Shapley indices, using names when they are available.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def shapley_indices(
    capacity: BaseCapacity,
) -> dict[ElementLabel, float]:
    """Return exact Shapley indices, using names when they are available."""
    _check_capacity(capacity)
    labels = _element_labels(capacity)
    cache: EventCache = {}
    return {
        label: _shapley_index(capacity, index, cache)
        for index, label in enumerate(labels)
    }

pairwise_interaction_index

Computes the exact Shapley interaction index of two distinct elements.

pairwise_interaction_index

pairwise_interaction_index(
    capacity: BaseCapacity,
    first: ElementLabel,
    second: ElementLabel,
) -> float

Compute one exact Shapley pairwise interaction from event values.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def pairwise_interaction_index(
    capacity: BaseCapacity,
    first: ElementLabel,
    second: ElementLabel,
) -> float:
    """Compute one exact Shapley pairwise interaction from event values."""
    first_index = _element_index(capacity, first)
    second_index = _element_index(capacity, second)
    if first_index == second_index:
        raise ValueError("Pairwise interaction requires two distinct elements.")
    return _pairwise_interaction_index(
        capacity,
        first_index,
        second_index,
        {},
    )

pairwise_interactions

Returns every pairwise interaction.

pairwise_interactions

pairwise_interactions(
    capacity: BaseCapacity,
) -> dict[tuple[ElementLabel, ElementLabel], float]

Return all exact pairwise interactions from event evaluations.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def pairwise_interactions(
    capacity: BaseCapacity,
) -> dict[tuple[ElementLabel, ElementLabel], float]:
    """Return all exact pairwise interactions from event evaluations."""
    _check_capacity(capacity)
    labels = _element_labels(capacity)
    cache: EventCache = {}
    return {
        (labels[first], labels[second]): _pairwise_interaction_index(
            capacity,
            first,
            second,
            cache,
        )
        for first, second in combinations(range(capacity.n_elements), 2)
    }

pairwise_interaction_matrix

Returns the same information in a symmetric NumPy matrix with a zero diagonal.

pairwise_interaction_matrix

pairwise_interaction_matrix(
    capacity: BaseCapacity,
) -> ndarray

Return a symmetric matrix of exact pairwise interactions.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def pairwise_interaction_matrix(capacity: BaseCapacity) -> np.ndarray:
    """Return a symmetric matrix of exact pairwise interactions."""
    _check_capacity(capacity)
    matrix = np.zeros((capacity.n_elements, capacity.n_elements), dtype=float)
    cache: EventCache = {}
    for first, second in combinations(range(capacity.n_elements), 2):
        value = _pairwise_interaction_index(
            capacity,
            first,
            second,
            cache,
        )
        matrix[first, second] = value
        matrix[second, first] = value
    return matrix

interaction_signs

Maps pairwise interactions to -1, 0, or 1 for redundancy, numerical neutrality, or complementarity.

interaction_signs

interaction_signs(
    capacity: BaseCapacity,
) -> dict[tuple[ElementLabel, ElementLabel], int]

Classify interactions as redundant, neutral or complementary.

Source code in src/capacities_ml_fin/base/interpretation/indices.py
def interaction_signs(
    capacity: BaseCapacity,
) -> dict[tuple[ElementLabel, ElementLabel], int]:
    """Classify interactions as redundant, neutral or complementary."""
    return {
        pair: 0 if np.isclose(value, 0.0, atol=1e-12) else int(np.sign(value))
        for pair, value in pairwise_interactions(capacity).items()
    }