Download experiments/sb_reference_validation_20261004/continuous_kernel_checks.py from tonynzh2/afdb6: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/datasets/tonynzh2/afdb6/resolve/main/experiments/sb_reference_validation_20261004/continuous_kernel_checks.py
- Command line
-
hf download hf://datasets/tonynzh2/afdb6/experiments/sb_reference_validation_20261004/continuous_kernel_checks.py
-
curl -L -o continuous_kernel_checks.py https://huggingface.co/datasets/tonynzh2/afdb6/resolve/main/experiments/sb_reference_validation_20261004/continuous_kernel_checks.py
5.36 kB
| """Independent continuous h-transform checks, including typed mixture marks. | |
| This is a second implementation of Gaussian formulas, not a call into the | |
| production emission/teacher functions. The gap oracle explicitly enumerates | |
| current-to-terminal subsequences, including the rank/binomial factor. | |
| """ | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from scipy.special import logsumexp | |
| from oracle_kernel import gap_remaining_loglik | |
| def logn(x, mean, variance): | |
| x,mean=np.asarray(x),np.asarray(mean) | |
| return float(-.5*(x.size*np.log(2*np.pi*variance)+np.square(x-mean).sum()/variance)) | |
| def run_checks(): | |
| rng=np.random.default_rng(60341) | |
| t=.43; horizon=1.; delta=horizon-t; sigma2=1.7; v0=2.3; beta=.9 | |
| # Real atom inventories: A=5, G=4; all matrices consist of actual atoms. | |
| target_types=['A','G','A'] | |
| target=[rng.normal(0,.4,(5,3)),rng.normal(0,.4,(4,3)),rng.normal(0,.4,(5,3))] | |
| pi={'A':.6,'G':.4}; mu={aa:np.zeros((count,3)) for aa,count in [('A',5),('G',4)]} | |
| current_types=['A']; current=[rng.normal(0,.4,(5,3))] | |
| birth=np.array([np.log(pi[a])+logn(y,mu[a],v0+horizon*sigma2) | |
| for a,y in zip(target_types,target)]) | |
| def likelihood(types,coords): | |
| emissions=np.full((len(types),3),-np.inf) | |
| for i,(a,x) in enumerate(zip(types,coords)): | |
| for j,(b,y) in enumerate(zip(target_types,target)): | |
| if a==b: emissions[i,j]=logn(y,x,delta*sigma2) | |
| return float(gap_remaining_loglik(emissions,birth,beta,delta)) | |
| h=likelihood(current_types,current) | |
| # Enumerate which terminal A descends from the single present A. | |
| assignment_logs=[] | |
| for existing in [0,2]: | |
| assignment_logs.append(logn(target[existing],current[0],delta*sigma2) | |
| +sum(birth[j] for j in range(3) if j!=existing)) | |
| weights=np.exp(assignment_logs-logsumexp(assignment_logs)) | |
| drift=sum(w*(target[j]-current[0])/delta for w,j in zip(weights,[0,2])) | |
| numerical=np.zeros_like(drift); eps=1e-5 | |
| for idx in np.ndindex(drift.shape): | |
| plus=current[0].copy();minus=plus.copy();plus[idx]+=eps;minus[idx]-=eps | |
| numerical[idx]=sigma2*(likelihood(['A'],[plus])-likelihood(['A'],[minus]))/(2*eps) | |
| drift_error=float(abs(drift-numerical).max()) | |
| flux_errors=[]; ratios=[] | |
| for rank in [0,1]: | |
| for aa in ['A','G']: | |
| # Several positions can contribute to one slot's marked intensity. | |
| for _ in range(8): | |
| mark=rng.normal(0,.4,mu[aa].shape) | |
| types=current_types.copy();coords=current.copy() | |
| types.insert(rank,aa);coords.insert(rank,mark) | |
| ref=np.log(beta/2)+np.log(pi[aa])+logn(mark,mu[aa],v0+t*sigma2) | |
| lhs=np.exp(ref+likelihood(types,coords)-h) | |
| rhs=0. | |
| for w,existing in zip(weights,[0,2]): | |
| for j in range(3): | |
| if j==existing or target_types[j]!=aa or int(j>existing)!=rank:continue | |
| vt=v0+t*sigma2;vT=v0+horizon*sigma2 | |
| mean=mu[aa]+vt/vT*(target[j]-mu[aa]);var=vt-vt*vt/vT | |
| rhs+=w/delta*np.exp(logn(mark,mean,var)) | |
| flux_errors.append(abs(lhs-rhs)/max(lhs,rhs,1e-200)) | |
| if rhs>0:ratios.append(lhs/rhs) | |
| # Substitution A -> W: current backbone retained, sidechain dimension 1 -> 10 atoms. | |
| q=.7;s_b=.35/19 | |
| xb=rng.normal(size=(4,3));yb=rng.normal(size=(4,3)) | |
| yw=rng.normal(0,.4,(10,3));mw=np.zeros_like(yw);mark=rng.normal(0,.4,yw.shape) | |
| pending=np.log(s_b/q)+np.log(-np.expm1(-q*delta))+logn(yb,xb,delta*sigma2)+logn(yw,mw,v0+horizon*sigma2) | |
| locked=logn(yb,xb,delta*sigma2)+logn(yw,mark,delta*sigma2) | |
| physical_density=np.exp(np.log(s_b)+logn(mark,mw,v0+t*sigma2)+locked-pending) | |
| vt=v0+t*sigma2;vT=v0+horizon*sigma2 | |
| expected_density=q/(-np.expm1(-q*delta))*np.exp(logn(mark,mw+vt/vT*(yw-mw),vt-vt*vt/vT)) | |
| substitution_error=abs(physical_density-expected_density)/max(physical_density,expected_density) | |
| # Zero-dimensional reset for Gly uses density exactly one. | |
| gly_log_density=logn(np.empty((0,3)),np.empty((0,3)),vt) | |
| # Deletion h is independent of coordinates; its transformed rate agrees. | |
| d=.35;pD=d/q*(-np.expm1(-q*delta)) | |
| deletion_error=abs(d/pD-q/(-np.expm1(-q*delta))) | |
| result=dict(scope='Independent typed all-atom gap and substitution marked kernels; no neural sampler or ligand rotations.', | |
| gap_current_A_target_AGA=True, gap_drift_max_abs_error=drift_error, | |
| gap_marked_intensity_max_relative_error=float(max(flux_errors)), | |
| gap_positive_mark_cases=len(ratios), | |
| substitution_A_to_W_marked_intensity_relative_error=float(substitution_error), | |
| gly_zero_dimensional_log_density=gly_log_density, | |
| deletion_hazard_abs_error=float(deletion_error)) | |
| assert drift_error<1e-7,result | |
| assert max(flux_errors)<1e-10,result | |
| assert substitution_error<1e-10 and gly_log_density==0 and deletion_error<1e-12,result | |
| return result | |
| if __name__=='__main__': | |
| result=run_checks();p=Path(__file__).resolve().parent/'results/continuous_kernel_checks.json' | |
| p.parent.mkdir(exist_ok=True);p.write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result,indent=2)) | |