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%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%20%20%20%20from%20sklearn.metrics%20import%20r2_score%0A%0A%20%20%20%20return%20LinearRegression%2C%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%2C%20r2_score%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%2024%3A%20R-Squared%2C%20Adjusted%20R-Squared%2C%20and%20Model%20Complexity%20Penalization%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B23%20EWA%20and%20Bias%20Correction%5D(23_ewa_and_bias_correction.py)%20%7C%20Next%20Note%3A%20%5B25%20Predictive%20R2%5D(25_predictive_r2.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%20In%20multiple%20linear%20regression%20and%20supervised%20learning%2C%20evaluating%20how%20well%20a%20model%20explains%20variance%20in%20the%20target%20variable%20%24y%24%20is%20a%20primary%20objective.%20The%20classical%20**Coefficient%20of%20Determination%20(%24R%5E2%24)**%20measures%20the%20proportion%20of%20total%20variance%20explained%20by%20the%20fitted%20hyperplane.%0A%0A%20%20%20%20However%2C%20raw%20%24R%5E2%24%20suffers%20from%20a%20severe%20mathematical%20pathology%3A%0A%20%20%20%201.%20**Monotonic%20Non-Decreasing%20Property**%3A%20In%20Ordinary%20Least%20Squares%20(OLS)%2C%20projecting%20the%20response%20vector%20%24%5Cmathbf%7By%7D%24%20onto%20a%20higher-dimensional%20predictor%20subspace%20%24%5Coperatorname%7Bspan%7D(%5Cmathbf%7BX%7D)%24%20cannot%20increase%20the%20residual%20sum%20of%20squares%3A%20%24%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%5E%7B(p%2B1)%7D%20%5Cleq%20%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%5E%7B(p)%7D%24.%20Adding%20any%20variable%20(even%20pure%20random%20Gaussian%20white%20noise%20or%20coin%20tosses)%20mathematically%20forces%20raw%20%24R%5E2%24%20to%20increase%20or%20remain%20unchanged.%0A%20%20%20%202.%20**Overfitting%20and%20False%20Feature%20Selection**%3A%20Relying%20on%20%24R%5E2%24%20to%20select%20features%20inevitably%20leads%20to%20saturated%2C%20overparameterized%20models%20that%20memorize%20training%20noise%2C%20inflating%20prediction%20variance%20on%20unseen%20test%20data.%0A%20%20%20%203.%20**Adjusted%20%24R%5E2%24%20(%24%5Cbar%7BR%7D%5E2%24)%20as%20an%20Unbiased%20Variance%20Ratio**%3A%20Ezekiel%20(1930)%20introduced%20Adjusted%20%24R%5E2%24%20by%20replacing%20sample%20sums%20of%20squares%20with%20their%20unbiased%20degrees-of-freedom%20estimators%3A%20dividing%20%24%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%24%20by%20%24n%20-%20p%20-%201%24%20and%20%24%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%24%20by%20%24n%20-%201%24.%0A%20%20%20%204.%20**The%20Critical%20%24F%20%3E%201%24%20Threshold%20Rule**%3A%20A%20celebrated%20statistical%20theorem%20proves%20that%20adding%20a%20predictor%20increases%20Adjusted%20%24R%5E2%24%20if%20and%20only%20if%20the%20absolute%20%24t%24-statistic%20of%20that%20predictor%20exceeds%201%20(or%20equivalently%2C%20the%20incremental%20partial%20%24F%24-statistic%20exceeds%201).%20If%20an%20added%20predictor%20contributes%20less%20explanation%20than%20expected%20from%20random%20noise%20(%24F%20%3C%201%24)%2C%20Adjusted%20%24R%5E2%24%20penalizes%20the%20model%20and%20strictly%20declines.%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%201.%20Analysis%20of%20Variance%20(ANOVA)%20Decomposition%0A%0A%20%20%20%20For%20a%20linear%20regression%20model%20with%20%24n%24%20observations%20and%20%24p%24%20predictors%20plus%20an%20intercept%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7By%7D%20%3D%20%5Cmathbf%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D%20%2B%20%5Cboldsymbol%7B%5Cepsilon%7D%2C%20%5Cquad%20%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20(p%20%2B%201)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20total%20variability%20in%20%24%5Cmathbf%7By%7D%24%20decomposes%20orthogonally%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%20%3D%20%5Ctext%7BSS%7D_%7B%5Ctext%7Breg%7D%7D%20%2B%20%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Total%20Sum%20of%20Squares%20(%24%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%24)%0A%20%20%20%20Total%20sample%20dispersion%20around%20the%20sample%20mean%20%24%5Cbar%7By%7D%24%2C%20with%20%24n%20-%201%24%20degrees%20of%20freedom%3A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20(y_i%20-%20%5Cbar%7By%7D)%5E2%20%3D%20%5C%7C%5Cmathbf%7By%7D%20-%20%5Cbar%7By%7D%5Cmathbf%7B1%7D%5C%7C_2%5E2%2C%20%5Cquad%20%5Ctext%7Bdf%7D_%7B%5Ctext%7Btot%7D%7D%20%3D%20n%20-%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Residual%20Sum%20of%20Squares%20(%24%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%24)%0A%20%20%20%20Unexplained%20variance%20orthogonal%20to%20the%20predictor%20column%20space%2C%20with%20%24n%20-%20p%20-%201%24%20degrees%20of%20freedom%3A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20(y_i%20-%20%5Chat%7By%7D_i)%5E2%20%3D%20%5C%7C%5Cmathbf%7By%7D%20-%20%5Chat%7B%5Cmathbf%7By%7D%7D%5C%7C_2%5E2%2C%20%5Cquad%20%5Ctext%7Bdf%7D_%7B%5Ctext%7Bres%7D%7D%20%3D%20n%20-%20p%20-%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Regression%20Sum%20of%20Squares%20(%24%5Ctext%7BSS%7D_%7B%5Ctext%7Breg%7D%7D%24)%0A%20%20%20%20Variability%20explained%20by%20the%20fitted%20hyperplane%2C%20with%20%24p%24%20degrees%20of%20freedom%3A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BSS%7D_%7B%5Ctext%7Breg%7D%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20(%5Chat%7By%7D_i%20-%20%5Cbar%7By%7D)%5E2%20%3D%20%5C%7C%5Chat%7B%5Cmathbf%7By%7D%7D%20-%20%5Cbar%7By%7D%5Cmathbf%7B1%7D%5C%7C_2%5E2%2C%20%5Cquad%20%5Ctext%7Bdf%7D_%7B%5Ctext%7Breg%7D%7D%20%3D%20p%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20The%20Coefficient%20of%20Determination%20(%24R%5E2%24)%0A%0A%20%20%20%20Raw%20%24R%5E2%24%20is%20the%20ratio%20of%20explained%20variance%20to%20total%20variance%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20R%5E2%20%3D%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Breg%7D%7D%7D%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%7D%20%3D%201%20-%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%7D%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20models%20with%20an%20intercept%2C%20%24R%5E2%20%5Cin%20%5B0%2C%201%5D%24.%20In%20the%20bivariate%20case%20(%24p%20%3D%201%24)%2C%20%24R%5E2%24%20is%20exactly%20the%20square%20of%20Pearson's%20correlation%20coefficient%3A%20%24R%5E2%20%3D%20r_%7Bxy%7D%5E2%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20Ezekiel's%20Adjusted%20%24R%5E2%24%20(%24%5Cbar%7BR%7D%5E2%24)%0A%0A%20%20%20%20Adjusted%20%24R%5E2%24%20corrects%20for%20model%20dimension%20by%20dividing%20each%20sum%20of%20squares%20by%20its%20respective%20degrees%20of%20freedom%2C%20transforming%20sums%20of%20squares%20into%20unbiased%20variance%20estimators%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7BR%7D%5E2%20%3D%201%20-%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%20%2F%20(n%20-%20p%20-%201)%7D%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%20%2F%20(n%20-%201)%7D%20%3D%201%20-%20%5Cfrac%7B%5Ctext%7BMSE%7D%7D%7B%5Ctext%7BMST%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%3A%0A%20%20%20%20-%20%24%5Ctext%7BMSE%7D%20%3D%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%7D%7Bn%20-%20p%20-%201%7D%24%20is%20the%20Mean%20Squared%20Error%20(unbiased%20estimator%20of%20error%20variance%20%24%5Csigma%5E2%24).%0A%20%20%20%20-%20%24%5Ctext%7BMST%7D%20%3D%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%7D%7Bn%20-%201%7D%24%20is%20the%20Mean%20Total%20Sum%20of%20Squares%20(sample%20variance%20%24s_y%5E2%24).%0A%0A%20%20%20%20%23%23%23%23%20Algebraic%20Relationship%20with%20Raw%20%24R%5E2%24%0A%20%20%20%20Expressing%20%24%5Cbar%7BR%7D%5E2%24%20in%20terms%20of%20%24R%5E2%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7BR%7D%5E2%20%3D%201%20-%20(1%20-%20R%5E2)%20%5Cleft(%5Cfrac%7Bn%20-%201%7D%7Bn%20-%20p%20-%201%7D%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Key%20implications%3A%0A%20%20%20%201.%20%24%5Cbar%7BR%7D%5E2%20%5Cleq%20R%5E2%24%20always%2C%20with%20equality%20holding%20if%20and%20only%20if%20%24R%5E2%20%3D%201%24%20or%20%24p%20%3D%200%24.%0A%20%20%20%202.%20%24%5Cbar%7BR%7D%5E2%24%20can%20be%20negative%20if%20%24%5Ctext%7BMSE%7D%20%3E%20%5Ctext%7BMST%7D%24%2C%20indicating%20that%20the%20model's%20predictions%20perform%20worse%20than%20the%20simple%20baseline%20average%20%24%5Cbar%7By%7D%24.%0A%20%20%20%203.%20As%20sample%20size%20%24n%20%5Cto%20%5Cinfty%24%20with%20fixed%20%24p%24%2C%20the%20adjustment%20factor%20%24%5Cfrac%7Bn%20-%201%7D%7Bn%20-%20p%20-%201%7D%20%5Cto%201%24%2C%20meaning%20%24%5Cbar%7BR%7D%5E2%20%5Cto%20R%5E2%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20The%20Incremental%20Partial%20%24F%24-Test%20Criterion%0A%0A%20%20%20%20When%20adding%20a%20new%20candidate%20predictor%20%24x_%7Bp%2B1%7D%24%20to%20a%20baseline%20model%20with%20%24p%24%20predictors%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7BR%7D%5E2_%7Bp%2B1%7D%20%3E%20%5Cbar%7BR%7D%5E2_p%20%5Ciff%20F_%7B%5Ctext%7Bpartial%7D%7D%20%3D%20%5Cfrac%7B%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D(p)%20-%20%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D(p%2B1)%7D%7B%5Ctext%7BMSE%7D(p%2B1)%7D%20%3E%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Since%20for%20a%20single%20parameter%20%24F_%7B%5Ctext%7Bpartial%7D%7D%20%3D%20t_%7Bp%2B1%7D%5E2%24%2C%20this%20yields%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7BR%7D%5E2_%7Bp%2B1%7D%20%3E%20%5Cbar%7BR%7D%5E2_p%20%5Ciff%20%7Ct_%7Bp%2B1%7D%7C%20%3E%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20establishes%20that%20Adjusted%20%24R%5E2%24%20will%20only%20increase%20if%20the%20predictor%20explains%20more%20variance%20than%20the%20expected%20noise%20contribution%20(%24%5Cmathbb%7BE%7D%5BF%5D%20%5Capprox%201%24%20under%20%24H_0%24).%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%205.%20Mathematical%20Summary%20Table%0A%0A%20%20%20%20%7C%20Metric%20%7C%20Degrees%20of%20Freedom%20%7C%20Monotonic%20in%20%24p%24%3F%20%7C%20Can%20be%20Negative%3F%20%7C%20Penalty%20for%20Noise%20Features%20%7C%20Primary%20Usage%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20**Raw%20%24R%5E2%24**%20%7C%20None%20(%24n%24%20ignored)%20%7C%20Yes%20(strictly%20non-decreasing)%20%7C%20No%20(with%20intercept)%20%7C%20None%20(rewards%20noise)%20%7C%20Goodness-of-fit%20on%20fixed%20models%20%7C%0A%20%20%20%20%7C%20**Adjusted%20%24R%5E2%24**%20%7C%20%24n%20-%20p%20-%201%24%20vs%20%24n%20-%201%24%20%7C%20No%20(peaks%20at%20optimal%20subset)%20%7C%20Yes%20(if%20MSE%20%3E%20MST)%20%7C%20Explicit%20penalty%20via%20%24%5Cfrac%7Bn-1%7D%7Bn-p-1%7D%24%20%7C%20Model%20comparison%2C%20nested%20feature%20screening%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Simulation%20Data%3A%20True%20Signal%20Predictors%20(x1%2C%20x2)%20%2B%20Pure%20Noise%20Predictors%20(x3%20...%20x10)%0A%20%20%20%20np.random.seed(47)%0A%20%20%20%20_n%20%3D%20100%0A%0A%20%20%20%20%23%20Genuine%20predictors%0A%20%20%20%20_x1%20%3D%20np.random.uniform(10.0%2C%2050.0%2C%20_n)%0A%20%20%20%20_x2%20%3D%20np.random.uniform(20.0%2C%2080.0%2C%20_n)%0A%20%20%20%20_noise%20%3D%20np.random.normal(0.0%2C%202.5%2C%20_n)%0A%0A%20%20%20%20%23%20True%20data-generating%20process%3A%20y%20%3D%2010%20%2B%200.8%20*%20x1%20%2B%201.5%20*%20x2%20%2B%20noise%0A%20%20%20%20_y%20%3D%2010.0%20%2B%200.8%20*%20_x1%20%2B%201.5%20*%20_x2%20%2B%20_noise%0A%0A%20%20%20%20%23%20Injected%20pure%20Gaussian%20noise%20variables%0A%20%20%20%20_noise_vars%20%3D%20%7Bf%22Noise_x%7Bi%7D%22%3A%20np.random.normal(0.0%2C%201.0%2C%20_n)%20for%20i%20in%20range(3%2C%2011)%7D%0A%0A%20%20%20%20_data_dict%20%3D%20%7B%22x1%22%3A%20_x1%2C%20%22x2%22%3A%20_x2%2C%20**_noise_vars%2C%20%22Target_y%22%3A%20_y%7D%0A%20%20%20%20df_regression%20%3D%20pd.DataFrame(_data_dict)%0A%20%20%20%20return%20(df_regression%2C)%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20df_regression%2C%20go%2C%20make_subplots%2C%20np%2C%20r2_score)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%20Subplot%201%3A%20Feature%20Correlation%20Heatmap%20(Diagnosing%20true%20signal%20vs%20noise)%0A%20%20%20%20%23%20Subplot%202%3A%20R%5E2%20vs%20Adjusted%20R%5E2%20Trajectory%20as%20predictors%20are%20added%20(1%20to%2010)%0A%20%20%20%20%23%20Subplot%203%3A%20Incremental%20F-statistic%20and%20Delta%20Adjusted%20R%5E2%20vs%20the%20F%3D1%20threshold%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D3%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Feature%20Correlation%20with%20Target%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20R%5E2%20vs.%20Adjusted%20R%5E2%20Complexity%20Curve%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Incremental%20Partial%20F-Statistic%20vs.%20F%3D1%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.09%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_target%20%3D%20df_regression%5B%22Target_y%22%5D%0A%20%20%20%20_features%20%3D%20%5Bc%20for%20c%20in%20df_regression.columns%20if%20c%20!%3D%20%22Target_y%22%5D%0A%20%20%20%20_n%20%3D%20len(_target)%0A%0A%20%20%20%20%23%20Subplot%201%3A%20Correlations%0A%20%20%20%20_corr_series%20%3D%20df_regression%5B_features%5D.apply(lambda%20col%3A%20col.corr(_target))%0A%20%20%20%20_colors%20%3D%20%5B%22%2310b981%22%20if%20abs(c)%20%3E%200.3%20else%20%22%2394a3b8%22%20for%20c%20in%20_corr_series%5D%0A%0A%20%20%20%20_fig.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%3D_features%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_corr_series%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D_colors%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Correlation%20with%20y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22%25%7Bx%7D%3A%20r%20%3D%20%25%7By%3A.3f%7D%3Cextra%3E%3C%2Fextra%3E%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%20Subplot%202%3A%20Incremental%20Model%20Fitting%0A%20%20%20%20_r2_vals%20%3D%20%5B%5D%0A%20%20%20%20_adj_r2_vals%20%3D%20%5B%5D%0A%20%20%20%20_f_stats%20%3D%20%5B%5D%0A%20%20%20%20_ss_res_prev%20%3D%20None%0A%0A%20%20%20%20for%20_p%20in%20range(1%2C%20len(_features)%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_sub_x%20%3D%20df_regression%5B_features%5B%3A_p%5D%5D%0A%20%20%20%20%20%20%20%20_mod%20%3D%20LinearRegression().fit(_sub_x%2C%20_target)%0A%20%20%20%20%20%20%20%20_preds%20%3D%20_mod.predict(_sub_x)%0A%20%20%20%20%20%20%20%20_r2%20%3D%20r2_score(_target%2C%20_preds)%0A%20%20%20%20%20%20%20%20_adj_r2%20%3D%201.0%20-%20(1.0%20-%20_r2)%20*%20(_n%20-%201)%20%2F%20(_n%20-%20_p%20-%201)%0A%20%20%20%20%20%20%20%20_r2_vals.append(_r2)%0A%20%20%20%20%20%20%20%20_adj_r2_vals.append(_adj_r2)%0A%0A%20%20%20%20%20%20%20%20_ss_res%20%3D%20np.sum((_target%20-%20_preds)%20**%202)%0A%20%20%20%20%20%20%20%20_mse%20%3D%20_ss_res%20%2F%20(_n%20-%20_p%20-%201)%0A%20%20%20%20%20%20%20%20if%20_ss_res_prev%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_f%20%3D%20(_ss_res_prev%20-%20_ss_res)%20%2F%20_mse%0A%20%20%20%20%20%20%20%20%20%20%20%20_f_stats.append(_f)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_f_stats.append(np.nan)%0A%20%20%20%20%20%20%20%20_ss_res_prev%20%3D%20_ss_res%0A%0A%20%20%20%20_p_axis%20%3D%20np.arange(1%2C%20len(_features)%20%2B%201)%0A%0A%20%20%20%20_fig.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%3D_p_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_r2_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Raw%20R%5E2%20(Monotonic)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22p%3D%25%7Bx%7D%3A%20R%5E2%3D%25%7By%3A.4f%7D%3Cextra%3E%3C%2Fextra%3E%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_fig.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%3D_p_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_adj_r2_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310b981%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Adjusted%20R%5E2%20(Penalized)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22p%3D%25%7Bx%7D%3A%20Adj%20R%5E2%3D%25%7By%3A.4f%7D%3Cextra%3E%3C%2Fextra%3E%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%20Subplot%203%3A%20Incremental%20F-statistic%0A%20%20%20%20_fig.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%3D_features%5B1%3A%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_f_stats%5B1%3A%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%5B%22%2310b981%22%20if%20(not%20np.isnan(f)%20and%20f%20%3E%201.0)%20else%20%22%23f43f5e%22%20for%20f%20in%20_f_stats%5B1%3A%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Partial%20F-Stat%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22%25%7Bx%7D%3A%20F%20%3D%20%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%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%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.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%3D%5B_features%5B1%5D%2C%20_features%5B-1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B1.0%2C%201.0%5D%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%23ef4444%22%2C%20dash%3D%22dash%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Threshold%20F%20%3D%201.0%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%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.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%3D480%2C%0A%20%20%20%20%20%20%20%20title%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%22Model%20Complexity%20Penalty%3A%20Raw%20R%5E2%20vs.%20Adjusted%20R%5E2%20and%20the%20F%3D1%20Criterion%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D0.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xanchor%3D%22center%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20font%3Ddict(size%3D16%2C%20family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22)%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.25%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D75%2C%20b%3D80)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Predictor%22%2C%20tickangle%3D45%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Pearson%20r%20with%20Target%22%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Number%20of%20Predictors%20(p)%22%2C%20tickvals%3D_p_axis%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Metric%20Value%22%2C%20range%3D%5B0.4%2C%201.02%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Added%20Predictor%22%2C%20tickangle%3D45%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Partial%20F-Statistic%22%2C%20range%3D%5B0.0%2C%2015.0%5D%2C%20row%3D1%2C%20col%3D3)%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%20Below%20we%20implement%20two%20rigorous%2C%20production-grade%20demonstrations%3A%0A%20%20%20%201.%20**Forward%20Stepwise%20Regression%20%26%20Complexity%20Diagnostics**%3A%20Exhaustively%20evaluating%20%24%5Ctext%7BSS%7D_%7B%5Ctext%7Bres%7D%7D%2C%20%5Ctext%7BSS%7D_%7B%5Ctext%7Btot%7D%7D%24%2C%20degrees%20of%20freedom%2C%20%24R%5E2%2C%20%5Cbar%7BR%7D%5E2%24%2C%20and%20partial%20%24F%24-statistics%2C%20confirming%20that%20Adjusted%20%24R%5E2%24%20peaks%20precisely%20at%20the%20true%20causal%20predictors%20(%24x_1%2C%20x_2%24).%0A%20%20%20%202.%20**The%20Negative%20Adjusted%20%24R%5E2%24%20Simulation**%3A%20Fitting%2010%20pure%20Gaussian%20noise%20predictors%20on%20a%20small%20sample%20(%24n%20%3D%2025%24)%2C%20demonstrating%20how%20raw%20%24R%5E2%24%20deceptively%20reports%20moderate%20fit%20while%20Adjusted%20%24R%5E2%24%20turns%20negative%2C%20diagnosing%20a%20completely%20invalid%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20df_regression%2C%20np%2C%20pd%2C%20r2_score)%3A%0A%20%20%20%20%23%20Example%201%3A%20Forward%20Stepwise%20Complexity%20Table%20with%20F-Test%20Diagnostics%0A%20%20%20%20_target%20%3D%20df_regression%5B%22Target_y%22%5D.to_numpy()%0A%20%20%20%20_features%20%3D%20%5Bc%20for%20c%20in%20df_regression.columns%20if%20c%20!%3D%20%22Target_y%22%5D%0A%20%20%20%20_n%20%3D%20len(_target)%0A%20%20%20%20_ss_tot%20%3D%20np.sum((_target%20-%20np.mean(_target))%20**%202)%0A%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20_prev_ss_res%20%3D%20_ss_tot%0A%0A%20%20%20%20for%20_p%20in%20range(1%2C%20len(_features)%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_current_features%20%3D%20_features%5B%3A_p%5D%0A%20%20%20%20%20%20%20%20_X_mat%20%3D%20df_regression%5B_current_features%5D.to_numpy()%0A%20%20%20%20%20%20%20%20_mod%20%3D%20LinearRegression().fit(_X_mat%2C%20_target)%0A%20%20%20%20%20%20%20%20_y_hat%20%3D%20_mod.predict(_X_mat)%0A%0A%20%20%20%20%20%20%20%20_ss_res%20%3D%20np.sum((_target%20-%20_y_hat)%20**%202)%0A%20%20%20%20%20%20%20%20_df_res%20%3D%20_n%20-%20_p%20-%201%0A%20%20%20%20%20%20%20%20_mse%20%3D%20_ss_res%20%2F%20_df_res%0A%0A%20%20%20%20%20%20%20%20_r2%20%3D%20r2_score(_target%2C%20_y_hat)%0A%20%20%20%20%20%20%20%20_adj_r2%20%3D%201.0%20-%20(1.0%20-%20_r2)%20*%20(_n%20-%201)%20%2F%20_df_res%0A%0A%20%20%20%20%20%20%20%20if%20_p%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_f_stat_str%20%3D%20%22-%22%0A%20%20%20%20%20%20%20%20%20%20%20%20_decision%20%3D%20%22Initial%20Single%20Feature%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_partial_f%20%3D%20(_prev_ss_res%20-%20_ss_res)%20%2F%20_mse%0A%20%20%20%20%20%20%20%20%20%20%20%20_f_stat_str%20%3D%20f%22%7B_partial_f%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20_decision%20%3D%20%22Adj%20R%5E2%20Increased%20(F%20%3E%201)%22%20if%20_partial_f%20%3E%201.0%20else%20%22Adj%20R%5E2%20Decreased%20(F%20%3C%3D%201%2C%20Overfitting)%22%0A%0A%20%20%20%20%20%20%20%20_rows.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Model%20Specification%22%3A%20f%22p%20%3D%20%7B_p%7D%20(%7B_current_features%5B-1%5D%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22SS_res%22%3A%20f%22%7B_ss_res%3A.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22df_res%22%3A%20_df_res%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Raw%20R%5E2%22%3A%20f%22%7B_r2%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Adjusted%20R%5E2%22%3A%20f%22%7B_adj_r2%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Partial%20F%22%3A%20_f_stat_str%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Diagnostic%20Outcome%22%3A%20_decision%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%20%20%20%20_prev_ss_res%20%3D%20_ss_res%0A%0A%20%20%20%20_df_stepwise%20%3D%20pd.DataFrame(_rows)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20np%2C%20pd%2C%20r2_score)%3A%0A%20%20%20%20%23%20Example%202%3A%20Negative%20Adjusted%20R%5E2%20on%20Pure%20Noise%20(n%20%3D%2025%2C%20p%20%3D%2012)%0A%20%20%20%20np.random.seed(123)%0A%20%20%20%20_n_noise%20%3D%2025%0A%20%20%20%20_p_noise%20%3D%2012%0A%0A%20%20%20%20%23%20Completely%20independent%20target%20and%20noise%20predictors%0A%20%20%20%20_y_pure_noise%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n_noise)%0A%20%20%20%20_X_pure_noise%20%3D%20np.random.normal(0.0%2C%201.0%2C%20(_n_noise%2C%20_p_noise))%0A%0A%20%20%20%20_mod_noise%20%3D%20LinearRegression().fit(_X_pure_noise%2C%20_y_pure_noise)%0A%20%20%20%20_y_pred_noise%20%3D%20_mod_noise.predict(_X_pure_noise)%0A%0A%20%20%20%20_r2_noise%20%3D%20r2_score(_y_pure_noise%2C%20_y_pred_noise)%0A%20%20%20%20_df_res_noise%20%3D%20_n_noise%20-%20_p_noise%20-%201%0A%20%20%20%20_adj_r2_noise%20%3D%201.0%20-%20(1.0%20-%20_r2_noise)%20*%20(_n_noise%20-%201)%20%2F%20_df_res_noise%0A%0A%20%20%20%20_ss_res_noise%20%3D%20np.sum((_y_pure_noise%20-%20_y_pred_noise)%20**%202)%0A%20%20%20%20_ss_tot_noise%20%3D%20np.sum((_y_pure_noise%20-%20np.mean(_y_pure_noise))%20**%202)%0A%0A%20%20%20%20_df_noise_demo%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%22Metric%22%3A%20%22Sample%20Size%20(n)%22%2C%20%22Value%22%3A%20str(_n_noise)%2C%20%22Significance%22%3A%20%22Small%20experimental%20sample%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Metric%22%3A%20%22Number%20of%20Predictors%20(p)%22%2C%20%22Value%22%3A%20str(_p_noise)%2C%20%22Significance%22%3A%20%22Pure%20random%20Gaussian%20noise%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Metric%22%3A%20%22Residual%20Degrees%20of%20Freedom%20(n%20-%20p%20-%201)%22%2C%20%22Value%22%3A%20str(_df_res_noise)%2C%20%22Significance%22%3A%20%22Severe%20loss%20of%20degrees%20of%20freedom%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Metric%22%3A%20%22Raw%20R%5E2%22%2C%20%22Value%22%3A%20f%22%7B_r2_noise%3A.4f%7D%22%2C%20%22Significance%22%3A%20%22Spuriously%20claims%2040-60%25%20variance%20explained%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Metric%22%3A%20%22Adjusted%20R%5E2%22%2C%20%22Value%22%3A%20f%22%7B_adj_r2_noise%3A.4f%7D%22%2C%20%22Significance%22%3A%20%22Negative!%20Exposes%20that%20model%20is%20worse%20than%20y_bar%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Metric%22%3A%20%22MSE%20%2F%20MST%20Ratio%22%2C%20%22Value%22%3A%20f%22%7B(_ss_res_noise%20%2F%20_df_res_noise)%20%2F%20(_ss_tot_noise%20%2F%20(_n_noise%20-%201))%3A.4f%7D%22%2C%20%22Significance%22%3A%20%22Error%20variance%20exceeds%20target%20variance%22%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%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
b0a36d7363fee5b712e58b1e68bd6efd