Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
recursive-self-improvement
artificial-intelligence
homotopy-theory
statistical-experiments
quantum-error-correction
reproducible-research
Download research/src/channels.py from PureOne/Xhorielun-Sigma: direct link, hf CLI and curl.
- Browser
- Download file 6.46 kB
-
https://huggingface.co/datasets/PureOne/Xhorielun-Sigma/resolve/main/research/src/channels.py
- Command line
-
hf download hf://datasets/PureOne/Xhorielun-Sigma/research/src/channels.py
-
curl -L -o channels.py https://huggingface.co/datasets/PureOne/Xhorielun-Sigma/resolve/main/research/src/channels.py
6.46 kB
| """Finite experiment certificates, column-stochastic convention. | |
| P[y][theta] is the probability of observable y conditional on environment theta. | |
| A recovery R[old_y][new_y] MUST be independent of theta. | |
| """ | |
| from fractions import Fraction as Q | |
| from itertools import product | |
| from math import log, sqrt | |
| from exact import matrix, mul, shape, sub, eye | |
| def validate_channel(p): | |
| m, n = shape(p) | |
| if not m or not n: | |
| raise ValueError('Empty experiment') | |
| if any(x < 0 for row in p for x in row): | |
| raise ValueError('Negative probability') | |
| if any(sum(p[i][j] for i in range(m)) != 1 for j in range(n)): | |
| raise ValueError('Column is not normalized') | |
| return True | |
| def tv(p, q): | |
| if len(p) != len(q): | |
| raise ValueError('Vector mismatch') | |
| return sum(abs(x-y) for x,y in zip(p,q))/2 | |
| def channel_tv(p, q): | |
| if shape(p) != shape(q): | |
| raise ValueError('Experiment shape mismatch') | |
| return max(tv(a,b) for a,b in zip(zip(*p),zip(*q))) | |
| def recovery_error(old, new, recovery): | |
| validate_channel(old); validate_channel(new); validate_channel(recovery) | |
| return channel_tv(old, mul(recovery,new)) | |
| def dobrushin(r): | |
| validate_channel(r) | |
| cols=list(zip(*r)) | |
| return max(tv(a,b) for a in cols for b in cols) | |
| def bsc(error): | |
| e=Q(error) | |
| if not 0<=e<=1: | |
| raise ValueError('Invalid error probability') | |
| return matrix([[1-e,e],[e,1-e]]) | |
| def bsc_recovery(old_error, new_error): | |
| p,q=Q(old_error),Q(new_error) | |
| if not 0<=q<=p<=Q(1,2) or q==Q(1,2): | |
| raise ValueError('Expected 0 <= q <= p <= 1/2 and q < 1/2') | |
| return bsc((p-q)/(1-2*q)) | |
| def bayes_value(p, prior, utility): | |
| """Optimal bounded decision value. utility[action][theta].""" | |
| validate_channel(p) | |
| prior=[Q(x) for x in prior] | |
| if len(prior)!=shape(p)[1] or any(x<0 for x in prior) or sum(prior)!=1: | |
| raise ValueError('Invalid prior') | |
| if not utility or any(not 0<=x<=1 for row in utility for x in row): | |
| raise ValueError('Utilities must be a nonempty table in [0,1]') | |
| if shape(utility)[1] != len(prior): | |
| raise ValueError('Utility dimension mismatch') | |
| return sum(max(sum(prior[t]*row[t]*u[t] for t in range(len(prior))) | |
| for u in utility) for row in p) | |
| def posterior_rows(p, prior): | |
| validate_channel(p) | |
| prior=[Q(x) for x in prior] | |
| if len(prior)!=shape(p)[1] or sum(prior)!=1 or any(x<=0 for x in prior): | |
| raise ValueError('A full-support prior is required') | |
| out=[] | |
| for row in p: | |
| mass=sum(a*b for a,b in zip(row,prior)) | |
| post=[a*b/mass for a,b in zip(row,prior)] if mass else [Q(0)]*len(prior) | |
| out.append((mass,post)) | |
| return out | |
| def quadratic_value(p, prior, normalized=True): | |
| """Expected optimal quadratic score; normalized reward lies in [0,1].""" | |
| val=sum(mass*sum(x*x for x in post) for mass,post in posterior_rows(p,prior)) | |
| return (1+val)/2 if normalized else val | |
| def innovation(old, new, recovery, prior): | |
| """Posterior dispersion for exact recovery. Returns unnormalized J.""" | |
| if recovery_error(old,new,recovery): | |
| raise ValueError('Dispersion identity requires exact recovery') | |
| po=posterior_rows(old,prior); pn=posterior_rows(new,prior) | |
| j=Q(0) | |
| for y,(mass,py) in enumerate(pn): | |
| for x,(_,px) in enumerate(po): | |
| j += mass*recovery[x][y]*sum((a-b)**2 for a,b in zip(py,px)) | |
| return j | |
| def sufficient_quotient(p): | |
| """Canonical deterministic sufficient partition by proportional rows. | |
| Zero-probability output rows are excluded from active classes, and assigned | |
| to the first class by the quotient map. They receive zero recovery mass. | |
| Returns quotient A, decoder B, and reduced experiment A P. | |
| """ | |
| validate_channel(p); groups={}; zero=[] | |
| for i,row in enumerate(p): | |
| mass=sum(row) | |
| if not mass: | |
| zero.append(i); continue | |
| key=tuple(x/mass for x in row) | |
| groups.setdefault(key,[]).append(i) | |
| classes=list(groups.values()); k=len(classes); m,n=shape(p) | |
| a=[[Q(0)]*m for _ in range(k)] | |
| b=[[Q(0)]*k for _ in range(m)] | |
| for c,indices in enumerate(classes): | |
| total=sum(sum(p[i]) for i in indices) | |
| for i in indices: | |
| a[c][i]=Q(1); b[i][c]=sum(p[i])/total | |
| for i in zero: | |
| a[0][i]=Q(1) | |
| reduced=mul(a,p) | |
| assert mul(b,reduced)==p | |
| return a,b,reduced | |
| def channel_radius(outcomes, states, samples_per_state, failure): | |
| """Uniform multinomial TV bound, by a union over all outcome subsets.""" | |
| if outcomes<1 or states<1 or samples_per_state<1 or not 0<failure<1: | |
| raise ValueError('Invalid confidence inputs') | |
| return min(1.0,sqrt(((outcomes+1)*log(2)+log(states/failure))/(2*samples_per_state))) | |
| def witness_adaptive_bound(old_emp, new_emp, r, old_radius, new_radius): | |
| return min(1.0,float(recovery_error(old_emp,new_emp,r))+old_radius+new_radius) | |
| def composed_recovery(recoveries): | |
| if not recoveries: | |
| raise ValueError('Need at least one recovery') | |
| result=eye(shape(recoveries[0])[0]) | |
| for r in recoveries: | |
| result=mul(result,r) | |
| return result | |
| def attenuated_debt(recoveries, defects): | |
| if len(recoveries)!=len(defects) or not recoveries: | |
| raise ValueError('Invalid lineage') | |
| prefix=eye(shape(recoveries[0])[0]); debt=Q(0) | |
| for r,e in zip(recoveries,defects): | |
| validate_channel(r) | |
| if not 0<=Q(e)<=1: | |
| raise ValueError('Defects must be in [0,1]') | |
| debt += dobrushin(prefix)*Q(e) | |
| prefix=mul(prefix,r) | |
| return min(Q(1),debt) | |
| def bayesian_reverse(old, new, recovery, prior): | |
| """Construct S[new_y][old_y] from the prior-coupled exact recovery. | |
| Zero-probability old outputs get an arbitrary normalized column. The | |
| returned channel need not recover new exactly; its defect is bounded | |
| by the innovation--reconstruction theorem. | |
| """ | |
| if recovery_error(old, new, recovery): | |
| raise ValueError('Bayesian reverse requires exact forward recovery') | |
| po = posterior_rows(old, prior) | |
| pn = posterior_rows(new, prior) | |
| m, n = len(po), len(pn) | |
| result = [[Q(0)] * m for _ in range(n)] | |
| for x, (old_mass, _) in enumerate(po): | |
| if not old_mass: | |
| result[0][x] = Q(1) | |
| else: | |
| for y, (new_mass, _) in enumerate(pn): | |
| result[y][x] = new_mass * recovery[x][y] / old_mass | |
| validate_channel(result) | |
| return result | |