Datasets:
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
reinforcement-learning
policy-optimization
klpo
exact-moment-replay
score-centering
stratified-sampling
License:
Download tests/test_sequential_audit.py from PureOne/Celestis-RL: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/datasets/PureOne/Celestis-RL/resolve/main/tests/test_sequential_audit.py
- Command line
-
hf download hf://datasets/PureOne/Celestis-RL/tests/test_sequential_audit.py
-
curl -L -o test_sequential_audit.py https://huggingface.co/datasets/PureOne/Celestis-RL/resolve/main/tests/test_sequential_audit.py
3.49 kB
| 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 | |
| def test_invalid_looks(looks): | |
| ledger=ConfidenceLedger() | |
| with pytest.raises(ValueError):SequentialParetoAudit([MetricSpec('r')],ledger,candidate_id='a',looks=looks) | |
| assert ledger.attempts==0 | |
| 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 | |