File size: 4,333 Bytes
a64600b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Analytical and independent-seed checks; no neural-model dependencies."""
import unittest

import numpy as np

from suite.null_commitment import (
    ORDERS, commitment_steps, exact_curve, fixed_order, monte_carlo,
    oracle_remap, response_statistics, target_support,
)


class IndependentTokenNullTests(unittest.TestCase):
    def setUp(self):
        self.mask = target_support(256)
        self.alpha = np.linspace(0, 1, 65)
        self.steps = 18
        self.b = .15

    def commit(self, order):
        return commitment_steps(fixed_order(self.mask, order), self.steps)

    def test_orders_preserve_positions_and_reveal_budget(self):
        counts = []
        for order in ORDERS:
            permutation = fixed_order(self.mask, order)
            np.testing.assert_array_equal(np.sort(permutation), np.arange(256))
            commit = commitment_steps(permutation, self.steps)
            self.assertTrue(np.all(commit >= 0))
            counts.append(np.bincount(commit, minlength=self.steps))
        for count in counts[1:]:
            np.testing.assert_array_equal(count, counts[0])
        balanced = fixed_order(self.mask, "balanced")
        np.testing.assert_array_equal(np.cumsum(self.mask[balanced]), np.arange(1, 257) * 64 // 256)

    def test_zero_coupling_can_have_narrow_duration_response(self):
        first = exact_curve(self.alpha, self.commit("target_first"), self.mask, self.steps, self.b)
        random = exact_curve(self.alpha, self.commit("random"), self.mask, self.steps, self.b)
        fs = response_statistics(self.alpha, first["target_match"])
        rs = response_statistics(self.alpha, random["target_match"])
        self.assertLess(fs["width80"], .3)
        self.assertGreater(rs["width80"], .55)
        self.assertGreater(rs["width80"] / fs["width80"], 2)
        self.assertAlmostEqual(fs["endpoint_gain"], 1 - self.b)
        self.assertAlmostEqual(rs["endpoint_gain"], 1 - self.b)

    def test_amplitude_schedule_is_linear_for_every_order(self):
        for order in ORDERS:
            curve = exact_curve(self.alpha, self.commit(order), self.mask, self.steps,
                                self.b, "amplitude")
            np.testing.assert_allclose(curve["target_match"], self.b + (1 - self.b) * self.alpha, atol=1e-14)
            self.assertAlmostEqual(response_statistics(self.alpha, curve["target_match"])["width80"], .8)

    def test_context_only_is_dormant_and_outside_support_is_untouched(self):
        commit = self.commit("target_first")
        for copy in (True, False):
            curve = exact_curve(self.alpha, commit, self.mask, self.steps, self.b,
                                reference_copy=copy)
            np.testing.assert_allclose(curve["outside_match"], self.b, atol=1e-14)
            np.testing.assert_array_equal(curve["direct_copy_count_outside"], 0)
            if not copy:
                np.testing.assert_allclose(curve["target_match"], self.b, atol=1e-14)
                self.assertIsNone(response_statistics(self.alpha, curve["target_match"])["width80"])
        mc = monte_carlo(self.alpha, commit, self.mask, self.steps, self.b, "duration", 32)
        self.assertEqual(mc["outside_changed_token_observations"], 0)

    def test_oracle_inverse_recovers_continuous_intermediate_expectations(self):
        for order in ORDERS:
            oracle = oracle_remap(self.alpha, self.commit(order), self.mask, self.steps, self.b)
            self.assertLess(oracle["max_absolute_error"], 1e-12)
            self.assertIn("not held-out", oracle["caveat"])
            np.testing.assert_allclose(oracle["achieved_level"], self.alpha, atol=1e-12)

    def test_monte_carlo_agrees_with_exact_without_changing_outside(self):
        commit = self.commit("random")
        for schedule in ("duration", "amplitude"):
            exact = exact_curve(self.alpha, commit, self.mask, self.steps, self.b, schedule)
            mc = monte_carlo(self.alpha, commit, self.mask, self.steps, self.b, schedule, 1024)
            error = np.abs(mc["target_match_mean"] - exact["target_match"])
            self.assertTrue(np.all(error < 5 * mc["target_match_se"] + 1e-12))
            self.assertLess(float(error.max()), .01)
            self.assertEqual(mc["outside_changed_token_observations"], 0)


if __name__ == "__main__":
    unittest.main()