File size: 4,519 Bytes
868c741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""Independent exact checks for finite-transcript and robustness certificates."""
from __future__ import annotations
import argparse
import json
from fractions import Fraction as Q
from pathlib import Path
from trajectory_certificates import (AffineForm, dot, interpolation_form,
    gd_coefficients, compensation_certificate, psd_exact, residual_lower_bound,
    trajectory_tube, linear_error_allowance)


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output',type=Path)
    args=parser.parse_args()
    interpolation_checks=0
    for h in [Q(1,5),Q(1),Q(9,5),Q(19,10)]:
        for mu in [Q(0),Q(1,10)]:
            lambdas=[Q(1,5),Q(3,5),Q(9,10)]
            states=[(Q(1),Q(-2),Q(3,2))]
            for k in range(4):
                states.append(tuple((1-h*l)*x for l,x in zip(lambdas,states[-1])))
            gradients=[tuple(l*x for l,x in zip(lambdas,z)) for z in states]
            values=[sum((l*x*x/2 for l,x in zip(lambdas,z)),Q(0)) for z in states]
            gram=[[dot(g,gp) for gp in gradients] for g in gradients]
            coeff=gd_coefficients(h,4)
            for i in range(5):
                for j in range(5):
                    dx=tuple(x-y for x,y in zip(states[i],states[j]))
                    dg=tuple(x-y for x,y in zip(gradients[i],gradients[j]))
                    residual=tuple(d-mu*x for d,x in zip(dg,dx))
                    direct=values[i]-values[j]-dot(gradients[j],dx)-mu*dot(dx,dx)/2-dot(residual,residual)/(2*(1-mu))
                    encoded=interpolation_form(coeff,i,j,mu).evaluate(values,gram)
                    assert encoded==direct and direct>=0
                    interpolation_checks+=1
    accepted=[]
    for h in [Q(1,10),Q(1),Q(7,4),Q(9,5),Q(1859,1000)]:
        target,weighted,residual,weights=compensation_certificate(h)
        assert all(v>=0 for v in weights.values())
        assert residual.loss==(Q(0),)*4 and residual.constant==0
        result=residual_lower_bound(residual,4,[0,2,4,6])
        assert result['verified'] and result['lower_bound_exact']=='0'
        accepted.append(str(h))
    _,_,beyond,_=compensation_certificate(Q(93,50))
    assert not psd_exact(beyond.gram)[0]
    # A failed original certificate above its range is not itself a counterexample.
    # The separate sharp-compensation verifier supplies actual interpolable data.
    assert psd_exact([[0,0],[0,1]])[0]
    assert not psd_exact([[0,1],[1,1]])[0]
    assert not psd_exact([[1,2],[2,1]])[0]
    assert psd_exact([[1,2],[2,4]])[0]
    assert psd_exact([[Q(1,3)]])[0]
    residual=AffineForm.make([Q(1,10),Q(-1,5)],[[-1,0],[0,2]],3)
    bounded=residual_lower_bound(residual,4,[1,2],1)
    assert bounded['verified'] and Q(bounded['lower_bound_exact'])==Q(-3,2)
    tube=trajectory_tube(Q(9,5),1,1,[Q(1,100000)]*3)
    allowance=linear_error_allowance([Q(2,3),-1,0,Q(1,3)],tube,[Q(1,1000000)]*4)
    assert allowance==Q('0.000038000648')
    # Independently realize an inexact quadratic trajectory and check every bound.
    quadratic_cases=0
    for mu in [Q(0),Q(1,10)]:
        for lam in [Q(1,5),Q(3,5),Q(1)]:
            for eta in [Q(1,2),Q(9,5)]:
                err=[Q((-1)**k,100) for k in range(8)]
                bounds=trajectory_tube(eta,1,lam,[abs(e) for e in err],mu=mu)
                x=y=Q(1)
                for k in range(9):
                    assert abs(y-x)<=bounds[k]['position']
                    assert abs(lam*(y*y-x*x)/2)<=bounds[k]['value']
                    if k<8:
                        x=(1-eta*lam)*x
                        y=(1-eta*lam)*y-eta*err[k]
                quadratic_cases+=1
    report={
        'all_exact_checks_passed':True,
        'arithmetic':'fractions.Fraction, no floating point assertions',
        'ambient_vs_gram_interpolation_equalities':interpolation_checks,
        'verified_compensation_steps':accepted,
        'out_of_range_certificate_rejected':True,
        'PSD_adversarial_cases':5,
        'residual_certificate_bound_exact':bounded['lower_bound_exact'],
        'three_step_error_allowance_exact':str(allowance),
        'inexact_quadratic_trajectory_cases':quadratic_cases,
        'scope':'Algebra and stated finite checks; the universal statements are proved in the manuscript.'}
    if args.output:
        args.output.parent.mkdir(parents=True,exist_ok=True)
        args.output.write_text(json.dumps(report,indent=2)+'\n')
    print(json.dumps(report,indent=2))


if __name__=='__main__':
    main()