"""Reproduce all six second-expansion cases with Python 3 standard library. Synthetic data only. No network, credentials, or production database access. Run: python verify-business-continuation-2.py """ import math,statistics,sqlite3,json,itertools def response_case(): raw=64/80; weighted=(120*(54/60)+80*(10/20))/200 assert raw==.8 and abs(weighted-.74)<1e-12 assert [64/200,(64+120)/200]==[.32,.92] assert (54+20)/(60+40)==.74 return dict(raw=raw,standardized=weighted,logical_bounds=[.32,.92],upper_after_ten_verified_failures=174/200) def paired_case(): before=[40,50,60,70,80,90];after=[38,47,59,66,78,87] d=[a-b for a,b in zip(before,after)];mean=statistics.mean(d);v=statistics.variance(d);se=math.sqrt(v/len(d)) critical=2.5705818356 # t_.975,5; precision retained for the stated teaching interval. interval=[mean-critical*se,mean+critical*se] assert d==[2,3,1,4,2,3] and mean==2.5 and abs(v-1.1)<1e-12 permuted=[a-b for a,b in zip(before,reversed(after))] assert statistics.mean(permuted)==mean and statistics.variance(permuted)>v return dict(differences=d,mean=mean,variance=v,se=se,t=mean/se,df=5,interval=interval,independent_twenty_null_rejection_probability=1-.95**20,bonferroni_threshold=.05/20,permuted_pair_variance=statistics.variance(permuted)) def foreign_keys_case(): db=sqlite3.connect(':memory:');db.execute('PRAGMA foreign_keys=OFF') db.executescript('CREATE TABLE customers(id INTEGER PRIMARY KEY); CREATE TABLE invoices(id INTEGER PRIMARY KEY,customer_id INTEGER REFERENCES customers(id)); INSERT INTO customers VALUES(1); INSERT INTO invoices VALUES(11,1),(12,99),(13,NULL);') db.execute('BEGIN');db.execute('PRAGMA foreign_keys=ON');assert db.execute('PRAGMA foreign_keys').fetchone()[0]==0;db.rollback() db.execute('PRAGMA foreign_keys=ON');assert db.execute('PRAGMA foreign_keys').fetchone()[0]==1 violations=db.execute('PRAGMA foreign_key_check').fetchall();assert violations==[('invoices',12,'customers',0)] # Fixture-only quarantine; this is not a policy for deleting real invoices. db.execute('CREATE TABLE quarantine AS SELECT * FROM invoices WHERE id=12');db.execute('DELETE FROM invoices WHERE id=12');db.commit() try:db.execute('INSERT INTO invoices VALUES(14,99)');raise AssertionError('Orphan accepted') except sqlite3.IntegrityError:db.rollback() db.execute('INSERT INTO invoices VALUES(15,NULL)');db.commit();assert not db.execute('PRAGMA foreign_key_check').fetchall() deferred=sqlite3.connect(':memory:');deferred.execute('PRAGMA foreign_keys=ON') deferred.executescript('CREATE TABLE parent(id INTEGER PRIMARY KEY); CREATE TABLE child(id INTEGER PRIMARY KEY,parent_id INTEGER REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED);') deferred.execute('BEGIN');deferred.execute('INSERT INTO child VALUES(1,7)');assert len(deferred.execute('PRAGMA foreign_key_check').fetchall())==1 deferred.execute('INSERT INTO parent VALUES(7)');assert not deferred.execute('PRAGMA foreign_key_check').fetchall();deferred.commit() deferred.execute('BEGIN');deferred.execute('INSERT INTO child VALUES(2,99)') try:deferred.commit();raise AssertionError('Deferred orphan committed') except sqlite3.IntegrityError: assert deferred.in_transaction;deferred.rollback() assert deferred.execute('SELECT * FROM child').fetchall()==[(1,7)] return dict(legacy_violations=violations,new_orphan_rejected=True,nullable_reference_accepted=True,deferred_parent_supplied_commits=True,failed_deferred_commit_requires_resolution=True) def null_join_case(): db=sqlite3.connect(':memory:');db.executescript("CREATE TABLE customers(id INTEGER PRIMARY KEY,name TEXT); INSERT INTO customers VALUES(1,'A'),(2,'B'),(3,'C'),(4,'D'); CREATE TABLE invoices(id INTEGER PRIMARY KEY,customer_id INTEGER,status TEXT,amount INTEGER); INSERT INTO invoices VALUES(11,1,'paid',100),(12,1,'open',50),(13,2,'open',80),(14,4,'paid',NULL);") query="SELECT c.name,COUNT(*),COUNT(i.id),COUNT(i.amount),SUM(i.amount) FROM customers c LEFT JOIN invoices i ON i.customer_id=c.id AND i.status='paid' GROUP BY c.id ORDER BY c.id" joined=db.execute(query).fetchall();assert joined==[('A',1,1,1,100),('B',1,0,0,None),('C',1,0,0,None),('D',1,1,0,None)] where=db.execute("SELECT c.name FROM customers c LEFT JOIN invoices i ON i.customer_id=c.id WHERE i.status='paid' ORDER BY c.id").fetchall();assert where==[('A',),('D',)] db.executescript('CREATE TABLE exclusions(customer_id INTEGER);INSERT INTO exclusions VALUES(1),(NULL);') bad=db.execute('SELECT id FROM customers WHERE id NOT IN(SELECT customer_id FROM exclusions)').fetchall();assert bad==[] good=db.execute('SELECT c.id FROM customers c WHERE NOT EXISTS(SELECT 1 FROM exclusions e WHERE e.customer_id=c.id) ORDER BY c.id').fetchall();assert good==[(2,),(3,),(4,)] db.execute("INSERT INTO invoices VALUES(15,4,'paid',30)");changed=db.execute(query).fetchall()[-1];assert changed==('D',2,2,1,30) return dict(on_filter=joined,where_filter=where,not_in_with_null=bad,not_exists=good,changed_D=changed) def ols(x,y): xb=statistics.mean(x);yb=statistics.mean(y);sxx=sum((v-xb)**2 for v in x);sxy=sum((v-xb)*(w-yb) for v,w in zip(x,y));b=sxy/sxx;a=yb-b*xb return a,b,sxx,sxy def ridge_case(): x=[1,2,3,4,5];y=[2,4,5,4,5];a,b,sxx,sxy=ols(x,y);lam=10;rb=sxy/(sxx+lam);ra=statistics.mean(y)-rb*statistics.mean(x) rss=lambda a,b:sum((v-a-b*u)**2 for u,v in zip(x,y)) assert abs(a-2.2)<1e-12 and b==.6 and rb==.3 and ra==3.1 assert abs(rss(ra,rb)-3.3)<1e-12 and abs(rss(ra,rb)+lam*rb*rb-4.2)<1e-12 _,_,scaled_sxx,scaled_sxy=ols([10*v for v in x],y);bad_scaled=10*scaled_sxy/(scaled_sxx+lam);correct_scaled=10*scaled_sxy/(scaled_sxx+100*lam) assert abs(correct_scaled-rb)<1e-12 and abs(bad_scaled-rb)>.29 return dict(ols=[a,b],ridge=[ra,rb],ridge_SSE=rss(ra,rb),ridge_objective=rss(ra,rb)+lam*rb*rb,ols_penalized_objective=rss(a,b)+lam*b*b,original_unit_slope_after_unadjusted_scaling=bad_scaled,original_unit_slope_after_adjusted_scaling=correct_scaled,predictions_at_6=[a+b*6,ra+rb*6]) def influence_case(): x=[1,2,3,4,20];y=[2,4,6,8,0];a,b,sxx,_=ols(x,y);h=1/5+(20-statistics.mean(x))**2/sxx;residual=y[-1]-(a+b*x[-1]);deleted=residual/(1-h);a4,b4,_,_=ols(x[:-1],y[:-1]);sse=sum((v-a-b*u)**2 for u,v in zip(x,y)) assert abs(a-5.44)<1e-12 and abs(b+.24)<1e-12 and abs(h-.984)<1e-12 and abs(residual+.64)<1e-12 and abs(deleted+40)<1e-10 and abs(sse-25.6)<1e-12 assert (a4,b4)==(0.0,2.0) changed=ols(x,[2,4,6,8,40]);assert changed[:2]==(0.0,2.0) return dict(all_record_fit=[a,b],four_record_fit=[a4,b4],SSE=sse,fifth_leverage=h,fifth_residual=residual,fifth_deleted_residual=deleted,changed_response_fit=changed[:2]) def run(): return {'response':response_case(),'paired':paired_case(),'foreign_keys':foreign_keys_case(),'null_joins':null_join_case(),'ridge':ridge_case(),'influence':influence_case(),'scope':'Synthetic reproducibility checks. Not production migration, concurrency, academic acceptance or learner testing.'} if __name__=='__main__':print(json.dumps(run(),indent=2))