Capacities API
This page documents the concrete finite-capacity objects and validation utilities.
BaseCapacity
BaseCapacity defines the representation-independent interface used by integrals, interpretation functions and the risk package. The key semantic method is event_value(event).
BaseCapacity
Bases: ABC
Abstract interface for immutable finite capacities.
A capacity assigns a normalized, monotone value to every event in a finite
universe. Concrete implementations may store a full table, sparse Möbius
coefficients, or compute event values dynamically. Algorithms consume this
interface through :meth:event_value and never require materialization.
Attributes
n_elements : int Number of elements in the event universe.
Notes
Capacity instances become immutable after successful validation. Subclasses
should call self._freeze() at the end of construction.
n_elements
abstractmethod
property
Number of elements in the finite capacity universe.
event_value
abstractmethod
nested_event_values
Evaluate the nested upper sets induced by an element ordering.
Source code in src/capacities_ml_fin/base/capacities/base.py
VariableUniverse
A VariableUniverse attaches stable names and indices to a finite capacity domain. Estimators create one automatically from DataFrame column names or from the fitted feature count.
VariableUniverse
dataclass
Immutable names and index mapping for a finite universe.
Parameters
var_names : iterable of str Unique, non-empty variable names in their canonical order.
Attributes
var_names : tuple of str Canonical ordered variable names. n_elements : int Size of the universe. name_to_index : mapping of str to int Read-only lookup from each name to its zero-based index.
from_size
classmethod
from_size(n_elements: int) -> VariableUniverse
Create a universe with generated names x0, x1, ... .
Source code in src/capacities_ml_fin/base/capacities/capacities.py
from_coalitions
classmethod
from_coalitions(
coalitions: Iterable[CoalitionInput],
) -> VariableUniverse
Infer the smallest universe containing all observed members.
Source code in src/capacities_ml_fin/base/capacities/capacities.py
ExplicitCapacity
Stores a complete table of capacity values \(\mu(A)\). Construction validates monotonicity and normalization. Use this class when the full table is known or when an explicit representation is desirable for inspection.
ExplicitCapacity
ExplicitCapacity(
values: Mapping[CoalitionInput, float],
*,
universe: VariableUniverse | None = None,
n_elements: int | None = None,
var_names: Iterable[str] | None = None
)
Bases: BaseCapacity
Explicit normalized monotone capacity over a finite universe.
Parameters
values : mapping
Capacity values keyed by coalitions expressed as names, indices, or
iterables thereof. Every non-empty coalition must be present; the empty
coalition is implicit and has value zero.
universe : VariableUniverse, optional
Existing universe. Mutually exclusive with n_elements and
var_names.
n_elements : int, optional
Universe size used to generate names x0, x1, and so on.
var_names : iterable of str, optional
Ordered variable names. If no universe argument is supplied, the
smallest universe is inferred from values.
Attributes
universe : VariableUniverse Resolved event universe. values : CapacityMap Immutable table of coalition values.
Raises
ValueError If the table is incomplete, non-normalized, or non-monotone.
Source code in src/capacities_ml_fin/base/capacities/capacities.py
value
Return the value of a coalition expressed by names or indices.
Source code in src/capacities_ml_fin/base/capacities/capacities.py
event_value
Return the stored value of a boolean event mask.
Source code in src/capacities_ml_fin/base/capacities/capacities.py
to_named_dict
Return the tabular capacity values keyed by variable names.
Source code in src/capacities_ml_fin/base/capacities/capacities.py
validate
KAdditiveCapacity
A concrete explicit capacity constructed from capacity values supplied for all coalitions up to order k. Higher-order values are completed from the implied low-order Möbius representation, and exact \(k\)-additivity is validated.
This object is different from ml.optimization.KAdditivity, which is a learning parameterization rather than a stored capacity.
KAdditiveCapacity
KAdditiveCapacity(
values: Mapping[CoalitionInput, float],
k: int,
*,
universe: VariableUniverse | None = None,
n_elements: int | None = None,
var_names: Iterable[str] | None = None
)
Bases: ExplicitCapacity
Explicit capacity reconstructed from values up to order k.
Parameters
values : mapping
Capacity values for every coalition whose cardinality is at most
k. Values for larger coalitions are completed from the implied
low-order Möbius coefficients.
k : int
Exact maximum order of non-zero Möbius coefficients. Must satisfy
1 <= k <= n_elements.
universe : VariableUniverse, optional
Existing universe. Mutually exclusive with n_elements and
var_names.
n_elements : int, optional
Number of elements when names are not supplied.
var_names : iterable of str, optional
Ordered variable names.
Attributes
k : int Validated additivity order. values : CapacityMap Completed explicit capacity table.
Raises
ValueError
If required low-order coalitions are missing or the completed capacity
is not normalized, monotone, and exactly k-additive.
Source code in src/capacities_ml_fin/base/capacities/k_additive.py
MobiusCapacity
Stores a sparse map of Möbius coefficients. value(A) returns the coefficient \(m(A)\), while event_value(event) returns the capacity \(\mu(A)\) reconstructed from all stored subset coefficients.
MobiusCapacity
MobiusCapacity(
coefficients: Mapping[CoalitionInput, float],
*,
universe: VariableUniverse | None = None,
n_elements: int | None = None,
var_names: Iterable[str] | None = None,
validation_tolerance: float = 1e-09
)
Bases: BaseCapacity
Sparse Möbius representation of an evaluable finite capacity.
Parameters
coefficients : mapping
Möbius coefficients keyed by coalitions. Unspecified coefficients are
interpreted as zero; the empty coalition must not be supplied.
universe : VariableUniverse, optional
Existing universe. Mutually exclusive with n_elements and
var_names.
n_elements : int, optional
Universe size when names are not supplied.
var_names : iterable of str, optional
Ordered variable names.
validation_tolerance : float, default=1e-9
Numerical tolerance for normalization and monotonicity checks.
Attributes
universe : VariableUniverse Resolved event universe. validation_tolerance : float Tolerance used during validation.
Notes
:meth:value returns a Möbius coefficient m(A) whereas
:meth:event_value reconstructs mu(A) = sum_{B subseteq A} m(B).
The full capacity table is not stored.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
value
Return a Mobius coefficient, or zero when it is not specified.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
event_value
Evaluate the capacity of an event from its Mobius coefficients.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
nested_event_values
Evaluate nested events without expanding the capacity table.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
validate
Check normalization and monotonicity of the Mobius capacity.
to_named_dict
Return the specified coefficients keyed by variable names.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
Möbius transforms
mobius_transform
Converts a complete ExplicitCapacity into an equivalent MobiusCapacity.
mobius_transform
mobius_transform(
capacity: ExplicitCapacity,
) -> MobiusCapacity
Compute the Mobius representation of an explicit capacity.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
inverse_mobius_transform
Expands a MobiusCapacity into a complete ExplicitCapacity.
inverse_mobius_transform
inverse_mobius_transform(
mobius_capacity: MobiusCapacity,
) -> ExplicitCapacity
Compute an explicit capacity from its Mobius representation.
Source code in src/capacities_ml_fin/base/capacities/mobius.py
Validation
validate_capacity
Exhaustively checks normalization and monotonicity on a finite event domain, subject to a configurable maximum number of elements.
validate_capacity
validate_capacity(
capacity: BaseCapacity,
*,
max_elements: int = 16,
tolerance: float = 1e-10
) -> None
Check normalization and monotonicity on a finite capacity domain.
Source code in src/capacities_ml_fin/base/capacities/validation.py
is_concave_capacity
Checks submodularity/concavity by exhaustive enumeration.
is_concave_capacity
is_concave_capacity(
capacity: BaseCapacity,
*,
max_elements: int = 8,
tolerance: float = 1e-10
) -> bool
Check submodularity of a capacity by exhaustive enumeration.
Source code in src/capacities_ml_fin/base/capacities/validation.py
is_convex_capacity
Checks supermodularity/convexity by exhaustive enumeration.
is_convex_capacity
is_convex_capacity(
capacity: BaseCapacity,
*,
max_elements: int = 8,
tolerance: float = 1e-10
) -> bool
Check supermodularity of a capacity by exhaustive enumeration.
Source code in src/capacities_ml_fin/base/capacities/validation.py
Lower-level container types
The following immutable containers support capacity implementations. Most end users will not need to construct them directly, but they are useful when extending the package.
CoalitionValue
CoalitionValue
dataclass
Immutable numeric value associated with one coalition.
CapacityMap
CapacityMap
dataclass
CapacityMap(capacities: InitVar[Iterable[CoalitionValue]])
Bases: CoalitionMap
Immutable, unvalidated tabular values of a set function.
MobiusMap
MobiusMap
dataclass
MobiusMap(
mobius_coefficients: InitVar[Iterable[CoalitionValue]],
)
Bases: CoalitionMap
Immutable, potentially sparse Mobius coefficients.
Utility functions
powerset
powerset
Compute the power set of a coalition.
Source code in src/capacities_ml_fin/base/capacities/utils.py
subset_encoding
subset_encoding
Encode a coalition using an integer bitmask.
Source code in src/capacities_ml_fin/base/capacities/utils.py
subset_decoding
subset_decoding
Decode an integer bitmask into a coalition.