"""Independent exact midpoint/tail oracle on small rational timelines.""" from fractions import Fraction from itertools import accumulate, product import json import unittest from model import mean_wait, tail_probability from onset import result, weighted_wait def oracle(gaps, weights, threshold): checks = list(accumulate(gaps)) mass = sum(weights) area = tail = Fraction(0) # All breakpoints are multiples of 1/2 day; quarter-day midpoints # integrate affine delay and constant tail indicators exactly. for tick in range(4 * sum(gaps)): t = Fraction(2 * tick + 1, 8) weight = weights[tick // 4] wait = next(check for check in checks if check > t) - t area += weight * wait / 4 tail += Fraction(weight, 4) * (wait > threshold) return area / mass, tail / mass class OnsetTests(unittest.TestCase): def test_json_round_trip(self): self.assertEqual(json.loads(json.dumps(result())), result()) def test_exact_crossover_not_only_one_chosen_weight(self): for weight in (0, Fraction(1, 2), 1, 2, 4, 10, 100): profile = [(7, weight), (21, 1), (7, weight), (21, 1)] regular = weighted_wait([14] * 4, profile)["mean_wait_days"] alternating = weighted_wait([7, 21] * 2, profile)["mean_wait_days"] self.assertEqual(alternating - regular, 7 * (2 - Fraction(weight)) / (weight + 3)) def test_rank_reversal_with_same_profile(self): profile = [(7, 4), (21, 1), (7, 4), (21, 1)] regular = weighted_wait([14] * 4, profile) alternating = weighted_wait([7, 21] * 2, profile) self.assertEqual(regular["normalizing_mass_days"], 98) self.assertEqual(regular["weighted_wait_area_days_squared"], 833) self.assertEqual(alternating["weighted_wait_area_days_squared"], 637) self.assertEqual(regular["mean_wait_days"], Fraction(17, 2)) self.assertEqual(alternating["mean_wait_days"], Fraction(13, 2)) self.assertEqual(alternating["probability_wait_exceeds_threshold"], Fraction(1, 7)) self.assertLess(mean_wait([14] * 4), mean_wait([7, 21] * 2)) self.assertGreater(regular["mean_wait_days"], alternating["mean_wait_days"]) def test_uniform_preserves_original_formulas(self): for count in range(1, 5): for gaps in product(range(1, 5), repeat=count): for threshold in (0, Fraction(1, 2), 2, 14): view = weighted_wait(gaps, [(sum(gaps), 1)], threshold) self.assertEqual(view["mean_wait_days"], mean_wait(gaps)) self.assertEqual(view["probability_wait_exceeds_threshold"], tail_probability(gaps, threshold)) def test_independent_weighted_timeline_oracle(self): for gaps in ((1,), (2,), (1, 2), (2, 1), (2, 2), (1, 3), (3, 1)): for weights in product(range(3), repeat=sum(gaps)): if not any(weights): continue profile = [(1, weight) for weight in weights] for threshold in (0, Fraction(1, 2), 1, Fraction(3, 2), 4): mean, tail = oracle(gaps, weights, threshold) view = weighted_wait(gaps, profile, threshold) self.assertEqual(view["mean_wait_days"], mean) self.assertEqual(view["probability_wait_exceeds_threshold"], tail) def test_weight_scale_invariance(self): view = weighted_wait([2, 3], [(1, 2), (4, 1)]) scaled = weighted_wait([2, 3], [(1, Fraction(2, 7)), (4, Fraction(1, 7))]) for field in ("mean_wait_days", "probability_wait_exceeds_threshold"): self.assertEqual(view[field], scaled[field]) def test_profile_split_does_not_change_results(self): self.assertEqual(weighted_wait([2, 3], [(5, 2)]), weighted_wait([2, 3], [(1, 2), (2, 2), (2, 2)])) def test_huge_duration_needs_no_daily_expansion(self): days = 10 ** 20 self.assertEqual(weighted_wait([days], [(days, 1)])["mean_wait_days"], days // 2) def test_zero_mass_segments_and_tail_boundaries(self): self.assertEqual(weighted_wait([3], [(1, 0), (1, 1), (1, 0)], 1)["mean_wait_days"], Fraction(3, 2)) self.assertEqual(weighted_wait([2], [(2, 1)], 2)["probability_wait_exceeds_threshold"], 0) self.assertEqual(weighted_wait([2], [(2, 1)], 0)["probability_wait_exceeds_threshold"], 1) def test_invalid_profiles(self): for profile in (None, [], "x", [1], [(1,)], [(2, 1, 4)], [(True, 1)], [(2.0, 1)], [(0, 1)], [(-2, 1)], [(2, -1)], [(2, True)], [(2, 1.0)], [(2, float("nan"))], [(2, 0)], [(3, 1)]): with self.subTest(profile=profile), self.assertRaises(ValueError): weighted_wait([2], profile) def test_invalid_schedules_and_thresholds(self): for gaps in ([], [0], [-1], [True], [1.0], "2"): with self.subTest(gaps=gaps), self.assertRaises(ValueError): weighted_wait(gaps, [(2, 1)]) for threshold in (True, -1, 1.0, float("inf"), "2", None): with self.subTest(threshold=threshold), self.assertRaises(ValueError): weighted_wait([2], [(2, 1)], threshold) def test_inputs_preserved(self): gaps, profile = [1, 2], [[1, 2], [2, 0]] weighted_wait(gaps, profile) self.assertEqual(gaps, [1, 2]) self.assertEqual(profile, [[1, 2], [2, 0]]) if __name__ == "__main__": unittest.main()