import%20marimo%0A%0A__generated_with%20%3D%20%220.23.11%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20simulatingrisk.doc_utils%20import%20docs_header%0A%0A%20%20%20%20docs_header()%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%20Hawk%2FDove%20with%20Multiple%20Risk-Attitudes%20%3A%20Payoff%20Significance%0A%0A%20%20%20%20The%20payoff%20analysis%20shows%20that%20the%20median%20and%20quartiles%20are%20lower%20for%20Risk-Moderates%20than%20for%20other%20Risk-Attitudes.%20Is%20that%20signficant%3F%20How%20confident%20are%20we%20in%20these%20numbers%3F%0A%0A%20%20%20%20**tl%3Bdr**%3A%20Confidence%20intervals%20based%20on%20bootstrap%20sampling%20(calculated%20two%20different%20ways)%20are%20very%20small.%20We%20are%20highly%20confident%20in%20these%20numbers.%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%20import%20altair%20as%20alt%0A%20%20%20%20import%20seaborn%20as%20sns%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20from%20utils%20import%20load_agent_data%0A%0A%20%20%20%20data_dir%20%3D%20Path(%22data%2Fno_adjustment%2F%22)%0A%20%20%20%20return%20alt%2C%20data_dir%2C%20load_agent_data%2C%20mo%2C%20pl%2C%20plt%2C%20sns%0A%0A%0A%40app.cell%0Adef%20_(data_dir%2C%20load_agent_data%2C%20pl)%3A%0A%20%20%20%20model_agent_df%20%3D%20(%0A%20%20%20%20%20%20%20%20pl.concat(load_agent_data(data_dir))%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%20calculate%20a%20scaled%20points%20value%20so%20we%20can%20compare%20across%20runs%20with%20different%20length%20and%20play%20neighborhood%0A%20%20%20%20%20%20%20%20%20%20%20%20scaled_points%3Dpl.col(%22points%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.truediv(pl.col(%22play_neighborhood%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20.truediv(pl.col(%22Step%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20.mul(100)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.collect()%0A%20%20%20%20)%0A%20%20%20%20return%20(model_agent_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%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20model_agent_df%2C%20sample_size)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20The%20full%20set%20of%20agent%20data%20is%20too%20large%20to%20process%20easily%2C%20so%20we%20test%20a%20sample%20for%20significance.%0A%0A%20%20%20%20-%20%7Bmodel_agent_df.height%3A%2C%7D%20total%20rows%20%0A%20%20%20%20-%20sample%20size%3A%20%7Bsample_size%3A%2C%7D%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%20The%20Seaborn%20plotting%20library%20has%20built-in%20support%20for%20%5Bbootstrap%20sampling%20for%20confidence%20intervals%5D(https%3A%2F%2Fseaborn.pydata.org%2Ftutorial%2Ferror_bars.html%23confidence-interval-error-bars)%2C%20so%20first%20we%20try%20that%20on%20our%20sample.%0A%0A%20%20%20%20The%20line%20chart%20with%20horizontal%20error%20bars%20is%20the%20based%20on%20the%20mean%20of%20the%20cumulative%20payoff%20distribution.%20The%20bootstrap%20sampling%20results%20in%20confidence%20interval%20bars%20that%20are%20very%20close%20to%20our%20data%20points.%20%20(Smaller%20samples%20resulted%20in%20slightly%20wider%20confidence%20intervals.)%0A%0A%20%20%20%20We%20pair%20that%20with%20a%20jitter%20plot%20to%20show%20the%20ranges%20of%20the%20distribution%20another%20way.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(model_agent_df%2C%20plt%2C%20sns)%3A%0A%0A%20%20%20%20%23%20sample_size%20%3D%20200_000%0A%20%20%20%20%23%20sample_size%20%3D%2010_000%20%20%20%23%20completes%20in%20~660ms%0A%20%20%20%20%23%20sample_size%20%3D%2020_000%20%20%23%20809ms%0A%20%20%20%20%23%20sample_size%20%3D%20300_000%20%20%23%208.82s%0A%20%20%20%20sample_size%20%3D%201_000_000%20%20%23%2014.85s%0A%0A%20%20%20%20sample_agents_df%20%3D%20model_agent_df.sample(sample_size)%0A%0A%20%20%20%20f%2C%20axs%20%3D%20plt.subplots(2%2C%20figsize%3D(12%2C%206)%2C%20sharex%3DTrue%2C%20layout%3D%22tight%22)%0A%20%20%20%20fig1%20%3D%20sns.pointplot(%0A%20%20%20%20%20%20%20%20y%3Dsample_agents_df%5B%22scaled_points%22%5D%2C%0A%20%20%20%20%20%20%20%20x%3Dsample_agents_df%5B%22risk_attitude%22%5D%2C%0A%20%20%20%20%20%20%20%20errorbar%3D%22ci%22%2C%0A%20%20%20%20%20%20%20%20capsize%3D0.3%2C%0A%20%20%20%20%20%20%20%20ax%3Daxs%5B0%5D%2C%0A%20%20%20%20)%0A%20%20%20%20fig1.set_ylabel(%22Mean%20Cumulative%20Payoff%22)%0A%20%20%20%20fig1.set_title(%22Cumulative%20Payoff%20Distribution%20by%20Risk%20Attitude%22)%0A%20%20%20%20fig2%20%3D%20sns.stripplot(%0A%20%20%20%20%20%20%20%20y%3Dsample_agents_df%5B%22scaled_points%22%5D%2C%0A%20%20%20%20%20%20%20%20x%3Dsample_agents_df%5B%22risk_attitude%22%5D%2C%0A%20%20%20%20%20%20%20%20jitter%3D0.3%2C%0A%20%20%20%20%20%20%20%20ax%3Daxs%5B1%5D%2C%0A%20%20%20%20)%0A%20%20%20%20fig2.set_xlabel(%22Risk%20Attitude%22)%0A%20%20%20%20fig2.set_ylabel(%22Cumulative%20Payoff%22)%0A%0A%20%20%20%20ci_jitter_meanplot%20%3D%20f.get_figure()%0A%20%20%20%20ci_jitter_meanplot.text(%0A%20%20%20%20%20%20%20%20x%3D0.5%2C%0A%20%20%20%20%20%20%20%20y%3D1.025%2C%0A%20%20%20%20%20%20%20%20s%3D%22With%2095%25%20confidence%20intervals%20based%20on%20mean%22%2C%0A%20%20%20%20%20%20%20%20horizontalalignment%3D%22center%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20uncomment%20to%20save%20as%20a%20static%20image%0A%20%20%20%20%23%20ci_jitter_meanplot.savefig(%22ci_jitter_mean_cumulative_payoff.png%22)%0A%20%20%20%20return%20sample_agents_df%2C%20sample_size%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%20A%20violin%20plot%20gives%20us%20a%20differnt%20way%20to%20see%20the%20shape%20of%20cumulative%20payoff%20distributions.%20(Again%20run%20on%20the%20sample.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20sample_agents_df%2C%20sns)%3A%0A%20%20%20%20fig_v%2C%20ax_v%20%3D%20plt.subplots(figsize%3D(12%2C%204))%0A%20%20%20%20vplot%20%3D%20sns.violinplot(%0A%20%20%20%20%20%20%20%20ax%3Dax_v%2C%20data%3Dsample_agents_df%2C%20x%3D%22risk_attitude%22%2C%20y%3D%22scaled_points%22%0A%20%20%20%20)%0A%0A%20%20%20%20vplot.set_xlabel(%22Risk%20Attitude%22)%0A%20%20%20%20vplot.set_ylabel(%22Cumulative%20Payoff%22)%0A%20%20%20%20vplot.set_title(%22Cumulative%20Payoff%20Distribution%20by%20Risk%20Attitude%22)%0A%0A%20%20%20%20%23%20save%20to%20file%0A%20%20%20%20%23%20vplot_fig%20%3D%20vplot.get_figure()%0A%20%20%20%20%23%20vplot_fig.savefig(%22cumulative_payoff_violinplot.png%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%20Manual%20bootstrap%20sampling%0A%0A%20%20%20%20As%20a%20comparison%20to%20the%20Seaborn%20plots%2C%20we%20implemented%20our%20own%20bootstrap%20sampling%20in%20polars%20and%20gather%20mean%2C%20median%2C%20and%20Q1%2FQ3.%20%20Even%20for%20a%20smaller%20subset%20of%20the%20data%2C%20this%20is%20computationally%20intensive%20and%20slow%2C%20so%20the%20samples%20were%20generated%20and%20collected%20separately%20in%20a%20script%20in%20this%20directory.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(model_agent_df%2C%20pl)%3A%0A%20%20%20%20payoff_stats_df%20%3D%20model_agent_df.group_by(%22risk_attitude%22).agg(%0A%20%20%20%20%20%20%20%20mean%3Dpl.col.scaled_points.mean()%2C%0A%20%20%20%20%20%20%20%20median%3Dpl.col.scaled_points.median()%2C%0A%20%20%20%20%20%20%20%20q1%3Dpl.col.scaled_points.quantile(0.25)%2C%0A%20%20%20%20%20%20%20%20q3%3Dpl.col.scaled_points.quantile(0.75)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20payoff_stats_df%0A%20%20%20%20return%20(payoff_stats_df%2C)%0A%0A%0A%40app.cell%0Adef%20_(data_dir%2C%20pl)%3A%0A%20%20%20%20%23%20load%20pre-calculated%20bootstrap%20sampled%20stats%0A%20%20%20%20bootstrap_stats_df%20%3D%20pl.read_csv(data_dir%20%2F%20%22payoff_bootstrap_stats_2pct_10k.csv%22)%0A%20%20%20%20return%20(bootstrap_stats_df%2C)%0A%0A%0A%40app.cell%0Adef%20_(bootstrap_stats_df)%3A%0A%20%20%20%20bootstrap_stats_df.height%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(alt%2C%20bootstrap_stats_df%2C%20pl)%3A%0A%20%20%20%20alt.data_transformers.enable(%22vegafusion%22)%0A%0A%20%20%20%20r4_bins%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(bootstrap_stats_df.filter(pl.col.risk_attitude.eq(4)))%0A%20%20%20%20%20%20%20%20.mark_bar()%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20alt.X(%22mean%3AQ%22%2C%20bin%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%22count()%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20r4_bins%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(bootstrap_stats_df%2C%20pl)%3A%0A%20%20%20%20%23%20calculate%2095%25%20confidence%20interval%20for%20each%20risk%20attitude%0A%0A%20%20%20%20sample_ci_bounds%20%3D%20bootstrap_stats_df.group_by(%22risk_attitude%22).agg(%0A%20%20%20%20%20%20%20%20%23%2095%25%20confidence%20interval%20for%20mean%0A%20%20%20%20%20%20%20%20mean_ci_lower%3Dpl.col.mean.quantile(0.025)%2C%0A%20%20%20%20%20%20%20%20mean_ci_upper%3Dpl.col.mean.quantile(0.975)%2C%0A%20%20%20%20%20%20%20%20%23%2095%25%20confidence%20interval%20for%20median%0A%20%20%20%20%20%20%20%20median_ci_lower%3Dpl.col.median.quantile(0.025)%2C%0A%20%20%20%20%20%20%20%20median_ci_upper%3Dpl.col.median.quantile(0.975)%2C%0A%20%20%20%20%20%20%20%20%23%20q1%20confidence%20interval%0A%20%20%20%20%20%20%20%20q1_ci_lower%3Dpl.col.q1.quantile(0.025)%2C%0A%20%20%20%20%20%20%20%20q1_ci_upper%3Dpl.col.q1.quantile(0.975)%2C%0A%20%20%20%20%20%20%20%20%23%20q3%20confidence%20interval%0A%20%20%20%20%20%20%20%20q3_ci_lower%3Dpl.col.q3.quantile(0.025)%2C%0A%20%20%20%20%20%20%20%20q3_ci_upper%3Dpl.col.q3.quantile(0.975)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20sample_ci_bounds.sort(%22risk_attitude%22)%0A%20%20%20%20return%20(sample_ci_bounds%2C)%0A%0A%0A%40app.cell%0Adef%20_(alt%2C%20bootstrap_stats_df%2C%20pl%2C%20sample_ci_bounds)%3A%0A%20%20%20%20%23%20graph%20results%20for%20risk%20attitude%204%20to%20check%20the%20logic%0A%20%20%20%20%23%20based%20on%20https%3A%2F%2Finferentialthinking.com%2Fchapters%2F13%2F2%2Fbootstrap%2F%0A%0A%20%20%20%20mean_r4_bin_chart%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(bootstrap_stats_df.filter(pl.col.risk_attitude.eq(4)))%0A%20%20%20%20%20%20%20%20.mark_bar()%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20alt.X(%22mean%3AQ%22%2C%20bin%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%22count()%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20mean_r4_rule_chart%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(sample_ci_bounds.filter(pl.col.risk_attitude.eq(4)))%0A%20%20%20%20%20%20%20%20.mark_rule(color%3D%22yellow%22%2C%20size%3D5)%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%22mean_ci_lower%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x2%3D%22mean_ci_upper%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.value(%22height%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20y%3D'count()'%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20(mean_r4_bin_chart%20%2B%20mean_r4_rule_chart)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(alt%2C%20payoff_stats_df%2C%20sample_ci_bounds)%3A%0A%20%20%20%20ci_bars%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(sample_ci_bounds)%0A%20%20%20%20%20%20%20%20.mark_rule(color%3D%22yellow%22%2C%20size%3D15%2C%20opacity%3D1.0)%0A%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%22mean_ci_lower%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y2%3D%22mean_ci_upper%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%22risk_attitude%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20stats_base_chart%20%3D%20alt.Chart(payoff_stats_df)%0A%20%20%20%20mean_points%20%3D%20stats_base_chart.mark_point(size%3D30).encode(%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22mean%22).scale(zero%3DFalse)%2C%20x%3D%22risk_attitude%22%0A%20%20%20%20)%0A%0A%20%20%20%20mean_line%20%3D%20stats_base_chart.mark_line().encode(%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22mean%22).scale(zero%3DFalse)%2C%20x%3D%22risk_attitude%22%0A%20%20%20%20)%0A%0A%20%20%20%20ci_bars%20%2B%20mean_points%20%2B%20mean_line%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(alt%2C%20payoff_stats_df%2C%20sample_ci_bounds)%3A%0A%20%20%20%20%23%20write%20a%20method%20to%20create%20the%20combined%20line%20%2B%20points%20chart%20with%20confidence%20interval%0A%0A%20%20%20%20def%20chart_stat_ci(measure)%3A%0A%20%20%20%20%20%20%20%20%23%20assumes%20sample_ci_bounds%20and%20payoff_stats_df%20are%20already%20set%20up%0A%0A%20%20%20%20%20%20%20%20%23%20draw%20rulers%20for%2095%25%20confidence%20interval%0A%20%20%20%20%20%20%20%20ci_bars%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20alt.Chart(sample_ci_bounds)%0A%20%20%20%20%20%20%20%20%20%20%20%20.mark_rule(color%3D%22yellow%22%2C%20size%3D15)%0A%20%20%20%20%20%20%20%20%20%20%20%20.encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Df%22%7Bmeasure%7D_ci_lower%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y2%3Df%22%7Bmeasure%7D_ci_upper%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D%22risk_attitude%22%2C%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%20stats_base_chart%20%3D%20alt.Chart(payoff_stats_df)%0A%20%20%20%20%20%20%20%20points%20%3D%20stats_base_chart.mark_point(size%3D10).encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(measure%2C%20title%3Df%22%7Bmeasure.title()%7D%22).scale(zero%3DFalse)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%22risk_attitude%22%2C%20title%3D%22Risk%20Attitude%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20line%20%3D%20stats_base_chart.mark_line().encode(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(measure).scale(zero%3DFalse)%2C%20x%3D%22risk_attitude%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20return%20points%20%2B%20line%20%2B%20ci_bars%0A%0A%20%20%20%20return%20(chart_stat_ci%2C)%0A%0A%0A%40app.cell%0Adef%20_(chart_stat_ci)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20chart_stat_ci(%22mean%22)%0A%20%20%20%20%20%20%20%20%7C%20chart_stat_ci(%22median%22)%0A%20%20%20%20%20%20%20%20%7C%20chart_stat_ci(%22q1%22)%0A%20%20%20%20%20%20%20%20%7C%20chart_stat_ci(%22q3%22)%0A%20%20%20%20).properties(%0A%20%20%20%20%20%20%20%20title%3D%22Statistics%20for%20cumulative%20payoff%20distribution%20with%20confidence%20intervals%22%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
0a711c8670742b82ca8dd0846beca379