|
| 1 | +"""Thin operation binding for exact finite-table mutual information.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from jacobian.contracts.capabilities import CapabilityDiagnostic |
| 6 | +from jacobian.domains._examples import example |
| 7 | +from jacobian.math.probability import ( |
| 8 | + FiniteJointTableMutualInformationRequest, |
| 9 | + FiniteJointTableMutualInformationResult, |
| 10 | + mutual_information, |
| 11 | +) |
| 12 | +from jacobian.operation_bindings import inline_operation |
| 13 | +from jacobian.operations import OperationRefusalError, OperationSpec |
| 14 | + |
| 15 | +_INVALID_REQUEST = CapabilityDiagnostic( |
| 16 | + code="INVALID_FINITE_JOINT_TABLE_REQUEST", |
| 17 | + stage="finite_joint_table_input_validation", |
| 18 | + message="Input does not satisfy the bounded normalized rational joint-table contract.", |
| 19 | + hint=( |
| 20 | + "Supply unique ordered labels, a nonnegative rational table with at most " |
| 21 | + "64 cells summing exactly to one, and a log base from 2 to 36." |
| 22 | + ), |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def _execute( |
| 27 | + request: FiniteJointTableMutualInformationRequest, |
| 28 | +) -> FiniteJointTableMutualInformationResult: |
| 29 | + try: |
| 30 | + return mutual_information(request) |
| 31 | + except (ArithmeticError, OverflowError, ValueError) as exc: |
| 32 | + raise OperationRefusalError( |
| 33 | + CapabilityDiagnostic( |
| 34 | + code="MUTUAL_INFORMATION_CERTIFICATE_BOUND_EXCEEDED", |
| 35 | + stage="finite_joint_mutual_information", |
| 36 | + message=str(exc), |
| 37 | + hint="Reduce rational denominator sizes or the positive support.", |
| 38 | + ) |
| 39 | + ) from exc |
| 40 | + |
| 41 | + |
| 42 | +MUTUAL_INFORMATION_CAPABILITY = inline_operation( |
| 43 | + OperationSpec( |
| 44 | + operation_id="probability.joint.mutual_information.compute", |
| 45 | + version="1", |
| 46 | + title="Exact finite-table mutual information certificate", |
| 47 | + description=( |
| 48 | + "Compute ordered marginals and every positive-support likelihood " |
| 49 | + "ratio for one bounded normalized rational joint table. Return the " |
| 50 | + "exact identity scale*I=log_base(product), without floating point." |
| 51 | + ), |
| 52 | + request_type=FiniteJointTableMutualInformationRequest, |
| 53 | + result_type=FiniteJointTableMutualInformationResult, |
| 54 | + execute=_execute, |
| 55 | + tags=( |
| 56 | + "probability", |
| 57 | + "information-theory", |
| 58 | + "mutual-information", |
| 59 | + "finite", |
| 60 | + "exact", |
| 61 | + "certificate", |
| 62 | + ), |
| 63 | + invalid_request=_INVALID_REQUEST, |
| 64 | + invocation_examples=( |
| 65 | + example( |
| 66 | + "perfectly_correlated_fair_bits", |
| 67 | + "Compute exact base-two mutual information for two identical fair bits.", |
| 68 | + { |
| 69 | + "row_labels": ["0", "1"], |
| 70 | + "column_labels": ["0", "1"], |
| 71 | + "probabilities": [ |
| 72 | + [ |
| 73 | + {"num": "1", "den": "2"}, |
| 74 | + {"num": "0", "den": "1"}, |
| 75 | + ], |
| 76 | + [ |
| 77 | + {"num": "0", "den": "1"}, |
| 78 | + {"num": "1", "den": "2"}, |
| 79 | + ], |
| 80 | + ], |
| 81 | + "log_base": 2, |
| 82 | + }, |
| 83 | + ), |
| 84 | + ), |
| 85 | + ) |
| 86 | +) |
| 87 | + |
| 88 | +__all__ = ["MUTUAL_INFORMATION_CAPABILITY"] |
0 commit comments