import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%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%20Decide%20whether%20to%20adopt%20a%20polished%20ridge%20fit%0A%0A%20%20%20%20A%20validation-selected%20ridge%20model%20is%20a%20practical%20bilevel%20problem%3A%20the%0A%20%20%20%20upper%20problem%20chooses%20the%20regularization%20penalty%2C%20while%20the%20lower%20problem%0A%20%20%20%20fits%20the%20regression%20coefficients.%20An%20epsilon-continuation%20solve%20permits%20a%0A%20%20%20%20small%20amount%20of%20lower-level%20suboptimality%2C%20so%20its%20validation%20score%20can%20be%0A%20%20%20%20slightly%20optimistic.%0A%0A%20%20%20%20This%20example%20fixes%20the%20selected%20penalty%2C%20retrains%20the%20coefficients%20with%0A%20%20%20%20%60polish()%60%2C%20and%20compares%20lower-level%20feasibility%20with%20validation%0A%20%20%20%20performance%20before%20asking%20which%20candidate%20to%20place%20in%20the%20live%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20cvxpy%20as%20cp%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20from%20blvpy%20import%20BilevelProblem%2C%20LowerProblem%0A%0A%20%20%20%20_style_path%20%3D%20Path(__file__).resolve().parents%5B1%5D%20%2F%20%22_shared%22%20%2F%20%22zhlatex.mplstyle%22%0A%20%20%20%20plt.style.use(_style_path)%0A%20%20%20%20return%20BilevelProblem%2C%20LowerProblem%2C%20cp%2C%20mo%2C%20np%2C%20plt%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%23%20Validation%20selects%20the%20ridge%20penalty%0A%0A%20%20%20%20Let%20%24(X_%7B%5Cmathrm%7Btr%7D%7D%2Cy_%7B%5Cmathrm%7Btr%7D%7D)%24%20and%0A%20%20%20%20%24(X_%7B%5Cmathrm%7Bval%7D%7D%2Cy_%7B%5Cmathrm%7Bval%7D%7D)%24%20be%20training%20and%20validation%20data.%0A%20%20%20%20The%20upper%20problem%20chooses%20the%20penalty%20%24%5Clambda%24%20by%20validation%20error%2C%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bminimize%7D%7D_%7B%5Clambda%2Cw%7D%5Cquad%0A%20%20%20%20%20%20%5Cfrac%7B1%7D%7Bm_%7B%5Cmathrm%7Bval%7D%7D%7D%0A%20%20%20%20%20%20%5ClVert%20X_%7B%5Cmathrm%7Bval%7D%7Dw-y_%7B%5Cmathrm%7Bval%7D%7D%5CrVert_2%5E2%2C%0A%20%20%20%20%20%20%5Cqquad%2010%5E%7B-4%7D%5Cleq%5Clambda%5Cleq10%2C%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20while%20the%20coefficients%20must%20solve%20the%20lower%20training%20problem%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20w%5Cin%5Cmathop%7B%5Cmathrm%7Bargmin%7D%7D_v%5Cquad%0A%20%20%20%20%20%20%5Cfrac%7B1%7D%7Bm_%7B%5Cmathrm%7Btr%7D%7D%7D%0A%20%20%20%20%20%20%5ClVert%20X_%7B%5Cmathrm%7Btr%7D%7Dv-y_%7B%5Cmathrm%7Btr%7D%7D%5CrVert_2%5E2%0A%20%20%20%20%20%20%2B%5Clambda%5ClVert%20v%5CrVert_2%5E2.%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20The%20positive%20penalty%20makes%20the%20lower%20response%20unique.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(1907)%0A%20%20%20%20n_features%20%3D%208%0A%20%20%20%20n_training%20%3D%2024%0A%20%20%20%20n_validation%20%3D%2080%0A%0A%20%20%20%20true_coefficients%20%3D%20np.array(%5B1.6%2C%20-1.2%2C%200.8%2C%200.0%2C%20-0.5%2C%200.35%2C%200.0%2C%200.6%5D)%0A%20%20%20%20X_training%20%3D%20rng.normal(size%3D(n_training%2C%20n_features))%0A%20%20%20%20X_validation%20%3D%20rng.normal(size%3D(n_validation%2C%20n_features))%0A%20%20%20%20feature_scale%20%3D%20np.std(X_training%2C%20axis%3D0)%0A%20%20%20%20X_training%20%3D%20X_training%20%2F%20feature_scale%0A%20%20%20%20X_validation%20%3D%20X_validation%20%2F%20feature_scale%0A%20%20%20%20y_training%20%3D%20X_training%20%40%20true_coefficients%20%2B%20rng.normal(scale%3D1.1%2C%20size%3Dn_training)%0A%20%20%20%20y_validation%20%3D%20X_validation%20%40%20true_coefficients%20%2B%20rng.normal(scale%3D0.25%2C%20size%3Dn_validation)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20X_training%2C%0A%20%20%20%20%20%20%20%20X_validation%2C%0A%20%20%20%20%20%20%20%20n_features%2C%0A%20%20%20%20%20%20%20%20n_training%2C%0A%20%20%20%20%20%20%20%20n_validation%2C%0A%20%20%20%20%20%20%20%20y_training%2C%0A%20%20%20%20%20%20%20%20y_validation%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20BilevelProblem%2C%0A%20%20%20%20LowerProblem%2C%0A%20%20%20%20X_training%2C%0A%20%20%20%20X_validation%2C%0A%20%20%20%20cp%2C%0A%20%20%20%20n_features%2C%0A%20%20%20%20n_training%2C%0A%20%20%20%20n_validation%2C%0A%20%20%20%20y_training%2C%0A%20%20%20%20y_validation%2C%0A)%3A%0A%20%20%20%20penalty%20%3D%20cp.Variable(%0A%20%20%20%20%20%20%20%20nonneg%3DTrue%2C%0A%20%20%20%20%20%20%20%20bounds%3D%5B1e-4%2C%2010.0%5D%2C%0A%20%20%20%20%20%20%20%20name%3D%22ridge_penalty%22%2C%0A%20%20%20%20)%0A%20%20%20%20coefficients%20%3D%20cp.Variable(n_features%2C%20name%3D%22coefficients%22)%0A%0A%20%20%20%20_training_loss%20%3D%20cp.sum_squares(X_training%20%40%20coefficients%20-%20y_training)%20%2F%20n_training%0A%20%20%20%20lower_problem%20%3D%20LowerProblem(%0A%20%20%20%20%20%20%20%20cp.Minimize(_training_loss%20%2B%20penalty%20*%20cp.sum_squares(coefficients))%2C%0A%20%20%20%20%20%20%20%20parameters%3D%5Bpenalty%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_validation_loss%20%3D%20cp.sum_squares(X_validation%20%40%20coefficients%20-%20y_validation)%20%2F%20n_validation%0A%20%20%20%20problem%20%3D%20BilevelProblem(cp.Minimize(_validation_loss)%2C%20lower_problem)%0A%20%20%20%20return%20coefficients%2C%20penalty%2C%20problem%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%23%20Solve%20to%20a%20practical%20continuation%20target%0A%0A%20%20%20%20We%20retain%20BLVPY's%20default%0A%20%20%20%20%24%5Cepsilon_%7B%5Cmathrm%7Binitial%7D%7D%3D10%5E%7B-1%7D%24%20and%20continue%20to%0A%20%20%20%20%24%5Cepsilon_%7B%5Cmathrm%7Btarget%7D%7D%3D10%5E%7B-4%7D%24.%20The%20returned%0A%20%20%20%20coefficients%20are%20valid%20for%20the%20epsilon-relaxed%20formulation%2C%20but%20they%20need%0A%20%20%20%20not%20be%20the%20fixed-penalty%20ridge%20solution.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(coefficients%2C%20cp%2C%20np%2C%20penalty%2C%20problem)%3A%0A%20%20%20%20epsilon_target%20%3D%201e-4%0A%20%20%20%20result%20%3D%20problem.solve(%0A%20%20%20%20%20%20%20%20epsilon_target%3Depsilon_target%2C%0A%20%20%20%20%20%20%20%20solver%3Dcp.IPOPT%2C%0A%20%20%20%20%20%20%20%20verbose%3DTrue%2C%0A%20%20%20%20)%0A%0A%20%20%20%20assert%20result.succeeded%2C%20result.message%0A%0A%20%20%20%20result_penalty%20%3D%20float(np.asarray(result.variable_values%5Bpenalty%5D))%0A%20%20%20%20result_coefficients%20%3D%20np.asarray(result.variable_values%5Bcoefficients%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20result_validation_mse%20%3D%20float(result.objective)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20epsilon_target%2C%0A%20%20%20%20%20%20%20%20result%2C%0A%20%20%20%20%20%20%20%20result_coefficients%2C%0A%20%20%20%20%20%20%20%20result_penalty%2C%0A%20%20%20%20%20%20%20%20result_validation_mse%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%23%20Retrain%20at%20the%20selected%20penalty%0A%0A%20%20%20%20%60polish()%60%20fixes%20the%20upper%20variable%20at%20the%20result%20snapshot%20and%20solves%20the%0A%20%20%20%20canonical%20lower%20problem%20with%20zero%20continuation%20allowance.%20Clarabel%20returns%0A%20%20%20%20a%20new%20complete%20candidate%2C%20but%20%60polish()%60%20does%20not%20assign%20it%20to%20the%20CVXPY%0A%20%20%20%20variables.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20X_training%2C%0A%20%20%20%20X_validation%2C%0A%20%20%20%20coefficients%2C%0A%20%20%20%20cp%2C%0A%20%20%20%20n_training%2C%0A%20%20%20%20n_validation%2C%0A%20%20%20%20np%2C%0A%20%20%20%20penalty%2C%0A%20%20%20%20problem%2C%0A%20%20%20%20result%2C%0A%20%20%20%20result_coefficients%2C%0A%20%20%20%20result_penalty%2C%0A%20%20%20%20result_validation_mse%2C%0A%20%20%20%20y_training%2C%0A%20%20%20%20y_validation%2C%0A)%3A%0A%20%20%20%20polished%20%3D%20problem.polish(%0A%20%20%20%20%20%20%20%20result%2C%0A%20%20%20%20%20%20%20%20solver%3Dcp.CLARABEL%2C%0A%20%20%20%20%20%20%20%20verbose%3DTrue%2C%0A%20%20%20%20)%0A%0A%20%20%20%20polished_penalty%20%3D%20float(np.asarray(polished.variable_values%5Bpenalty%5D))%0A%20%20%20%20polished_coefficients%20%3D%20np.asarray(polished.variable_values%5Bcoefficients%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20polished_validation_mse%20%3D%20float(polished.objective)%0A%0A%20%20%20%20result_lower_objective%20%3D%20float(%0A%20%20%20%20%20%20%20%20np.sum(np.square(X_training%20%40%20result_coefficients%20-%20y_training))%20%2F%20n_training%0A%20%20%20%20%20%20%20%20%2B%20result_penalty%20*%20np.sum(np.square(result_coefficients))%0A%20%20%20%20)%0A%20%20%20%20polished_lower_objective%20%3D%20float(%0A%20%20%20%20%20%20%20%20np.sum(np.square(X_training%20%40%20polished_coefficients%20-%20y_training))%20%2F%20n_training%0A%20%20%20%20%20%20%20%20%2B%20polished_penalty%20*%20np.sum(np.square(polished_coefficients))%0A%20%20%20%20)%0A%20%20%20%20coefficient_change%20%3D%20float(np.linalg.norm(polished_coefficients%20-%20result_coefficients))%0A%0A%20%20%20%20_result_validation_check%20%3D%20float(%0A%20%20%20%20%20%20%20%20np.sum(np.square(X_validation%20%40%20result_coefficients%20-%20y_validation))%20%2F%20n_validation%0A%20%20%20%20)%0A%20%20%20%20_polished_validation_check%20%3D%20float(%0A%20%20%20%20%20%20%20%20np.sum(np.square(X_validation%20%40%20polished_coefficients%20-%20y_validation))%20%2F%20n_validation%0A%20%20%20%20)%0A%20%20%20%20np.testing.assert_allclose(result_validation_mse%2C%20_result_validation_check%2C%20atol%3D1e-10%2C%20rtol%3D0.0)%0A%20%20%20%20np.testing.assert_allclose(polished_validation_mse%2C%20_polished_validation_check%2C%20atol%3D1e-10%2C%20rtol%3D0.0)%0A%0A%20%20%20%20assert%20polished.feasible%0A%20%20%20%20assert%20polished_validation_mse%20%3E%20result_validation_mse%0A%20%20%20%20assert%20polished_lower_objective%20%3C%20result_lower_objective%0A%20%20%20%20np.testing.assert_allclose(polished_penalty%2C%20result_penalty%2C%20atol%3D1e-12%2C%20rtol%3D0.0)%0A%20%20%20%20polished_named_values%20%3D%20%7B%0A%20%20%20%20%20%20%20%20_variable.name()%3A%20np.array2string(np.asarray(_value)%2C%20precision%3D4%2C%20suppress_small%3DTrue)%0A%20%20%20%20%20%20%20%20for%20_variable%2C%20_value%20in%20polished.variable_values.items()%0A%20%20%20%20%7D%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20coefficient_change%2C%0A%20%20%20%20%20%20%20%20polished%2C%0A%20%20%20%20%20%20%20%20polished_lower_objective%2C%0A%20%20%20%20%20%20%20%20polished_named_values%2C%0A%20%20%20%20%20%20%20%20polished_penalty%2C%0A%20%20%20%20%20%20%20%20polished_validation_mse%2C%0A%20%20%20%20%20%20%20%20result_lower_objective%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20coefficient_change%2C%0A%20%20%20%20epsilon_target%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20polished%2C%0A%20%20%20%20polished_lower_objective%2C%0A%20%20%20%20polished_named_values%2C%0A%20%20%20%20polished_penalty%2C%0A%20%20%20%20polished_validation_mse%2C%0A%20%20%20%20result_lower_objective%2C%0A%20%20%20%20result_penalty%2C%0A%20%20%20%20result_validation_mse%2C%0A)%3A%0A%20%20%20%20_snapshots%20%3D%20%22%3Cbr%3E%22.join(f%22%60%7Bname%7D%60%3A%20%60%7Bvalue%7D%60%22%20for%20name%2C%20value%20in%20polished_named_values.items())%0A%20%20%20%20_result_row%20%3D%20(%0A%20%20%20%20%20%20%20%20f%22%7C%20%24%5C%5Cepsilon%3D%7Bepsilon_target%3A.0e%7D%24%20solve%20%7C%20%7Bresult_penalty%3A.6f%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20f%22%7Bresult_validation_mse%3A.6f%7D%20%7C%20%7Bresult_lower_objective%3A.6f%7D%20%7C%22%0A%20%20%20%20)%0A%20%20%20%20_polished_row%20%3D%20(%0A%20%20%20%20%20%20%20%20f%22%7C%20Polished%20%7C%20%7Bpolished_penalty%3A.6f%7D%20%7C%20%7Bpolished_validation_mse%3A.6f%7D%20%7C%20%7Bpolished_lower_objective%3A.6f%7D%20%7C%22%0A%20%20%20%20)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%20Compare%20the%20candidates%0A%0A%20%20%20%20%7C%20Candidate%20%7C%20Ridge%20penalty%20%7C%20Validation%20MSE%20%7C%20Lower%20ridge%20objective%20%7C%0A%20%20%20%20%7C---%7C---%3A%7C---%3A%7C---%3A%7C%0A%20%20%20%20%7B_result_row%7D%0A%20%20%20%20%7B_polished_row%7D%0A%0A%20%20%20%20The%20coefficient%20vector%20moves%20by%0A%20%20%20%20%24%5C%5ClVert%20w_%7B%7B%5C%5Cmathrm%7B%7Bpolished%7D%7D%7D%7D-w_%7B%7B%5C%5Cepsilon%7D%7D%5C%5CrVert_2%0A%20%20%20%20%3D%7Bcoefficient_change%3A.5f%7D%24%20while%20the%20penalty%20remains%20fixed.%20The%20lower%0A%20%20%20%20objective%20improves%20by%20about%0A%20%20%20%20%24%7Bresult_lower_objective%20-%20polished_lower_objective%3A.1e%7D%24%2C%20but%20validation%0A%20%20%20%20MSE%20increases%20by%0A%20%20%20%20%24%7B100.0%20*%20-polished.objective_improvement_ratio%3A.1f%7D%5C%5C%25%24.%0A%0A%20%20%20%20The%20immutable%20%60PolishResult%60%20exposes%20all%20the%20information%20needed%20for%20the%0A%20%20%20%20decision%3A%0A%0A%20%20%20%20-%20%60feasible%60%3A%20%60%7Bstr(polished.feasible).lower()%7D%60%0A%20%20%20%20-%20%60objective%60%3A%20%60%7Bpolished.objective%3A.6f%7D%60%0A%20%20%20%20-%20%60objective_improvement_ratio%60%3A%20%60%7Bpolished.objective_improvement_ratio%3A.6f%7D%60%0A%20%20%20%20-%20%60variable_values%60%3A%3Cbr%3E%7B_snapshots%7D%0A%0A%20%20%20%20The%20negative%20ratio%20means%20that%20the%20polished%20minimization%20objective%20is%0A%20%20%20%20worse.%20Its%20feasibility%20flag%20nevertheless%20confirms%20that%20the%20complete%0A%20%20%20%20fixed-penalty%20candidate%20passes%20BLVPY's%20standard%20checks.%20The%20live%20CVXPY%0A%20%20%20%20state%20still%20contains%20the%20epsilon-relaxed%20result%20at%20this%20point.%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%23%23%20Why%20the%20validation%20score%20gets%20worse%0A%0A%20%20%20%20For%20this%20unconstrained%20ridge%20problem%2C%20the%20fixed-penalty%20response%20can%20also%0A%20%20%20%20be%20computed%20from%20its%20linear%20optimality%20system.%20The%20curve%20below%20evaluates%0A%20%20%20%20validation%20MSE%20along%20those%20responses.%20The%20polished%20point%20lies%20on%20that%0A%20%20%20%20path%2C%20while%20the%20epsilon-relaxed%20point%20sits%20below%20it%20at%20the%20same%20selected%0A%20%20%20%20penalty.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20X_training%2C%0A%20%20%20%20X_validation%2C%0A%20%20%20%20n_features%2C%0A%20%20%20%20n_training%2C%0A%20%20%20%20n_validation%2C%0A%20%20%20%20np%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20polished_penalty%2C%0A%20%20%20%20polished_validation_mse%2C%0A%20%20%20%20result_penalty%2C%0A%20%20%20%20result_validation_mse%2C%0A%20%20%20%20y_training%2C%0A%20%20%20%20y_validation%2C%0A)%3A%0A%20%20%20%20_penalty_grid%20%3D%20np.geomspace(1e-4%2C%2010.0%2C%20160)%0A%20%20%20%20_gram%20%3D%20X_training.T%20%40%20X_training%20%2F%20n_training%0A%20%20%20%20_cross_product%20%3D%20X_training.T%20%40%20y_training%20%2F%20n_training%0A%20%20%20%20_identity%20%3D%20np.eye(n_features)%0A%20%20%20%20_path_validation_mse%20%3D%20%5B%5D%0A%20%20%20%20for%20_penalty%20in%20_penalty_grid%3A%0A%20%20%20%20%20%20%20%20_path_coefficients%20%3D%20np.linalg.solve(_gram%20%2B%20_penalty%20*%20_identity%2C%20_cross_product)%0A%20%20%20%20%20%20%20%20_path_validation_mse.append(np.sum(np.square(X_validation%20%40%20_path_coefficients%20-%20y_validation))%20%2F%20n_validation)%0A%0A%20%20%20%20_figure%2C%20_axis%20%3D%20plt.subplots(figsize%3D(7.0%2C%204.5))%0A%20%20%20%20_axis.semilogx(%0A%20%20%20%20%20%20%20%20_penalty_grid%2C%0A%20%20%20%20%20%20%20%20_path_validation_mse%2C%0A%20%20%20%20%20%20%20%20color%3D%220.25%22%2C%0A%20%20%20%20%20%20%20%20linewidth%3D2.0%2C%0A%20%20%20%20%20%20%20%20label%3D%22Fixed-response%20ridge%20path%22%2C%0A%20%20%20%20)%0A%20%20%20%20_axis.scatter(%0A%20%20%20%20%20%20%20%20%5Bresult_penalty%5D%2C%0A%20%20%20%20%20%20%20%20%5Bresult_validation_mse%5D%2C%0A%20%20%20%20%20%20%20%20color%3D%22tab%3Aorange%22%2C%0A%20%20%20%20%20%20%20%20marker%3D%22s%22%2C%0A%20%20%20%20%20%20%20%20s%3D75%2C%0A%20%20%20%20%20%20%20%20label%3Dr%22%24%5Cepsilon%24-relaxed%20result%22%2C%0A%20%20%20%20%20%20%20%20zorder%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20_axis.scatter(%0A%20%20%20%20%20%20%20%20%5Bpolished_penalty%5D%2C%0A%20%20%20%20%20%20%20%20%5Bpolished_validation_mse%5D%2C%0A%20%20%20%20%20%20%20%20color%3D%22tab%3Agreen%22%2C%0A%20%20%20%20%20%20%20%20marker%3D%22*%22%2C%0A%20%20%20%20%20%20%20%20s%3D140%2C%0A%20%20%20%20%20%20%20%20label%3D%22Polished%20candidate%22%2C%0A%20%20%20%20%20%20%20%20zorder%3D4%2C%0A%20%20%20%20)%0A%20%20%20%20_axis.set_xlabel(r%22Ridge%20penalty%20%24%5Clambda%24%22)%0A%20%20%20%20_axis.set_ylabel(%22Validation%20MSE%22)%0A%20%20%20%20_axis.legend(frameon%3DFalse)%0A%20%20%20%20_figure.tight_layout()%0A%20%20%20%20plt.show()%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%23%23%20Decide%20which%20candidate%20to%20keep%0A%0A%20%20%20%20Feasibility%20and%20upper-objective%20quality%20answer%20different%20questions.%20The%0A%20%20%20%20polished%20coefficients%20are%20consistent%20with%20the%20fitted%20ridge%20model%2C%20but%0A%20%20%20%20they%20cost%20about%202.7%25%20in%20validation%20MSE.%20Whether%20that%20consistency%20is%20worth%0A%20%20%20%20the%20degradation%20depends%20on%20the%20application.%0A%0A%20%20%20%20Keeping%20the%20default%20selection%20means%20knowingly%20accepting%20the%0A%20%20%20%20epsilon-relaxed%20result.%20If%20neither%20candidate%20is%20acceptable%2C%20rerun%20the%0A%20%20%20%20bilevel%20solve%20with%20a%20tighter%20epsilon%20target.%20Changing%20the%20selection%20below%0A%20%20%20%20explicitly%20assigns%20every%20variable%20from%20the%20chosen%20immutable%20snapshot.%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%20candidate_choice%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Keep%20epsilon-relaxed%20result%22%3A%20%22result%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Adopt%20polished%20candidate%22%3A%20%22polished%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22Keep%20epsilon-relaxed%20result%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Candidate%20to%20place%20in%20the%20model%22%2C%0A%20%20%20%20)%0A%20%20%20%20candidate_choice%0A%20%20%20%20return%20(candidate_choice%2C)%0A%0A%0A%40app.cell%0Adef%20_(candidate_choice%2C%20polished%2C%20result)%3A%0A%20%20%20%20selected_candidate%20%3D%20polished%20if%20candidate_choice.value%20%3D%3D%20%22polished%22%20else%20result%0A%20%20%20%20selected_name%20%3D%20%22Polished%20candidate%22%20if%20candidate_choice.value%20%3D%3D%20%22polished%22%20else%20%22Epsilon-relaxed%20result%22%0A%0A%20%20%20%20for%20_variable%2C%20_value%20in%20selected_candidate.variable_values.items()%3A%0A%20%20%20%20%20%20%20%20_variable.project_and_assign(_value)%0A%0A%20%20%20%20selected_objective%20%3D%20float(selected_candidate.objective)%0A%20%20%20%20return%20selected_name%2C%20selected_objective%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20selected_name%2C%20selected_objective)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20Current%20model%20state%0A%0A%20%20%20%20-%20Selected%20snapshot%3A%20**%7Bselected_name%7D**%0A%20%20%20%20-%20Validation%20MSE%3A%20**%7Bselected_objective%3A.6f%7D**%0A%0A%20%20%20%20This%20state%20changed%20only%20through%20the%20explicit%20%60project_and_assign%60%20loop%0A%20%20%20%20above.%20Toggling%20the%20selector%20assigns%20the%20other%20complete%20snapshot%20without%0A%20%20%20%20modifying%20either%20result%20object.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1c516bd4cae4c36491e686bc69b1e188