import Mathlib /-! # A counterexample to Conjecture 2 of Malinovsky (2020) Y. Malinovsky, "Conjectures on optimal nested generalized group testing algorithm", Applied Stochastic Models in Business and Industry 36 (2020) 1029-1036, arXiv:1805.01345. Restated as open in Malinovsky and Skorniakov, arXiv:2506.15797 (2025). Setting: N units, unit i is defective with probability p_i, independently. A pooled test of a set of units is positive exactly when the set contains a defective unit. Conjecture 2: if every p_i lies in [1 - 1/sqrt 2, (3 - sqrt 5)/2], then the generalized pairwise testing algorithm (GPTA), run on the best initial order of the units, is an optimal nested procedure. Counterexample: seven units, three with p = 0.38 and four with p = 0.30. A nested procedure needs 6.4821401768 tests on average. GPTA needs at least 6.4840553768 for every initial order. STATUS: machine-checked on 26 November 2026 at https://live.lean-lang.org (Lean 4.37.0-rc1, "Latest Mathlib" project), no errors and no warnings. The check was run on this file with the comments removed; comments do not affect what Lean verifies. Axioms reported by Lean: every_arrangement, alt_ok, gpta_best: propext, Classical.choice (kernel only). conjecture2_false: the same plus Quot.sound and one native_decide axiom (gpta_all, 5040 orders evaluated by compiled code). A defect pattern is a number D below 128: unit i is defective when bit i of D is set. All quantities are exact natural numbers: probabilities are scaled by 100^7. A function q gives 100 times the probability that a unit is good (62 or 70). -/ namespace GeneralizedGroupTesting /-- An adaptive procedure: stop, or test a pool and continue with the first branch if the pool is negative and with the second branch if it is positive. -/ inductive Proc where | done : Proc | test : List Nat → Proc → Proc → Proc def allUnits : List Nat := [0, 1, 2, 3, 4, 5, 6] /-- The pool S tests positive on defect pattern D. -/ def isPos (D : Nat) (S : List Nat) : Bool := S.any (fun i => D.testBit i) /-- Number of tests the procedure uses on defect pattern D. -/ def run : Proc → Nat → Nat | .done, _ => 0 | .test S n p, D => 1 + (if isPos D S then run p D else run n D) /-- The sequence of test results the procedure sees on defect pattern D. -/ def trace : Proc → Nat → List Bool | .done, _ => [] | .test S n p, D => if isPos D S then true :: trace p D else false :: trace n D /-- 100^7 times the probability of defect pattern D. -/ def weight (q : Nat → Nat) (D : Nat) : Nat := ((List.range 7).map (fun i => if D.testBit i then 100 - q i else q i)).prod /-- 100^7 times the expected number of tests. -/ def cost (q : Nat → Nat) (P : Proc) : Nat := ((List.range 128).map (fun D => weight q D * run P D)).sum /-- The procedure identifies every defect pattern: different patterns give different sequences of results. -/ def valid (P : Proc) : Bool := decide ((List.range 128).map (trace P)).Nodup def norm (X : List Nat) : List Nat := if X.length = 1 then [] else X /-- The procedure is nested (Sobel and Groll 1959; Malinovsky 2020, Section 1.1). B is the binomial set and D the set known to contain a defective. If D is empty a subset of B is tested. If D is not empty a proper subset of D is tested; when that test is positive the rest of D returns to B. A set D with one unit is classified. -/ def nested : Proc → List Nat → List Nat → Bool | .done, B, D => B.isEmpty && D.isEmpty | .test S n p, B, D => !S.isEmpty && decide S.Nodup && (if D.isEmpty then S.all (fun x => B.contains x) && nested n (B.filter (fun x => !S.contains x)) [] && nested p (B.filter (fun x => !S.contains x)) (norm S) else S.all (fun x => D.contains x) && decide (S.length < D.length) && nested n B (norm (D.filter (fun x => !S.contains x))) && nested p (B ++ D.filter (fun x => !S.contains x)) (norm S)) /-- GPTA (Malinovsky 2020, Definition 1) on the order a, b, rest. Test the pair a, b. If negative, go on with rest. If positive, test the unit j1 with the larger q. If j1 is good the other unit j2 is defective and the procedure goes on with rest. If j1 is defective the procedure goes on with j2, rest. -/ def gp (q : Nat → Nat) : Nat → List Nat → Proc | a, [] => .test [a] .done .done | a, b :: rest => let j1 := if q b ≤ q a then a else b let j2 := if q b ≤ q a then b else a let cont : Proc := match rest with | [] => .done | c :: r => gp q c r .test [a, b] cont (.test [j1] cont (gp q j2 rest)) def gptaProc (q : Nat → Nat) : List Nat → Proc | [] => .done | a :: rest => gp q a rest /-- The competing nested procedure. It also tests pairs, but it chooses each pair from the units that are left: a 0.62 unit with a 0.70 unit when it can, the two 0.70 units when one 0.62 unit and two 0.70 units are left. Its arguments are a bound on the recursion depth, the 0.62 units left and the 0.70 units left. -/ def alt : Nat → List Nat → List Nat → Proc | 0, _, _ => .done | _ + 1, [], [] => .done | _ + 1, [l], [] => .test [l] .done .done | f + 1, l1 :: l2 :: r, [] => .test [l1, l2] (alt f r []) (.test [l1] (alt f r []) (alt f (l2 :: r) [])) | _ + 1, [], [h] => .test [h] .done .done | f + 1, [], h1 :: h2 :: r => .test [h1, h2] (alt f [] r) (.test [h1] (alt f [] r) (alt f [] (h2 :: r))) | f + 1, [l], [h1, h2] => .test [h1, h2] (alt f [l] []) (.test [h1] (alt f [l] []) (alt f [l] [h2])) | f + 1, l :: lr, h :: hr => .test [l, h] (alt f lr hr) (.test [h] (alt f lr hr) (alt f (l :: lr) hr)) /-! ## Part A. Units with names: units 0, 1, 2 have q = 0.62 and units 3..6 have q = 0.70 -/ def qn (i : Nat) : Nat := if i < 3 then 62 else 70 def altP : Proc := alt 14 [0, 1, 2] [3, 4, 5, 6] /-- The weights add up to 100^7, so cost / 100^7 is an expected value. -/ theorem weight_total : ((List.range 128).map (weight qn)).sum = 100 ^ 7 := by decide +kernel /-- The competing procedure is valid and nested, and its expected number of tests is 648214017680000 / 100^7 = 6.4821401768. Checked by the Lean kernel. -/ theorem alt_ok : valid altP = true ∧ nested altP allUnits [] = true ∧ cost qn altP = 648214017680000 := by decide +kernel /-- The best order for GPTA: valid, nested, expected number of tests 6.4840553768. Checked by the Lean kernel. -/ theorem gpta_best : valid (gptaProc qn [0, 3, 4, 1, 5, 6, 2]) = true ∧ nested (gptaProc qn [0, 3, 4, 1, 5, 6, 2]) allUnits [] = true ∧ cost qn (gptaProc qn [0, 3, 4, 1, 5, 6, 2]) = 648405537680000 := by decide +kernel /-- No order of the seven named units does better. All 5040 orders are evaluated by compiled code (native_decide). Part B repeats the check inside the kernel. -/ theorem gpta_all : ∀ σ ∈ allUnits.permutations, nested (gptaProc qn σ) allUnits [] = true ∧ 648405537680000 ≤ cost qn (gptaProc qn σ) := by native_decide /-- Conjecture 2 fails: a valid nested procedure beats GPTA for every order of the units. -/ theorem conjecture2_false : ∃ P : Proc, valid P = true ∧ nested P allUnits [] = true ∧ ∀ σ : List Nat, σ.Perm allUnits → cost qn P < cost qn (gptaProc qn σ) := by refine ⟨altP, alt_ok.1, alt_ok.2.1, fun σ h => ?_⟩ have h1 := (gpta_all σ (List.mem_permutations.2 h)).2 have h2 := alt_ok.2.2 omega /-! ## Part B. The same result, checked by the kernel alone Units of equal probability are interchangeable, so an order is fixed by saying which kind of unit stands at each of the seven places of the queue. There are 35 such arrangements. Place i holds a unit with q = arrangement m i, and GPTA runs through the places 0, 1, ..., 6 in order. -/ /-- The arrangement coded by m: place i holds a 0.62 unit when bit i of m is set. -/ def arrangement (m : Nat) : List Nat := (List.range 7).map (fun i => if m.testBit i then 62 else 70) def qOf (τ : List Nat) (i : Nat) : Nat := τ.getD i 0 def lows (τ : List Nat) : List Nat := (List.range 7).filter (fun i => τ.getD i 0 == 62) def highs (τ : List Nat) : List Nat := (List.range 7).filter (fun i => τ.getD i 0 == 70) /-- For every arrangement with three 0.62 units and four 0.70 units, the competing procedure is valid and nested and costs 6.4821401768, and GPTA costs at least 6.4840553768. Checked by the Lean kernel. -/ theorem every_arrangement : ∀ m ∈ List.range 128, (arrangement m).count 62 = 3 → valid (alt 14 (lows (arrangement m)) (highs (arrangement m))) = true ∧ nested (alt 14 (lows (arrangement m)) (highs (arrangement m))) allUnits [] = true ∧ cost (qOf (arrangement m)) (alt 14 (lows (arrangement m)) (highs (arrangement m))) = 648214017680000 ∧ 648405537680000 ≤ cost (qOf (arrangement m)) (gptaProc (qOf (arrangement m)) allUnits) := by decide +kernel /-! ## The two probabilities lie in the interval of the conjecture -/ theorem probabilities_in_range : 1 - 1 / Real.sqrt 2 ≤ (30 : ℝ) / 100 ∧ (38 : ℝ) / 100 ≤ (3 - Real.sqrt 5) / 2 := by have h2 : Real.sqrt 2 ^ 2 = 2 := Real.sq_sqrt (by norm_num) have h5 : Real.sqrt 5 ^ 2 = 5 := Real.sq_sqrt (by norm_num) have n2 : 0 ≤ Real.sqrt 2 := Real.sqrt_nonneg 2 have n5 : 0 ≤ Real.sqrt 5 := Real.sqrt_nonneg 5 have p2 : 0 < Real.sqrt 2 := Real.sqrt_pos.2 (by norm_num) have a2 : Real.sqrt 2 ≤ 10 / 7 := by nlinarith have a5 : Real.sqrt 5 ≤ 56 / 25 := by nlinarith constructor · have h7 : (7 : ℝ) / 10 ≤ 1 / Real.sqrt 2 := by rw [le_div_iff₀ p2] linarith linarith · linarith end GeneralizedGroupTesting #print axioms GeneralizedGroupTesting.conjecture2_false #print axioms GeneralizedGroupTesting.every_arrangement #print axioms GeneralizedGroupTesting.gpta_best #print axioms GeneralizedGroupTesting.probabilities_in_range