%22%22%22A%20marimo%20notebook%20that%20explains%20single%20predictions%20of%20an%20EBM%3A%20counterfactual%2C%20sufficient%20reason%2C%20completions.%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20import%20attrs%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20interpret.glassbox%20import%20ExplainableBoostingClassifier%0A%20%20%20%20from%20sklearn.datasets%20import%20fetch_openml%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%0A%20%20%20%20from%20ebaml%20import%20(%0A%20%20%20%20%20%20%20%20DataScientist%2C%0A%20%20%20%20%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20%20%20%20%20ExplainInstanceRequest%2C%0A%20%20%20%20%20%20%20%20Layperson%2C%0A%20%20%20%20%20%20%20%20LocalOptions%2C%0A%20%20%20%20%20%20%20%20PromptOptions%2C%0A%20%20%20%20%20%20%20%20compute_local_facts%2C%0A%20%20%20%20%20%20%20%20describe_ebm%2C%0A%20%20%20%20%20%20%20%20explain_instance%2C%0A%20%20%20%20%20%20%20%20facts_to_texts%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DataScientist%2C%0A%20%20%20%20%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20%20%20%20%20ExplainInstanceRequest%2C%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20Layperson%2C%0A%20%20%20%20%20%20%20%20LocalOptions%2C%0A%20%20%20%20%20%20%20%20PromptOptions%2C%0A%20%20%20%20%20%20%20%20attrs%2C%0A%20%20%20%20%20%20%20%20compute_local_facts%2C%0A%20%20%20%20%20%20%20%20describe_ebm%2C%0A%20%20%20%20%20%20%20%20explain_instance%2C%0A%20%20%20%20%20%20%20%20facts_to_texts%2C%0A%20%20%20%20%20%20%20%20fetch_openml%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20os%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20%20%20%20%20train_test_split%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Explain%20one%20prediction%20of%20an%20EBM%0A%0A%20%20%20%20The%20global%20explanation%20of%20an%20Explainable%20Boosting%20Machine%20(EBM)%20says%20how%20the%20model%20works%20*in%20general*%3A%20see%0A%20%20%20%20%60titanic.py%60%20for%20how%20to%20ask%20an%20LLM%20for%20it.%20This%20notebook%20is%20about%20the%20*local*%20question%3A%20**why%20did%20the%20model%20predict%0A%20%20%20%20this%20for%20this%20passenger%3F**%0A%0A%20%20%20%20The%20answer%20has%20two%20parts%2C%20and%20the%20split%20is%20the%20point%20of%20the%20design%3A%0A%0A%20%20%20%201.%20**The%20facts%20are%20computed%2C%20not%20generated.**%20Python%20reads%20the%20scores%20and%20the%20confidence%20intervals%20of%20the%20EBM%20and%0A%20%20%20%20%20%20%20works%20out%20three%20things%20exactly.%20No%20LLM%20is%20involved%2C%20so%20they%20cannot%20be%20hallucinated%20and%20they%20are%20reproducible.%0A%20%20%20%202.%20**The%20LLM%20only%20puts%20the%20facts%20in%20words.**%20It%20gets%20the%20global%20explanation%20of%20the%20model%2C%20the%20facts%2C%20and%20an%0A%20%20%20%20%20%20%20*audience*%2C%20and%20writes%20the%20explanation.%0A%0A%20%20%20%20The%20three%20kinds%20of%20fact%20are%20the%20explainability%20queries%20of%0A%20%20%20%20%5BBarcel%C3%B3%20et%20al.%2C%20*Model%20Interpretability%20through%20the%20Lens%20of%20Computational%20Complexity*%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2010.12265)%3A%0A%0A%20%20%20%20%7C%20Query%20%7C%20Question%20it%20answers%20%7C%20Explanation%20%7C%0A%20%20%20%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%0A%20%20%20%20%7C%20Minimum%20change%20required%20%7C%20Fewest%20inputs%20to%20change%20so%20that%20the%20prediction%20flips%3F%20%7C%20Counterfactual%20%7C%0A%20%20%20%20%7C%20Minimum%20sufficient%20reason%20%7C%20Fewest%20inputs%20that%20settle%20it%2C%20whatever%20the%20others%20are%3F%20%7C%20Sufficient%20reason%20%7C%0A%20%20%20%20%7C%20Count%20completions%20%7C%20With%20only%20some%20inputs%20known%2C%20what%20share%20of%20completions%20get%20this%20prediction%3F%20%7C%20Completions%20%7C%0A%0A%20%20%20%20The%20first%20two%20are%20exact%20for%20an%20EBM%20without%20interactions%2C%20because%20the%20model%20is%20a%20sum%20of%20one%20effect%20per%20feature.%20The%0A%20%20%20%20third%20is%20estimated%20by%20sampling.%20See%20the%20documentation%20page%20%22Local%20explanations%22%20for%20the%20details%20and%20the%20limits.%0A%0A%20%20%20%20The%20cells%20that%20call%20the%20LLM%20only%20run%20when%20you%20press%20their%20button.%20The%20default%20model%20needs%20an%20OpenRouter%20API%20key%3A%0A%20%20%20%20%60OPENROUTER_API_KEY%60%20from%20the%20environment%20that%20started%20the%20notebook%20(see%20the%20documentation%20page%20%22Setup%22)%2C%20or%20the%0A%20%20%20%20password%20field%20below%20when%20it%20is%20not%20set.%20To%20use%20another%20model%2C%20pass%20%60model%3D%60%20to%20the%20functions%3A%20see%20the%20documentation%0A%20%20%20%20page%20%22Choosing%20the%20model%22.%20Everything%20before%20these%20cells%20works%20without%20a%20key.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20os)%3A%0A%20%20%20%20key_from_env%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20api_key_input%20%3D%20None%20if%20key_from_env%20else%20mo.ui.text(kind%3D%22password%22%2C%20label%3D%22OpenRouter%20API%20key%22%2C%20full_width%3DTrue)%0A%20%20%20%20key_status%20%3D%20(%0A%20%20%20%20%20%20%20%20mo.callout(%22Loaded%20%60OPENROUTER_API_KEY%60%20from%20the%20environment.%22%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20if%20key_from_env%0A%20%20%20%20%20%20%20%20else%20mo.vstack(%5Bmo.callout(%22%60OPENROUTER_API_KEY%60%20is%20not%20set%20in%20the%20environment.%22%2C%20kind%3D%22warn%22)%2C%20api_key_input%5D)%0A%20%20%20%20)%0A%20%20%20%20key_status%0A%20%20%20%20return%20(api_key_input%2C)%0A%0A%0A%40app.cell%0Adef%20_(api_key_input%2C%20os)%3A%0A%20%20%20%20%23%20a%20typed%20key%20only%20lives%20in%20this%20process%3B%20the%20BAML%20clients%20read%20the%20variable%20when%20they%20send%20a%20request%0A%20%20%20%20if%20api_key_input%20is%20not%20None%20and%20api_key_input.value%3A%0A%20%20%20%20%20%20%20%20os.environ%5B%22OPENROUTER_API_KEY%22%5D%20%3D%20api_key_input.value%0A%20%20%20%20has_api_key%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20return%20(has_api_key%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Load%20the%20data%20and%20train%20an%20EBM%0A%0A%20%20%20%20The%20same%20Titanic%20model%20as%20in%20%60titanic.py%60.%20Local%20explanations%20need%20*every*%20input%20of%20the%20passenger%2C%20so%20the%20passengers%0A%20%20%20%20to%20explain%20are%20the%20ones%20with%20no%20missing%20value.%20The%20EBM%20itself%20is%20trained%20on%20all%20the%20rows.%20It%20has%20to%20be%20a%20binary%0A%20%20%20%20classifier%20without%20pairwise%20interactions%20(%60interactions%3D0%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20fetch_openml%2C%20pl%2C%20train_test_split)%3A%0A%20%20%20%20%23%20The%20liac-arff%20parser%20reads%20the%20categories%20as%20text.%20The%20pandas%20parser%20of%20scikit-learn%201.9%20fails%20on%20pandas%203.1.%0A%20%20%20%20titanic%20%3D%20pl.from_pandas(fetch_openml(%22titanic%22%2C%20version%3D1%2C%20as_frame%3DTrue%2C%20parser%3D%22liac-arff%22).frame)%0A%20%20%20%20features%20%3D%20titanic.select(%22pclass%22%2C%20%22sex%22%2C%20%22age%22%2C%20%22sibsp%22%2C%20%22parch%22%2C%20%22fare%22%2C%20%22embarked%22).with_columns(%0A%20%20%20%20%20%20%20%20pl.col(%22sex%22%2C%20%22embarked%22).cast(pl.String)%2C%0A%20%20%20%20)%0A%20%20%20%20survived%20%3D%20titanic%5B%22survived%22%5D.cast(pl.String).cast(pl.Int8)%0A%20%20%20%20x_train%2C%20x_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(features%2C%20survived%2C%20test_size%3D0.2%2C%20random_state%3D42)%0A%20%20%20%20ebm%20%3D%20ExplainableBoostingClassifier(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42)%0A%20%20%20%20ebm.fit(x_train%2C%20y_train)%0A%20%20%20%20complete%20%3D%20x_test.drop_nulls()%0A%20%20%20%20return%20complete%2C%20ebm%2C%20x_test%2C%20y_test%0A%0A%0A%40app.cell%0Adef%20_(complete%2C%20ebm%2C%20mo%2C%20x_test%2C%20y_test)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20Test%20accuracy%3A%20**%7Bebm.score(x_test%2C%20y_test)%3A.3f%7D**.%20**%7Blen(complete)%7D**%20of%20the%20%7Blen(x_test)%7D%20test%20passengers%20have%0A%20%20%20%20no%20missing%20value%20and%20can%20be%20explained.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Pick%20a%20passenger%0A%0A%20%20%20%20Any%20passenger%20works.%20Passenger%200%20is%20a%20good%20start%3B%20then%20look%20for%20one%20the%20model%20is%20unsure%20about%2C%20and%20compare.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(complete%2C%20mo)%3A%0A%20%20%20%20passenger%20%3D%20mo.ui.slider(0%2C%20len(complete)%20-%201%2C%20value%3D0%2C%20label%3D%22Passenger%22%2C%20show_value%3DTrue)%0A%20%20%20%20passenger%0A%20%20%20%20return%20(passenger%2C)%0A%0A%0A%40app.cell%0Adef%20_(complete%2C%20ebm%2C%20mo%2C%20passenger)%3A%0A%20%20%20%20instance%20%3D%20complete.row(passenger.value%2C%20named%3DTrue)%0A%20%20%20%20survival%20%3D%20ebm.predict_proba(complete%5Bpassenger.value%20%3A%20passenger.value%20%2B%201%5D)%5B0%5D%5B1%5D%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22Passenger%20%22%0A%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22**%7Bname%7D**%20%3D%20%7Bvalue%7D%22%20for%20name%2C%20value%20in%20instance.items())%0A%20%20%20%20%20%20%20%20%2B%20f%22.%5Cn%5CnThe%20EBM%20gives%20a%20survival%20probability%20of%20**%7Bsurvival%3A.2f%7D**.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(instance%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20facts%2C%20without%20any%20LLM%0A%0A%20%20%20%20%60compute_local_facts%60%20computes%20the%20prediction%20and%20the%20three%20explanations.%20Its%20options%20(a%20%60LocalOptions%60)%20are%3A%0A%0A%20%20%20%20-%20%60confidence_level%60%3A%20how%20sure%20a%20claim%20has%20to%20be.%20Every%20score%20is%20moved%20by%20that%20many%20standard%20deviations%20*against*%0A%20%20%20%20%20%20the%20claim%2C%20so%20a%20counterfactual%20still%20flips%20the%20prediction%2C%20and%20a%20sufficient%20reason%20still%20holds%2C%20when%20the%20model's%0A%20%20%20%20%20%20effects%20sit%20at%20the%20unfavourable%20edge%20of%20their%20confidence%20interval.%20%600%60%20ignores%20the%20uncertainty.%0A%20%20%20%20-%20%60min_support%60%3A%20the%20share%20of%20the%20training%20data%20a%20value%20needs%20to%20be%20offered%20as%20an%20alternative.%20It%20keeps%20freak%0A%20%20%20%20%20%20values%20(a%20500-year-old%20passenger)%20out%20of%20the%20counterfactual.%0A%0A%20%20%20%20Move%20the%20slider%20and%20watch%20the%20explanations%20get%20harder%20to%20prove%3A%20more%20changes%20are%20needed%20to%20flip%20the%20prediction%2C%0A%20%20%20%20and%20more%20inputs%20are%20needed%20to%20settle%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20confidence%20%3D%20mo.ui.slider(0.0%2C%200.99%2C%20step%3D0.01%2C%20value%3D0.95%2C%20label%3D%22Confidence%20level%22%2C%20show_value%3DTrue)%0A%20%20%20%20confidence%0A%20%20%20%20return%20(confidence%2C)%0A%0A%0A%40app.cell%0Adef%20_(LocalOptions%2C%20compute_local_facts%2C%20confidence%2C%20ebm%2C%20instance)%3A%0A%20%20%20%20facts%20%3D%20compute_local_facts(ebm%2C%20instance%2C%20LocalOptions(confidence_level%3Dconfidence.value))%0A%20%20%20%20return%20(facts%2C)%0A%0A%0A%40app.cell%0Adef%20_(attrs%2C%20facts%2C%20mo)%3A%0A%20%20%20%20prediction%20%3D%20facts.prediction%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Prediction%20and%20contributions%0A%0A%20%20%20%20The%20model%20predicts%20class%20**%7Bprediction.decision%7D**%20with%20probability%20**%7Bprediction.output%3A.2f%7D**%2C%20and%20a%0A%20%20%20%20%7Bfacts.confidence_level%3A.0%25%7D%20confidence%20interval%20of%20**%5B%7Bprediction.lower%3A.2f%7D%2C%20%7Bprediction.upper%3A.2f%7D%5D**.%20The%0A%20%20%20%20interval%20comes%20from%20adding%20the%20variances%20of%20the%20effects%20of%20the%20features%2C%20which%20assumes%20they%20are%20independent.%0A%0A%20%20%20%20The%20contributions%20are%20in%20log-odds%3A%20a%20positive%20score%20pushes%20towards%20class%201%20(survived).%20Largest%20first%3A%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(%5Battrs.asdict(c)%20for%20c%20in%20facts.contributions%5D%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(attrs%2C%20facts%2C%20mo)%3A%0A%20%20%20%20counterfactual%20%3D%20facts.counterfactual%0A%20%20%20%20if%20counterfactual%20is%20None%3A%0A%20%20%20%20%20%20%20%20counterfactual_view%20%3D%20mo.md(%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Counterfactual%0A%0A%20%20%20%20%20%20%20%20**No%20change%20flips%20the%20prediction**%20at%20this%20confidence%20level%2C%20even%20when%20every%20input%20is%20changed%20to%20its%20most%0A%20%20%20%20%20%20%20%20favourable%20realistic%20value.%0A%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20counterfactual_view%20%3D%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%23%23%20Counterfactual%20(minimum%20change%20required)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20**%7Blen(counterfactual.changes)%7D**%20change(s)%20are%20enough%20to%20flip%20the%20prediction%2C%20and%20**no%20smaller%20set%20of%0A%20%20%20%20%20%20%20%20%20%20%20%20changes%20does**.%20After%20them%20the%20other%20class%20has%20probability%20**%7Bcounterfactual.new_output%3A.2f%7D**.%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(%5Battrs.asdict(c)%20for%20c%20in%20counterfactual.changes%5D%2C%20selection%3DNone)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20counterfactual_view%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(facts%2C%20mo)%3A%0A%20%20%20%20sufficient%20%3D%20facts.sufficient_reason%0A%20%20%20%20if%20sufficient%20is%20None%3A%0A%20%20%20%20%20%20%20%20sufficient_view%20%3D%20mo.md(%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Sufficient%20reason%0A%0A%20%20%20%20%20%20%20%20**There%20is%20none**%20at%20this%20confidence%20level%3A%20even%20with%20every%20input%20known%2C%20the%20model's%20uncertainty%20is%20too%20large%20to%0A%20%20%20%20%20%20%20%20guarantee%20the%20prediction.%20Lower%20the%20confidence%20level%20to%20see%20one.%0A%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20sufficient_view%20%3D%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Sufficient%20reason%20(minimum%20sufficient%20reason)%0A%0A%20%20%20%20%20%20%20%20Once%20**%7B%22%2C%20%22.join(sufficient.features)%7D**%20are%20known%2C%20the%20prediction%20is%20settled%3A%20it%20cannot%20change%20whatever%20the%0A%20%20%20%20%20%20%20%20other%20inputs%20are%2C%20and%20**no%20smaller%20set%20of%20inputs**%20does%20this.%20The%20guaranteed%20margin%2C%20in%20log-odds%2C%20is%0A%20%20%20%20%20%20%20%20**%7Bsufficient.guaranteed_margin%3A.2f%7D**.%0A%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20sufficient_view%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(attrs%2C%20facts%2C%20mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Completions%20(count%20completions)%0A%0A%20%20%20%20Reveal%20the%20inputs%20one%20by%20one%2C%20in%20the%20order%20of%20how%20much%20each%20guarantees%2C%20and%20count%20how%20many%20of%20the%20possible%0A%20%20%20%20completions%20of%20the%20unknown%20inputs%20get%20the%20same%20prediction.%20The%20unknown%20inputs%20are%20sampled%20from%20the%20training%20data%0A%20%20%20%20(10%2C000%20samples%2C%20fixed%20seed)%2C%20so%20these%20shares%20are%20estimates%2C%20not%20guarantees.%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(%5Battrs.asdict(s)%20for%20s%20in%20facts.completions%5D%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Check%20the%20guarantees%0A%0A%20%20%20%20The%20first%20two%20explanations%20are%20claims%20about%20*every*%20possible%20input%2C%20so%20they%20can%20be%20tested.%20Take%20the%20sufficient%0A%20%20%20%20reason%2C%20keep%20the%20passenger's%20values%20for%20those%20features%2C%20and%20fill%20all%20the%20other%20features%20with%20the%20values%20of%0A%20%20%20%20randomly%20chosen%20other%20passengers.%20The%20prediction%20must%20not%20change%2C%20ever.%20(This%20uses%20%60confidence_level%3D0%60%20and%0A%20%20%20%20%60min_support%3D0%60%2C%20so%20that%20the%20claim%20covers%20every%20value%2C%20even%20rare%20ones.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LocalOptions%2C%20complete%2C%20compute_local_facts%2C%20ebm%2C%20instance%2C%20mo%2C%20np)%3A%0A%20%20%20%20strict%20%3D%20compute_local_facts(ebm%2C%20instance%2C%20LocalOptions(confidence_level%3D0%2C%20min_support%3D0))%0A%20%20%20%20original%20%3D%20strict.prediction.decision%0A%20%20%20%20if%20strict.sufficient_reason%20is%20None%3A%0A%20%20%20%20%20%20%20%20check_view%20%3D%20mo.md(%22No%20sufficient%20reason%20to%20check.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20check_rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20%20%20%20%20check_pool%20%3D%20complete.to_dicts()%0A%20%20%20%20%20%20%20%20check_same%20%3D%200%0A%20%20%20%20%20%20%20%20check_trials%20%3D%20500%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(check_trials)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20check_other%20%3D%20check_pool%5Bint(check_rng.integers(len(check_pool)))%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20check_mixed%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3A%20value%20if%20name%20in%20strict.sufficient_reason.features%20else%20check_other%5Bname%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20value%20in%20instance.items()%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20check_same%20%2B%3D%20int(compute_local_facts(ebm%2C%20check_mixed).prediction.decision%20%3D%3D%20original)%0A%20%20%20%20%20%20%20%20check_view%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22With%20**%7B'%2C%20'.join(strict.sufficient_reason.features)%7D**%20fixed%2C%20**%7Bcheck_same%7D**%20of%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Bcheck_trials%7D**%20random%20completions%20kept%20the%20prediction%20(%7Boriginal%7D).%20It%20should%20be%20all%20of%20them.%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20check_view%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20the%20LLM%20reads%0A%0A%20%20%20%20%60facts_to_texts%60%20turns%20the%20facts%20into%20one%20JSON%20text%20per%20explanation.%20This%20is%20the%20*audience*%20at%20work%2C%20and%20it%20happens%0A%20%20%20%20before%20the%20LLM%20sees%20anything%3A%0A%0A%20%20%20%20-%20A%20**layperson**%20(the%20default)%20knows%20no%20numbers.%20Scores%2C%20probabilities%2C%20log-odds%20and%20intervals%20are%20replaced%20by%0A%20%20%20%20%20%20words%20(%60strongly%60%2C%20%60very%20likely%60)%2C%20so%20the%20LLM%20has%20no%20model%20number%20to%20repeat.%20The%20prompt%20also%20forbids%20numbers%20and%0A%20%20%20%20%20%20jargon.%0A%20%20%20%20-%20A%20**data%20scientist**%20gets%20the%20scores%2C%20the%20probabilities%2C%20the%20confidence%20interval%20and%20the%20confidence%20level%2C%20and%20is%0A%20%20%20%20%20%20told%20to%20quote%20them.%0A%0A%20%20%20%20Both%20are%20subclasses%20of%20%60Audience%60.%20The%20notebook%20%60audiences.py%60%20shows%20how%20to%20write%20your%20own.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DataScientist%2C%20Layperson%2C%20facts%2C%20facts_to_texts%2C%20mo)%3A%0A%20%20%20%20audience_texts%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22layperson%22%3A%20facts_to_texts(facts%2C%20Layperson())%2C%0A%20%20%20%20%20%20%20%20%22data_scientist%22%3A%20facts_to_texts(facts%2C%20DataScientist())%2C%0A%20%20%20%20%7D%0A%20%20%20%20mo.ui.tabs(%7B%0A%20%20%20%20%20%20%20%20kind%3A%20mo.hstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bmo.md(f%22**%7Bname%7D**%5Cn%60%60%60json%5Cn%7Bgetattr(texts%2C%20kind)%7D%5Cn%60%60%60%22)%20for%20name%2C%20texts%20in%20audience_texts.items()%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20align%3D%22start%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20justify%3D%22start%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20kind%20in%20(%22prediction%22%2C%20%22counterfactual%22%2C%20%22sufficient_reason%22%2C%20%22completions%22)%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Put%20it%20in%20words%0A%0A%20%20%20%20%60explain_instance%60%20needs%20the%20**global%20explanation**%20of%20the%20model%2C%20computed%20once%20and%20prepended%20to%20every%20prompt%2C%20so%0A%20%20%20%20the%20LLM%20knows%20what%20the%20features%20mean%20and%20which%20effects%20are%20surprising.%20Get%20it%20with%20%60describe_ebm%60%20(this%20is%20the%20slow%0A%20%20%20%20step%3A%20one%20chain%20per%20feature%2C%20then%20a%20summary).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20describe_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20the%20model%22)%0A%20%20%20%20describe_button%0A%20%20%20%20return%20(describe_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20PromptOptions%2C%0A%20%20%20%20describe_button%2C%0A%20%20%20%20describe_ebm%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20has_api_key%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20describe_button.value%2C%20mo.md(%22Press%20the%20button%20to%20describe%20the%20model%20first.%22))%0A%20%20%20%20mo.stop(not%20has_api_key%2C%20mo.callout(%22Enter%20your%20OpenRouter%20API%20key%20above%20first.%22%2C%20kind%3D%22warn%22))%0A%20%20%20%20dataset_description%20%3D%20(%0A%20%20%20%20%20%20%20%20%22The%20RMS%20Titanic%20sank%20in%20April%201912.%20Each%20row%20is%20a%20passenger%3B%20the%20outcome%20is%20whether%20they%20survived.%20%22%0A%20%20%20%20%20%20%20%20%22pclass%20is%20the%20ticket%20class%20(1%20first%2C%203%20third)%2C%20sibsp%20and%20parch%20count%20relatives%20aboard%2C%20%22%0A%20%20%20%20%20%20%20%20%22fare%20is%20in%201912%20pounds%2C%20embarked%20is%20the%20port%20(C%2C%20Q%20or%20S).%20%22%0A%20%20%20%20%20%20%20%20%22The%20graphs%20show%20contributions%20in%20log-odds%20towards%20survival.%22%0A%20%20%20%20)%0A%20%20%20%20with%20mo.status.spinner(%22Describing%20the%20model...%22)%3A%0A%20%20%20%20%20%20%20%20global_explanation%20%3D%20describe_ebm(%0A%20%20%20%20%20%20%20%20%20%20%20%20DescribeEBMRequest(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ebm%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_concurrency%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prompt%3DPromptOptions(dataset_description%3Ddataset_description)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20).text%0A%20%20%20%20mo.md(global_explanation)%0A%20%20%20%20return%20dataset_description%2C%20global_explanation%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20audience%20%3D%20mo.ui.dropdown(options%3D%5B%22layperson%22%2C%20%22data_scientist%22%5D%2C%20value%3D%22layperson%22%2C%20label%3D%22Audience%22)%0A%20%20%20%20explain_button%20%3D%20mo.ui.run_button(label%3D%22Explain%20this%20prediction%22)%0A%20%20%20%20mo.hstack(%5Baudience%2C%20explain_button%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20audience%2C%20explain_button%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ExplainInstanceRequest%2C%0A%20%20%20%20LocalOptions%2C%0A%20%20%20%20PromptOptions%2C%0A%20%20%20%20audience%2C%0A%20%20%20%20confidence%2C%0A%20%20%20%20dataset_description%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20explain_button%2C%0A%20%20%20%20explain_instance%2C%0A%20%20%20%20global_explanation%2C%0A%20%20%20%20instance%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20explain_button.value%2C%20mo.md(%22Press%20the%20button%20to%20explain%20the%20prediction%20of%20the%20passenger%20above.%22))%0A%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(three%20explanations%2C%20concurrently)...%22)%3A%0A%20%20%20%20%20%20%20%20explanation%20%3D%20explain_instance(%0A%20%20%20%20%20%20%20%20%20%20%20%20ExplainInstanceRequest(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ebm%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20instance%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20global_explanation%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20audience%3Daudience.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20local%3DLocalOptions(confidence_level%3Dconfidence.value)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prompt%3DPromptOptions(dataset_description%3Ddataset_description)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20What%20would%20have%20to%20change%0A%0A%20%20%20%20%7Bexplanation.counterfactual%7D%0A%0A%20%20%20%20%23%23%23%20Which%20inputs%20settle%20it%0A%0A%20%20%20%20%7Bexplanation.sufficient_reason%7D%0A%0A%20%20%20%20%23%23%23%20How%20certain%20it%20gets%20as%20inputs%20become%20known%0A%0A%20%20%20%20%7Bexplanation.completions%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Run%20it%20again%20with%20the%20other%20audience%20and%20compare.%20The%20facts%20are%20the%20same%3B%20the%20words%20are%20not.%20Because%20the%20facts%20are%0A%20%20%20%20computed%20and%20not%20generated%2C%20%60explanation.facts%60%20holds%20the%20numbers%20behind%20the%20text%2C%20so%20any%20sentence%20can%20be%20checked.%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
369c28584d3d0f6bfb055dc0629444e4