Download tests/test_risk_math.py from PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6: direct link, hf CLI and curl.
- Browser
- Download file 7.24 kB
-
https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/tests/test_risk_math.py
- Command line
-
hf download hf://datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/tests/test_risk_math.py
-
curl -L -o test_risk_math.py https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/tests/test_risk_math.py
7.24 kB
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0,str(Path(__file__).resolve().parents[1]/"src")) | |
| """Independent generator, boundary-value and convergence checks.""" | |
| import math | |
| import numpy as np | |
| import unittest | |
| from scipy.integrate import quad | |
| from scipy.special import hyp1f1, expi | |
| from risk_math import (mean_exit_time, mean_exit_upper, laplace_exit, | |
| laplace_chernoff_upper, ou_continuous_bounds, | |
| survival_fv, mean_exit_fv, laplace_exit_fv, | |
| simulate_nested_sampled_exit, _h_integral, | |
| additive_exponential_upper,poisson_clearance) | |
| def test_mean_integral_and_special_function_independently(): | |
| for beta in (1e-6,0.1,1.0,6.25,12.5,25.0): | |
| radius = math.sqrt(2*beta) | |
| actual = mean_exit_time(radius,1.0,1.0) | |
| independent,_ = quad(lambda x: math.expm1(x)/x if x else 1.0,0,beta) | |
| assert math.isclose(actual,independent/2,rel_tol=3e-13) | |
| if beta>0.01: | |
| closed = (expi(beta)-np.euler_gamma-math.log(beta))/2 | |
| assert math.isclose(actual,closed,rel_tol=3e-13) | |
| def test_mean_exit_boundary_and_brownian_limit(): | |
| assert mean_exit_time(1,1,0.3,1)==0 | |
| for kappa in (1e-3,1e-5,1e-7): | |
| mean = mean_exit_time(1,kappa,0.3) | |
| assert abs(mean-1/(4*0.3)) < kappa | |
| assert math.isclose(mean_exit_time(1,1,0.3,0.999999), | |
| (math.exp(1/0.6)-1)*1e-6,rel_tol=3e-6) | |
| def test_laplace_boundary_normalization_and_mean_derivative(): | |
| for beta in (0.1,1,6.25,12.5): | |
| R = math.sqrt(2*beta) | |
| assert laplace_exit(0,R,1,1)==1 | |
| assert laplace_exit(0.3,R,1,1,R)==1 | |
| h=1e-7/mean_exit_time(R,1,1) | |
| derivative=(1-laplace_exit(h,R,1,1))/h | |
| assert math.isclose(derivative,mean_exit_time(R,1,1),rel_tol=2e-6) | |
| def test_kummer_generator_equation(): | |
| # Differentiate with exact special-function derivative identities, not | |
| # finite differences of the same implementation. | |
| for a in (0.01,0.3,3.0): | |
| for x in (0.0,0.2,2.0,7.0): | |
| f = hyp1f1(a,1,x) | |
| fp = a*hyp1f1(a+1,2,x) | |
| fpp = a*(a+1)/2*hyp1f1(a+2,3,x) | |
| assert abs(x*fpp+(1-x)*fp-a*f) < 5e-11*max(1,abs(f)) | |
| def test_positive_poisson_generator_equation(): | |
| # h'(x)=(e^x-1)/x, xh''+(1-x)h'=1. | |
| for x in (1e-4,0.1,1,5,12): | |
| hp=math.expm1(x)/x | |
| hpp=((x-1)*math.exp(x)+1)/(x*x) | |
| assert abs(x*hpp+(1-x)*hp-1) < 2e-9 | |
| def test_fv_mean_and_transform_refinement(): | |
| for beta in (0.5,2.0,6.25,12.5): | |
| R=math.sqrt(2*beta) | |
| exact_m=mean_exit_time(R,1,1) | |
| exact_l=laplace_exit(0.2,R,1,1) | |
| errors=[] | |
| for cells in (80,160,320): | |
| error_m=abs(mean_exit_fv(R,1,1,cells)/exact_m-1) | |
| error_l=abs(laplace_exit_fv(0.2,R,1,1,cells)/exact_l-1) | |
| errors.append(max(error_m,error_l)) | |
| assert errors[-1] < errors[0]/10 | |
| assert errors[-1] < 3e-4 | |
| def test_survival_monotonicity_and_analytic_sandwich(): | |
| for beta in (0.5,2,6.25,12.5): | |
| R=math.sqrt(2*beta) | |
| ts=np.array((0,0.02,0.1,0.5,1,2,5)) | |
| numerical=survival_fv(R,ts,1,1,400) | |
| p=numerical['exit_probability'] | |
| assert p[0]==0 | |
| valid=p[np.isfinite(p)] | |
| assert np.all(np.diff(valid)>=-1e-12) | |
| assert np.all((valid>=0)&(valid<=1)) | |
| for t,pv in zip(ts[1:],p[1:]): | |
| if not np.isfinite(pv): | |
| continue | |
| b=ou_continuous_bounds(R,float(t),1,1) | |
| # PDE is approximate; explicit numerical tolerance is allowed. | |
| assert b['endpoint_lower'] <= pv+4e-4 | |
| assert pv <= b['upper']+4e-4 | |
| def test_certificates_improve_weak_long_horizon_v5_bound(): | |
| # beta=12.5, T=10: direct v5 is vacuous and block is >1, while | |
| # the positive-Poisson bound remains below 0.001. | |
| bounds=ou_continuous_bounds(1,10,1,0.04) | |
| assert bounds['v5_direct_upper']==1 | |
| assert bounds['v5_block_upper']==1 | |
| assert bounds['poisson_upper'] < 0.001 | |
| assert bounds['upper'] >= bounds['endpoint_lower'] | |
| def test_laplace_certificate_retains_short_time_information(): | |
| R,kappa,d,T=1,1,0.08,0.03 | |
| lap=laplace_chernoff_upper(R,T,kappa,d) | |
| poisson=mean_exit_upper(R,T,kappa,d) | |
| assert 0<lap['upper']<poisson/1e8 | |
| assert lap['s']>0 | |
| assert math.isclose(lap['upper'], | |
| math.exp(lap['s']*T)*laplace_exit(lap['s'],R,kappa,d), | |
| rel_tol=3e-13) | |
| def test_lyapunov_bound_with_independent_generator_maximum(): | |
| out=additive_exponential_upper(1,2,1,0.08) | |
| beta=1/(2*0.08) | |
| a=out['a'] | |
| xs=np.linspace(0,beta,5001) | |
| generator=2*a*np.exp(a*xs)*(1-(1-a)*xs) | |
| assert generator.max() <= out['compensator']*(1+1e-10) | |
| def test_nested_samples_are_ordered_on_identical_paths(): | |
| result=simulate_nested_sampled_exit(1,1,1,0.2,0.01, | |
| sample_strides=(1,2,10),trials=3000,seed=12) | |
| assert result['counts']['1']>=result['counts']['2']>=result['counts']['10'] | |
| def test_invalid_inputs(): | |
| with unittest.TestCase().assertRaises(ValueError): mean_exit_time(0,1,1) | |
| with unittest.TestCase().assertRaises(ValueError): laplace_exit(-1,1,1,1) | |
| with unittest.TestCase().assertRaises(ValueError): mean_exit_time(1,1,1,1.1) | |
| with unittest.TestCase().assertRaises(ValueError): survival_fv(1,[-1],1,1) | |
| with unittest.TestCase().assertRaises(ValueError): mean_exit_time(math.sqrt(2*710),1,1) | |
| with unittest.TestCase().assertRaises(ValueError): mean_exit_upper(math.sqrt(2*710),1,1,1) | |
| with unittest.TestCase().assertRaises(ValueError): _h_integral(710) | |
| def test_clearance_root_meets_union_budget(): | |
| for T,events,budget in ((10,1,0.001),(2,10,0.01),(0.02,3,1e-4)): | |
| R=poisson_clearance(T,1,0.04,budget,events) | |
| assert events*mean_exit_upper(R,T,1,0.04)<=budget*(1+1e-13) | |
| def test_finite_large_supported_poisson_is_positive(): | |
| R=math.sqrt(2*650) | |
| assert math.isfinite(mean_exit_time(R,1,1)) | |
| assert 0<mean_exit_upper(R,1,1,1)<1e-270 | |
| def test_fv_rejects_ill_conditioned_range_and_marks_unresolved_tail(): | |
| with unittest.TestCase().assertRaises(ValueError): survival_fv(math.sqrt(2*50),[1,100],1,1) | |
| with unittest.TestCase().assertRaises(ValueError): mean_exit_fv(math.sqrt(2*50),1,1) | |
| with unittest.TestCase().assertRaises(ValueError): laplace_exit_fv(.2,math.sqrt(2*50),1,1) | |
| result=survival_fv(math.sqrt(2*6.25),[0,.03,2],1,1) | |
| assert result['exit_probability'][0]==0 | |
| assert np.isnan(result['exit_probability'][1]) | |
| assert result['resolved'].tolist()==[True,False,True] | |
| assert result['principal_eigenvalue']>0 | |
| def test_positive_upper_underflow_is_rounded_up(): | |
| assert mean_exit_upper(math.sqrt(2*650),1e-300,1,1)>0 | |
| result=laplace_chernoff_upper(1,.001,1,.08) | |
| assert result['upper']>0 | |
| assert result['log_upper']<math.log(np.nextafter(0.0,1.0)) | |
| assert result['underflow_rounded_up'] | |
| class RiskTests(unittest.TestCase): | |
| pass | |
| for _name,_function in list(globals().items()): | |
| if _name.startswith('test_') and callable(_function): | |
| setattr(RiskTests,_name,lambda self,f=_function:f()) | |
| del _name,_function | |
| if __name__ == '__main__': | |
| unittest.main(verbosity=2) | |