import%20marimo%0A%0A__generated_with%20%3D%20%220.25.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20METADATA%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22data_leakage%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Data%20Leakage%22%2C%0A%20%20%20%20%20%20%20%20%22kind%22%3A%20%22concept%22%2C%0A%20%20%20%20%20%20%20%20%22difficulty%22%3A%20%22beginner%22%2C%0A%20%20%20%20%20%20%20%20%22status%22%3A%20%22complete%22%2C%0A%20%20%20%20%20%20%20%20%22topics%22%3A%20%5B%22leakage%22%2C%20%22data_splitting%22%2C%20%22evaluation%22%5D%2C%0A%20%20%20%20%20%20%20%20%22related_models%22%3A%20%5B%5D%2C%0A%20%20%20%20%20%20%20%20%22prerequisites%22%3A%20%5B%22train_validation_test%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20mo.md(f%22%23%20%7BMETADATA%5B'name'%5D%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20rule%0A%0A%20%20%20%20Leakage%20lets%20training%20or%20evaluation%20use%20information%20that%20will%20not%20exist%20when%20a%20real%20prediction%20is%20made.%0A%0A%20%20%20%20%23%23%20Freeze%20time%20at%20the%20prediction%20moment%0A%0A%20%20%20%20Imagine%20predicting%20whether%20a%20package%20will%20arrive%20late%20using%20a%20field%20recorded%20after%20delivery.%20The%20model%20is%20accurate%2C%20but%20the%20experiment%20has%20answered%20an%20impossible%20question.%0A%0A%20%20%20%20%23%23%20Why%20leakage%20is%20dangerous%0A%0A%20%20%20%20Leakage%20produces%20unusually%20strong%20offline%20results%20and%20unusually%20disappointing%20systems.%20More%20sophisticated%20models%20often%20exploit%20leaked%20signals%20even%20more%20effectively.%0A%0A%20%20%20%20%23%23%20What%20we%20will%20compare%0A%0A%20%20%20%20**Question%3A**%20what%20happens%20when%20a%20post-outcome%20feature%20is%20included%20alongside%20legitimate%20prediction-time%20inputs%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Start%20with%20the%20prediction%20moment%0A%0A%20%20%20%20Every%20prediction%20problem%20needs%20a%20precise%20time%20%24t_0%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20t_0%3D%5Ctext%7Bthe%20moment%20when%20the%20prediction%20must%20be%20made%7D.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20A%20feature%20is%20valid%20only%20if%20its%20value%20can%20be%20known%20at%20or%20before%20%24t_0%24.%20A%20field%20recorded%20after%20%24t_0%24%20may%20be%20highly%20correlated%20with%20the%20target%2C%20but%20it%20is%20not%20valid%20evidence%20for%20this%20prediction.%0A%0A%20%20%20%20%23%23%23%20Delivery%20example%0A%0A%20%20%20%20Goal%3A%20predict%20at%20dispatch%20time%20whether%20a%20package%20will%20arrive%20late.%0A%0A%20%20%20%20%7C%20Feature%20%7C%20Available%20at%20dispatch%3F%20%7C%20Valid%3F%20%7C%0A%20%20%20%20%7C---%7C---%3A%7C---%3A%7C%0A%20%20%20%20%7C%20Travel%20distance%20%7C%20yes%20%7C%20yes%20%7C%0A%20%20%20%20%7C%20Current%20traffic%20%7C%20yes%20%7C%20yes%20%7C%0A%20%20%20%20%7C%20Final%20delivery%20status%20%7C%20no%20%7C%20no%20%7C%0A%20%20%20%20%7C%20Customer%20complaint%20after%20delivery%20%7C%20no%20%7C%20no%20%7C%0A%0A%20%20%20%20The%20invalid%20fields%20do%20not%20improve%20the%20real%20system.%20They%20only%20make%20the%20offline%20experiment%20easier.%0A%0A%20%20%20%20%23%23%20Four%20common%20forms%20of%20leakage%0A%0A%20%20%20%20%23%23%23%201.%20Target%20leakage%0A%0A%20%20%20%20A%20feature%20contains%20the%20answer%20or%20a%20consequence%20of%20the%20answer.%20Examples%20include%20final%20status%2C%20treatment%20outcome%2C%20or%20a%20refund%20created%20after%20fraud%20was%20confirmed.%0A%0A%20%20%20%20%23%23%23%202.%20Split%20leakage%0A%0A%20%20%20%20The%20same%20entity%20or%20a%20near-duplicate%20appears%20in%20training%20and%20evaluation.%20The%20system%20can%20remember%20the%20entity%20instead%20of%20learning%20a%20transferable%20pattern.%0A%0A%20%20%20%20%23%23%23%203.%20Preprocessing%20leakage%0A%0A%20%20%20%20A%20transformation%20is%20fitted%20before%20the%20split.%20For%20example%2C%20using%20the%20global%20mean%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmu_%7Ball%7D%3D%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7Dx_i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20to%20fill%20missing%20training%20values%20lets%20evaluation%20examples%20influence%20training.%20Compute%20%24%5Cmu_%7Btrain%7D%24%20from%20training%20data%20only%2C%20then%20apply%20that%20fixed%20value%20to%20validation%20and%20test%20data.%0A%0A%20%20%20%20%23%23%23%204.%20Temporal%20leakage%0A%0A%20%20%20%20Future%20observations%20influence%20a%20past%20prediction%2C%20often%20through%20random%20splitting%2C%20rolling%20statistics%2C%20or%20features%20whose%20timestamps%20were%20ignored.%0A%0A%20%20%20%20%23%23%20Why%20the%20test%20score%20does%20not%20protect%20you%0A%0A%20%20%20%20A%20held-out%20test%20set%20is%20not%20automatically%20safe.%20If%20both%20training%20and%20test%20rows%20contain%20the%20same%20leaked%20feature%2C%20the%20test%20score%20faithfully%20measures%20an%20impossible%20system.%20Correct%20splitting%20and%20correct%20feature%20availability%20are%20separate%20requirements.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20sklearn.linear_model%20import%20LogisticRegression%0A%20%20%20%20from%20sklearn.metrics%20import%20accuracy_score%2C%20roc_auc_score%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(31)%0A%20%20%20%20n%20%3D%20700%0A%20%20%20%20distance%20%3D%20rng.gamma(2.5%2C%202.0%2C%20n)%0A%20%20%20%20traffic%20%3D%20rng.normal(0%2C%201%2C%20n)%0A%20%20%20%20late_probability%20%3D%201%20%2F%20(1%20%2B%20np.exp(-(-2.1%20%2B%200.22%20*%20distance%20%2B%200.9%20*%20traffic)))%0A%20%20%20%20late%20%3D%20rng.binomial(1%2C%20late_probability)%0A%20%20%20%20legitimate%20%3D%20np.c_%5Bdistance%2C%20traffic%5D%0A%20%20%20%20post_delivery_status%20%3D%20late%20%2B%20rng.normal(0%2C%200.08%2C%20n)%0A%20%20%20%20leaked%20%3D%20np.c_%5Blegitimate%2C%20post_delivery_status%5D%0A%20%20%20%20train%2C%20test%20%3D%20train_test_split(np.arange(n)%2C%20test_size%3D0.35%2C%20random_state%3D5%2C%20stratify%3Dlate)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20LogisticRegression%2C%0A%20%20%20%20%20%20%20%20accuracy_score%2C%0A%20%20%20%20%20%20%20%20late%2C%0A%20%20%20%20%20%20%20%20leaked%2C%0A%20%20%20%20%20%20%20%20legitimate%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20post_delivery_status%2C%0A%20%20%20%20%20%20%20%20roc_auc_score%2C%0A%20%20%20%20%20%20%20%20test%2C%0A%20%20%20%20%20%20%20%20train%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Both%20datasets%20describe%20the%20same%20deliveries.%20The%20only%20difference%20is%20that%20the%20second%0A%20%20%20%20one%20contains%20a%20status%20recorded%20after%20delivery.%20We%20fit%20the%20same%20simple%20procedure%20to%0A%20%20%20%20both%2C%20so%20any%20spectacular%20improvement%20comes%20from%20forbidden%20information%20rather%20than%0A%20%20%20%20better%20learning.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LogisticRegression%2C%0A%20%20%20%20accuracy_score%2C%0A%20%20%20%20late%2C%0A%20%20%20%20leaked%2C%0A%20%20%20%20legitimate%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20post_delivery_status%2C%0A%20%20%20%20roc_auc_score%2C%0A%20%20%20%20test%2C%0A%20%20%20%20train%2C%0A)%3A%0A%20%20%20%20results%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20features%20in%20%5B(%22available%20at%20prediction%20time%22%2C%20legitimate)%2C%20(%22includes%20leaked%20outcome%22%2C%20leaked)%5D%3A%0A%20%20%20%20%20%20%20%20model%20%3D%20LogisticRegression().fit(features%5Btrain%5D%2C%20late%5Btrain%5D)%0A%20%20%20%20%20%20%20%20predicted_probability%20%3D%20model.predict_proba(features%5Btest%5D)%5B%3A%2C%201%5D%0A%20%20%20%20%20%20%20%20results.append((name%2C%20accuracy_score(late%5Btest%5D%2C%20predicted_probability%20%3E%3D%200.5)%2C%20roc_auc_score(late%5Btest%5D%2C%20predicted_probability)))%0A%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%204))%0A%20%20%20%20axes%5B0%5D.bar(%5Bitem%5B0%5D%20for%20item%20in%20results%5D%2C%20%5Bitem%5B2%5D%20for%20item%20in%20results%5D%2C%20color%3D%5B%22%2366706a%22%2C%20%22crimson%22%5D)%0A%20%20%20%20axes%5B0%5D.set(title%3D%22Leaked%20information%20creates%20a%20fantasy%20score%22%2C%20ylabel%3D%22test%20ROC%20AUC%22%2C%20ylim%3D(0%2C%201.05))%0A%20%20%20%20axes%5B0%5D.tick_params(axis%3D%22x%22%2C%20rotation%3D15)%0A%20%20%20%20axes%5B1%5D.scatter(post_delivery_status%5Btest%5D%2C%20late%5Btest%5D%2C%20alpha%3D0.25)%0A%20%20%20%20axes%5B1%5D.set(title%3D%22The%20leaked%20feature%20nearly%20contains%20the%20answer%22%2C%20xlabel%3D%22post-delivery%20status%22%2C%20ylabel%3D%22late%20outcome%22)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20most%20deceptive%20symptom%0A%0A%20%20%20%20Defining%20features%20before%20defining%20the%20prediction%20timestamp%2C%20or%20randomly%20splitting%20entities%20and%20time%20periods%20that%20share%20information.%0A%0A%20%20%20%20%23%23%20Audit%20the%20origin%20and%20time%20of%20every%20feature%0A%0A%20%20%20%20For%20every%20feature%2C%20ask%20when%20it%20becomes%20available%20and%20how%20it%20was%20computed.%20Investigate%20implausibly%20high%20scores%2C%20duplicates%2C%20target-derived%20fields%2C%20and%20preprocessing%20fitted%20before%20splitting.%0A%0A%20%20%20%20%23%23%20Safe%20default%0A%0A%20%20%20%20Write%20the%20prediction%20timestamp%20first%2C%20permit%20only%20information%20available%20at%20that%20moment%2C%20and%20fit%20every%20learned%20transformation%20inside%20the%20training%20split.%0A%0A%20%20%20%20%23%23%20Previous%20and%20next%0A%0A%20%20%20%20-%20Previous%3A%20%5BGeneralization%5D(%2Fconcepts%2Fgeneralization)%20defines%20the%20behavior%20evaluation%20should%20estimate.%0A%20%20%20%20-%20Next%3A%20%5BMetrics%5D(%2Fconcepts%2Fmetrics)%20explains%20how%20valid%20predictions%20are%20summarized.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
f91ff1e079cc00d59602ae9fc3d91865