|
9 | 9 | audited_error, brute_force_budget_gain, decide_after_report, |
10 | 10 | decide_public, expected_error_under_rule, optimal_fixed_budget, |
11 | 11 | public_error, rule_audit_value, |
| 12 | + weighted_public_error, weighted_audited_error, weighted_audit_value, |
| 13 | + weighted_decide_public, weighted_decide_after_report, |
| 14 | + weighted_expected_error_under_rule, weighted_rule_audit_value, |
12 | 15 | ) |
13 | 16 |
|
14 | 17 |
|
@@ -85,3 +88,68 @@ def test_invalid_inputs_fail_closed(): |
85 | 88 | AuditCandidate("foo",1) |
86 | 89 | with pytest.raises(ValueError): |
87 | 90 | AuditCandidate("",Q(1)) |
| 91 | + |
| 92 | + |
| 93 | +def test_weighted_bayes_threshold_and_named_channels(): |
| 94 | + reliable=AuditChannel(Q(1,2),Q(9,10),Q(1,10)) |
| 95 | + weak=AuditChannel(Q(1,2),Q(3,5),Q(2,5)) |
| 96 | + assert weighted_public_error(reliable,false_grant_cost=Q(2)) == Q(1,2) |
| 97 | + assert weighted_audited_error(reliable,false_grant_cost=Q(2)) == Q(3,20) |
| 98 | + assert weighted_audit_value(reliable,false_grant_cost=Q(2)) == Q(7,20) |
| 99 | + assert weighted_audit_value(weak,false_grant_cost=Q(2)) == Q(0) |
| 100 | + assert not weighted_decide_public(reliable,false_grant_cost=Q(2)) |
| 101 | + likely=AuditChannel(Q(4,5),Q(9,10),Q(1,10)) |
| 102 | + assert weighted_decide_public(likely,false_grant_cost=Q(2)) |
| 103 | + assert weighted_decide_after_report(reliable,report=True, |
| 104 | + false_grant_cost=Q(2)) |
| 105 | + assert not weighted_decide_after_report(reliable,report=False, |
| 106 | + false_grant_cost=Q(2)) |
| 107 | + |
| 108 | + |
| 109 | +def test_all_weighted_bayes_decisions_match_exact_risk_grid(): |
| 110 | + grid=(Q(0),Q(1,4),Q(1,2),Q(3,4),Q(1)) |
| 111 | + for c_fp in (Q(1,2),Q(1),Q(2),Q(5)): |
| 112 | + for p,t,f in product(grid,repeat=3): |
| 113 | + ch=AuditChannel(p,t,f) |
| 114 | + a=weighted_public_error(ch,false_grant_cost=c_fp) |
| 115 | + b=weighted_audited_error(ch,false_grant_cost=c_fp) |
| 116 | + assert Q(0) <= b <= a |
| 117 | + assert weighted_audit_value(ch,false_grant_cost=c_fp)==a-b |
| 118 | + assert weighted_expected_error_under_rule( |
| 119 | + ch,ch,inspect=False,false_grant_cost=c_fp |
| 120 | + )==a |
| 121 | + assert weighted_expected_error_under_rule( |
| 122 | + ch,ch,inspect=True,false_grant_cost=c_fp |
| 123 | + )==b |
| 124 | + if c_fp==Q(1): |
| 125 | + assert a==public_error(ch) |
| 126 | + assert b==audited_error(ch) |
| 127 | + |
| 128 | + |
| 129 | +def test_weighted_policy_may_be_worse_with_wrong_audit_model(): |
| 130 | + true=AuditChannel(Q(3,5),Q(1,2),Q(1,2)) |
| 131 | + estimated=AuditChannel(Q(3,5),Q(99,100),Q(1,100)) |
| 132 | + assert weighted_rule_audit_value( |
| 133 | + true,estimated,false_grant_cost=Q(1) |
| 134 | + )==-Q(1,10) |
| 135 | + |
| 136 | + |
| 137 | +def test_eca02_frozen_policy_cost_crossover_is_exact(): |
| 138 | + # These counts are post-readout observations; this test is an accounting |
| 139 | + # identity, not preregistered validation of a newly optimized policy. |
| 140 | + fitted_fp,fitted_fn=461,492 |
| 141 | + uncertainty_fp,uncertainty_fn=343,644 |
| 142 | + break_even=Q(76,59) |
| 143 | + assert break_even*(fitted_fp-uncertainty_fp)==uncertainty_fn-fitted_fn |
| 144 | + assert Q(1)*fitted_fp+fitted_fn < Q(1)*uncertainty_fp+uncertainty_fn |
| 145 | + assert Q(2)*fitted_fp+fitted_fn > Q(2)*uncertainty_fp+uncertainty_fn |
| 146 | + |
| 147 | + |
| 148 | +def test_invalid_weighted_costs_fail_closed(): |
| 149 | + ch=AuditChannel(Q(1,2),Q(1,2),Q(1,2)) |
| 150 | + for cost in (Q(0),Q(-1,4)): |
| 151 | + with pytest.raises(ValueError): |
| 152 | + weighted_audit_value(ch,false_grant_cost=cost) |
| 153 | + with pytest.raises(ValueError): |
| 154 | + weighted_decide_public(ch,false_grant_cost=Q(1), |
| 155 | + missed_grant_cost=Q(0)) |
0 commit comments