From 1c26cd11bd32e15b8e1541f1ddfb564ad390bda0 Mon Sep 17 00:00:00 2001 From: Christopher Toth Date: Mon, 19 Oct 2026 17:08:56 -0600 Subject: [PATCH 1/2] Normalize fusion without product-scale zero tests --- .../fix-fusion-product-scaling-issue-3.md | 22 +++++ src/doxa/opinion.py | 81 ++++++++++-------- tests/test_fusion_scaling.py | 85 +++++++++++++++++++ 3 files changed, 151 insertions(+), 37 deletions(-) create mode 100644 agent-notes/fix-fusion-product-scaling-issue-3.md create mode 100644 tests/test_fusion_scaling.py diff --git a/agent-notes/fix-fusion-product-scaling-issue-3.md b/agent-notes/fix-fusion-product-scaling-issue-3.md new file mode 100644 index 0000000..8d5b2b1 --- /dev/null +++ b/agent-notes/fix-fusion-product-scaling-issue-3.md @@ -0,0 +1,22 @@ +# Doxa issue 3 + +## Goal +Fix product-scale-dependent WBF/CCF behavior and open a separate draft PR. No Propstore source/pin edits and no merge or ready transition. + +## State +Separate branch/worktree fix/fusion-product-scaling-issue-3 at published aa137b0. Global instructions, Doxa source/tests/CI, local notes, and exact Propstore caller chains already read; no local project instructions found. + +## Smallest change and decision +Roughly forty source lines of numerical reformulation at the two existing operators. WBF's common product cancels, leaving weights (1-u)/u. CCF normalizes nonnegative compromise terms after subtracting their largest log scale; no change to mathematical formulas, base-rate convention, or genuinely zero-compromise fallback. Each residual belief/disbelief vector contains an exact zero because consensus is its minimum, so their full products vanish and need not be subtracted from the whole-frame product. + +## Evidence and scope +At aa137b0 src/doxa/opinion.py:467-486 WBF rejects a positive product-scaled denominator below 1e-9. Lines 692-698 similarly route a positive CCF compromise sum below 1e-9 into the zero branch. The reported 14/16-source examples are representable and occur before underflow. Larger profiles also underflow the direct products, so tests cover both regimes. + +## Next step +Initial eleven scaling regressions: eight failed and three passed before implementation. After cancelling WBF's common product and rescaling CCF compromise terms in log space, all eleven passed. Added two more CCF cases in which belief, disbelief, and whole-frame compromise share weights copies,copies,1, including 600 sources where every raw product underflows. The final regression file passes all thirteen in 0.65s. + +Command: `uv run --no-project --offline --python C:/Users/Q/code/doxa/.venv/Scripts/python.exe python -B -m pytest -p no:cacheprovider tests/test_fusion_scaling.py -q`, with PYTHONPATH selecting this src and UV_CACHE_DIR selecting this worktree's cache. No dependency installation. + +For the reported x=(.6,.3,.1), y=(.4,.3,.3) pools: at fourteen sources, CCF compromise mass is 3.0618e-10; at sixteen sources WBF denominator is 5.94864e-11. Both are positive representable values below 1e-9, so the original failure is a false zero test, not underflow. Actual product underflow is separately covered by the 400/600-source regressions. + +Next: commit, publish a draft PR, and read CI's full-suite/typing results. Base rates, dogmatic routing, and the true-zero fallback remain at their current owners. diff --git a/src/doxa/opinion.py b/src/doxa/opinion.py index d76bb15..371466f 100644 --- a/src/doxa/opinion.py +++ b/src/doxa/opinion.py @@ -462,14 +462,9 @@ def wbf(cls, *opinions: Opinion) -> Opinion: sum(op.a for op in opinions) / N, ) - # Definition 4 Case 1. For each source, the belief/disbelief term is - # weighted by (1-u_i) times the product of every other uncertainty. - total_prod = math.prod(op.u for op in opinions) - prod_except = [total_prod / op.u for op in opinions] - weighted_terms = [ - (1.0 - op.u) * prod_except[i] - for i, op in enumerate(opinions) - ] + # Definition 4 Case 1. Cancel the common product of uncertainties + # before normalization: its magnitude depends exponentially on N. + weighted_terms = [(1.0 - op.u) / op.u for op in opinions] num_b = sum( op.b * weighted_terms[i] @@ -479,11 +474,11 @@ def wbf(cls, *opinions: Opinion) -> Opinion: op.d * weighted_terms[i] for i, op in enumerate(opinions) ) - num_u = sum(1.0 - op.u for op in opinions) * total_prod + num_u = sum(1.0 - op.u for op in opinions) kappa = sum(weighted_terms) - if abs(kappa) < _TOL: - raise ValueError("WBF denominator κ ≈ 0") + if kappa <= 0.0: + raise ValueError("WBF denominator must be positive") b_fused = num_b / kappa d_fused = num_d / kappa @@ -669,41 +664,53 @@ def _ccf_binomial(opinions: list[Opinion]) -> Opinion: # Product of all uncertainties (pre-uncertainty mass). u_pre = math.prod(op.u for op in opinions) - # Term 1 of Eq. 7 (residue weighted by other actors' u) plus term - # 2 for x ∈ R(X) (joint commitment to the same singleton). For - # binomial singletons, terms 3 and 4 of Eq. 7 contribute zero - # when computing b^comp({x}) or b^comp({¬x}). - sum_b_res_weighted = sum( - res_b[i] * math.prod(opinions[j].u for j in range(N) if j != i) - for i in range(N) - ) - sum_d_res_weighted = sum( - res_d[i] * math.prod(opinions[j].u for j in range(N) if j != i) - for i in range(N) + # Each residual vector contains a zero at its consensus minimum, so + # prod(res_b) = prod(res_d) = 0. Keep the remaining positive compromise + # terms in log space until a common scale can be removed. + log_u = [math.log(op.u) if op.u > 0.0 else 0.0 for op in opinions] + zero_count = sum(op.u <= 0.0 for op in opinions) + log_u_product = math.fsum(log_u) + log_other_u = [ + log_u_product - log_u[i] + if zero_count == 0 + else log_u_product + if zero_count == 1 and op.u <= 0.0 + else -math.inf + for i, op in enumerate(opinions) + ] + log_comp_b = [ + math.log(residual) + log_other_u[i] + for i, residual in enumerate(res_b) + if residual > 0.0 + ] + log_comp_d = [ + math.log(residual) + log_other_u[i] + for i, residual in enumerate(res_d) + if residual > 0.0 + ] + log_comp_X = sum( + math.log(b + d) if b + d > 0.0 else -math.inf + for b, d in zip(res_b, res_d) ) - comp_b = sum_b_res_weighted + math.prod(res_b) - comp_d = sum_d_res_weighted + math.prod(res_d) - - # b^comp(X) — disagreement term. Inclusion-exclusion form of - # "all residual-mass tuples minus the all-{x} and all-{¬x} tuples." - prod_res_total = math.prod(res_b[i] + res_d[i] for i in range(N)) - comp_X = prod_res_total - math.prod(res_b) - math.prod(res_d) - - comp_sum = comp_b + comp_d + comp_X + scale = max((*log_comp_b, *log_comp_d, log_comp_X)) # --- Step 3: Normalization --- - # When comp_sum ≈ 0 (no compromise mass to distribute), put any + # When every compromise term is zero, put any # residual missing mass directly into u. This handles self-fusion # and full-agreement edge cases that Def 5 Remark 4 doesn't cover. - if comp_sum < _TOL: + if scale == -math.inf: b_fused = cons_b d_fused = cons_d u_fused = max(0.0, 1.0 - cons_sum) else: - eta = (1.0 - cons_sum - u_pre) / comp_sum - u_fused = u_pre + eta * comp_X - b_fused = cons_b + eta * comp_b - d_fused = cons_d + eta * comp_d + comp_b = math.fsum(math.exp(term - scale) for term in log_comp_b) + comp_d = math.fsum(math.exp(term - scale) for term in log_comp_d) + comp_X = math.exp(log_comp_X - scale) + comp_sum = comp_b + comp_d + comp_X + remaining = 1.0 - cons_sum - u_pre + u_fused = u_pre + remaining * (comp_X / comp_sum) + b_fused = cons_b + remaining * (comp_b / comp_sum) + d_fused = cons_d + remaining * (comp_d / comp_sum) # Clamp tiny float drift from the constructor. b_fused = max(0.0, b_fused) diff --git a/tests/test_fusion_scaling.py b/tests/test_fusion_scaling.py new file mode 100644 index 0000000..6b93154 --- /dev/null +++ b/tests/test_fusion_scaling.py @@ -0,0 +1,85 @@ +from __future__ import annotations + +import math + +import pytest + +from doxa import Opinion +from doxa.argumentation import BipolarOpinionGraph, evaluate + + +def _pool(copies: int) -> list[Opinion]: + return [Opinion(.6, .3, .1, .5)] * copies + [ + Opinion(.4, .3, .3, .5) + ] * copies + + +@pytest.mark.parametrize("copies", [1, 6, 8, 200]) +def test_wbf_preserves_the_answer_when_the_profile_is_replicated(copies: int) -> None: + fused = Opinion.wbf(*_pool(copies)) + + assert (fused.b, fused.d, fused.u, fused.a) == pytest.approx( + (19 / 34, .3, 12 / 85, .5) + ) + + +@pytest.mark.parametrize("copies", [6, 7, 8, 200]) +def test_ccf_normalizes_positive_compromise_mass_at_any_scale(copies: int) -> None: + pool = _pool(copies) + fused = Opinion.ccf(*pool) + # Every residual disbelief is zero. All compromise mass therefore goes + # to belief, leaving only the product of uncertainties outside consensus. + uncertainty = math.prod(opinion.u for opinion in pool) + + assert fused.b == pytest.approx(.7 - uncertainty) + assert fused.d == pytest.approx(.3) + assert fused.u == pytest.approx(uncertainty, rel=1e-10, abs=1e-300) + + +@pytest.mark.parametrize("copies", [8, 200]) +def test_auto_retains_wbf_for_large_nondogmatic_profiles(copies: int) -> None: + fused = Opinion.fuse(*_pool(copies)) + + assert (fused.b, fused.d, fused.u) == pytest.approx((19 / 34, .3, 12 / 85)) + + +@pytest.mark.parametrize("copies", [8, 300]) +def test_ccf_preserves_ratios_when_all_compromise_products_are_tiny(copies: int) -> None: + pool = [Opinion(.6, .2, .2, .5)] * copies + [ + Opinion(.4, .4, .2, .5) + ] * copies + fused = Opinion.ccf(*pool) + uncertainty = math.prod(opinion.u for opinion in pool) + remaining = .4 - uncertainty + # The common product cancels to compromise weights copies,copies,1. + denominator = 2 * copies + 1 + + assert (fused.b, fused.d, fused.u) == pytest.approx( + ( + .4 + remaining * copies / denominator, + .2 + remaining * copies / denominator, + uncertainty + remaining / denominator, + ) + ) + + +def test_graph_accrual_normalizes_a_fourteen_parent_compromise() -> None: + pool = _pool(7) + leaves = tuple(str(index) for index in range(len(pool))) + edges = frozenset((leaf, "target") for leaf in leaves) + graph = BipolarOpinionGraph( + arguments=frozenset((*leaves, "target")), + intrinsic={ + **dict(zip(leaves, pool)), + "target": Opinion.vacuous(.5), + }, + supports=edges, + attacks=frozenset(), + edge_opinions={edge: Opinion.dogmatic_true(.5) for edge in edges}, + ) + + fused = evaluate(graph)["target"] + + assert fused.b == pytest.approx(.69999999997813) + assert fused.d == pytest.approx(.3) + assert fused.u == pytest.approx(2.187e-11, rel=1e-10, abs=1e-300) From 8c97aba18e1773dc10340304d902365a1446f80a Mon Sep 17 00:00:00 2001 From: Christopher Toth Date: Mon, 19 Oct 2026 17:10:39 -0600 Subject: [PATCH 2/2] docs: record fusion draft PR and CI evidence --- agent-notes/fix-fusion-product-scaling-issue-3.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent-notes/fix-fusion-product-scaling-issue-3.md b/agent-notes/fix-fusion-product-scaling-issue-3.md index 8d5b2b1..cbce35a 100644 --- a/agent-notes/fix-fusion-product-scaling-issue-3.md +++ b/agent-notes/fix-fusion-product-scaling-issue-3.md @@ -19,4 +19,4 @@ Command: `uv run --no-project --offline --python C:/Users/Q/code/doxa/.venv/Scri For the reported x=(.6,.3,.1), y=(.4,.3,.3) pools: at fourteen sources, CCF compromise mass is 3.0618e-10; at sixteen sources WBF denominator is 5.94864e-11. Both are positive representable values below 1e-9, so the original failure is a false zero test, not underflow. Actual product underflow is separately covered by the 400/600-source regressions. -Next: commit, publish a draft PR, and read CI's full-suite/typing results. Base rates, dogmatic routing, and the true-zero fallback remain at their current owners. +Published and fetched draft PR https://github.com/ctoth/doxa/pull/4 at source commit 1c26cd11bd32e15b8e1541f1ddfb564ad390bda0. CI run 39339779362 passed typing and full-suite tests on Python 3.11, 3.12, and 3.13. Base rates, dogmatic routing, and the true-zero fallback remain at their current owners. No merge or ready transition performed. This final note update records those results; verify the note-only final head before handoff.