import%20marimo%0A%0A__generated_with%20%3D%20%220.23.11%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Parameter%20Significance%20-%20Hawk%2FDove%20with%20Evolving%20Risk-Attitudes%22%2C%0A%20%20%20%20html_head_file%3D%22..%2Fdocs_head.html%22%2C%0A)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20simulatingrisk.doc_utils%20import%20docs_header%0A%20%20%20%20from%20simulatingrisk.hawkdovemulti.batch_run%20import%20params%0A%0A%20%20%20%20docs_header()%0A%20%20%20%20return%20(params%2C)%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%20Parameter%20significance%20for%20Hawk%2FDove%20with%20Evolving%20Risk-Attitudes%0A%0A%20%20%20%20To%20determine%20which%20parameters%20have%20an%20impact%20on%20the%20risk-attitudes%20in%20the%20population%20when%20the%20simulation%20converges%2C%20and%20to%20test%20the%20robustness%20of%20our%20model%2C%20we%20calculate%20the%20correlation%20between%20the%20varied%20simulation%20parameters%20and%20the%20percent%20of%20the%20population%20that%20is%20risk-seeking%2C%20risk-moderate%2C%20and%20risk-averse%20at%20the%20end%20of%20the%20simulation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20polars%20import%20col%20as%20c%20%20%23%20for%20short-hand%20column%20references%0A%20%20%20%20import%20altair%20as%20alt%0A%0A%20%20%20%20data_dir%20%3D%20Path(%22data%2Fdefault%2F%22)%0A%20%20%20%20return%20alt%2C%20c%2C%20data_dir%2C%20mo%2C%20pl%0A%0A%0A%40app.cell%0Adef%20_(data_dir%2C%20pl)%3A%0A%20%20%20%20df%20%3D%20pl.scan_csv(%5Bstr(file)%20for%20file%20in%20data_dir.glob(%22*_model.csv%22)%5D).collect()%0A%20%20%20%20return%20(df%2C)%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%20Calculate%20percentages%20for%20each%20group%20of%20risk%20attitudes%3A%0A%0A%20%20%20%20-%20Risk-Seeking%20%3A%20r%20%3D%200%2C%201%2C%202%0A%20%20%20%20-%20Risk-Moderate%20%3A%20r%20%3D%203%2C%204%2C%205%2C%206%0A%20%20%20%20-%20Risk-Averse%20%3A%20r%20%3D%207%2C%208%2C%209%0A%0A%20%20%20%20Then%20generate%20and%20graph%20a%20correlation%20matrix%20for%20the%20starting%20simulation%20parameters%20and%20those%20percentages.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(c%2C%20df)%3A%0A%20%20%20%20%23%20for%20each%20simulation%20run%2C%20calculate%20what%20percent%20of%20the%20population%20was%20risk%20seeking%2Fmoderate%2Faverse%0A%20%20%20%20%23%20after%20all%20adjustments%20were%20made%0A%0A%20%20%20%20%23%20limit%20to%20converged%20simulations%20only%0A%20%20%20%20agent_risk_pcts_df%20%3D%20(%0A%20%20%20%20%20%20%20%20df.filter(status%3D%22converged%22)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20total_r0%3Dc.total_r0.fill_null(strategy%3D%22zero%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20total_r9%3Dc.total_r9.fill_null(strategy%3D%22zero%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20r0%20%2B%20r1%20%2B%20r2%0A%20%20%20%20%20%20%20%20%20%20%20%20pct_risk_inclined%3D(c.total_r0.add(c.total_r1.add(c.total_r2))).truediv(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.total_agents%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20r3%20%2B%20r4%20%2B%20r5%20%2B%20r6%0A%20%20%20%20%20%20%20%20%20%20%20%20pct_risk_moderate%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.total_r3.add(c.total_r4).add(c.total_r5).add(c.total_r6)%0A%20%20%20%20%20%20%20%20%20%20%20%20).truediv(c.total_agents)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20r7%20%2B%20r8%20%2B%20r9%0A%20%20%20%20%20%20%20%20%20%20%20%20pct_risk_avoidant%3D(c.total_r7.add(c.total_r8).add(c.total_r9)).truediv(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.total_agents%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20add%20total%20to%20make%20sure%20everything%20adds%20up%20correctly%0A%20%20%20%20%20%20%20%20%20%20%20%20total_pct%3Dc.pct_risk_inclined.add(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c.pct_risk_moderate.add(c.pct_risk_avoidant)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20convert%20categorical%20variables%20into%20multiple%20true%2Ffalse%20categories%0A%20%20%20%20agent_risk_pcts_df%20%3D%20agent_risk_pcts_df.to_dummies(%22risk_distribution%22).to_dummies(%0A%20%20%20%20%20%20%20%20%22adjust_payoff%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20agent_risk_pcts_df%0A%20%20%20%20return%20(agent_risk_pcts_df%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(agent_risk_pcts_df%2C%20params%2C%20pl)%3A%0A%0A%20%20%20%20%23%20parameters%20we%20are%20interested%20in%20testing%20correlation%0A%20%20%20%20corr_variables%20%3D%20%5B%22pct_risk_inclined%22%2C%20%22pct_risk_moderate%22%2C%20%22pct_risk_avoidant%22%5D%0A%0A%20%20%20%20param_variables%20%3D%20%5B%0A%20%20%20%20%20%20%20%20p%0A%20%20%20%20%20%20%20%20for%20p%2C%20val%20in%20params%5B%22default%22%5D.items()%0A%20%20%20%20%20%20%20%20%23%20omit%20categoricals%20we%20have%20converted%20and%20single-value%20params%20(no%20variance)%0A%20%20%20%20%20%20%20%20if%20p%20not%20in%20%5B%22risk_distribution%22%2C%20%22adjust_payoff%22%5D%0A%20%20%20%20%20%20%20%20and%20isinstance(val%2C%20list)%0A%20%20%20%20%20%20%20%20and%20len(val)%20%3E%201%0A%20%20%20%20%5D%0A%20%20%20%20param_variables.extend(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20c%20in%20agent_risk_pcts_df.columns%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20c.startswith(%22risk_distribution_%22)%20or%20c.startswith(%22adjust_payoff%22)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20df_corr%20%3D%20agent_risk_pcts_df.select(*corr_variables%2C%20*param_variables).corr()%0A%0A%20%20%20%20%23%20add%20row%20labels%20and%20order%20them%20first%0A%20%20%20%20df_corr%20%3D%20df_corr.with_columns(pl.Series(%22var_x%22%2C%20df_corr.columns)).select(%0A%20%20%20%20%20%20%20%20%22var_x%22%2C%20*df_corr.columns%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20df_corr%0A%20%20%20%20return%20corr_variables%2C%20df_corr%2C%20param_variables%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(c%2C%20corr_variables%2C%20df_corr%2C%20param_variables%2C%20pl)%3A%0A%20%20%20%20def%20param_group(param_name)%3A%0A%20%20%20%20%20%20%20%20%23%20add%20group%20names%20to%20parameters%20so%20they%20can%20be%20displayed%20in%20logical%20groups%0A%20%20%20%20%20%20%20%20if%20param_name.startswith(%22risk_distribution%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22risk%20distribution%22%0A%20%20%20%20%20%20%20%20if%20param_name.endswith(%22neighborhood%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22neighborhood%20size%22%0A%20%20%20%20%20%20%20%20if%20param_name.startswith(%22adjust_payoff%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22adjust%20payoff%22%0A%20%20%20%20%20%20%20%20return%20%22%22%0A%0A%20%20%20%20df_corr_long%20%3D%20(%0A%20%20%20%20%20%20%20%20df_corr.unpivot(%0A%20%20%20%20%20%20%20%20%20%20%20%20on%3Ddf_corr.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20index%3D%22var_x%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20variable_name%3D%22var_y%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20value_name%3D%22correlation%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.filter(c.var_y.is_in(corr_variables))%0A%20%20%20%20%20%20%20%20.filter(c.var_x.is_in(param_variables))%0A%20%20%20%20%20%20%20%20.with_columns(%20%20%23%20something%20is%20slightly%20off%20with%20the%20unpivot%20that%20we%20need%20this...%0A%20%20%20%20%20%20%20%20%20%20%20%20correlation%3Dc.correlation.cast(pl.Float32)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20add%20formatted%20correlation%20number%20for%20display%0A%20%20%20%20%20%20%20%20%20%20%20%20correlation_str%3Dpl.col.correlation.map_elements(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lambda%20x%3A%20f%22%7Bx%3A%2C.2f%7D%22%2C%20return_dtype%3Dpl.String%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20group%3Dc.var_x.map_elements(param_group%2C%20return_dtype%3Dpl.String)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20adjust%20parameter%20names%20for%20readability%20and%20avoid%20redundancy%20with%20group%20names%0A%20%20%20%20%20%20%20%20%20%20%20%20var_x%3Dpl.col(%22var_x%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.str.replace_all(%22_neighborhood%22%2C%20%22%22%2C%20literal%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20.str.replace_all(%22risk_distribution_%22%2C%20%22%22%2C%20literal%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20.str.replace_all(%22adjust_payoff_%22%2C%20%22%22%2C%20literal%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20.str.replace_all(%22_%22%2C%20%22%20%22%2C%20literal%3DTrue)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20%23%20df_corr_long%0A%20%20%20%20return%20(df_corr_long%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(alt%2C%20df_corr_long%2C%20y_axis_sort)%3A%0A%20%20%20%20%23%20flip%20the%20axes%20and%20display%20the%20chart%20longwise%20-%20easier%20to%20read%0A%0A%20%20%20%20_heatmap_corr_chart%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(df_corr_long)%0A%20%20%20%20%20%20%20%20.mark_rect()%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var_x%3AO%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var_y%3AO%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22%22%2C%20%20%23%20Population%20Risk-Attitudes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sort%3Dy_axis_sort%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20axis%3Dalt.Axis(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20orient%3D%22top%22%2C%20%20%23%20move%20axis%20to%20top%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20labelAngle%3D-25%2C%20%20%23%20angle%20the%20labels%20(negative%20%3D%20counterclockwise)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20labelExpr%3D%22%7B'pct_risk_inclined'%3A%20'%25%20Risk-Seeking'%2C%20'pct_risk_moderate'%3A%20'%25%20Risk-Moderate'%2C%20'pct_risk_avoidant'%3A%20'%25%20Risk-Averse'%7D%5Bdatum.value%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20display%20color%20legend%20at%20bottom%2C%20match%20the%20chart%20width%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3Dalt.Color(%22correlation%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.scale(scheme%3D%22spectral%22%2C%20domain%3D%5B-0.43%2C%200.43%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20.legend(orient%3D%22bottom%22%2C%20gradientLength%3D350)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_text_corr_chart%20%3D%20_heatmap_corr_chart.mark_text().encode(%0A%20%20%20%20%20%20%20%20y%3D%22var_x%3AO%22%2C%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22var_y%3AO%22%2C%20sort%3Dy_axis_sort)%2C%0A%20%20%20%20%20%20%20%20text%3D%22correlation_str%22%2C%0A%20%20%20%20%20%20%20%20%23%20for%20contrast%2C%20vary%20the%20color%20based%20on%20the%20intensity%20of%20the%20correlation%2C%20negative%20or%20positive%0A%20%20%20%20%20%20%20%20color%3Dalt.condition(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22abs(datum.correlation)%20%3E%200.29%22%2C%20alt.value(%22white%22)%2C%20alt.value(%22black%22)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20facet%20by%20group%20so%20that%20so%20that%20related%20parameters%20can%20be%20compared%20together%0A%20%20%20%20(_heatmap_corr_chart%20%2B%20_text_corr_chart).properties(%0A%20%20%20%20%20%20%20%20width%3D350%2C%20height%3Dalt.Step(30)%0A%20%20%20%20).facet(%0A%20%20%20%20%20%20%20%20row%3Dalt.Row(%22group%3AN%22%2C%20title%3D%22%22)%2C%0A%20%20%20%20%20%20%20%20title%3D%22Parameter%20correlation%20with%20population%20risk%20attitudes%22%2C%0A%20%20%20%20).resolve_scale(y%3D%22independent%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(alt%2C%20df_corr_long)%3A%0A%20%20%20%20%23%20wide%20version%20of%20the%20correlation%20chart%20-%20uncomment%20last%20statement%20to%20view%0A%0A%20%20%20%20y_axis_sort%20%3D%20%5B%22pct_risk_inclined%22%2C%20%22pct_risk_moderate%22%2C%20%22pct_risk_avoidant%22%5D%0A%0A%20%20%20%20_heatmap_corr_chart%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(df_corr_long)%0A%20%20%20%20%20%20%20%20.mark_rect()%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var_x%3AO%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20title%3D%22Simulation%20Parameters%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20axis%3Dalt.Axis(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20orient%3D%22top%22%2C%20%20%23%20move%20axis%20to%20top%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20labelAngle%3D-45%2C%20%20%23%20angle%20the%20labels%20(negative%20%3D%20counterclockwise)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var_y%3AO%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22Population%20Risk-Attitudes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sort%3Dy_axis_sort%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20axis%3Dalt.Axis(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20labelExpr%3D%22%7B'pct_risk_inclined'%3A%20'%25%20Risk-Seeking'%2C%20'pct_risk_moderate'%3A%20'%25%20Risk-Moderate'%2C%20'pct_risk_avoidant'%3A%20'%25%20Risk-Averse'%7D%5Bdatum.value%5D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3Dalt.Color(%22correlation%22).scale(scheme%3D%22spectral%22%2C%20domainMid%3D0)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_text_corr_chart%20%3D%20_heatmap_corr_chart.mark_text().encode(%0A%20%20%20%20%20%20%20%20x%3D%22var_x%3AO%22%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22var_y%3AO%22%2C%20sort%3Dy_axis_sort)%2C%0A%20%20%20%20%20%20%20%20text%3D%22correlation_str%22%2C%0A%20%20%20%20%20%20%20%20%23%20for%20contrast%2C%20vary%20the%20color%20based%20on%20the%20intensity%20of%20the%20correlation%2C%20negative%20or%20positive%0A%20%20%20%20%20%20%20%20color%3Dalt.condition(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22abs(datum.correlation)%20%3E%200.29%22%2C%20alt.value(%22white%22)%2C%20alt.value(%22black%22)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20facet%20by%20group%20so%20that%20so%20that%20related%20parameters%20can%20be%20compared%20together%0A%20%20%20%20%23%20(_heatmap_corr_chart%20%2B%20_text_corr_chart).resolve_axis(y%3D%22shared%22).properties(%0A%20%20%20%20%23%20%20%20%20%20width%3Dalt.Step(55)%2C%20height%3D300%0A%20%20%20%20%23%20).facet(%0A%20%20%20%20%23%20%20%20%20%20column%3Dalt.Column(%22group%3AN%22%2C%20title%3D%22Parameter%20Group%22)%2C%0A%20%20%20%20%23%20%20%20%20%20title%3D%22Simulation%20parameter%20correlation%20with%20population%20risk%20attitudes%22%2C%0A%20%20%20%20%23%20).resolve_scale(x%3D%22independent%22)%0A%20%20%20%20return%20(y_axis_sort%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
6ae0fa246faabb3a7fcb5bc28d125e45