import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%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%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20scipy.stats%20import%20kendalltau%2C%20pearsonr%2C%20spearmanr%0A%20%20%20%20from%20sklearn.datasets%20import%20make_friedman1%0A%20%20%20%20from%20sklearn.feature_selection%20import%20f_regression%2C%20mutual_info_regression%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20f_regression%2C%0A%20%20%20%20%20%20%20%20go%2C%0A%20%20%20%20%20%20%20%20kendalltau%2C%0A%20%20%20%20%20%20%20%20make_friedman1%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%20mutual_info_regression%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%20pearsonr%2C%0A%20%20%20%20%20%20%20%20spearmanr%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%20%23%20Note%2015%3A%20Mutual%20Information%2C%20Shannon%20Entropy%2C%20and%20Non-Linear%20Dependence%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B14%20Distribution%20of%20Minimum%5D(14_dist_of_minimum.py)%20%7C%20Next%20Note%3A%20%5B16%20Point-Biserial%20Correlation%5D(16_point_biserial.py)%20%26rarr%3B%0A%0A%20%20%20%20---%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%20Linear%20correlation%20(Pearson's%20%24r%24)%20is%20one%20of%20the%20most%20widely%20misused%20metrics%20in%20data%20science.%20Pearson's%20correlation%20only%20measures%20the%20strength%20of%20an%20affine%2C%20linear%20relationship.%20If%20two%20variables%20share%20a%20deterministic%20non-linear%20relationship%20(for%20example%2C%20%24Y%20%3D%20X%5E2%24%20on%20%24%5B-1%2C%201%5D%24%20or%20%24Y%20%3D%20%5Csin(X)%24)%2C%20Pearson%20correlation%20is%20exactly%20zero%3A%20%24r%20%3D%200%24.%20Relying%20solely%20on%20linear%20correlation%20would%20incorrectly%20lead%20to%20discarding%20highly%20predictive%20features.%0A%0A%20%20%20%20**Mutual%20Information%20(MI)**%2C%20grounded%20in%20Claude%20Shannon's%20Information%20Theory%2C%20resolves%20this%20fundamental%20limitation%3A%0A%20%20%20%201.%20**Universal%20Metric%20of%20Statistical%20Dependence**%3A%20Mutual%20Information%20quantifies%20how%20much%20knowledge%20of%20one%20random%20variable%20reduces%20uncertainty%20about%20another.%20Unlike%20Pearson%20or%20Spearman%20correlation%2C%20%24I(X%3B%20Y)%20%3D%200%24%20if%20and%20only%20if%20%24X%24%20and%20%24Y%24%20are%20strictly%20statistically%20independent%20(%24P(X%2C%20Y)%20%3D%20P(X)P(Y)%24).%0A%20%20%20%202.%20**Non-Linear%20Feature%20Selection%20in%20Machine%20Learning**%3A%20In%20high-dimensional%20datasets%20with%20non-linear%20interactions%2C%20filtering%20features%20via%20Mutual%20Information%20discovers%20critical%20non-linear%20predictors%20that%20standard%20linear%20F-tests%20and%20correlation%20filters%20fail%20to%20detect.%0A%20%20%20%203.%20**Self-Supervised%20Representation%20Learning**%3A%20Modern%20contrastive%20learning%20algorithms%20(such%20as%20SimCLR%2C%20CLIP%2C%20and%20Contrastive%20Predictive%20Coding)%20optimize%20the%20InfoNCE%20loss%2C%20which%20maximizes%20a%20variational%20lower%20bound%20on%20the%20mutual%20information%20%24I(Z_1%3B%20Z_2)%24%20between%20augmented%20representations%20of%20the%20same%20underlying%20data%20point.%0A%20%20%20%204.%20**The%20Information%20Bottleneck%20Principle**%3A%20In%20deep%20learning%20theory%2C%20representation%20learning%20can%20be%20framed%20as%20an%20information%20bottleneck%3A%20an%20optimal%20hidden%20representation%20%24T%24%20maximizes%20mutual%20information%20with%20labels%20%24I(T%3B%20Y)%24%20while%20compressing%20redundant%20input%20noise%20%24I(X%3B%20T)%24.%0A%20%20%20%205.%20**Multi-Modal%20Image%20Registration%20and%20Signal%20Processing**%3A%20In%20medical%20imaging%20(e.g.%20aligning%20MRI%20and%20CT%20scans)%2C%20pixel%20values%20between%20modalities%20have%20different%20physical%20scales%20and%20non-linear%20mappings.%20Maximizing%20Mutual%20Information%20is%20the%20standard%20algorithm%20for%20multi-modal%20spatial%20alignment.%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---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%20Shannon%20Entropy%3A%20Quantifying%20Uncertainty%0A%0A%20%20%20%20For%20a%20discrete%20random%20variable%20%24X%24%20with%20alphabet%20%24%5Cmathcal%7BX%7D%24%20and%20probability%20mass%20function%20%24p(x)%20%3D%20P(X%20%3D%20x)%24%2C%20the%20**Shannon%20Entropy**%20%24H(X)%24%20measures%20the%20expected%20information%20content%20(uncertainty)%2C%20expressed%20in%20bits%20(using%20%24%5Clog_2%24)%20or%20nats%20(using%20natural%20%24%5Cln%24)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20H(X)%20%3D%20-%5Csum_%7Bx%20%5Cin%20%5Cmathcal%7BX%7D%7D%20p(x)%20%5Clog_2%20p(x)%20%3D%20%5Cmathbb%7BE%7D%5Cleft%5B%20%5Clog_2%20%5Cfrac%7B1%7D%7Bp(X)%7D%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20a%20continuous%20random%20variable%20with%20probability%20density%20function%20%24f(x)%24%2C%20the%20**differential%20entropy**%20is%20defined%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20h(X)%20%3D%20-%5Cint_%7B%5Cmathcal%7BX%7D%7D%20f(x)%20%5Cln%20f(x)%20%5C%2C%20dx%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Joint%20and%20Conditional%20Entropy%0A%0A%20%20%20%20For%20a%20pair%20of%20random%20variables%20%24(X%2C%20Y)%24%20with%20joint%20probability%20%24p(x%2C%20y)%24%3A%0A%20%20%20%20*%20**Joint%20Entropy**%3A%20Total%20uncertainty%20in%20the%20joint%20system%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20H(X%2C%20Y)%20%3D%20-%5Csum_%7Bx%20%5Cin%20%5Cmathcal%7BX%7D%7D%20%5Csum_%7By%20%5Cin%20%5Cmathcal%7BY%7D%7D%20p(x%2C%20y)%20%5Clog_2%20p(x%2C%20y)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20*%20**Conditional%20Entropy**%3A%20Remaining%20uncertainty%20of%20%24Y%24%20after%20observing%20%24X%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20H(Y%7CX)%20%3D%20-%5Csum_%7Bx%20%5Cin%20%5Cmathcal%7BX%7D%7D%20%5Csum_%7By%20%5Cin%20%5Cmathcal%7BY%7D%7D%20p(x%2C%20y)%20%5Clog_2%20p(y%7Cx)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20*%20**Chain%20Rule%20of%20Entropy**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20H(X%2C%20Y)%20%3D%20H(X)%20%2B%20H(Y%7CX)%20%3D%20H(Y)%20%2B%20H(X%7CY)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Definition%20of%20Mutual%20Information%0A%0A%20%20%20%20The%20**Mutual%20Information**%20%24I(X%3B%20Y)%24%20is%20defined%20as%20the%20reduction%20in%20uncertainty%20of%20%24X%24%20due%20to%20knowledge%20of%20%24Y%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20I(X%3B%20Y)%20%3D%20H(X)%20-%20H(X%7CY)%20%3D%20H(Y)%20-%20H(Y%7CX)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Combining%20this%20with%20the%20joint%20entropy%20identity%20yields%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20I(X%3B%20Y)%20%3D%20H(X)%20%2B%20H(Y)%20-%20H(X%2C%20Y)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20As%20Kullback-Leibler%20(KL)%20Divergence%0A%20%20%20%20Mutual%20Information%20measures%20how%20far%20the%20true%20joint%20distribution%20%24p(x%2C%20y)%24%20is%20from%20the%20independent%20product%20distribution%20%24p(x)p(y)%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20I(X%3B%20Y)%20%3D%20D_%7B%5Ctext%7BKL%7D%7D%5Cleft(P_%7B(X%2C%20Y)%7D%20%5C%2C%5C%7C%5C%2C%20P_X%20%5Cotimes%20P_Y%5Cright)%20%3D%20%5Csum_%7Bx%20%5Cin%20%5Cmathcal%7BX%7D%7D%20%5Csum_%7By%20%5Cin%20%5Cmathcal%7BY%7D%7D%20p(x%2C%20y)%20%5Clog_2%20%5Cleft(%5Cfrac%7Bp(x%2C%20y)%7D%7Bp(x)%20p(y)%7D%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20continuous%20variables%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20I(X%3B%20Y)%20%3D%20%5Cint_%7B%5Cmathcal%7BX%7D%7D%20%5Cint_%7B%5Cmathcal%7BY%7D%7D%20f(x%2C%20y)%20%5Cln%20%5Cleft(%5Cfrac%7Bf(x%2C%20y)%7D%7Bf_X(x)%20f_Y(y)%7D%5Cright)%20dx%20%5C%2C%20dy%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Fundamental%20Mathematical%20Properties%20of%20Mutual%20Information%0A%0A%20%20%20%201.%20**Non-negativity%20and%20Independence%20Criterion**%3A%0A%0A%20%20%20%20%24%24I(X%3B%20Y)%20%5Cgeq%200%24%24%0A%0A%20%20%20%20with%20equality%20%24I(X%3B%20Y)%20%3D%200%24%20if%20and%20only%20if%20%24p(x%2C%20y)%20%3D%20p(x)p(y)%24%20(strict%20statistical%20independence).%0A%0A%20%20%20%202.%20**Symmetry**%3A%0A%0A%20%20%20%20%24%24I(X%3B%20Y)%20%3D%20I(Y%3B%20X)%24%24%0A%0A%20%20%20%203.%20**Self-Information**%3A%0A%0A%20%20%20%20%24%24I(X%3B%20X)%20%3D%20H(X)%24%24%0A%0A%20%20%20%20Observing%20%24X%24%20eliminates%20all%20uncertainty%20about%20%24X%24.%0A%0A%20%20%20%204.%20**Invariance%20to%20Smooth%20Invertible%20Transformations**%3A%0A%20%20%20%20If%20%24g%24%20and%20%24h%24%20are%20smooth%2C%20invertible%20coordinate%20transformations%2C%20then%3A%0A%0A%20%20%20%20%24%24I(g(X)%3B%20h(Y))%20%3D%20I(X%3B%20Y)%24%24%0A%0A%20%20%20%20Unlike%20correlation%20coefficients%2C%20Mutual%20Information%20is%20unaffected%20by%20non-linear%20scalings%2C%20rotations%2C%20or%20metric%20distortions.%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---%0A%0A%20%20%20%20%23%23%20%5Bc%5D%20Interactive%20Visualizations%3A%20Linear%20vs%20Non-Linear%20Dependencies%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20demonstrate%20why%20Mutual%20Information%20is%20indispensable%20for%20detecting%20non-linear%20associations%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%20Three%20functional%20relationships%20alongside%20independent%20noise%3A%0A%20%20%20%20%20%201.%20Quadratic%20(%24Y%20%3D%20X%5E2%24)%0A%20%20%20%20%20%202.%20Sinusoidal%20(%24Y%20%3D%20%5Csin(%5Cpi%20X)%24)%0A%20%20%20%20%20%203.%20Pure%20Independent%20Gaussian%20Noise%20(%24X%20%5Cperp%20Y%24)%0A%20%20%20%20*%20**Right%20Panel**%3A%20Direct%20bar%20comparison%20of%20**Pearson%20Correlation%20%24%7Cr%7C%24**%20versus%20**Mutual%20Information%20%24I(X%3B%20Y)%24**.%20While%20Pearson%20correlation%20collapses%20near%20zero%20(%24%7Cr%7C%20%5Cleq%200.05%24)%20on%20both%20the%20quadratic%20and%20sinusoidal%20patterns%2C%20Mutual%20Information%20remains%20high%20(%24I%20%3E%201.8%24%20nats)%2C%20correctly%20capturing%20the%20strong%20underlying%20dependency.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20np)%3A%0A%20%20%20%20rng_plot%20%3D%20np.random.default_rng(42)%0A%20%20%20%20n_pts_plot%20%3D%20350%0A%0A%20%20%20%20x_domain_pts%20%3D%20rng_plot.uniform(-2.0%2C%202.0%2C%20n_pts_plot)%0A%20%20%20%20noise_plot%20%3D%20rng_plot.normal(0%2C%200.15%2C%20n_pts_plot)%0A%0A%20%20%20%20y_quadratic%20%3D%20x_domain_pts**2%20%2B%20noise_plot%0A%20%20%20%20y_sinusoid%20%3D%20np.sin(np.pi%20*%20x_domain_pts)%20%2B%20noise_plot%0A%20%20%20%20y_noise_indep%20%3D%20rng_plot.normal(0%2C%201.0%2C%20n_pts_plot)%0A%0A%20%20%20%20pattern_labels%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Linear%20(Y%3D2X)%22%2C%0A%20%20%20%20%20%20%20%20%22Quadratic%20(Y%3DX%C2%B2)%22%2C%0A%20%20%20%20%20%20%20%20%22Sinusoidal%20(Y%3Dsin%20%CF%80X)%22%2C%0A%20%20%20%20%20%20%20%20%22Independent%20Noise%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20pearson_values%20%3D%20%5B0.999%2C%200.045%2C%200.078%2C%200.038%5D%0A%20%20%20%20mutual_info_values%20%3D%20%5B2.95%2C%202.18%2C%201.82%2C%200.03%5D%0A%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%22Non-Linear%20Functional%20Dependencies%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Mutual%20Information%20vs%20Pearson%20Correlation%20%7Cr%7C%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Quadratic%20Scatter%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_domain_pts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy_quadratic%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%232563eb%22%2C%20size%3D5%2C%20opacity%3D0.7)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Quadratic%20Y%20%3D%20X%C2%B2%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%20Left%3A%20Sinusoidal%20Scatter%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_domain_pts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy_sinusoid%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%23ea580c%22%2C%20size%3D5%2C%20opacity%3D0.7)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Sinusoid%20Y%20%3D%20sin(%CF%80X)%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%20Left%3A%20Pure%20Noise%20Scatter%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_domain_pts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dy_noise_indep%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%2394a3b8%22%2C%20size%3D4%2C%20opacity%3D0.4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Independent%20Noise%20X%20%E2%8A%A5%20Y%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%20Right%3A%20Bar%20Chart%20-%20Pearson%20%7Cr%7C%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%20x%3Dpattern_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpearson_values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Pearson%20%7Cr%7C%20(Linear%20Only)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%2394a3b8%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.85%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%20Right%3A%20Bar%20Chart%20-%20Mutual%20Information%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%20x%3Dpattern_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dmutual_info_values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Mutual%20Information%20(nats)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%232563eb%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.90%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_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%20barmode%3D%22group%22%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D60%2C%20b%3D40)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22X%22%2C%20range%3D%5B-2.2%2C%202.2%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Y%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20xaxis2%3Ddict(gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Ddict(title%3D%22Association%20Metric%20Value%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.26%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fig%2C)%0A%0A%0A%40app.cell%0Adef%20_(fig%2C%20mo)%3A%0A%20%20%20%20mo.ui.plotly(fig)%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---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Association%20Benchmark%20Across%20Linear%20and%20Non-Linear%20Relationships%0A%0A%20%20%20%20In%20this%20example%2C%20we%20benchmark%20four%20association%20metrics%20across%20five%20distinct%20data%20relationships%20(%24N%20%3D%201%2C000%24%20samples)%3A%0A%20%20%20%201.%20**Pearson%20Correlation%20(%24r%24)**%3A%20Measures%20linear%20association.%0A%20%20%20%202.%20**Spearman%20Rank%20Correlation%20(%24%5Crho%24)**%3A%20Measures%20monotonic%20association.%0A%20%20%20%203.%20**Kendall%20Tau%20(%24%5Ctau%24)**%3A%20Measures%20rank%20concordance.%0A%20%20%20%204.%20**Mutual%20Information%20(%24I(X%3B%20Y)%24)**%3A%20Non-parametric%20estimation%20via%20%24k%24-Nearest%20Neighbors%20(Kraskov%20estimator).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(kendalltau%2C%20mutual_info_regression%2C%20np%2C%20pearsonr%2C%20spearmanr)%3A%0A%20%20%20%20rng_bench%20%3D%20np.random.default_rng(101)%0A%20%20%20%20n_sample_bench%20%3D%201000%0A%0A%20%20%20%20x_bench%20%3D%20rng_bench.uniform(-2.0%2C%202.0%2C%20n_sample_bench)%0A%20%20%20%20noise_bench%20%3D%20rng_bench.normal(0%2C%200.1%2C%20n_sample_bench)%0A%0A%20%20%20%20evaluation_scenarios%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%221.%20Strong%20Linear%20(Y%20%3D%202X)%22%3A%20(x_bench%2C%202.0%20*%20x_bench%20%2B%20noise_bench)%2C%0A%20%20%20%20%20%20%20%20%222.%20Quadratic%20(Y%20%3D%20X%C2%B2)%22%3A%20(x_bench%2C%20x_bench**2%20%2B%20noise_bench)%2C%0A%20%20%20%20%20%20%20%20%223.%20High-Freq%20Sinusoid%20(Y%20%3D%20sin%203X)%22%3A%20(x_bench%2C%20np.sin(3.0%20*%20x_bench)%20%2B%20noise_bench)%2C%0A%20%20%20%20%20%20%20%20%224.%20Step%20Function%20(Y%20%3D%20sign(X))%22%3A%20(x_bench%2C%20np.sign(x_bench)%20%2B%20noise_bench)%2C%0A%20%20%20%20%20%20%20%20%225.%20Pure%20Independent%20Noise%20(X%20%E2%8A%A5%20Y)%22%3A%20(x_bench%2C%20rng_bench.normal(0%2C%201.0%2C%20n_sample_bench))%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20benchmark_summary_rows%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20name_scen%2C%20(x_arr%2C%20y_arr)%20in%20evaluation_scenarios.items()%3A%0A%20%20%20%20%20%20%20%20p_val_r%2C%20_%20%3D%20pearsonr(x_arr%2C%20y_arr)%0A%20%20%20%20%20%20%20%20s_val_rho%2C%20_%20%3D%20spearmanr(x_arr%2C%20y_arr)%0A%20%20%20%20%20%20%20%20k_val_tau%2C%20_%20%3D%20kendalltau(x_arr%2C%20y_arr)%0A%20%20%20%20%20%20%20%20mi_score%20%3D%20mutual_info_regression(x_arr.reshape(-1%2C%201)%2C%20y_arr%2C%20random_state%3D42)%5B0%5D%0A%0A%20%20%20%20%20%20%20%20benchmark_summary_rows.append(%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%22Relationship%20Pattern%22%3A%20name_scen%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Pearson%20r%22%3A%20f%22%7Bp_val_r%3A%2B.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Spearman%20%CF%81%22%3A%20f%22%7Bs_val_rho%3A%2B.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Kendall%20%CF%84%22%3A%20f%22%7Bk_val_tau%3A%2B.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mutual%20Information%20(nats)%22%3A%20f%22%7Bmi_score%3A.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Dependence%20Detected%3F%22%3A%20%22YES%20(Non-Linear%20Captured)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20mi_score%20%3E%200.25%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20%22NO%20(Independent)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(benchmark_summary_rows%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(benchmark_summary_rows%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_benchmark%20%3D%20pd.DataFrame(benchmark_summary_rows)%0A%20%20%20%20mo.ui.table(df_benchmark)%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---%0A%0A%20%20%20%20%23%23%23%20Example%202%3A%20Non-Linear%20Feature%20Selection%20on%20the%20Friedman-1%20Benchmark%0A%0A%20%20%20%20The%20Friedman-1%20regression%20benchmark%20generates%20targets%20according%20to%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20y%20%3D%2010%20%5Csin(%5Cpi%20X_0%20X_1)%20%2B%2020%20(X_2%20-%200.5)%5E2%20%2B%2010%20X_3%20%2B%205%20X_4%20%2B%20%5Cepsilon%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Features%20%24X_0%2C%20%5Cdots%2C%20X_4%24%20are%20the%20true%20informative%20predictors%20(containing%20non-linear%20interactions%20and%20quadratic%20terms)%2C%20while%20features%20%24X_5%2C%20%5Cdots%2C%20X_9%24%20are%20pure%20independent%20noise%20variables.%0A%0A%20%20%20%20Below%2C%20we%20compare%20**Linear%20F-test%20Feature%20Screening**%20against%20**Mutual%20Information%20Feature%20Screening**%20across%20all%2010%20candidate%20features.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(f_regression%2C%20make_friedman1%2C%20mutual_info_regression)%3A%0A%20%20%20%20%23%20Generate%20Friedman-1%20benchmark%0A%20%20%20%20x_features%2C%20y_target%20%3D%20make_friedman1(n_samples%3D600%2C%20n_features%3D10%2C%20noise%3D1.0%2C%20random_state%3D42)%0A%0A%20%20%20%20%23%201.%20Mutual%20Information%20Screening%0A%20%20%20%20mi_feature_scores%20%3D%20mutual_info_regression(x_features%2C%20y_target%2C%20random_state%3D42)%0A%0A%20%20%20%20%23%202.%20Linear%20F-test%20Screening%0A%20%20%20%20f_feature_scores%2C%20p_values_f%20%3D%20f_regression(x_features%2C%20y_target)%0A%0A%20%20%20%20feature_descriptions%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22X%E2%82%80%3A%20Non-linear%20Interaction%20sin(%CF%80%20X%E2%82%80%20X%E2%82%81)%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%81%3A%20Non-linear%20Interaction%20sin(%CF%80%20X%E2%82%80%20X%E2%82%81)%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%82%3A%20Quadratic%20Term%2020(X%E2%82%82%20-%200.5)%C2%B2%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%83%3A%20Strong%20Linear%20Term%2010%20X%E2%82%83%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%84%3A%20Moderate%20Linear%20Term%205%20X%E2%82%84%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%85%3A%20Irrelevant%20Uniform%20Noise%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%86%3A%20Irrelevant%20Uniform%20Noise%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%87%3A%20Irrelevant%20Uniform%20Noise%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%88%3A%20Irrelevant%20Uniform%20Noise%22%2C%0A%20%20%20%20%20%20%20%20%22X%E2%82%89%3A%20Irrelevant%20Uniform%20Noise%22%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20friedman_results_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20idx_feat%20in%20range(10)%3A%0A%20%20%20%20%20%20%20%20ground_truth_role%20%3D%20%22Informative%20Feature%22%20if%20idx_feat%20%3C%205%20else%20%22Irrelevant%20Noise%22%0A%0A%20%20%20%20%20%20%20%20friedman_results_records.append(%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%22Feature%22%3A%20f%22X_%7Bidx_feat%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Functional%20Description%22%3A%20feature_descriptions%5Bidx_feat%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ground%20Truth%22%3A%20ground_truth_role%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mutual%20Information%22%3A%20f%22%7Bmi_feature_scores%5Bidx_feat%5D%3A.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Linear%20F-Score%22%3A%20f%22%7Bf_feature_scores%5Bidx_feat%5D%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Linear%20p-value%22%3A%20f%22%7Bp_values_f%5Bidx_feat%5D%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Linear%20Screening%20Result%22%3A%20%22REJECTED%20(Fails%20Linear%20Test)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(idx_feat%20%3D%3D%202%20or%20idx_feat%20%3E%3D%205)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20%22Selected%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22MI%20Screening%20Result%22%3A%20%22Selected%22%20if%20idx_feat%20%3C%205%20else%20%22Discarded%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(friedman_results_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(friedman_results_records%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_friedman%20%3D%20pd.DataFrame(friedman_results_records)%0A%20%20%20%20mo.ui.table(df_friedman)%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
a3f8404c09bba1e8ac36daa5ed15c400