Review index · Download raw source
reference/statistics/oracle_same_generator.py
1from pathlib import Path2import json, csv, hashlib, datetime, time, math3import numpy as np4import practical_morphology as pm5P=Path(__file__).resolve().parent6START=datetime.datetime.now(datetime.timezone.utc).isoformat()7tstart=time.perf_counter()8results=json.loads((P/"practical_morphology_results.json").read_text(encoding="utf-8"))9rows=[]; lookup={}10for scenario in results["scenarios"]:11 khz=scenario["sample_rate_khz"];n_eq=scenario["N_plateau_equivalent_3ms_PE"]12 b=scenario["background_cps"];truth=scenario["truth_receiver_rise_10_90_us"]13 d=pm.setup(khz)14 draw=pm.draw_case(d,n_eq,b,truth,scenario["seed"],scenario["trials"])15 fast_choices=[truth] if truth in [30,80] else [30,80]16 for fast in fast_choices:17 muf=b*d["dt"]+(n_eq/pm.T)*pm.source_integrals(d["edges"],draw["onset"],fast)18 mus=b*d["dt"]+(n_eq/pm.T)*pm.source_integrals(d["edges"],draw["onset"],200)19 ll=(draw["full"]*np.log(muf/mus)-(muf-mus)).sum(axis=1)20 wrong=(ll<0 if truth in [30,80] else ll>0)21 tie=ll==022 # Exact likelihood ties have randomized decisions with an independent fixed seed.23 rng=np.random.default_rng(scenario["seed"]+700000+fast)24 wrong=wrong | (tie & (rng.uniform(size=len(ll))<.5))25 row={"N_plateau_equivalent_3ms_PE":n_eq,"background_cps":b,"sample_rate_khz":khz,26 "truth_receiver_rise_10_90_us":truth,"fast_comparator_us":fast,27 "scenario_seed":scenario["seed"],"trials":len(ll),28 "wrong_oracle":int(wrong.sum()),"oracle_error":float(wrong.mean()),29 "individual_95pct_Wilson_interval":pm.wilson(wrong.sum(),len(ll)),30 "ties":int(tie.sum()),"known_nuisance_only":True}31 rows.append(row);lookup[(n_eq,b,khz,fast,truth)]=row32paired=[]33for n in pm.COUNTS:34 for b in pm.BACKGROUNDS:35 for khz in pm.SAMPLE_KHZ:36 for fast in [30,80]:37 f=lookup[(n,b,khz,fast,fast)];s=lookup[(n,b,khz,fast,200)]38 pe=(f["oracle_error"]+s["oracle_error"])/239 # Familywise95% Hoeffding coverage over54 balanced estimates of2000 independent,40 # potentially non-identically distributed Bernoulli errors.41 eps=math.sqrt(math.log(2*54/.05)/(2*(f["trials"]+s["trials"])))42 paired.append({"N_plateau_equivalent_3ms_PE":n,"background_cps":b,"sample_rate_khz":khz,43 "fast_comparator_us":fast,"slow_comparator_us":200,44 "trials_fast":f["trials"],"trials_slow":s["trials"],45 "balanced_error":pe,"familywise_95pct_Hoeffding_halfwidth":eps,46 "interval_lower":max(0,pe-eps),"interval_upper":min(1,pe+eps)})47# Direct current-rerun match to original deterministic CSVs and the original seeded MC.48old=P.parent.parent.parent.parent/"research/sources_population/ideal_poisson_rise_classification"49# Resolve from explicit stable source root rather than relying on directory depth.50old=Path("D:/Photometer_research_20261022/research/sources_population/ideal_poisson_rise_classification")51same=[]52previous=json.loads((P/"oracle_reference/rerun_verification.json").read_text()) if (P/"oracle_reference/rerun_verification.json").exists() else {}53expected={r["file"]:r["original_sha256"] for r in previous.get("csv_matches",[])}54for name in ["source_count_expectations.csv","rise_pair_affinity_bounds.csv",55 "pulse_detection_affinity_bounds.csv","limited_poisson_MC.csv",56 "single_bin_exact_controls.csv"]:57 z=hashlib.sha256((P/"oracle_reference"/name).read_bytes()).hexdigest()58 a=hashlib.sha256((old/name).read_bytes()).hexdigest() if (old/name).exists() else expected[name]59 assert a==z60 same.append({"file":name,"original_sha256":a,"revision_sha256":z,"byte_identical":True,61 "original_available_this_run":(old/name).exists()})62revision_summary_path=P/"oracle_reference/calculation_summary.json"63oracle=json.loads(revision_summary_path.read_text())64# The copied scientific script retains its historical study start constant.65# Record that explicitly so the revision rerun is not assigned old execution time.66oracle["original_study_start_utc"]="2026-10-22T00:48:38Z"67oracle["revision_rerun_note"]="Numerical algorithm and seeded results match the original; copied script START metadata now records this execution. See rerun_verification.json."68revision_summary_path.write_text(json.dumps(oracle,indent=2),encoding="utf-8")69rerun={"status":"EXECUTED_AND_ALL_FIVE_RESULT_CSVS_BYTE_IDENTICAL_TO_PRIOR_VERIFIED_RUN",70 "verified_utc":datetime.datetime.now(datetime.timezone.utc).isoformat(),71 "script_original_sha256":hashlib.sha256((old/"calculate_ideal_poisson_rise.py").read_bytes()).hexdigest() if (old/"calculate_ideal_poisson_rise.py").exists() else previous.get("script_original_sha256"),72 "script_revision_sha256":hashlib.sha256((P/"oracle_reference/calculate_ideal_poisson_rise.py").read_bytes()).hexdigest(),73 "csv_matches":same,"script_difference":"Only START timestamp metadata changed from the copied original; algorithm and all five result CSVs are unchanged."}74(P/"oracle_reference/rerun_verification.json").write_text(json.dumps(rerun,indent=2),encoding="utf-8")75pm.save_csv(P/"oracle_same_generator_comparisons.csv",76 [{k:v for k,v in r.items() if k!="individual_95pct_Wilson_interval"} for r in rows])77pm.save_csv(P/"oracle_balanced_errors.csv",paired)78out={"status":"EXECUTED_KNOWN_NUISANCE_ORACLE_ON_SAME_SEEDED_GENERATOR",79 "start_utc":START,"finished_utc":datetime.datetime.now(datetime.timezone.utc).isoformat(),80 "runtime_s":time.perf_counter()-tstart,"truth_rows":rows,"balanced_pair_rows":paired,81 "qualifications":["The oracle knows exact onset, peak rate, background and which fast morphology competes with200us; all physical hardware-bin means are otherwise the same.",82 "Paired fast/slow rows use independent synthetic trials; each practical truth scenario is reused only for its relevant oracle comparisons. They are not additional independent population data.",83 "No fitted likelihood or classification rejection in the binary oracle. Its Bayes rule has equal prior weights for the fixed pair; this does not imply equal mission population priors.",84 "The practical3-class or grouped decision has different unknown parameters and an acceptance/rejection rule, so a finite-sample comparison of accepted errors alone is not a universal ordering.",85 "The paired error is estimated over random sampling phase with the oracle told each phase. Not the error for unknown onset.",86 "Familywise Hoeffding intervals cover Monte Carlo implementation uncertainty only, not instrument or source-model systematic uncertainty."],87 "oracle_rerun":rerun}88(P/"oracle_current_generator_results.json").write_text(json.dumps(out,indent=2),encoding="utf-8")89print(json.dumps({"finished":True,"runtime_s":out["runtime_s"],"rows":len(rows),"balanced_pairs":len(paired),"original_rerun_match":True}),flush=True)9091