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%20Food%20Pricing%20with%20the%20Stigler%20Diet%0A%0A%20%20%20%20This%20example%20uses%20the%20Stigler%20diet%20dataset%20to%20study%20a%20bilevel%20pricing%0A%20%20%20%20problem%3A%20a%20merchant%20sets%20food%20markups%20and%20anticipates%20the%20consumer's%0A%20%20%20%20least-cost%20basket.%20A%20baseline%20linear%20program%20identifies%20the%20foods%20whose%0A%20%20%20%20prices%20the%20merchant%20may%20change%20and%20supplies%20the%20initial%20consumer%20basket.%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%20import%20csv%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%20plt.style.use(Path(__file__).resolve().parents%5B1%5D%20%2F%20%22_shared%22%20%2F%20%22zhlatex.mplstyle%22)%0A%20%20%20%20return%20BilevelProblem%2C%20LowerProblem%2C%20Path%2C%20cp%2C%20csv%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%20Data%20source%0A%0A%20%20%20%20The%20offline%20table%20is%20derived%20from%20the%2077%20foods%20and%20nine%20nutrient%20columns%0A%20%20%20%20in%20the%20pinned%0A%20%20%20%20%5BOR-Tools%20Stigler%20example%5D(https%3A%2F%2Fgithub.com%2Fgoogle%2For-tools%2Fblob%2F100f66e6242ab8bf8d32feb8f3bf086db66ae2b5%2Fortools%2Flinear_solver%2Fsamples%2Fstigler_diet.py).%0A%20%20%20%20Every%20nutrient%0A%20%20%20%20coefficient%20is%20normalized%20per%201939%20dollar%20spent%20on%20a%20food%3B%20the%20calorie%0A%20%20%20%20column%20is%20measured%20in%20*thousands*%20of%20kcal%20per%20dollar.%20The%20package%20units%0A%20%20%20%20are%20included%20for%20readable%20result%20labels.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20csv%2C%20np)%3A%0A%20%20%20%20data_path%20%3D%20Path(__file__).resolve().parents%5B1%5D%20%2F%20%22_shared%22%20%2F%20%22data%22%20%2F%20%22stigler_diet.csv%22%0A%20%20%20%20with%20data_path.open(encoding%3D%22utf-8%22%2C%20newline%3D%22%22)%20as%20data_file%3A%0A%20%20%20%20%20%20%20%20food_rows%20%3D%20list(csv.DictReader(data_file))%0A%0A%20%20%20%20nutrient_columns%20%3D%20(%0A%20%20%20%20%20%20%20%20%22calories_thousand_kcal_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22protein_g_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22calcium_g_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22iron_mg_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22vitamin_a_kiu_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22vitamin_b1_mg_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22vitamin_b2_mg_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22niacin_mg_per_dollar%22%2C%0A%20%20%20%20%20%20%20%20%22vitamin_c_mg_per_dollar%22%2C%0A%20%20%20%20)%0A%20%20%20%20nutrient_minimums%20%3D%20np.array(%5B3.0%2C%2070.0%2C%200.8%2C%2012.0%2C%205.0%2C%201.8%2C%202.7%2C%2018.0%2C%2075.0%5D)%0A%20%20%20%20nutrient_matrix%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5B%5Bfloat(food_row%5Bcolumn%5D)%20for%20food_row%20in%20food_rows%5D%20for%20column%20in%20nutrient_columns%5D%2C%0A%20%20%20%20%20%20%20%20dtype%3Dfloat%2C%0A%20%20%20%20)%0A%20%20%20%20food_names%20%3D%20np.array(%5Bfood_row%5B%22food%22%5D%20for%20food_row%20in%20food_rows%5D%2C%20dtype%3Dobject)%0A%20%20%20%20food_units%20%3D%20np.array(%5Bfood_row%5B%22unit%22%5D%20for%20food_row%20in%20food_rows%5D%2C%20dtype%3Dobject)%0A%0A%20%20%20%20assert%20food_names.size%20%3D%3D%2077%0A%20%20%20%20assert%20nutrient_matrix.shape%20%3D%3D%20(9%2C%2077)%0A%20%20%20%20assert%20np.all(np.isfinite(nutrient_matrix))%0A%20%20%20%20return%20food_names%2C%20food_units%2C%20nutrient_matrix%2C%20nutrient_minimums%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%20Baseline%20least-cost%20basket%0A%0A%20%20%20%20Let%20%24x_i%5Cgeq0%24%20be%20daily%20dollars%20spent%20on%20food%20%24i%24%2C%20let%0A%20%20%20%20%24A%5Cin%5Cmathbf%7BR%7D%5E%7B9%5Ctimes77%7D%24%20contain%20nutrient%20quantities%20per%20dollar%2C%20and%0A%20%20%20%20let%20%24b%24%20contain%20the%20daily%20nutrient%20minimums.%20The%20baseline%20problem%20is%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bminimize%7D%7D_x%20%26%20%5Cmathbf%7B1%7D%5ETx%20%5C%5C%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bsubject%5C%20to%7D%7D%20%26%20Ax%5Csucceq%20b%2C%5Cquad%20x%5Csucceq0.%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20We%20solve%20it%20independently%20with%20CVXPY.%20Its%20active%20foods%20define%20the%20set%0A%20%20%20%20of%20prices%20available%20to%20the%20merchant%2C%20and%20its%20solution%20initializes%20the%0A%20%20%20%20consumer%20basket%20in%20the%20bilevel%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(cp%2C%20food_names%2C%20np%2C%20nutrient_matrix%2C%20nutrient_minimums)%3A%0A%20%20%20%20number_of_foods%20%3D%20food_names.size%0A%20%20%20%20baseline_variable%20%3D%20cp.Variable(number_of_foods%2C%20nonneg%3DTrue%2C%20name%3D%22baseline_food_spending%22)%0A%20%20%20%20baseline_problem%20%3D%20cp.Problem(%0A%20%20%20%20%20%20%20%20cp.Minimize(cp.sum(baseline_variable))%2C%0A%20%20%20%20%20%20%20%20%5Bnutrient_matrix%20%40%20baseline_variable%20%3E%3D%20nutrient_minimums%5D%2C%0A%20%20%20%20)%0A%20%20%20%20baseline_problem.solve(%0A%20%20%20%20%20%20%20%20solver%3Dcp.CLARABEL%2C%0A%20%20%20%20%20%20%20%20tol_gap_abs%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_gap_rel%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_feas%3D1e-10%2C%0A%20%20%20%20)%0A%20%20%20%20baseline_food_spending%20%3D%20np.asarray(baseline_variable.value%2C%20dtype%3Dfloat)%0A%20%20%20%20baseline_annual_cost%20%3D%20365.0%20*%20float(baseline_problem.value)%0A%20%20%20%20baseline_active_indices%20%3D%20np.flatnonzero(baseline_food_spending%20%3E%201e-7)%0A%0A%20%20%20%20assert%20baseline_problem.status%20%3D%3D%20cp.OPTIMAL%0A%20%20%20%20assert%20np.min(nutrient_matrix%20%40%20baseline_food_spending%20-%20nutrient_minimums)%20%3E%3D%20-1e-8%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20baseline_active_indices%2C%0A%20%20%20%20%20%20%20%20baseline_annual_cost%2C%0A%20%20%20%20%20%20%20%20baseline_food_spending%2C%0A%20%20%20%20%20%20%20%20number_of_foods%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20baseline_active_indices%2C%0A%20%20%20%20baseline_annual_cost%2C%0A%20%20%20%20baseline_food_spending%2C%0A%20%20%20%20food_names%2C%0A%20%20%20%20food_units%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20baseline_rows%20%3D%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f%22%7C%20%7Bfood_names%5Bindex%5D%7D%20%7C%20%7Bfood_units%5Bindex%5D%7D%20%7C%20%24%7B365.0%20*%20baseline_food_spending%5Bindex%5D%3A.6f%7D%20%7C%22%0A%20%20%20%20%20%20%20%20for%20index%20in%20baseline_active_indices%0A%20%20%20%20)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%20Baseline%20basket%0A%0A%20%20%20%20%7C%20active%20food%20%7C%20package%20unit%20%7C%20annual%20expenditure%20%7C%0A%20%20%20%20%7C%20---%20%7C%20---%20%7C%20---%3A%20%7C%0A%20%20%20%20%7Bbaseline_rows%7D%0A%0A%20%20%20%20The%20independently%20computed%20baseline%20costs%20**%24%7Bbaseline_annual_cost%3A.7f%7D%0A%20%20%20%20per%20year**.%20The%20active%20foods%20above%20define%20the%20markup%20index%20set%20%24I%24%20used%0A%20%20%20%20below.%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%20Bilevel%20pricing%20model%0A%0A%20%20%20%20Let%20%24I%24%20index%20the%20foods%20in%20the%20baseline%20basket%20and%20let%0A%20%20%20%20%24m%5Cin%5Cmathbf%7BR%7D%5E%7B%7CI%7C%7D%24%20be%20their%20fractional%20markups.%20A%20selection%20matrix%0A%20%20%20%20%24P%5Cin%5Cmathbf%7BR%7D%5E%7B77%5Ctimes%7CI%7C%7D%24%20inserts%20those%20markups%20into%20the%20full%20price%0A%20%20%20%20vector.%20The%20consumer%20solves%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20S(m)%3D%5Cmathop%7B%5Cmathrm%7Bargmin%7D%7D_x%0A%20%20%20%20%20%20%26%20(%5Cmathbf%7B1%7D%2BPm)%5ETx%20%5C%5C%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bsubject%5C%20to%7D%7D%0A%20%20%20%20%20%20%26%20Ax%5Csucceq%20b%2C%20%5C%5C%0A%20%20%20%20%20%20%26%20x%5Csucceq0.%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20Anticipating%20that%20response%2C%20the%20merchant%20solves%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bminimize%7D%7D_%7Bm%2Cx%7D%0A%20%20%20%20%20%20%26%20-m%5ETx_I%2B0.005%5ClVert%20m%5CrVert_2%5E2%20%5C%5C%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bsubject%5C%20to%7D%7D%0A%20%20%20%20%20%20%26%200%5Cpreceq%20m%5Cpreceq%5Cmathbf%7B1%7D%2C%5Cquad%20x%5Cin%20S(m).%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20Here%20%24m%5ETx_I%24%20is%20daily%20markup%20revenue%20measured%20in%201939%20dollars.%20The%20small%0A%20%20%20%20quadratic%20term%20regularizes%20the%20markup%20vector.%20BLVPY%20uses%20optimistic%0A%20%20%20%20semantics%2C%20so%20the%20returned%20basket%20is%20the%20merchant-favorable%20response%20among%0A%20%20%20%20those%20within%20the%20final%20lower-level%20tolerance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20BilevelProblem%2C%0A%20%20%20%20LowerProblem%2C%0A%20%20%20%20baseline_active_indices%2C%0A%20%20%20%20baseline_food_spending%2C%0A%20%20%20%20cp%2C%0A%20%20%20%20np%2C%0A%20%20%20%20number_of_foods%2C%0A%20%20%20%20nutrient_matrix%2C%0A%20%20%20%20nutrient_minimums%2C%0A)%3A%0A%20%20%20%20number_of_markups%20%3D%20baseline_active_indices.size%0A%20%20%20%20selection_matrix%20%3D%20np.zeros((number_of_foods%2C%20number_of_markups))%0A%20%20%20%20selection_matrix%5Bbaseline_active_indices%2C%20np.arange(number_of_markups)%5D%20%3D%201.0%0A%0A%20%20%20%20markup%20%3D%20cp.Variable(number_of_markups%2C%20name%3D%22markup%22)%0A%20%20%20%20food_spending%20%3D%20cp.Variable(number_of_foods%2C%20nonneg%3DTrue%2C%20name%3D%22food_spending%22)%0A%20%20%20%20markup.value%20%3D%20np.full(number_of_markups%2C%200.2)%0A%20%20%20%20food_spending.value%20%3D%20baseline_food_spending%0A%0A%20%20%20%20consumer_price%20%3D%201.0%20%2B%20selection_matrix%20%40%20markup%0A%20%20%20%20consumer_problem%20%3D%20LowerProblem(%0A%20%20%20%20%20%20%20%20cp.Minimize(consumer_price%20%40%20food_spending)%2C%0A%20%20%20%20%20%20%20%20%5Bnutrient_matrix%20%40%20food_spending%20%3E%3D%20nutrient_minimums%5D%2C%0A%20%20%20%20%20%20%20%20parameters%3D%5Bmarkup%5D%2C%0A%20%20%20%20)%0A%20%20%20%20merchant_problem%20%3D%20BilevelProblem(%0A%20%20%20%20%20%20%20%20cp.Minimize(-markup%20%40%20food_spending%5Bbaseline_active_indices%5D%20%2B%200.005%20*%20cp.sum_squares(markup))%2C%0A%20%20%20%20%20%20%20%20consumer_problem%2C%0A%20%20%20%20%20%20%20%20upper_constraints%3D%5Bmarkup%20%3E%3D%200.0%2C%20markup%20%3C%3D%201.0%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20merchant_problem.validate()%0A%20%20%20%20return%20food_spending%2C%20markup%2C%20merchant_problem%2C%20selection_matrix%0A%0A%0A%40app.cell%0Adef%20_(merchant_problem)%3A%0A%20%20%20%20result%20%3D%20merchant_problem.solve(%0A%20%20%20%20%20%20%20%20epsilon_initial%3D1e-2%2C%0A%20%20%20%20%20%20%20%20epsilon_target%3D1e-5%2C%0A%20%20%20%20%20%20%20%20solver_options%3D%7B%22max_iter%22%3A%201000%7D%2C%0A%20%20%20%20%20%20%20%20verbose%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20diagnostics%20%3D%20merchant_problem.gap_diagnostics(result)%0A%20%20%20%20return%20diagnostics%2C%20result%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%20Independent%20response%20checks%0A%0A%20%20%20%20At%20the%20selected%20markups%2C%20we%20solve%20the%20consumer%20LP%20again%20with%20CVXPY.%20We%0A%20%20%20%20also%20maximize%20markup%20revenue%20over%20baskets%20whose%20marked%20cost%20is%20within%20the%0A%20%20%20%20final%20epsilon%20of%20that%20exact%20lower%20optimum.%20The%20second%20LP%20checks%20BLVPY's%0A%20%20%20%20optimistic%20response%20directly%20rather%20than%20assuming%20a%20unique%20basket.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20baseline_active_indices%2C%0A%20%20%20%20cp%2C%0A%20%20%20%20food_spending%2C%0A%20%20%20%20markup%2C%0A%20%20%20%20np%2C%0A%20%20%20%20number_of_foods%2C%0A%20%20%20%20nutrient_matrix%2C%0A%20%20%20%20nutrient_minimums%2C%0A%20%20%20%20result%2C%0A%20%20%20%20selection_matrix%2C%0A)%3A%0A%20%20%20%20optimized_markup%20%3D%20np.asarray(markup.value%2C%20dtype%3Dfloat)%0A%20%20%20%20optimized_food_spending%20%3D%20np.asarray(food_spending.value%2C%20dtype%3Dfloat)%0A%20%20%20%20marked_price%20%3D%201.0%20%2B%20selection_matrix%20%40%20optimized_markup%0A%0A%20%20%20%20fixed_response_variable%20%3D%20cp.Variable(number_of_foods%2C%20nonneg%3DTrue%2C%20name%3D%22fixed_markup_response%22)%0A%20%20%20%20fixed_response_problem%20%3D%20cp.Problem(%0A%20%20%20%20%20%20%20%20cp.Minimize(marked_price%20%40%20fixed_response_variable)%2C%0A%20%20%20%20%20%20%20%20%5Bnutrient_matrix%20%40%20fixed_response_variable%20%3E%3D%20nutrient_minimums%5D%2C%0A%20%20%20%20)%0A%20%20%20%20fixed_response_problem.solve(%0A%20%20%20%20%20%20%20%20solver%3Dcp.CLARABEL%2C%0A%20%20%20%20%20%20%20%20tol_gap_abs%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_gap_rel%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_feas%3D1e-10%2C%0A%20%20%20%20)%0A%20%20%20%20fixed_optimal_cost%20%3D%20float(fixed_response_problem.value)%0A%20%20%20%20returned_consumer_cost%20%3D%20float(marked_price%20%40%20optimized_food_spending)%0A%0A%20%20%20%20optimistic_variable%20%3D%20cp.Variable(number_of_foods%2C%20nonneg%3DTrue%2C%20name%3D%22optimistic_response%22)%0A%20%20%20%20optimistic_problem%20%3D%20cp.Problem(%0A%20%20%20%20%20%20%20%20cp.Maximize(optimized_markup%20%40%20optimistic_variable%5Bbaseline_active_indices%5D)%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20nutrient_matrix%20%40%20optimistic_variable%20%3E%3D%20nutrient_minimums%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marked_price%20%40%20optimistic_variable%20%3C%3D%20fixed_optimal_cost%20%2B%20result.final_epsilon%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%20%20%20%20optimistic_problem.solve(%0A%20%20%20%20%20%20%20%20solver%3Dcp.CLARABEL%2C%0A%20%20%20%20%20%20%20%20tol_gap_abs%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_gap_rel%3D1e-10%2C%0A%20%20%20%20%20%20%20%20tol_feas%3D1e-10%2C%0A%20%20%20%20)%0A%0A%20%20%20%20selected_daily_revenue%20%3D%20float(optimized_markup%20%40%20optimized_food_spending%5Bbaseline_active_indices%5D)%0A%20%20%20%20optimistic_daily_revenue%20%3D%20float(optimistic_problem.value)%0A%20%20%20%20response_cost_gap%20%3D%20returned_consumer_cost%20-%20fixed_optimal_cost%0A%20%20%20%20optimistic_revenue_gap%20%3D%20optimistic_daily_revenue%20-%20selected_daily_revenue%0A%0A%20%20%20%20assert%20result.succeeded%2C%20result.message%0A%20%20%20%20assert%20fixed_response_problem.status%20%3D%3D%20cp.OPTIMAL%0A%20%20%20%20assert%20optimistic_problem.status%20%3D%3D%20cp.OPTIMAL%0A%20%20%20%20assert%20-1e-7%20%3C%3D%20response_cost_gap%20%3C%3D%20result.final_epsilon%20%2B%201e-7%0A%20%20%20%20assert%20-1e-7%20%3C%3D%20optimistic_revenue_gap%20%3C%3D%202.0%20*%20result.final_epsilon%0A%20%20%20%20assert%20np.min(nutrient_matrix%20%40%20optimized_food_spending%20-%20nutrient_minimums)%20%3E%3D%20-1e-6%0A%20%20%20%20assert%20np.all((optimized_markup%20%3E%3D%20-1e-8)%20%26%20(optimized_markup%20%3C%3D%201.0%20%2B%201e-8))%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20marked_price%2C%0A%20%20%20%20%20%20%20%20optimistic_revenue_gap%2C%0A%20%20%20%20%20%20%20%20optimized_food_spending%2C%0A%20%20%20%20%20%20%20%20optimized_markup%2C%0A%20%20%20%20%20%20%20%20response_cost_gap%2C%0A%20%20%20%20%20%20%20%20selected_daily_revenue%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20diagnostics%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20optimistic_revenue_gap%2C%0A%20%20%20%20response_cost_gap%2C%0A%20%20%20%20result%2C%0A%20%20%20%20selected_daily_revenue%2C%0A)%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%20Pricing%20result%0A%0A%20%20%20%20%7C%20quantity%20%7C%20value%20%7C%0A%20%20%20%20%7C%20---%20%7C%20---%3A%20%7C%0A%20%20%20%20%7C%20status%20%7C%20%60%7Bresult.status%7D%60%20%7C%0A%20%20%20%20%7C%20annual%20markup%20revenue%20%7C%20%24%7B365.0%20*%20selected_daily_revenue%3A.6f%7D%20%7C%0A%20%20%20%20%7C%20upper%20objective%20%7C%20%7Bresult.objective%3A.8f%7D%20%7C%0A%20%20%20%20%7C%20final%20epsilon%20%7C%20%7Bresult.final_epsilon%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20maximum%20lifted%20residual%20%7C%20%7Bresult.residuals.max_violation%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20signed%20source%20gap%20%7C%20%7Bdiagnostics.source_gap%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20fixed-markup%20consumer%20cost%20gap%20%7C%20%7Bresponse_cost_gap%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20optimistic%20revenue%20check%20gap%20%7C%20%7Boptimistic_revenue_gap%3A.3e%7D%20%7C%0A%0A%20%20%20%20The%20merchant%20raises%20prices%20on%20the%20baseline%20basket%20and%20induces%20a%20small%0A%20%20%20%20substitution%20toward%20evaporated%20milk.%20The%20revenue%20shown%20is%20attached%20to%20an%0A%20%20%20%20epsilon-optimal%2C%20leader-favorable%20consumer%20response%3B%20it%20should%20not%20be%20read%0A%20%20%20%20as%20the%20outcome%20of%20an%20exact%20tie-breaking%20rule.%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%20Markups%20and%20basket%20substitution%0A%0A%20%20%20%20The%20left%20panel%20reports%20the%20optimized%20fractional%20markups.%20The%20right%0A%20%20%20%20panel%20compares%20annual%20contributions%20to%20the%20baseline%20basket%20cost%20with%0A%20%20%20%20contributions%20to%20the%20marked-price%20basket.%20It%20includes%20every%20food%20used%20by%0A%20%20%20%20either%20solution%2C%20making%20the%20substitution%20visible.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20baseline_active_indices%2C%0A%20%20%20%20baseline_food_spending%2C%0A%20%20%20%20food_names%2C%0A%20%20%20%20marked_price%2C%0A%20%20%20%20np%2C%0A%20%20%20%20optimized_food_spending%2C%0A)%3A%0A%20%20%20%20basket_indices%20%3D%20np.flatnonzero((baseline_food_spending%20%3E%201e-7)%20%7C%20(optimized_food_spending%20%3E%201e-7))%0A%20%20%20%20display_name_map%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Wheat%20Flour%20(Enriched)%22%3A%20%22Wheat%20flour%22%2C%0A%20%20%20%20%20%20%20%20%22Evaporated%20Milk%20(can)%22%3A%20%22Evaporated%20milk%22%2C%0A%20%20%20%20%20%20%20%20%22Liver%20(Beef)%22%3A%20%22Beef%20liver%22%2C%0A%20%20%20%20%20%20%20%20%22Navy%20Beans%2C%20Dried%22%3A%20%22Navy%20beans%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20basket_labels%20%3D%20%5Bdisplay_name_map.get(food_names%5Bindex%5D%2C%20food_names%5Bindex%5D)%20for%20index%20in%20basket_indices%5D%0A%20%20%20%20markup_labels%20%3D%20%5Bdisplay_name_map.get(food_names%5Bindex%5D%2C%20food_names%5Bindex%5D)%20for%20index%20in%20baseline_active_indices%5D%0A%20%20%20%20baseline_annual_contributions%20%3D%20365.0%20*%20baseline_food_spending%5Bbasket_indices%5D%0A%20%20%20%20optimized_annual_contributions%20%3D%20365.0%20*%20marked_price%5Bbasket_indices%5D%20*%20optimized_food_spending%5Bbasket_indices%5D%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20baseline_annual_contributions%2C%0A%20%20%20%20%20%20%20%20basket_labels%2C%0A%20%20%20%20%20%20%20%20markup_labels%2C%0A%20%20%20%20%20%20%20%20optimized_annual_contributions%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20Path%2C%0A%20%20%20%20baseline_annual_contributions%2C%0A%20%20%20%20basket_labels%2C%0A%20%20%20%20markup_labels%2C%0A%20%20%20%20np%2C%0A%20%20%20%20optimized_annual_contributions%2C%0A%20%20%20%20optimized_markup%2C%0A%20%20%20%20plt%2C%0A)%3A%0A%20%20%20%20figure_dir%20%3D%20Path(__file__).resolve().parent%20%2F%20%22figures%22%0A%20%20%20%20figure_dir.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%0A%20%20%20%20fig%2C%20(markup_axis%2C%20basket_axis)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(9.5%2C%204.8))%0A%20%20%20%20markup_positions%20%3D%20np.arange(len(markup_labels))%0A%20%20%20%20markup_axis.barh(markup_positions%2C%20100.0%20*%20optimized_markup%2C%20color%3D%22C3%22%2C%20alpha%3D0.85)%0A%20%20%20%20markup_axis.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Markup%20(percent)%22%2C%0A%20%20%20%20%20%20%20%20yticks%3Dmarkup_positions%2C%0A%20%20%20%20%20%20%20%20yticklabels%3Dmarkup_labels%2C%0A%20%20%20%20)%0A%20%20%20%20markup_axis.invert_yaxis()%0A%0A%20%20%20%20basket_positions%20%3D%20np.arange(len(basket_labels))%0A%20%20%20%20bar_width%20%3D%200.38%0A%20%20%20%20basket_axis.barh(%0A%20%20%20%20%20%20%20%20basket_positions%20-%20bar_width%20%2F%202.0%2C%0A%20%20%20%20%20%20%20%20baseline_annual_contributions%2C%0A%20%20%20%20%20%20%20%20height%3Dbar_width%2C%0A%20%20%20%20%20%20%20%20label%3D%22Baseline%22%2C%0A%20%20%20%20%20%20%20%20color%3D%22grey%22%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.8%2C%0A%20%20%20%20)%0A%20%20%20%20basket_axis.barh(%0A%20%20%20%20%20%20%20%20basket_positions%20%2B%20bar_width%20%2F%202.0%2C%0A%20%20%20%20%20%20%20%20optimized_annual_contributions%2C%0A%20%20%20%20%20%20%20%20height%3Dbar_width%2C%0A%20%20%20%20%20%20%20%20label%3D%22Marked%20price%22%2C%0A%20%20%20%20%20%20%20%20color%3D%22C2%22%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.85%2C%0A%20%20%20%20)%0A%20%20%20%20basket_axis.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Annual%20basket%20contribution%20(1939%20dollars)%22%2C%0A%20%20%20%20%20%20%20%20yticks%3Dbasket_positions%2C%0A%20%20%20%20%20%20%20%20yticklabels%3Dbasket_labels%2C%0A%20%20%20%20)%0A%20%20%20%20basket_axis.invert_yaxis()%0A%20%20%20%20basket_axis.legend(frameon%3DFalse%2C%20fontsize%3D12)%0A%0A%20%20%20%20fig.tight_layout()%0A%20%20%20%20figure_path%20%3D%20figure_dir%20%2F%20%22stigler_food_pricing.pdf%22%0A%20%20%20%20fig.savefig(figure_path%2C%20bbox_inches%3D%22tight%22)%0A%20%20%20%20plt.show()%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
0ac734cbbc8df019b98e8e482aed7bf1