from fractions import Fraction as F from itertools import product from math import comb from pathlib import Path import json import unittest import zero_runs as model def avoidance_recurrence(p, n, length): """Scalar no-run recurrence, independent of the trailing-state algorithm.""" a, b = p.numerator, p.denominator safe = [b ** step for step in range(min(n, length - 1) + 1)] if n >= length: safe.append(b ** length - (b - a) ** length) for step in range(length + 1, n + 1): safe.append(b * safe[-1] - a * (b - a) ** length * safe[step - length - 1]) return 1 - F(safe[n], b ** n) def gap_count(p, n, length): """Count bounded zero-gaps conditioned on the number of failures.""" a, b = p.numerator, p.denominator safe = 0 for failures in range(n + 1): arrangements = sum( (-1) ** excluded * comb(failures + 1, excluded) * comb(n - excluded * length, failures) for excluded in range(min(failures + 1, (n - failures) // length) + 1) ) if arrangements < 0: raise ArithmeticError("negative count of gap arrangements") safe += arrangements * a ** failures * (b - a) ** (n - failures) return 1 - F(safe, b ** n) def exhaustive(p, n, length): total, prefix, disjoint = F(), F(), F() hits = [F()] * n for sequence in product((0, 1), repeat=n): weight = p ** sum(sequence) * (1 - p) ** (n - sum(sequence)) if n >= length and not any(sequence[:length]): prefix += weight if any(not any(sequence[start:start + length]) for start in range(0, n - length + 1, length)): disjoint += weight for stop in range(length, n + 1): if not any(sequence[stop - length:stop]): total += weight hits[stop - 1] += weight break return total, prefix, disjoint, hits class ZeroWindowTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.packet = model.calculate() def test_exact_retained_packet(self): self.assertEqual(self.packet, json.loads(Path(__file__).with_name("results.json").read_text())) def test_exhaustive_small_cases_and_first_hits(self): for n in range(9): for length in range(1, n + 3): for p in (F(1, 5), F(1, 2), F(2, 3)): with self.subTest(n=n, length=length, p=p): run, prefix, disjoint, hits = exhaustive(p, n, length) actual = model.first_run(p, n, length) self.assertEqual(actual["probability"], run) self.assertEqual(actual["first_hit"], hits) self.assertEqual(model.fixed_prefix(p, n, length), prefix) self.assertEqual(model.disjoint_blocks(p, n, length), disjoint) def test_two_independent_full_scale_formulas(self): expected = model.first_run(F(1, 100), 1000, 300)["probability"] self.assertEqual(avoidance_recurrence(F(1, 100), 1000, 300), expected) self.assertEqual(gap_count(F(1, 100), 1000, 300), expected) def test_independent_oracles_small_grid(self): for n in range(13): for length in range(1, n + 3): for p in (F(0), F(1, 5), F(1, 2), F(2, 3), F(1)): expected = model.first_run(p, n, length)["probability"] self.assertEqual(avoidance_recurrence(p, n, length), expected) self.assertEqual(gap_count(p, n, length), expected) def test_probability_mass_and_first_hit_locations(self): actual = model.first_run(F(1, 100), 1000, 300) self.assertEqual(sum(actual["first_hit"], F()), actual["probability"]) self.assertTrue(all(x == 0 for x in actual["first_hit"][:299])) self.assertEqual(actual["first_hit"][299], F(99, 100) ** 300) self.assertTrue(all(x >= 0 for x in actual["first_hit"])) def test_single_window_policies_agree(self): for length in (1, 2, 7, 300): for p in (F(0), F(1, 100), F(1, 2), F(1)): expected = (1 - p) ** length self.assertEqual(model.first_run(p, length, length)["probability"], expected) self.assertEqual(model.fixed_prefix(p, length, length), expected) self.assertEqual(model.disjoint_blocks(p, length, length), expected) def test_no_time_for_a_window(self): for method in (model.fixed_prefix, model.disjoint_blocks): self.assertEqual(method(F(1, 100), 299, 300), 0) self.assertEqual(model.first_run(F(1, 100), 299, 300)["probability"], 0) def test_length_one_and_probability_endpoints(self): for p in (F(0), F(1, 100), F(1)): self.assertEqual(model.first_run(p, 17, 1)["probability"], 1 - p ** 17) self.assertEqual(model.first_run(F(0), 1000, 300)["probability"], 1) self.assertEqual(model.first_run(F(1), 1000, 300)["probability"], 0) self.assertEqual(model.first_run(F(0), 0, 1)["probability"], 0) def test_event_inclusions_and_type_one_error(self): p, n, length = F(1, 100), 1000, 300 fixed = model.fixed_prefix(p, n, length) disjoint = model.disjoint_blocks(p, n, length) rolling = model.first_run(p, n, length)["probability"] self.assertLess(fixed, F(1, 20)) self.assertLess(fixed, disjoint) self.assertLess(disjoint, rolling) self.assertGreater(disjoint, F(1, 20)) self.assertLessEqual(rolling, min(F(1), (n - length + 1) * fixed)) def test_overlapping_windows_are_not_independent(self): p, n, length = F(1, 100), 1000, 300 wrong = 1 - (1 - (1 - p) ** length) ** (n - length + 1) self.assertNotEqual(model.first_run(p, n, length)["probability"], wrong) def test_monotonicity_in_risk_and_horizon(self): values = [model.first_run(p, 40, 7)["probability"] for p in (F(1, 100), F(1, 10), F(1, 2), F(9, 10))] self.assertEqual(values, sorted(values, reverse=True)) values = [model.first_run(F(1, 10), n, 7)["probability"] for n in (0, 6, 7, 14, 40)] self.assertEqual(values, sorted(values)) def test_invalid_inputs_rejected_in_all_policies(self): for p, n, length in [(0.01, 1000, 300), (F(-1), 10, 3), (F(2), 10, 3), (F(1, 2), -1, 3), (F(1, 2), True, 3), (F(1, 2), 10, 0), (F(1, 2), 10, True), (F(1, 2), 10.0, 3), (F(1, 2), 10, 3.0)]: for method in (model.first_run, model.fixed_prefix, model.disjoint_blocks): with self.subTest(method=method.__name__, args=(p, n, length)): with self.assertRaises(ValueError): method(p, n, length) if __name__ == "__main__": unittest.main()