File size: 3,488 Bytes
f95cbd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import numpy as np
import pytest
from celestis_rl.sequential_audit import MetricSpec,SequentialParetoAudit,confidence_radius
from celestis_rl.certificates import ConfidenceLedger


def test_radius_formula_and_range():
    n=1000;v=.01;width=2.;alpha=.01
    r,b=confidence_radius(n,v,width,alpha,'bernstein')
    assert r==pytest.approx(math.sqrt(2*v*math.log(2/alpha)/n)+7*width*math.log(2/alpha)/(3*(n-1)))
    r,b=confidence_radius(n,v,width,alpha,'hybrid')
    h,_=confidence_radius(n,v,width,alpha/2,'hoeffding')
    e,_=confidence_radius(n,v,width,alpha/2,'bernstein')
    assert r==min(h,e)


def test_low_variance_faster_acceptance():
    looks=(64,128,256,512,1024,2048,4096,8192,16384)
    audits=[SequentialParetoAudit([MetricSpec('reward')],ConfidenceLedger(),candidate_id='v1',looks=looks,method=m) for m in ['hybrid','hoeffding']]
    for audit in audits:audit.add(np.full(16384,.55),np.full(16384,.5),candidate_id='v1')
    assert audits[0].result.accepted and audits[1].result.accepted
    assert audits[0].n<audits[1].n


def test_multi_metric_rejects_hidden_cost_regression():
    specs=[MetricSpec('reward'),MetricSpec('cost',direction=-1)]
    audit=SequentialParetoAudit(specs,ConfidenceLedger(),candidate_id='bad',looks=(64,2048))
    result=audit.add(np.tile([.9,.7],(2048,1)),np.tile([.5,.5],(2048,1)),candidate_id='bad')
    assert not result.accepted and result.lower_gain_bounds['cost']<0
    assert audit.finished
    with pytest.raises(RuntimeError):audit.add([[.9,.7]],[[.5,.5]],candidate_id='bad')


def test_batch_partition_and_resume(tmp_path):
    kwargs=dict(candidate_id='frozen',looks=(64,128,256,512))
    a=SequentialParetoAudit([MetricSpec('value',min_gain=.9)],ConfidenceLedger(),**kwargs)
    b=SequentialParetoAudit([MetricSpec('value',min_gain=.9)],ConfidenceLedger(),**kwargs)
    rng=np.random.default_rng(22);c=rng.uniform(.2,.8,512);d=rng.uniform(.2,.8,512)
    a.add(c,d,candidate_id='frozen')
    b.add(c[:90],d[:90],candidate_id='frozen');b.save(tmp_path/'audit.json')
    b=SequentialParetoAudit.load(tmp_path/'audit.json');b.add(c[90:],d[90:],candidate_id='frozen')
    assert a.n==b.n and a.look_index==b.look_index
    assert np.allclose(a.mean,b.mean,atol=1e-15)
    assert np.allclose(a.m2,b.m2,atol=1e-15)


def test_irreversible_ledger_and_proposal_guard(tmp_path):
    ledger=ConfidenceLedger(delta=.01)
    a=SequentialParetoAudit([MetricSpec('r')],ledger,candidate_id='a',ledger_path=tmp_path/'ledger.json')
    assert ConfidenceLedger.load(tmp_path/'ledger.json').attempts==1
    b=SequentialParetoAudit([MetricSpec('r')],ledger,candidate_id='b')
    assert b.attempt_delta==a.attempt_delta/4
    with pytest.raises(ValueError):a.add([.5],[.2],candidate_id='b')
    with pytest.raises(ValueError):a.add([1.1],[.2],candidate_id='a')
    assert a.n==0

@pytest.mark.parametrize('looks',[(1,),(),(32,16),(True,),(32,32)])
def test_invalid_looks(looks):
    ledger=ConfidenceLedger()
    with pytest.raises(ValueError):SequentialParetoAudit([MetricSpec('r')],ledger,candidate_id='a',looks=looks)
    assert ledger.attempts==0

@pytest.mark.parametrize('seed',range(5))
def test_no_acceptance_deterministic_negative_margin(seed):
    rng=np.random.default_rng(seed)
    base=rng.uniform(.3,.7,1024);candidate=base-.02
    audit=SequentialParetoAudit([MetricSpec('r')],ConfidenceLedger(),candidate_id='negative',looks=(32,128,1024))
    res=audit.add(candidate,base,candidate_id='negative')
    assert not res.accepted