import%20marimo%0A%0A__generated_with%20%3D%20%220.10.7%22%0Aapp%20%3D%20marimo.App(app_title%3D%22optimal%20bayesian%20ranking%20ch2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20optimal%20bayesian%20ranking%0A%20%20%20%20%20%20%20%20%3Ch3%20align%3D'center'%3Echapter%202%3A%20to%20infinity%3C%2Fh3%3E%0A%20%20%20%20%20%20%20%20%3Cp%20align%3D'center'%3Eby%20miraia%20s.%20chiou%20%C2%A9%202024%3C%2Fp%3E%0A%0A%20%20%20%20%20%20%20%20%23%23%20introduction%0A%0A%20%20%20%20%20%20%20%20in%20the%20previous%20chapter%2C%20i%20demonstrated%20how%20to%20derive%20the%20%5Brule%20of%20succession%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRule_of_succession)%2C%20aka%20the%20%5Bbayes%20estimator%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBinomial_distribution%23Estimation_of_parameters)%20with%20a%20uniform%20prior.%20in%20a%20ranking%20context%2C%20this%20represents%20the%20mean%20of%20the%20distribution%20of%20potential%20true%20means%20%24%5Cmu_%5CMu%20%3D%20%5Cdfrac%7Bt%20%5Cmu_s%20%2B%201%7D%7Bt%2B2%7D%24.%0A%0A%20%20%20%20%20%20%20%20in%20this%20chapter%2C%20i%20will%20show%20original%20research%20extending%20the%20estimator%20to%20the%20inifinite%20dirichlet%20case.%0A%0A%20%20%20%20%20%20%20%20we'll%20be%20looking%20at%20some%20ternary%20plots.%20if%20you%20haven't%20seen%20them%20before%2C%20this%20%5Btutorial%5D(https%3A%2F%2Fgrapherhelp.goldensoftware.com%2FGraphs%2FReading_Ternary_Diagrams.htm)%20may%20help.%20to%20get%20you%20acquainted%2C%20i%20show%20the%20three%20axes%20of%20a%20ternary%20plot%2C%20along%20with%20an%20moveable%20point.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20mpltern%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20import%20fuz.log%20as%20flog%0A%20%20%20%20return%20flog%2C%20mo%2C%20mpltern%2C%20np%2C%20plt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20w_ternr%2C%20w_ternt)%3A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.md('%23%23%23%20move%20the%20point%20on%20the%20plot')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.hstack(%5Bmo.md('%24r%24%20axis')%2C%20w_ternr%5D%2C%20widths%3D%5B1%2C%205%5D%2C%20align%3D'center')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.hstack(%5Bmo.md('%24t%24%20axis')%2C%20w_ternt%5D%2C%20widths%3D%5B1%2C%205%5D%2C%20align%3D'center')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D'info'%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20w_ternr%2C%20w_ternt)%3A%0A%20%20%20%20ternr%20%3D%20w_ternr.value%0A%20%20%20%20ternt%20%3D%20w_ternt.value%0A%20%20%20%20ternl%20%3D%20round(1%20-%20w_ternr.value%20-%20w_ternt.value%2C%202)%0A%20%20%20%20mo.callout(mo.md(f%22%22%22%24r%2Ct%2Cl%20%3D%20%5C%5C%7B%7B%7Bternr%7D%2C%20%7Bternt%7D%2C%20%7Bternl%7D%5C%5C%7D%7D%24%22%22%22))%0A%20%20%20%20return%20ternl%2C%20ternr%2C%20ternt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20plt%2C%20ternl%2C%20ternr%2C%20ternt)%3A%0A%20%20%20%20x_small%20%3D%20np.linspace(0%2C%201%2C%20129)%0A%20%20%20%20x_other%20%3D%20(1%20-%20x_small)%20%2F%202%0A%20%20%20%20dx_small%20%3D%20x_small%5B1%5D%20-%20x_small%5B0%5D%0A%20%20%20%20p1%20%3D%20np.vstack(%5Bx_small%2C%20x_other%2C%20x_other%5D).T%0A%20%20%20%20p2%20%3D%20np.vstack(%5Bx_other%2C%20x_small%2C%20x_other%5D).T%0A%20%20%20%20p3%20%3D%20np.vstack(%5Bx_other%2C%20x_other%2C%20x_small%5D).T%0A%20%20%20%20_fig%20%3D%20plt.figure(figsize%3D(5%2C%204))%0A%20%20%20%20_fig.subplots_adjust(top%3D0.8%2C%20bottom%3D0.15)%0A%20%20%20%20_ax%20%3D%20plt.subplot(projection%3D'ternary')%0A%20%20%20%20_ax.plot(p1%5B%3A%2C%200%5D%2C%20p1%5B%3A%2C%201%5D%2C%20p1%5B%3A%2C%202%5D%2C%20color%3D'steelblue'%2C%20label%3D'top%20axis')%0A%20%20%20%20_ax.plot(p2%5B%3A%2C%200%5D%2C%20p2%5B%3A%2C%201%5D%2C%20p2%5B%3A%2C%202%5D%2C%20color%3D'darkorange'%2C%20label%3D'left%20axis')%0A%20%20%20%20_ax.plot(p3%5B%3A%2C%200%5D%2C%20p3%5B%3A%2C%201%5D%2C%20p3%5B%3A%2C%202%5D%2C%20color%3D'forestgreen'%2C%20label%3D'right%20axis')%0A%20%20%20%20_ax.scatter(ternt%2C%20ternl%2C%20ternr%2C%20color%3D'crimson')%0A%20%20%20%20_ax.set_tlabel('%24t%24')%0A%20%20%20%20_ax.set_llabel('%24l%24')%0A%20%20%20%20_ax.set_rlabel('%24r%24')%0A%20%20%20%20_ax.taxis.set_label_position('tick1')%0A%20%20%20%20_ax.laxis.set_label_position('tick1')%0A%20%20%20%20_ax.raxis.set_label_position('tick1')%0A%20%20%20%20_ax.legend(fontsize%3D9%2C%20framealpha%3D0.3)%0A%20%20%20%20_ax.grid(alpha%3D0.4)%0A%20%20%20%20_fig%0A%20%20%20%20return%20dx_small%2C%20p1%2C%20p2%2C%20p3%2C%20x_other%2C%20x_small%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20multinomial%20to%20dirichlet%0A%0A%20%20%20%20%20%20%20%20let's%20start%20by%20setting%20the%20number%20of%20ratings%20for%20an%20item%20in%20the%203-star%20rating%20system.%20in%20a%20sense%2C%20this%20is%20a%203-dimensional%20system.%20the%20three%20dimensions%20are%201-star%2C%202-star%2C%20and%203-star%2C%20or%20%24%5C%7B0%2C%200.5%2C%201%5C%7D%24.%0A%0A%20%20%20%20%20%20%20%20just%20as%20an%20example%2C%20in%20a%205-star%20rating%20system%2C%20the%20five%20dimensions%20could%20be%201-5%20or%20%24%5C%7B0%2C%200.25%2C%200.5%2C%200.75%2C%201%5C%7D%24%0A%0A%20%20%20%20%20%20%20%20play%20with%20the%20rating%20counts%20below%20to%20see%20how%20the%20corresponding%20distributions%20change!%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20scipy%20import%20stats%0A%0A%20%20%20%20import%20fuz.dists%20as%20fd%0A%20%20%20%20import%20fuz.marimo%20as%20fmo%0A%20%20%20%20import%20fuz.plot%20as%20fp%0A%20%20%20%20return%20fd%2C%20fmo%2C%20fp%2C%20stats%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(fmo%2C%20mo)%3A%0A%20%20%20%20w_3star_stack%2C%20(w_3star1%2C%20w_3star2%2C%20w_3star3)%20%3D%20fmo.make_star_widget(%0A%20%20%20%20%20%20%20%20n_stars%3D3%2C%20star0%3D(1%2C%202%2C%203)%2C%20max_ratings%3D12%0A%20%20%20%20)%0A%20%20%20%20mo.callout(w_3star_stack%2C%20kind%3D'success')%0A%20%20%20%20return%20w_3star1%2C%20w_3star2%2C%20w_3star3%2C%20w_3star_stack%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20w_3star1%2C%20w_3star2%2C%20w_3star3)%3A%0A%20%20%20%20trials_3star%20%3D%20np.array(%5Bw_3star1.value%2C%20w_3star2.value%2C%20w_3star3.value%5D)%0A%20%20%20%20alpha_3star%20%3D%20trials_3star%20%2B%201%0A%20%20%20%20return%20alpha_3star%2C%20trials_3star%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(flog%2C%20stats%2C%20trials_3star)%3A%0A%20%20%20%20n_3star%20%3D%20trials_3star.sum()%0A%20%20%20%20p_3star%20%3D%20flog.norm(trials_3star)%0A%20%20%20%20multi_3star%20%3D%20stats.multinomial(n%3Dn_3star%2C%20p%3Dp_3star)%0A%20%20%20%20return%20multi_3star%2C%20n_3star%2C%20p_3star%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22in%20this%20system%2C%20the%20analogous%20distribution%20to%20the%20binomial%20is%20the%20multinomial.%20it%20can%20be%20represented%20by%20a%20ternary%20plot%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fp%2C%20multi_3star)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20fp.plot_multinomial(multi_3star%2C%20title%3D'3-star%20multinomial%20pmf')%0A%20%20%20%20_fig.set_figwidth(5)%0A%20%20%20%20_fig.set_figheight(4)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22the%20analogous%20distribution%20to%20the%20beta%20is%20the%20dirichlet.%20it%20is%20characterized%20by%20%24%5Calpha%24%2C%20a%20vector%20of%20the%20number%20of%20ratings%20%2B%201%20for%20each%20dimension.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(alpha_3star%2C%20fd%2C%20fp)%3A%0A%20%20%20%20d_3star%20%3D%20fd.Scored(alpha_3star%2C%20%5B1%2C%202%2C%203%5D)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20fp.plot_scored_pdf(d_3star%2C%20title%3D'3-star%20dirichlet%20pdf')%0A%20%20%20%20_fig.set_figwidth(5)%0A%20%20%20%20_fig.set_figheight(4)%0A%20%20%20%20_fig%0A%20%20%20%20return%20(d_3star%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20just%20like%20in%20chapter%201%2C%20you%20can%20get%20the%20dirichlet%20from%20the%20multinomial.%20it's%20harder%20to%20visualize%20but%20sweeping%20%24p%24%20along%20top%2C%20left%2C%20and%20right%20cross-sections%20of%20the%20ternary%20plot%2C%20you%20can%20see%20that%20the%20distributions%20match.%20again%2C%20we%20use%20a%20mode-based%20parameterization%20to%20get%20the%20matching%20distribution.%0A%0A%20%20%20%20%20%20%20%20the%20multinomial-derived%20pdf%20is%20the%20opaque%20line%2C%20while%20the%20corresponding%20dirichlet%20plot%20is%20the%20translucent%20thick%20line.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20scipy.integrate%20import%20romb%0A%20%20%20%20return%20alt%2C%20pd%2C%20romb%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20alt%2C%0A%20%20%20%20d_3star%2C%0A%20%20%20%20dx_small%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20n_3star%2C%0A%20%20%20%20p1%2C%0A%20%20%20%20p2%2C%0A%20%20%20%20p3%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20romb%2C%0A%20%20%20%20stats%2C%0A%20%20%20%20trials_3star%2C%0A%20%20%20%20x_small%2C%0A)%3A%0A%20%20%20%20_ps%20%3D%20%5Bp1%2C%20p2%2C%20p3%5D%0A%20%20%20%20_dfs%20%3D%20%5B%5D%0A%20%20%20%20for%20_i%2C%20_p%20in%20enumerate(_ps)%3A%0A%20%20%20%20%20%20%20%20_ym%20%3D%20stats.multinomial.pmf(trials_3star%2C%20n%3Dn_3star%2C%20p%3D_p)%0A%20%20%20%20%20%20%20%20_ym%20%3D%20_ym%20%2F%20romb(_ym%2C%20dx_small)%0A%20%20%20%20%20%20%20%20_yd%20%3D%20d_3star.pdf(_p.T)%0A%20%20%20%20%20%20%20%20_yd%20%3D%20_yd%20%2F%20romb(_yd%2C%20dx_small)%0A%20%20%20%20%20%20%20%20_df%20%3D%20pd.DataFrame(%7B'x'%3A%20x_small%2C%20'multinomial'%3A%20_ym%2C%20'dirichlet'%3A%20_yd%7D)%0A%20%20%20%20%20%20%20%20_df%5B'axis'%5D%20%3D%20_i%20%2B%201%0A%20%20%20%20%20%20%20%20_dfs.append(_df)%0A%20%20%20%20_df%20%3D%20pd.concat(_dfs)%0A%20%20%20%20_base%20%3D%20alt.Chart(_df).encode(%0A%20%20%20%20%20%20%20%20alt.X('x')%2C%20alt.Color('axis%3AN'%2C%20scale%3Dalt.Scale(scheme%3D'category10'))%0A%20%20%20%20)%0A%20%20%20%20_multi_chart%20%3D%20_base.mark_line().encode(y%3D'multinomial')%0A%20%20%20%20_diri_chart%20%3D%20_base.mark_line(opacity%3D0.2%2C%20strokeWidth%3D12).encode(y%3D'dirichlet')%0A%20%20%20%20_chart%20%3D%20alt.layer(_multi_chart%2C%20_diri_chart)%0A%20%20%20%20mo.ui.altair_chart(_chart)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20in%20our%20specific%203-star%20case%2C%20the%20rule%20of%20succession%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Bequation%7D%0A%20%20%20%20%20%20%20%20%5Cmu_%5CMu%20%3D%20%5Cfrac%7Bt_1%20%2B%201%7D%7Bt%2B3%7D%20%2B%20%5Cfrac%7B2(t_2%20%2B%201)%7D%7Bt%2B3%7D%20%2B%20%5Cfrac%7B3(t_3%20%2B%201)%7D%7Bt%2B3%7D%0A%20%20%20%20%20%20%20%20%5Cend%7Bequation%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20as%20we%20expand%20to%20more%20dimensions%20%2F%20more%20stars%2C%20the%20general%20rule%20is%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Bequation%7D%0A%20%20%20%20%20%20%20%20%5Cmu_%5CMu%20%3D%20%5Csum_%7Bd%3D1%7D%5Em%5Cfrac%7Bx_d(t_d%20%2B%201)%7D%7Bt%20%2B%20m%7D%0A%20%20%20%20%20%20%20%20%5Cend%7Bequation%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20where%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Baligned%7D%0A%20%20%20%20%20%20%20%20%5Cmu_%5CMu%20%26%3D%20%5Ctext%7Bthe%20mean%20of%20possible%20true%20means%7D%5C%5C%0A%20%20%20%20%20%20%20%20N%20%26%3D%20%5Ctext%7Bnumber%20of%20dimensions%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20x_d%20%26%3D%20%5Ctext%7Bvalue%20of%20dimension%20%7D%20d%5C%5C%0A%20%20%20%20%20%20%20%20t%20%26%3D%20%5Ctext%7Btotal%20number%20of%20trials%20(ratings)%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20t_d%20%26%3D%20%5Ctext%7Bnumber%20of%20trials%20for%20dimension%20%7D%20d%5C%5C%0A%20%20%20%20%20%20%20%20%5Cend%7Baligned%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20note%20that%20this%20works%20for%20the%20binary%20case%20(ch1).%20working%20backwards%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Baligned%7D%0A%20%20%20%20%20%20%20%20%5Cmu_s%20%26%3D%20%5Cfrac%7B0%5Ccdot%20t_1%20%2B%201%20%5Ccdot%20t_2%7D%7Bt%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Cfrac%7Bt_2%7D%7Bt%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Cmu_%5CMu%20%26%3D%20%5Cfrac%20%7Bt%5Cmu_s%20%2B%201%7D%7Bt%2B2%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Cfrac%7Bt(t_2%20%2F%20t)%2B1%7D%7Bt%2B2%7D%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Cfrac%7Bt_2%20%2B%201%7D%7Bt%2B2%7D%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Cfrac%7B0(t_1%20%2B%201)%7D%7Bt%2B2%7D%20%2B%20%5Cfrac%7B1(t_2%20%2B%201)%7D%7Bt%2B2%7D%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Csum_%7Bd%3D1%7D%5EN%5Cfrac%7Bx_d(t_d%20%2B%201)%7D%7Bt%20%2B%20N%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Cend%7Baligned%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20reaching%20infinity%0A%0A%20%20%20%20%20%20%20%20what%20if%20you%20want%20to%20allow%20for%20arbitrary%20ratings%3F%20ex.%20instead%20of%20a%204%20star%20rating%2C%20allow%20for%204.23%2C%203%2C%202.718281828...%20etc.%20we%20can%20gradually%20extend%20the%20dirichlet%20dimensions%20toward%20infinity%20and%20see%20what%20happens.%0A%0A%20%20%20%20%20%20%20%20to%20do%20this%20we%20create%20dimensions%20along%20the%20interval%20%24%5B0%2C1%5D%24%20and%20increase%20the%20alpha%20for%20that%20dimension%20by%20one%20(using%20mode%20parameterization%2C%20meaning%20each%20alpha%20starts%20with%201).%20for%20visualization%20purposes%20we%20increase%20the%20dimensions%20by%20100%20each%20time%20and%20only%20allow%20user%20input%20wih%20a%20resolution%20of%200.01.%20then%20we%20try%20to%20figure%20out%20the%20mean%2C%20which%20would%20be%20the%20rule%20of%20succession...%20%0A%0A%20%20%20%20%20%20%20%20however...%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fd%2C%20np)%3A%0A%20%20%20%20def%20get_like_infdir(vals%2C%20cnts%2C%20n_dim%3D1001)%3A%0A%20%20%20%20%20%20%20%20scores%20%3D%20np.linspace(0%2C%201%2C%20n_dim)%0A%20%20%20%20%20%20%20%20alpha%20%3D%20np.ones(n_dim)%0A%20%20%20%20%20%20%20%20for%20val%2C%20cnt%20in%20zip(vals%2C%20cnts)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ind%20%3D%20round((n_dim%20-%201)%20*%20val)%0A%20%20%20%20%20%20%20%20%20%20%20%20alpha%5Bind%5D%20%2B%3D%20cnt%0A%20%20%20%20%20%20%20%20return%20fd.Scored(alpha%2C%20scores)%0A%20%20%20%20return%20(get_like_infdir%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20w_infval%20%3D%20mo.ui.slider(0.01%2C%200.99%2C%200.01%2C%200.01%2C%20full_width%3DTrue%2C%20show_value%3DTrue)%0A%20%20%20%20w_infcnt%20%3D%20mo.ui.slider(1%2C%20100%2C%201%2C%201%2C%20full_width%3DTrue%2C%20show_value%3DTrue)%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.md('%23%23%23%20add%20a%20single%20value')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.hstack(%5Bmo.md('score')%2C%20w_infval%5D%2C%20widths%3D%5B1%2C%205%5D%2C%20align%3D'center')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.hstack(%5Bmo.md('count')%2C%20w_infcnt%5D%2C%20widths%3D%5B1%2C%205%5D%2C%20align%3D'center')%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D'warn'%2C%0A%20%20%20%20)%0A%20%20%20%20return%20w_infcnt%2C%20w_infval%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(alt%2C%20get_like_infdir%2C%20mo%2C%20np%2C%20pd%2C%20w_infcnt%2C%20w_infval)%3A%0A%20%20%20%20infx%20%3D%20np.arange(100%2C%2010001%2C%20100)%0A%20%20%20%20_mus%2C%20_ys%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20_ndim%20in%20infx%3A%0A%20%20%20%20%20%20%20%20d%20%3D%20get_like_infdir(%5Bw_infval.value%5D%2C%20%5Bw_infcnt.value%5D%2C%20n_dim%3D_ndim)%0A%20%20%20%20%20%20%20%20_mus.append(d.mu)%0A%20%20%20%20%20%20%20%20_ys.append((d.mu%20-%200.5)%20*%20_ndim)%0A%0A%20%20%20%20infdf1%20%3D%20pd.DataFrame(%7B'dimensions'%3A%20infx%2C%20'mu'%3A%20_mus%2C%20'y'%3A%20_ys%7D)%0A%20%20%20%20_chart%20%3D%20(%0A%20%20%20%20%20%20%20%20alt.Chart(infdf1).mark_line().encode(alt.X('dimensions')%2C%20alt.Y('mu').scale(zero%3DFalse))%0A%20%20%20%20)%0A%20%20%20%20mo.ui.altair_chart(_chart)%0A%20%20%20%20return%20d%2C%20infdf1%2C%20infx%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20no%20matter%20what%2C%20the%20bayes%20estimator%20%2F%20rule%20of%20succession%20approaches%20%240.5%24%20as%20the%20number%20of%20dimensions%20approaches%20%24%5Cinfty%24.%0A%0A%20%20%20%20%20%20%20%20this%20is%20not%20that%20useful%20then...%20what%20can%20we%20do%3F%0A%0A%20%20%20%20%20%20%20%20let's%20start%20by%20subtracting%200.5%2C%20so%20the%20estimator%20is%20not%20centered%20on%200.5%2C%20and%20multiply%20by%20the%20number%20of%20dimensions%20to%20normalize%20it.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(alt%2C%20infdf1%2C%20mo)%3A%0A%20%20%20%20_chart%20%3D%20alt.Chart(infdf1).mark_line().encode(alt.X('dimensions')%2C%20alt.Y('y').scale(zero%3DFalse))%0A%20%20%20%20mo.ui.altair_chart(_chart)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20this%20is%20a%20lot%20better!%20now%20this%20approaches%20a%20value%2C%20and%20can%20potentially%20be%20used%20for%20ranking.%20after%20some%20experimentation%2C%20i%20figured%20out%20the%20asymptote.%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Balign%7D%0A%20%20%20%20%20%20%20%20%5Csum_%7Bi%3D1%7D%5EN(%5Ctextrm%7Bmode%7D_i-0.5)(%5Calpha_i-1)%0A%20%20%20%20%20%20%20%20%5Cend%7Balign%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20w_infcnt%2C%20w_infval)%3A%0A%20%20%20%20mo.md(rf%22%22%22so%20for%20our%20current%20floating-point%20score%20of%20%24%7Bw_infval.value%7D%24%20and%20score%20count%20of%20%24%7Bw_infcnt.value%7D%24%2C%20the%20asymptote%20is%20%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%7B(w_infval.value%20-%200.5)*(w_infcnt.value)%3A0.5g%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20note%20that%200.5%20in%20the%20equation%20is%20the%20midpoint%20of%20the%20scale%200-1.%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(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20here's%20a%20derivation.%20note%20that%20this%20derivation%20may%20be%20wrong%20due%20to%20my%20lack%20of%20math%20skills%2C%20but%20i%20have%20empirically%20confirmed%20the%20result%20to%20be%20correct.%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cbegin%7Baligned%7D%0A%20%20%20%20%20%20%20%20%5Calpha_i%20%26%5Cge%201%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Calpha_0%20%26%3D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Calpha_i%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Cmu(x_i)%20%26%3D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_0%7D%5Ccdot%20x_i%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Csum_%7Bi%3D1%7D%5EN%20%5Calpha_i%7D%5Ccdot%20x_i%5C%5C%0A%20%20%20%20%20%20%20%20m%20%26%3D%20%5Ctext%7Bscale%20midpoint%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20f(x_i)%20%26%3D%20N%5Ccdot(%5Cmu(x_i)%20-%20m)%20%5C%5C%0A%20%20%20%20%20%20%20%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20f(x_i)%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20N%5Ccdot(%5Cmu(x_i)%20-%20m)%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7DN%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Csum_%7Bi%3D1%7D%5EN%20%5Calpha_i%7D%5Ccdot%20x_i%20%5Cright)-Nm%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20N%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7BN%20%5Ccdot%20%5Calpha_%7Bavg%7D%7D%20%5Ccdot%20x_i%20%5Cright)%20-%20Nm%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20N%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7BN%20%5Ccdot%20%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20x_i%20%5Cright)%20-%20Nm%20%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20x_i%20%5Cright)%20-%20Nm%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20x_i%20%5Cright)%20-%20N%20%5Ccdot%20m%20%2B%20N%20%5Ccdot%20m%20-%20N%20%5Ccdot%20m%20%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20%5Cleft(%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20x_i%20-%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20m%20%5Cright)%20%20%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cfrac%7B%5Calpha_i%7D%7B%5Calpha_%7B%5Cinfty%7D%7D%20%5Ccdot%20(x_i%20-%20m)%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20%5Csum_%7Bi%3D1%7D%5EN%20(%5Calpha_i%20-%201)%20%5Ccdot%20(x_i%20-%20m)%5C%5C%0A%20%20%20%20%20%20%20%20%26%3D%20%5Csum_%7Bi%3D1%7D%5EN(x_i-m)(%5Calpha_i-1)%5C%5C%0A%20%20%20%20%20%20%20%20%5Cend%7Baligned%7D%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20miraia's%20infinite%20rule%20of%20succession%20%2F%20infinite-dimensional%20factor%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bequation%7D%0A%20%20%20%20%5Clim_%7BN%20%5Cto%20%5Cinfty%7D%20f(x_i)%20%3D%20%5Csum_%7Bi%3D1%7D%5EN(x_i-m)(%5Calpha_i-1)%5C%5C%0A%20%20%20%20%5Cend%7Bequation%7D%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20kind%3D'success'%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20how%20is%20this%20useful%3F%20we%20can%20use%20it%20for%20ranking!%20now%20that%20we're%20past%20the%20introductory%20concepts%2C%20in%20the%20next%20chapter%20we'll%20dive%20into%20optimal%20bayesian%20ranking.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%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%23%23%20additional%20code%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%23%23%23%20widgets%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20w_ternr%20%3D%20mo.ui.slider(0%2C%201%2C%200.01%2C%20value%3D0.5%2C%20full_width%3DTrue%2C%20show_value%3DTrue)%0A%20%20%20%20mo.accordion(%7B'ternary%20r%20widget'%3A%20w_ternr%7D)%0A%20%20%20%20return%20(w_ternr%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20get_t%2C%20set_t%20%3D%20mo.state(0)%0A%20%20%20%20return%20get_t%2C%20set_t%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(get_t%2C%20mo%2C%20set_t%2C%20w_ternr)%3A%0A%20%20%20%20_t%20%3D%20get_t()%0A%20%20%20%20_max%20%3D%201%20-%20w_ternr.value%0A%20%20%20%20if%20_t%20%3E%20_max%3A%0A%20%20%20%20%20%20%20%20_t%20%3D%20_max%0A%20%20%20%20w_ternt%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%200%2C%0A%20%20%20%20%20%20%20%201%20-%20w_ternr.value%2C%0A%20%20%20%20%20%20%20%200.01%2C%0A%20%20%20%20%20%20%20%20value%3D_t%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%2C%0A%20%20%20%20%20%20%20%20show_value%3DTrue%2C%0A%20%20%20%20%20%20%20%20on_change%3Dlambda%20v%3A%20set_t(v)%2C%0A%20%20%20%20)%0A%20%20%20%20mo.accordion(%7B'ternary%20t%20widget'%3A%20w_ternt%7D)%0A%20%20%20%20return%20(w_ternt%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
0d11d0f43df2f4b60443eeb07cf5dac7443e6d5490db315b96d13f5fab5ddd61