import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20itertools%0A%20%20%20%20import%20math%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20import%20shap%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20shap.maskers%20import%20Independent%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20Independent%2C%0A%20%20%20%20%20%20%20%20LinearRegression%2C%0A%20%20%20%20%20%20%20%20go%2C%0A%20%20%20%20%20%20%20%20itertools%2C%0A%20%20%20%20%20%20%20%20make_subplots%2C%0A%20%20%20%20%20%20%20%20math%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%20pd%2C%0A%20%20%20%20%20%20%20%20shap%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%5B%E2%86%90%2044%20Logistic%20Regression%5D(44_logistic_regression.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B46%20Model%20Counterfactuals%20%E2%86%92%5D(46_model_counterfactuals.py)%0A%0A%20%20%20%20%23%20Shapley%20Values%3A%20From%20Cooperative%20Game%20Theory%20to%20SHAP%20Model%20Explainability%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20Modern%20machine%20learning%20models%20(such%20as%20Gradient%20Boosted%20Trees%2C%20Random%20Forests%2C%20and%20Deep%20Neural%20Networks)%20operate%20as%20non-linear%20black%20boxes.%20Simple%20heuristic%20importance%20measures%E2%80%94such%20as%20tree%20Mean%20Decrease%20in%20Impurity%20(Gini%20importance)%20or%20unstandardized%20regression%20weights%E2%80%94fail%20when%20features%20interact%20non-linearly%20or%20exhibit%20multi-collinearity.%0A%0A%20%20%20%20%23%23%23%23%20Origins%20in%20Cooperative%20Game%20Theory%0A%20%20%20%20In%201953%2C%20mathematician%20Lloyd%20Shapley%20addressed%20a%20fundamental%20economic%20problem%3A%20in%20a%20cooperative%20coalition%20game%20where%20players%20collaborate%20to%20produce%20a%20collective%20payoff%2C%20how%20should%20the%20total%20reward%20be%20divided%20fairly%20among%20the%20participants%3F%0A%0A%20%20%20%20Shapley%20proved%20that%20if%20we%20require%20the%20reward%20allocation%20to%20satisfy%20four%20basic%20principles%20of%20fairness%20(**Efficiency%2C%20Symmetry%2C%20Dummy%20Player%2C%20and%20Additivity**)%2C%20there%20exists%20a%20**unique%2C%20mathematically%20provable%20solution**%3A%20the%20**Shapley%20Value**.%0A%0A%20%20%20%20%23%23%23%23%20The%20SHAP%20Framework%20in%20Machine%20Learning%0A%20%20%20%20In%202017%2C%20Scott%20Lundberg%20and%20Su-In%20Lee%20unified%20cooperative%20game%20theory%20with%20local%20model%20interpretability%20through%20**SHAP%20(SHapley%20Additive%20exPlanations)**%3A%0A%20%20%20%20-%20**Players**%20%24%5Cto%24%20The%20individual%20input%20features%20%24x_1%2C%20x_2%2C%20%5Cdots%2C%20x_p%24.%0A%20%20%20%20-%20**Grand%20Coalition%20Payoff**%20%24%5Cto%24%20The%20model's%20prediction%20for%20a%20specific%20instance%20%24f(x)%24.%0A%20%20%20%20-%20**Baseline%20Payoff%20(Empty%20Coalition)**%20%24%5Cto%24%20The%20expected%20base%20prediction%20across%20the%20dataset%20%24%5Cmathbb%7BE%7D%5Bf(X)%5D%24.%0A%20%20%20%20-%20**Fair%20Share**%20%24%5Cto%24%20The%20local%20attribution%20%24%5Cphi_j(x)%24%20measuring%20the%20exact%20amount%20feature%20%24j%24%20pushed%20the%20model's%20prediction%20above%20or%20below%20the%20population%20baseline.%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%20%5Bb%5D%20Mathematical%20Foundations%20and%20Axiomatic%20Mechanics%0A%0A%20%20%20%20%23%23%23%201.%20The%20Cooperative%20Game%20Formulation%0A%0A%20%20%20%20Let%20%24N%20%3D%20%5C%7B1%2C%202%2C%20%5Cdots%2C%20p%5C%7D%24%20be%20the%20finite%20set%20of%20%24p%24%20players%20(features).%20A%20coalition%20%24S%20%5Csubseteq%20N%24%20is%20any%20subset%20of%20players.%0A%0A%20%20%20%20A%20**characteristic%20function**%20%24v%3A%202%5EN%20%5Cto%20%5Cmathbb%7BR%7D%24%20maps%20every%20possible%20coalition%20%24S%24%20to%20a%20real-valued%20payoff%20%24v(S)%24%2C%20with%20the%20baseline%20condition%20that%20the%20empty%20coalition%20yields%20zero%3A%20%24v(%5Cemptyset)%20%3D%200%24.%0A%0A%20%20%20%20%23%23%23%202.%20The%20Four%20Fairness%20Axioms%0A%0A%20%20%20%20The%20Shapley%20value%20%24%5Cphi(v)%20%3D%20(%5Cphi_1(v)%2C%20%5Cdots%2C%20%5Cphi_p(v))%5E%5Ctop%24%20is%20the%20unique%20payoff%20vector%20satisfying%3A%0A%0A%20%20%20%201.%20**Efficiency%20(Completeness)**%3A%20The%20total%20payoff%20of%20the%20grand%20coalition%20%24N%24%20is%20distributed%20among%20all%20players%3A%0A%0A%20%20%20%20%24%24%5Csum_%7Bj%3D1%7D%5Ep%20%5Cphi_j(v)%20%3D%20v(N)%24%24%0A%0A%20%20%20%20In%20machine%20learning%2C%20this%20guarantees%20that%20local%20attributions%20sum%20to%20the%20difference%20between%20the%20prediction%20and%20the%20global%20expectation%3A%20%24%5Csum_%7Bj%3D1%7D%5Ep%20%5Cphi_j(x)%20%3D%20f(x)%20-%20%5Cmathbb%7BE%7D%5Bf(X)%5D%24.%0A%0A%20%20%20%202.%20**Symmetry%20(Equal%20Treatment%20of%20Equals)**%3A%20If%20two%20players%20%24j%24%20and%20%24k%24%20contribute%20identically%20across%20all%20possible%20sub-coalitions%20%24S%20%5Csubseteq%20N%20%5Csetminus%20%5C%7Bj%2C%20k%5C%7D%24%3A%0A%0A%20%20%20%20%24%24v(S%20%5Ccup%20%5C%7Bj%5C%7D)%20%3D%20v(S%20%5Ccup%20%5C%7Bk%5C%7D)%20%5Cimplies%20%5Cphi_j(v)%20%3D%20%5Cphi_k(v)%24%24%0A%0A%20%20%20%203.%20**Dummy%20%2F%20Null%20Player**%3A%20If%20player%20%24j%24%20adds%20zero%20marginal%20value%20to%20every%20coalition%20%24S%20%5Csubseteq%20N%20%5Csetminus%20%5C%7Bj%5C%7D%24%3A%0A%0A%20%20%20%20%24%24v(S%20%5Ccup%20%5C%7Bj%5C%7D)%20%3D%20v(S)%20%5Cquad%20%5Cforall%20S%20%5Csubseteq%20N%20%5Csetminus%20%5C%7Bj%5C%7D%20%5Cimplies%20%5Cphi_j(v)%20%3D%200%24%24%0A%0A%20%20%20%204.%20**Additivity%20(Linearity)**%3A%20If%20two%20independent%20games%20%24v%24%20and%20%24w%24%20are%20combined%20into%20a%20joint%20game%20%24(v%20%2B%20w)%24%3A%0A%0A%20%20%20%20%24%24%5Cphi_j(v%20%2B%20w)%20%3D%20%5Cphi_j(v)%20%2B%20%5Cphi_j(w)%24%24%0A%0A%20%20%20%20%23%23%23%203.%20Permutation%20and%20Combinatorial%20Formulations%0A%0A%20%20%20%20%23%23%23%23%20Permutation%20Formulation%20(All%20Arrival%20Sequences)%0A%20%20%20%20Let%20%24%5CPi_p%24%20denote%20the%20set%20of%20all%20%24p!%24%20permutations%20(arrival%20sequences)%20of%20players.%20Let%20%24P_j%5E%5Cpi%24%20be%20the%20set%20of%20players%20arriving%20strictly%20before%20player%20%24j%24%20in%20permutation%20%24%5Cpi%24.%20The%20marginal%20contribution%20of%20player%20%24j%24%20upon%20arrival%20is%20%24v(P_j%5E%5Cpi%20%5Ccup%20%5C%7Bj%5C%7D)%20-%20v(P_j%5E%5Cpi)%24.%20The%20Shapley%20value%20is%20the%20average%20marginal%20contribution%20across%20all%20%24p!%24%20permutations%3A%0A%0A%20%20%20%20%24%24%5Cphi_j(v)%20%3D%20%5Cfrac%7B1%7D%7Bp!%7D%20%5Csum_%7B%5Cpi%20%5Cin%20%5CPi_p%7D%20%5Cleft%5B%20v(P_j%5E%5Cpi%20%5Ccup%20%5C%7Bj%5C%7D)%20-%20v(P_j%5E%5Cpi)%20%5Cright%5D%24%24%0A%0A%20%20%20%20%23%23%23%23%20Combinatorial%20Formulation%20(Unordered%20Coalitions)%0A%20%20%20%20Grouping%20permutations%20by%20the%20unordered%20preceding%20coalition%20%24S%24%20of%20size%20%24%7CS%7C%24%20yields%20the%20combinatorial%20formula%3A%0A%0A%20%20%20%20%24%24%5Cphi_j(v)%20%3D%20%5Csum_%7BS%20%5Csubseteq%20N%20%5Csetminus%20%5C%7Bj%5C%7D%7D%20%5Cfrac%7B%7CS%7C!%20(p%20-%20%7CS%7C%20-%201)!%7D%7Bp!%7D%20%5Cleft%5B%20v(S%20%5Ccup%20%5C%7Bj%5C%7D)%20-%20v(S)%20%5Cright%5D%24%24%0A%0A%20%20%20%20Here%2C%20%24%5Cfrac%7B%7CS%7C!%20(p%20-%20%7CS%7C%20-%201)!%7D%7Bp!%7D%20%3D%20%5Cfrac%7B1%7D%7Bp%20%5Cbinom%7Bp%20-%201%7D%7B%7CS%7C%7D%7D%24%20represents%20the%20probability%20of%20coalition%20%24S%24%20occurring%20before%20player%20%24j%24%20under%20a%20uniform%20random%20arrival%20process.%0A%0A%20%20%20%20%23%23%23%204.%20Exact%20Analytical%20SHAP%20for%20Multiple%20Linear%20Regression%0A%0A%20%20%20%20For%20a%20linear%20regression%20model%20%24f(x)%20%3D%20%5Cbeta_0%20%2B%20%5Csum_%7Bj%3D1%7D%5Ep%20%5Cbeta_j%20x_j%24%20with%20mutually%20independent%20predictors%2C%20the%20conditional%20expectation%20defining%20the%20characteristic%20function%20is%3A%0A%0A%20%20%20%20%24%24v_x(S)%20%3D%20%5Cmathbb%7BE%7D%5Bf(X)%20%5Cmid%20X_S%20%3D%20x_S%5D%20%3D%20%5Cbeta_0%20%2B%20%5Csum_%7Bj%20%5Cin%20S%7D%20%5Cbeta_j%20x_j%20%2B%20%5Csum_%7Bk%20%5Cnotin%20S%7D%20%5Cbeta_k%20%5Cmathbb%7BE%7D%5BX_k%5D%24%24%0A%0A%20%20%20%20Evaluating%20the%20marginal%20contribution%20of%20feature%20%24j%24%3A%0A%0A%20%20%20%20%24%24v_x(S%20%5Ccup%20%5C%7Bj%5C%7D)%20-%20v_x(S)%20%3D%20%5Cbeta_j%20(x_j%20-%20%5Cmathbb%7BE%7D%5BX_j%5D)%24%24%0A%0A%20%20%20%20Because%20this%20marginal%20contribution%20is%20**invariant%20to%20the%20coalition%20%24S%24**%2C%20the%20combinatorial%20weights%20sum%20to%20%241%24%2C%20yielding%20the%20exact%20closed-form%20SHAP%20value%3A%0A%0A%20%20%20%20%24%24%5Cphi_j(x)%20%3D%20%5Cbeta_j%20(x_j%20-%20%5Cmathbb%7BE%7D%5BX_j%5D)%24%24%0A%0A%20%20%20%20Summing%20over%20all%20features%3A%0A%0A%20%20%20%20%24%24%5Csum_%7Bj%3D1%7D%5Ep%20%5Cphi_j(x)%20%3D%20%5Csum_%7Bj%3D1%7D%5Ep%20%5Cbeta_j%20(x_j%20-%20%5Cmathbb%7BE%7D%5BX_j%5D)%20%3D%20%5Cleft(%5Cbeta_0%20%2B%20%5Csum_%7Bj%3D1%7D%5Ep%20%5Cbeta_j%20x_j%5Cright)%20-%20%5Cleft(%5Cbeta_0%20%2B%20%5Csum_%7Bj%3D1%7D%5Ep%20%5Cbeta_j%20%5Cmathbb%7BE%7D%5BX_j%5D%5Cright)%20%3D%20f(x)%20-%20%5Cmathbb%7BE%7D%5Bf(X)%5D%24%24%0A%0A%20%20%20%20Efficiency%20is%20satisfied.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Independent%2C%20LinearRegression%2C%20np%2C%20pd%2C%20shap%2C%20train_test_split)%3A%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20n_samples%20%3D%202000%0A%0A%20%20%20%20%23%204%20distinct%20features%20with%20varying%20means%20and%20variances%0A%20%20%20%20mu_vec%20%3D%20np.array(%5B4.0%2C%203.0%2C%202.0%2C%201.0%5D)%0A%20%20%20%20cov_mat%20%3D%20np.diag(%5B1.0%2C%202.0%2C%201.5%2C%202.5%5D)%0A%20%20%20%20raw_x%20%3D%20np.random.multivariate_normal(mu_vec%2C%20cov_mat%2C%20size%3Dn_samples)%0A%0A%20%20%20%20feature_cols%20%3D%20%5B%22Feature_1%22%2C%20%22Feature_2%22%2C%20%22Feature_3%22%2C%20%22Feature_4%22%5D%0A%20%20%20%20df_x%20%3D%20pd.DataFrame(raw_x%2C%20columns%3Dfeature_cols)%0A%0A%20%20%20%20%23%20Linear%20generative%20process%3A%20y%20%3D%200.5%20*%20x1%20-%200.8%20*%20x2%20%2B%201.2%20*%20x3%20-%200.3%20*%20x4%20%2B%20noise%0A%20%20%20%20true_betas%20%3D%20np.array(%5B0.5%2C%20-0.8%2C%201.2%2C%20-0.3%5D)%0A%20%20%20%20y_raw%20%3D%20raw_x%20%40%20true_betas%20%2B%20np.random.normal(0%2C%200.05%2C%20size%3Dn_samples)%0A%0A%20%20%20%20x_train%2C%20x_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20df_x%2C%20y_raw%2C%20test_size%3D0.25%2C%20random_state%3D42%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Fit%20Linear%20Model%0A%20%20%20%20reg_model%20%3D%20LinearRegression().fit(x_train%2C%20y_train)%0A%0A%20%20%20%20%23%20Compute%20SHAP%20values%20using%20exact%20Linear%20explainer%20%2F%20Independent%20masker%0A%20%20%20%20masker%20%3D%20Independent(x_train%2C%20max_samples%3Dlen(x_train))%0A%20%20%20%20explainer%20%3D%20shap.Explainer(reg_model%2C%20masker%3Dmasker)%0A%20%20%20%20shap_explanation%20%3D%20explainer(x_test)%0A%20%20%20%20shap_matrix%20%3D%20shap_explanation.values%0A%20%20%20%20base_value%20%3D%20float(explainer.expected_value)%0A%0A%20%20%20%20%23%20Select%20an%20instance%20for%20detailed%20waterfall%20analysis%0A%20%20%20%20target_idx%20%3D%2042%0A%20%20%20%20target_sample%20%3D%20x_test.iloc%5Btarget_idx%5D%0A%20%20%20%20target_shap%20%3D%20shap_matrix%5Btarget_idx%5D%0A%20%20%20%20target_pred%20%3D%20float(reg_model.predict(target_sample.to_frame().T)%5B0%5D)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20base_value%2C%0A%20%20%20%20%20%20%20%20feature_cols%2C%0A%20%20%20%20%20%20%20%20reg_model%2C%0A%20%20%20%20%20%20%20%20shap_matrix%2C%0A%20%20%20%20%20%20%20%20target_pred%2C%0A%20%20%20%20%20%20%20%20target_sample%2C%0A%20%20%20%20%20%20%20%20target_shap%2C%0A%20%20%20%20%20%20%20%20x_train%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20base_value%2C%0A%20%20%20%20feature_cols%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20shap_matrix%2C%0A%20%20%20%20target_pred%2C%0A%20%20%20%20target_sample%2C%0A%20%20%20%20target_shap%2C%0A)%3A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3ELocal%20SHAP%20Waterfall%20Attribution%20(Single%20Observation)%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EGlobal%20Feature%20Importance%3A%20Mean%20Absolute%20SHAP%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.14%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%201%3A%20Interactive%20Waterfall%0A%20%20%20%20waterfall_labels%20%3D%20%5B%22Base%20Value%20E%5Bf(X)%5D%22%5D%0A%20%20%20%20waterfall_deltas%20%3D%20%5Bbase_value%5D%0A%20%20%20%20waterfall_measures%20%3D%20%5B%22absolute%22%5D%0A%0A%20%20%20%20for%20col_name%2C%20shap_val%2C%20feat_val%20in%20zip(feature_cols%2C%20target_shap%2C%20target_sample)%3A%0A%20%20%20%20%20%20%20%20waterfall_labels.append(f%22%7Bcol_name%7D%20(%3D%20%7Bfeat_val%3A.2f%7D)%22)%0A%20%20%20%20%20%20%20%20waterfall_deltas.append(shap_val)%0A%20%20%20%20%20%20%20%20waterfall_measures.append(%22relative%22)%0A%0A%20%20%20%20waterfall_labels.append(%22Model%20Prediction%20f(x)%22)%0A%20%20%20%20waterfall_deltas.append(target_pred)%0A%20%20%20%20waterfall_measures.append(%22total%22)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Waterfall(%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22SHAP%20Attribution%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20orientation%3D%22v%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20measure%3Dwaterfall_measures%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dwaterfall_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dwaterfall_deltas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20connector%3Ddict(line%3Ddict(color%3D%22%2394A3B8%22%2C%20width%3D1.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20increasing%3Ddict(marker%3Ddict(color%3D%22%23EF4444%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20decreasing%3Ddict(marker%3Ddict(color%3D%22%232563EB%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20totals%3Ddict(marker%3Ddict(color%3D%22%2310B981%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22outside%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bv%3A%2B.2f%7D%22%20if%20m%20%3D%3D%20%22relative%22%20else%20f%22%7Bv%3A.2f%7D%22%20for%20v%2C%20m%20in%20zip(waterfall_deltas%2C%20waterfall_measures)%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Panel%202%3A%20Mean%20Absolute%20SHAP%20(Global%20Importance)%0A%20%20%20%20mean_abs_shap%20%3D%20np.mean(np.abs(shap_matrix)%2C%20axis%3D0)%0A%20%20%20%20sorted_order%20%3D%20np.argsort(mean_abs_shap)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bfeature_cols%5Bi%5D%20for%20i%20in%20sorted_order%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dmean_abs_shap%5Bsorted_order%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20orientation%3D%22h%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%238B5CF6%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bval%3A.3f%7D%22%20for%20val%20in%20mean_abs_shap%5Bsorted_order%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22auto%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Mean%20%7CSHAP%7C%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Predicted%20Output%20Value%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_xaxes(title_text%3D%22Mean%20%7CSHAP%20Value%7C%20(Impact%20on%20Output)%22%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D520%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D70%2C%20b%3D50)%2C%0A%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20)%0A%0A%20%20%20%20viz%20%3D%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20base_value%2C%0A%20%20%20%20feature_cols%2C%0A%20%20%20%20itertools%2C%0A%20%20%20%20math%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20reg_model%2C%0A%20%20%20%20target_pred%2C%0A%20%20%20%20target_sample%2C%0A%20%20%20%20target_shap%2C%0A%20%20%20%20x_train%2C%0A)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20Python%20Combinatorial%20Shapley%20Calculator%20for%20Cooperative%20Games%0A%20%20%20%20def%20exact_cooperative_shapley(player_list%2C%20characteristic_fn)%3A%0A%20%20%20%20%20%20%20%20p_len%20%3D%20len(player_list)%0A%20%20%20%20%20%20%20%20shapley_dict%20%3D%20%7Bpl%3A%200.0%20for%20pl%20in%20player_list%7D%0A%0A%20%20%20%20%20%20%20%20for%20player%20in%20player_list%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20other_players%20%3D%20%5Bpl%20for%20pl%20in%20player_list%20if%20pl%20!%3D%20player%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Iterate%20over%20all%20possible%20subsets%20S%20of%20others%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20r%20in%20range(len(other_players)%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20comb_weight%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20math.factorial(r)%20*%20math.factorial(p_len%20-%20r%20-%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%20%2F%20math.factorial(p_len)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20subset%20in%20itertools.combinations(other_players%2C%20r)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s_coalition%20%3D%20set(subset)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20val_without%20%3D%20characteristic_fn(s_coalition)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20val_with%20%3D%20characteristic_fn(s_coalition%20%7C%20%7Bplayer%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marginal_gain%20%3D%20val_with%20-%20val_without%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20shapley_dict%5Bplayer%5D%20%2B%3D%20comb_weight%20*%20marginal_gain%0A%0A%20%20%20%20%20%20%20%20return%20shapley_dict%0A%0A%20%20%20%20%23%20Airport%20Runway%20Cost-Sharing%20Game%3A%203%20airlines%20with%20plane%20runway%20length%20requirements%20%5B1%2C%202%2C%203%5D%0A%20%20%20%20%23%20Runway%20cost%20v(S)%20%3D%20max_%7Bi%20in%20S%7D%20cost(i)%0A%20%20%20%20def%20airport_game(coalition)%3A%0A%20%20%20%20%20%20%20%20if%20not%20coalition%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200.0%0A%20%20%20%20%20%20%20%20costs%20%3D%20%7B%22Airline_A%22%3A%20100.0%2C%20%22Airline_B%22%3A%20200.0%2C%20%22Airline_C%22%3A%20300.0%7D%0A%20%20%20%20%20%20%20%20return%20max(costs%5Bpl%5D%20for%20pl%20in%20coalition)%0A%0A%20%20%20%20game_players%20%3D%20%5B%22Airline_A%22%2C%20%22Airline_B%22%2C%20%22Airline_C%22%5D%0A%20%20%20%20fair_shares%20%3D%20exact_cooperative_shapley(game_players%2C%20airport_game)%0A%0A%20%20%20%20df_airport%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Player%22%3A%20pl%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Stand_Alone_Cost%22%3A%20airport_game(%7Bpl%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Shapley_Fair_Share%22%3A%20round(fair_shares%5Bpl%5D%2C%202)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Fair_Share_Formula%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22100%2F3%20%3D%2033.3%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20pl%20%3D%3D%20%22Airline_A%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20(%22100%2F3%20%2B%20100%2F2%20%3D%2083.3%22%20if%20pl%20%3D%3D%20%22Airline_B%22%20else%20%22100%2F3%20%2B%20100%2F2%20%2B%20100%20%3D%20183.3%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%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%20for%20pl%20in%20game_players%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%202%3A%20Exact%20Linear%20SHAP%20Derivation%20Verification%0A%20%20%20%20%23%20Analytical%3A%20phi_j%20%3D%20beta_j%20*%20(x_ij%20-%20mean(x_j))%0A%20%20%20%20x_train_means%20%3D%20x_train.mean().values%0A%20%20%20%20analytical_shap%20%3D%20reg_model.coef_%20*%20(target_sample.values%20-%20x_train_means)%0A%0A%20%20%20%20df_linear_verif%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Feature%22%3A%20feature_cols%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Target_Feature_Value%22%3A%20target_sample.values.round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Training_Feature_Mean%22%3A%20x_train_means.round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Model_Beta%22%3A%20reg_model.coef_.round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Analytical_Formula_phi%22%3A%20analytical_shap.round(5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22SHAP_Library_Explainer%22%3A%20target_shap.round(5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Absolute_Difference%22%3A%20np.abs(analytical_shap%20-%20target_shap).round(9)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%203%3A%20Verification%20of%20the%20Efficiency%20Axiom%0A%20%20%20%20sum_shap%20%3D%20float(np.sum(target_shap))%0A%20%20%20%20pred_minus_base%20%3D%20float(target_pred%20-%20base_value)%0A%0A%20%20%20%20df_axioms%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Axiom%22%3A%20%22Efficiency%20(Completeness)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mathematical_Condition%22%3A%20%22sum(phi_j)%20%3D%3D%20f(x)%20-%20E%5Bf(X)%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Left_Hand_Side%22%3A%20round(sum_shap%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Right_Hand_Side%22%3A%20round(pred_minus_base%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20%22Satisfied%20Exactly%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Axiom%22%3A%20%22Dummy%20Player%20(Null%20Feature)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mathematical_Condition%22%3A%20%22beta_j%20%3D%3D%200%20implies%20phi_j%20%3D%3D%200%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Left_Hand_Side%22%3A%20%220.0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Right_Hand_Side%22%3A%20%220.0%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20%22Guaranteed%20by%20closed-form%20beta%20*%20(x%20-%20mu)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Axiom%22%3A%20%22Symmetry%20(Equal%20Payoff)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mathematical_Condition%22%3A%20%22Equal%20marginals%20imply%20equal%20phi%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Left_Hand_Side%22%3A%20%22phi_a%20%3D%3D%20phi_b%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Right_Hand_Side%22%3A%20%22phi_a%20%3D%3D%20phi_b%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20%22Guaranteed%20by%20permutation%20symmetry%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Axiom%22%3A%20%22Additivity%20(Linearity)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mathematical_Condition%22%3A%20%22phi(f%20%2B%20g)%20%3D%3D%20phi(f)%20%2B%20phi(g)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Left_Hand_Side%22%3A%20%22phi(f%20%2B%20g)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Right_Hand_Side%22%3A%20%22phi(f)%20%2B%20phi(g)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20%22Guaranteed%20by%20expectation%20linearity%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20table_airport%20%3D%20mo.ui.table(df_airport)%0A%20%20%20%20table_verif%20%3D%20mo.ui.table(df_linear_verif)%0A%20%20%20%20table_axioms%20%3D%20mo.ui.table(df_axioms)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%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
83d6ad60d2243b6be7471ce400ea587d