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%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%20statsmodels.api%20as%20sm%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20sklearn.datasets%20import%20load_breast_cancer%0A%20%20%20%20from%20sklearn.metrics%20import%20accuracy_score%2C%20confusion_matrix%2C%20roc_auc_score%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%20accuracy_score%2C%0A%20%20%20%20%20%20%20%20confusion_matrix%2C%0A%20%20%20%20%20%20%20%20go%2C%0A%20%20%20%20%20%20%20%20load_breast_cancer%2C%0A%20%20%20%20%20%20%20%20make_subplots%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%20roc_auc_score%2C%0A%20%20%20%20%20%20%20%20sm%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%2043%20Matrix%20Energy%20and%20Definiteness%5D(43_energy.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B45%20Shapley%20Values%20%E2%86%92%5D(45_shapley.py)%0A%0A%20%20%20%20%23%20Logistic%20Regression%3A%20Log-Odds%2C%20Sigmoid%20Link%2C%20Odds%20Ratios%2C%20and%20Maximum%20Likelihood%20Estimation%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%20Logistic%20regression%20is%20the%20foundational%20parametric%20model%20for%20binary%20classification%2C%20probabilistic%20risk%20scoring%2C%20clinical%20epidemiological%20trials%2C%20credit%20underwriting%2C%20and%20causal%20propensity%20score%20matching.%0A%0A%20%20%20%20%23%23%23%23%20Why%20Ordinary%20Least%20Squares%20(OLS)%20Fails%20for%20Binary%20Outcomes%0A%20%20%20%20When%20modeling%20a%20binary%20response%20%24y_i%20%5Cin%20%5C%7B0%2C%201%5C%7D%24%2C%20fitting%20a%20standard%20linear%20regression%20model%20%24y%20%3D%20X%20%5Cbeta%20%2B%20%5Cepsilon%24%20introduces%20structural%20violations%20of%20classical%20statistical%20theory%3A%0A%20%20%20%201.%20**Nonsensical%20Unbounded%20Probabilities**%3A%20Linear%20functions%20produce%20predictions%20outside%20the%20valid%20unit%20interval%20%24%5B0%2C%201%5D%24.%20A%20model%20predicting%20a%20probability%20of%20%24-0.35%24%20or%20%241.42%24%20violates%20the%20Kolmogorov%20axioms%20of%20probability.%0A%20%20%20%202.%20**Severe%20Heteroscedasticity**%3A%20The%20binary%20outcome%20variance%20depends%20directly%20on%20the%20mean%3A%0A%0A%20%20%20%20%24%24%5Coperatorname%7BVar%7D(y_i%20%7C%20x_i)%20%3D%20p_i(1%20-%20p_i)%24%24%0A%0A%20%20%20%20Because%20error%20variances%20vary%20across%20observations%2C%20OLS%20standard%20errors%20are%20inconsistent%20and%20hypothesis%20tests%20(%24t%24-tests%20and%20%24F%24-tests)%20become%20invalid.%0A%20%20%20%203.%20**Discrete%20Non-Gaussian%20Residuals**%3A%20Residuals%20take%20only%20two%20discrete%20values%20(%241%20-%20%5Chat%7By%7D_i%24%20or%20%24-%5Chat%7By%7D_i%24)%2C%20violating%20the%20normality%20assumption%20required%20for%20finite-sample%20inference.%0A%0A%20%20%20%20%23%23%23%23%20The%20Logit%20Link%20Transformation%0A%20%20%20%20Logistic%20regression%20resolves%20these%20flaws%20by%20modeling%20the%20**log-odds**%20(logit)%20of%20the%20positive%20class%20as%20an%20unconstrained%20linear%20function%20of%20predictors%3A%0A%0A%20%20%20%20%24%24%5Ceta%20%3D%20%5Cln%5Cleft(%20%5Cfrac%7Bp%7D%7B1%20-%20p%7D%20%5Cright)%20%3D%20X%20%5Cbeta%24%24%0A%0A%20%20%20%20Inverting%20this%20transformation%20maps%20the%20unconstrained%20real%20line%20%24(-%5Cinfty%2C%20%5Cinfty)%24%20back%20onto%20%24(0%2C%201)%24%20via%20the%20**Sigmoid%20activation%20function**.%0A%0A%20%20%20%20%23%23%23%23%20Intuitive%20Interpretation%20via%20Odds%20Ratios%0A%20%20%20%20Unlike%20neural%20networks%20or%20black-box%20tree%20ensembles%2C%20logistic%20regression%20provides%20transparent%2C%20interpretable%20coefficients.%20Exponentiating%20a%20feature's%20coefficient%20yields%20its%20**Odds%20Ratio**%20(%24%5Ctext%7BOR%7D%20%3D%20e%5E%7B%5Cbeta_j%7D%24)%2C%20indicating%20the%20multiplicative%20factor%20by%20which%20the%20odds%20of%20the%20outcome%20change%20for%20each%20one-unit%20increase%20in%20that%20predictor.%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%20Likelihood%20Mechanics%0A%0A%20%20%20%20%23%23%23%201.%20Odds%2C%20Log-Odds%2C%20and%20the%20Sigmoid%20Link%0A%0A%20%20%20%20Let%20%24p%20%3D%20P(Y%20%3D%201%20%7C%20X)%24%20denote%20the%20conditional%20probability%20of%20the%20event%20of%20interest.%0A%0A%20%20%20%20%23%23%23%23%20The%20Odds%0A%20%20%20%20The%20odds%20represent%20the%20ratio%20of%20the%20probability%20of%20success%20to%20the%20probability%20of%20failure%3A%0A%0A%20%20%20%20%24%24%5Coperatorname%7BOdds%7D%20%3D%20%5Cfrac%7Bp%7D%7B1%20-%20p%7D%20%5Cin%20(0%2C%20%5Cinfty)%24%24%0A%0A%20%20%20%20-%20When%20%24p%20%3D%200.5%24%2C%20%24%5Coperatorname%7BOdds%7D%20%3D%201%24%20(even%20chance).%0A%20%20%20%20-%20When%20%24p%20%3D%200.8%24%2C%20%24%5Coperatorname%7BOdds%7D%20%3D%204%24%20(success%20is%204%20times%20as%20likely%20as%20failure).%0A%20%20%20%20-%20When%20%24p%20%3D%200.2%24%2C%20%24%5Coperatorname%7BOdds%7D%20%3D%200.25%24%20(failure%20is%204%20times%20as%20likely%20as%20success).%0A%0A%20%20%20%20%23%23%23%23%20The%20Logit%20(Log-Odds)%20Link%0A%20%20%20%20The%20natural%20logarithm%20of%20the%20odds%20maps%20the%20positive%20semi-infinite%20interval%20%24(0%2C%20%5Cinfty)%24%20onto%20%24(-%5Cinfty%2C%20%5Cinfty)%24%3A%0A%0A%20%20%20%20%24%24%5Coperatorname%7Blogit%7D(p)%20%3D%20%5Cln%5Cleft(%20%5Cfrac%7Bp%7D%7B1%20-%20p%7D%20%5Cright)%20%3D%20%5Cbeta_0%20%2B%20%5Cbeta_1%20x_1%20%2B%20%5Cdots%20%2B%20%5Cbeta_k%20x_k%20%3D%20x%5E%5Ctop%20%5Cbeta%24%24%0A%0A%20%20%20%20%23%23%23%23%20The%20Sigmoid%20Function%0A%20%20%20%20Solving%20for%20%24p%24%20yields%20the%20standard%20logistic%20sigmoid%20function%20%24%5Csigma(%5Ceta)%24%3A%0A%0A%20%20%20%20%24%24p%20%3D%20%5Csigma(x%5E%5Ctop%20%5Cbeta)%20%3D%20%5Cfrac%7B1%7D%7B1%20%2B%20e%5E%7B-x%5E%5Ctop%20%5Cbeta%7D%7D%20%3D%20%5Cfrac%7Be%5E%7Bx%5E%5Ctop%20%5Cbeta%7D%7D%7B1%20%2B%20e%5E%7Bx%5E%5Ctop%20%5Cbeta%7D%7D%24%24%0A%0A%20%20%20%20The%20first%20derivative%20of%20the%20sigmoid%20has%20the%20convenient%20algebraic%20property%3A%0A%0A%20%20%20%20%24%24%5Cfrac%7Bd%5Csigma%7D%7Bd%5Ceta%7D%20%3D%20%5Csigma(%5Ceta)(1%20-%20%5Csigma(%5Ceta))%20%3D%20p(1%20-%20p)%24%24%0A%0A%20%20%20%20%23%23%23%202.%20The%20Odds%20Ratio%20(OR)%0A%0A%20%20%20%20Consider%20the%20effect%20of%20increasing%20a%20single%20predictor%20%24x_j%24%20by%20one%20unit%20(%24x_j%20%5Cto%20x_j%20%2B%201%24)%20while%20holding%20all%20other%20features%20constant%3A%0A%0A%20%20%20%20%24%24%5Coperatorname%7BOdds%7D(x_j%20%2B%201)%20%3D%20%5Cexp%5Cleft(%20%5Cbeta_0%20%2B%20%5Cdots%20%2B%20%5Cbeta_j(x_j%20%2B%201)%20%2B%20%5Cdots%20%5Cright)%20%3D%20%5Cexp(%5Cbeta_j)%20%5Ccdot%20%5Coperatorname%7BOdds%7D(x_j)%24%24%0A%0A%20%20%20%20The%20Odds%20Ratio%20for%20predictor%20%24j%24%20is%3A%0A%0A%20%20%20%20%24%24%5Ctext%7BOR%7D_j%20%3D%20%5Cfrac%7B%5Coperatorname%7BOdds%7D(x_j%20%2B%201)%7D%7B%5Coperatorname%7BOdds%7D(x_j)%7D%20%3D%20e%5E%7B%5Cbeta_j%7D%24%24%0A%0A%20%20%20%20-%20If%20%24%5Cbeta_j%20%3E%200%20%5Cimplies%20%5Ctext%7BOR%7D_j%20%3E%201%24%3A%20A%20one-unit%20increase%20in%20%24x_j%24%20increases%20the%20odds%20by%20%24(e%5E%7B%5Cbeta_j%7D%20-%201)%20%5Ctimes%20100%5C%25%24.%0A%20%20%20%20-%20If%20%24%5Cbeta_j%20%3C%200%20%5Cimplies%20%5Ctext%7BOR%7D_j%20%3C%201%24%3A%20A%20one-unit%20increase%20in%20%24x_j%24%20decreases%20the%20odds%20by%20%24(1%20-%20e%5E%7B%5Cbeta_j%7D)%20%5Ctimes%20100%5C%25%24.%0A%20%20%20%20-%20If%20%24%5Cbeta_j%20%3D%200%20%5Cimplies%20%5Ctext%7BOR%7D_j%20%3D%201%24%3A%20Predictor%20%24x_j%24%20has%20no%20association%20with%20the%20outcome.%0A%0A%20%20%20%20The%20%24(1%20-%20%5Calpha)%24%20confidence%20interval%20for%20the%20Odds%20Ratio%20is%20computed%20by%20exponentiating%20the%20coefficient%20bounds%3A%0A%0A%20%20%20%20%24%24%5Ctext%7BCI%7D_%7B1-%5Calpha%7D(%5Ctext%7BOR%7D_j)%20%3D%20%5Cleft%5B%20%5Cexp%5Cleft(%5Chat%7B%5Cbeta%7D_j%20-%20z_%7B1-%5Calpha%2F2%7D%20%5Ccdot%20%5Coperatorname%7BSE%7D(%5Chat%7B%5Cbeta%7D_j)%5Cright)%2C%20%5C%20%5Cexp%5Cleft(%5Chat%7B%5Cbeta%7D_j%20%2B%20z_%7B1-%5Calpha%2F2%7D%20%5Ccdot%20%5Coperatorname%7BSE%7D(%5Chat%7B%5Cbeta%7D_j)%5Cright)%20%5Cright%5D%24%24%0A%0A%20%20%20%20%23%23%23%203.%20Maximum%20Likelihood%20Estimation%20(MLE)%20and%20IRLS%0A%0A%20%20%20%20Assuming%20independent%20Bernoulli%20trials%20%24y_i%20%5Csim%20%5Coperatorname%7BBernoulli%7D(p_i)%24%2C%20the%20joint%20likelihood%20function%20is%3A%0A%0A%20%20%20%20%24%24L(%5Cbeta)%20%3D%20%5Cprod_%7Bi%3D1%7D%5EN%20p_i%5E%7By_i%7D%20(1%20-%20p_i)%5E%7B1%20-%20y_i%7D%24%24%0A%0A%20%20%20%20The%20log-likelihood%20is%3A%0A%0A%20%20%20%20%24%24%5Cln%20L(%5Cbeta)%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cleft%5B%20y_i%20%5Cln%20p_i%20%2B%20(1%20-%20y_i)%20%5Cln%20(1%20-%20p_i)%20%5Cright%5D%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cleft%5B%20y_i%20(x_i%5E%5Ctop%20%5Cbeta)%20-%20%5Cln%5Cleft(1%20%2B%20e%5E%7Bx_i%5E%5Ctop%20%5Cbeta%7D%5Cright)%20%5Cright%5D%24%24%0A%0A%20%20%20%20The%20gradient%20(score%20vector)%20is%3A%0A%0A%20%20%20%20%24%24%5Cnabla_%5Cbeta%20%5Cln%20L%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20(y_i%20-%20p_i)%20x_i%20%3D%20X%5E%5Ctop%20(y%20-%20p)%24%24%0A%0A%20%20%20%20The%20negative%20Hessian%20(Fisher%20Information%20matrix)%20is%3A%0A%0A%20%20%20%20%24%24-H%20%3D%20-%5Cnabla_%5Cbeta%5E2%20%5Cln%20L%20%3D%20%5Csum_%7Bi%3D1%7D%5EN%20p_i%20(1%20-%20p_i)%20x_i%20x_i%5E%5Ctop%20%3D%20X%5E%5Ctop%20W%20X%24%24%0A%0A%20%20%20%20where%20%24W%20%3D%20%5Coperatorname%7Bdiag%7D(p_1(1%20-%20p_1)%2C%20%5Cdots%2C%20p_N(1%20-%20p_N))%24.%20Since%20%240%20%3C%20p_i%20%3C%201%24%2C%20%24W%24%20is%20strictly%20positive%20definite%2C%20proving%20that%20%24%5Cln%20L(%5Cbeta)%24%20is%20**strictly%20concave**%20with%20a%20unique%20global%20maximum.%0A%0A%20%20%20%20Parameters%20are%20estimated%20iteratively%20via%20Newton-Raphson%20%2F%20Iteratively%20Reweighted%20Least%20Squares%20(IRLS)%3A%0A%0A%20%20%20%20%24%24%5Cbeta%5E%7B(t%2B1)%7D%20%3D%20%5Cbeta%5E%7B(t)%7D%20%2B%20(X%5E%5Ctop%20W_t%20X)%5E%7B-1%7D%20X%5E%5Ctop%20(y%20-%20p_t)%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(load_breast_cancer%2C%20np%2C%20pd%2C%20sm%2C%20train_test_split)%3A%0A%20%20%20%20%23%20Load%20standardized%20clinical%20breast%20cancer%20dataset%0A%20%20%20%20cancer%20%3D%20load_breast_cancer()%0A%20%20%20%20feature_subset%20%3D%20%5B%22mean%20radius%22%2C%20%22mean%20texture%22%2C%20%22mean%20smoothness%22%2C%20%22mean%20compactness%22%5D%0A%20%20%20%20feat_indices%20%3D%20%5Blist(cancer.feature_names).index(f)%20for%20f%20in%20feature_subset%5D%0A%0A%20%20%20%20x_raw%20%3D%20cancer.data%5B%3A%2C%20feat_indices%5D%0A%20%20%20%20%23%20In%20cancer%20dataset%3A%200%20%3D%20Malignant%2C%201%20%3D%20Benign.%20Keep%201%20as%20Benign.%0A%20%20%20%20y_raw%20%3D%20cancer.target%0A%0A%20%20%20%20%23%20Standardize%20predictors%20for%20numerical%20stability%20and%20coefficient%20interpretability%0A%20%20%20%20x_mean%20%3D%20np.mean(x_raw%2C%20axis%3D0)%0A%20%20%20%20x_std%20%3D%20np.std(x_raw%2C%20axis%3D0)%0A%20%20%20%20x_std_data%20%3D%20(x_raw%20-%20x_mean)%20%2F%20x_std%0A%0A%20%20%20%20clean_feature_names%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Radius_Standardized%22%2C%0A%20%20%20%20%20%20%20%20%22Texture_Standardized%22%2C%0A%20%20%20%20%20%20%20%20%22Smoothness_Standardized%22%2C%0A%20%20%20%20%20%20%20%20%22Compactness_Standardized%22%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20df_features%20%3D%20pd.DataFrame(x_std_data%2C%20columns%3Dclean_feature_names)%0A%0A%20%20%20%20%23%20Train-test%20split%0A%20%20%20%20x_train_raw%2C%20x_test_raw%2C%20y_train%2C%20y_test%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20df_features.values%2C%20y_raw%2C%20test_size%3D0.3%2C%20random_state%3D42%2C%20stratify%3Dy_raw%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20intercept%20column%0A%20%20%20%20x_train_const%20%3D%20sm.add_constant(x_train_raw)%0A%20%20%20%20x_test_const%20%3D%20sm.add_constant(x_test_raw)%0A%0A%20%20%20%20%23%20Fit%20statsmodels%20Logit%0A%20%20%20%20logit_model%20%3D%20sm.Logit(y_train%2C%20x_train_const)%0A%20%20%20%20logit_res%20%3D%20logit_model.fit(disp%3DFalse)%0A%0A%20%20%20%20%23%20Predictions%0A%20%20%20%20test_pred_prob%20%3D%20logit_res.predict(x_test_const)%0A%20%20%20%20test_pred_class%20%3D%20(test_pred_prob%20%3E%3D%200.5).astype(int)%0A%0A%20%20%20%20%23%20Univariate%20slice%20for%20Sigmoid%20curve%20visualization%3A%20Radius%20feature%20only%0A%20%20%20%20x_radius%20%3D%20x_train_raw%5B%3A%2C%200%5D%0A%20%20%20%20radius_grid%20%3D%20np.linspace(x_radius.min()%20-%200.5%2C%20x_radius.max()%20%2B%200.5%2C%20200)%0A%0A%20%20%20%20%23%20Univariate%20logistic%20fit%0A%20%20%20%20uni_model%20%3D%20sm.Logit(y_train%2C%20sm.add_constant(x_radius)).fit(disp%3DFalse)%0A%20%20%20%20uni_probs%20%3D%20uni_model.predict(sm.add_constant(radius_grid))%0A%0A%20%20%20%20%23%20OLS%20linear%20regression%20for%20comparison%0A%20%20%20%20ols_model%20%3D%20sm.OLS(y_train%2C%20sm.add_constant(x_radius)).fit()%0A%20%20%20%20ols_preds%20%3D%20ols_model.predict(sm.add_constant(radius_grid))%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20clean_feature_names%2C%0A%20%20%20%20%20%20%20%20logit_res%2C%0A%20%20%20%20%20%20%20%20ols_preds%2C%0A%20%20%20%20%20%20%20%20radius_grid%2C%0A%20%20%20%20%20%20%20%20test_pred_class%2C%0A%20%20%20%20%20%20%20%20test_pred_prob%2C%0A%20%20%20%20%20%20%20%20uni_probs%2C%0A%20%20%20%20%20%20%20%20x_radius%2C%0A%20%20%20%20%20%20%20%20x_train_const%2C%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20y_train%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20clean_feature_names%2C%0A%20%20%20%20go%2C%0A%20%20%20%20logit_res%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20ols_preds%2C%0A%20%20%20%20radius_grid%2C%0A%20%20%20%20uni_probs%2C%0A%20%20%20%20x_radius%2C%0A%20%20%20%20y_train%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%3ESigmoid%20Probability%20Curve%20vs%20Linear%20Regression%20(OLS)%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%3Cb%3EOdds%20Ratios%20with%2095%25%20Confidence%20Intervals%20(Forest%20Plot)%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%20Sigmoid%20vs%20Linear%0A%20%20%20%20%23%20Jittered%20scatter%20of%20true%20training%20points%0A%20%20%20%20y_jitter%20%3D%20y_train%20%2B%20np.random.normal(0%2C%200.025%2C%20len(y_train))%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_radius%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy_jitter%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%2364748B%22%2C%20size%3D5%2C%20opacity%3D0.45)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Observed%20Data%20(Jittered)%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Sigmoid%20Curve%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dradius_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Duni_probs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%232563EB%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Logistic%20Sigmoid%20Probability%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Linear%20Regression%20Line%20(showing%20unconstrained%20overshoot)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dradius_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dols_preds%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23DC2626%22%2C%20width%3D2%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22OLS%20Linear%20Regression%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Decision%20threshold%20line%20at%20p%20%3D%200.5%0A%20%20%20%20fig.add_hline(%0A%20%20%20%20%20%20%20%20y%3D0.5%2C%0A%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310B981%22%2C%20width%3D1.5%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20annotation_text%3D%22Decision%20Threshold%20(p%20%3D%200.5)%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22bottom%20right%22%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%20Forest%20Plot%20of%20Odds%20Ratios%0A%20%20%20%20coef_vals%20%3D%20logit_res.params%5B1%3A%5D%0A%20%20%20%20se_vals%20%3D%20logit_res.bse%5B1%3A%5D%0A%20%20%20%20odds_ratios%20%3D%20np.exp(coef_vals)%0A%20%20%20%20or_lower%20%3D%20np.exp(coef_vals%20-%201.96%20*%20se_vals)%0A%20%20%20%20or_upper%20%3D%20np.exp(coef_vals%20%2B%201.96%20*%20se_vals)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dodds_ratios%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dclean_feature_names%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D10%2C%20color%3D%22%238B5CF6%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20error_x%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20type%3D%22data%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20symmetric%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20array%3Dor_upper%20-%20odds_ratios%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arrayminus%3Dodds_ratios%20-%20or_lower%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visible%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3D%22%238B5CF6%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20thickness%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3D6%2C%0A%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%20name%3D%22Odds%20Ratio%20(95%25%20CI)%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%20%23%20Reference%20line%20at%20OR%20%3D%201.0%20(no%20effect)%0A%20%20%20%20fig.add_vline(%0A%20%20%20%20%20%20%20%20x%3D1.0%2C%0A%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23DC2626%22%2C%20width%3D1.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20annotation_text%3D%22OR%20%3D%201.0%20(No%20Effect)%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22top%20left%22%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_xaxes(title_text%3D%22Standardized%20Mean%20Radius%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22Predicted%20Probability%20P(Y%3D1)%22%2C%20range%3D%5B-0.15%2C%201.15%5D%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_xaxes(title_text%3D%22Odds%20Ratio%20(Log%20Scale)%22%2C%20type%3D%22log%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20fig.update_yaxes(autorange%3D%22reversed%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%3D500%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%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.28%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%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%20accuracy_score%2C%0A%20%20%20%20clean_feature_names%2C%0A%20%20%20%20confusion_matrix%2C%0A%20%20%20%20logit_res%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20roc_auc_score%2C%0A%20%20%20%20test_pred_class%2C%0A%20%20%20%20test_pred_prob%2C%0A%20%20%20%20x_train_const%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20NumPy%20Newton-Raphson%20%2F%20IRLS%20Solver%20from%20scratch%0A%20%20%20%20def%20newton_raphson_logistic(x_mat%2C%20y_vec%2C%20max_iter%3D25%2C%20tol%3D1e-8)%3A%0A%20%20%20%20%20%20%20%20n%2C%20p_dim%20%3D%20x_mat.shape%0A%20%20%20%20%20%20%20%20beta%20%3D%20np.zeros(p_dim)%0A%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(max_iter)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Predicted%20probabilities%3A%20sigma(X%20beta)%0A%20%20%20%20%20%20%20%20%20%20%20%20eta%20%3D%20x_mat%20%40%20beta%0A%20%20%20%20%20%20%20%20%20%20%20%20p%20%3D%201.0%20%2F%20(1.0%20%2B%20np.exp(-np.clip(eta%2C%20-30%2C%2030)))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Gradient%3A%20X%5ET%20(y%20-%20p)%0A%20%20%20%20%20%20%20%20%20%20%20%20grad%20%3D%20x_mat.T%20%40%20(y_vec%20-%20p)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Weight%20diagonal%20matrix%20W%20%3D%20diag(p%20*%20(1%20-%20p))%0A%20%20%20%20%20%20%20%20%20%20%20%20w_diag%20%3D%20p%20*%20(1.0%20-%20p)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Hessian%3A%20-X%5ET%20W%20X%0A%20%20%20%20%20%20%20%20%20%20%20%20hess%20%3D%20-x_mat.T%20%40%20(w_diag%5B%3A%2C%20None%5D%20*%20x_mat)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Newton%20step%3A%20beta_new%20%3D%20beta%20-%20H%5E(-1)%20grad%0A%20%20%20%20%20%20%20%20%20%20%20%20delta%20%3D%20np.linalg.solve(-hess%2C%20grad)%0A%20%20%20%20%20%20%20%20%20%20%20%20beta%20%3D%20beta%20%2B%20delta%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20np.linalg.norm(delta)%20%3C%20tol%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20%20%20%20%20%23%20Compute%20covariance%20matrix%20inv(X%5ET%20W%20X)%0A%20%20%20%20%20%20%20%20cov_beta%20%3D%20np.linalg.inv(-hess)%0A%20%20%20%20%20%20%20%20se%20%3D%20np.sqrt(np.diag(cov_beta))%0A%20%20%20%20%20%20%20%20return%20beta%2C%20se%0A%0A%20%20%20%20beta_scratch%2C%20se_scratch%20%3D%20newton_raphson_logistic(x_train_const%2C%20y_train)%0A%0A%20%20%20%20df_verification%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%22Parameter%22%3A%20%5B%22Intercept%22%5D%20%2B%20clean_feature_names%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22From_Scratch_Beta%22%3A%20np.round(beta_scratch%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Statsmodels_Beta%22%3A%20np.round(logit_res.params%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22From_Scratch_SE%22%3A%20np.round(se_scratch%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Statsmodels_SE%22%3A%20np.round(logit_res.bse%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Max_Diff%22%3A%20np.abs(beta_scratch%20-%20logit_res.params).round(8)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%202%3A%20Comprehensive%20Odds%20Ratio%20and%20Hypothesis%20Testing%20Table%0A%20%20%20%20or_values%20%3D%20np.exp(logit_res.params)%0A%20%20%20%20ci_mat%20%3D%20np.asarray(logit_res.conf_int())%0A%20%20%20%20or_ci_low%20%3D%20np.exp(ci_mat%5B%3A%2C%200%5D)%0A%20%20%20%20or_ci_high%20%3D%20np.exp(ci_mat%5B%3A%2C%201%5D)%0A%0A%20%20%20%20df_or_summary%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%22Variable%22%3A%20%5B%22Intercept%22%5D%20%2B%20clean_feature_names%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Coefficient_Beta%22%3A%20logit_res.params.round(4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Std_Error%22%3A%20logit_res.bse.round(4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Wald_z_stat%22%3A%20logit_res.tvalues.round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22p_value%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%3C%200.001%22%20if%20p%20%3C%200.001%20else%20f%22%7Bp%3A.4f%7D%22%20for%20p%20in%20logit_res.pvalues%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Odds_Ratio%22%3A%20np.round(or_values%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%2295%25_CI_OR%22%3A%20%5Bf%22%5B%7Bl%3A.3f%7D%2C%20%7Bh%3A.3f%7D%5D%22%20for%20l%2C%20h%20in%20zip(or_ci_low%2C%20or_ci_high)%5D%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%20Model%20Evaluation%20Diagnostics%20on%20Holdout%20Test%20Partition%0A%20%20%20%20test_acc%20%3D%20accuracy_score(y_test%2C%20test_pred_class)%0A%20%20%20%20test_auc%20%3D%20roc_auc_score(y_test%2C%20test_pred_prob)%0A%20%20%20%20cm%20%3D%20confusion_matrix(y_test%2C%20test_pred_class)%0A%0A%20%20%20%20df_diagnostics%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%22Evaluation_Metric%22%3A%20%22Classification%20Accuracy%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_Set_Value%22%3A%20f%22%7Btest_acc%20*%20100%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Threshold%22%3A%20%22p%20%3E%3D%200.5%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%22Evaluation_Metric%22%3A%20%22Area%20Under%20ROC%20Curve%20(AUC)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_Set_Value%22%3A%20f%22%7Btest_auc%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Threshold%22%3A%20%22Rank-order%20across%20all%20cutoffs%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%22Evaluation_Metric%22%3A%20%22True%20Negatives%20(Malignant%20correctly%20flagged)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_Set_Value%22%3A%20str(cm%5B0%2C%200%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Threshold%22%3A%20%22Class%200%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%22Evaluation_Metric%22%3A%20%22True%20Positives%20(Benign%20correctly%20flagged)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Test_Set_Value%22%3A%20str(cm%5B1%2C%201%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Threshold%22%3A%20%22Class%201%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_verif%20%3D%20mo.ui.table(df_verification)%0A%20%20%20%20table_or%20%3D%20mo.ui.table(df_or_summary)%0A%20%20%20%20table_diag%20%3D%20mo.ui.table(df_diagnostics)%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
88252265c8cb872a8f198025a4f59886