"""Reliability-access bounds. All rates and units must be supplied consistently.""" import math import numpy as np from scipy.stats import binom def kl_bernoulli(rho, p): if not 0 < p < 1 or not 0 < rho < 1: raise ValueError('Require probabilities strictly between 0 and 1') return rho*math.log(rho/p)+(1-rho)*math.log((1-rho)/(1-p)) def raw_error(p0, birth, repair, time, transduction): if min(birth,repair,time)<0 or not 0<=transduction<=1: raise ValueError('Invalid rates, time, or transduction error') rate=birth+repair p=p0 if rate==0 else birth/rate+(p0-birth/rate)*math.exp(-rate*time) return transduction+(1-transduction)*p def union_bound(regions,b,rho,p): return 1.0 if p>=rho else min(1.0,regions*math.exp(-b*kl_bernoulli(rho,p))) def exact_module_failure(b,rho,p): return float(binom.sf(math.ceil(rho*b)-1,b,p)) def needed_redundancy(regions,delta,rho,p): return math.inf if p>=rho else math.ceil(math.log(regions/delta)/kl_bernoulli(rho,p)) def min_wait(p0,birth,repair,transduction,p_star): target=(p_star-transduction)/(1-transduction) if target>=p0: return 0. rate=birth+repair if rate==0 or target<=birth/rate: return math.inf pss=birth/rate return math.log((p0-pss)/(target-pss))/rate def depletion_repair(b,mu0,kappa,dim=3): return mu0*max(0.,1.-kappa*b**(2./dim)) def topo_order(events,edges): """Fixed prerequisite DAG. Access-graph cuts must be computed by caller.""" incoming={e:0 for e in events}; outgoing={e:[] for e in events} for a,b in edges: incoming[b]+=1; outgoing[a].append(b) ready=sorted(e for e in events if incoming[e]==0); answer=[] while ready: v=ready.pop(0); answer.append(v) for w in outgoing[v]: incoming[w]-=1 if incoming[w]==0: ready.append(w); ready.sort() if len(answer)!=len(events): raise ValueError('Cyclic fabrication prerequisites') return answer