%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.10%2C%3C3.15%22%0A%23%0A%23%20%5Btool.marimo-studio%5D%0A%23%20default%20%3D%20%22animation%22%0A%23%20runtimes%20%3D%20%5B%22server%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.1%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Convolution%20of%20dice%22)%0A%0A%0A%40app.cell%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%20numpy%20as%20np%0A%0A%20%20%20%20return%20mo%2C%20np%2C%20plt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20introduction(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Convolution%20of%20dice%0A%0A%20%20%20%20Every%20pair%20of%20die%20outcomes%20occupies%20a%20cell%20of%20a%20joint%20probability%20table.%0A%20%20%20%20Cells%20on%20one%20diagonal%20have%20the%20same%20sum.%20Adding%20their%20probabilities%20gives%0A%20%20%20%20the%20distribution%20of%20the%20sum.%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20P(X%2BY%3Ds)%3D%5Csum_x%20P(X%3Dx)P(Y%3Ds-x).%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20Inspired%20by%203Blue1Brown's%20*Convolutions*%20and%20its%20diagonal-sum%20scenes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20controls(mo)%3A%0A%20%20%20%20bias%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%200%2C%201%2C%20step%3D0.1%2C%20value%3D0.4%2C%20label%3D%22Bias%20toward%20six%22%2C%20show_value%3DTrue%0A%20%20%20%20)%0A%20%20%20%20mo.hstack(%5Bbias%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20(bias%2C)%0A%0A%0A%40app.cell%0Adef%20distributions(bias%2C%20np)%3A%0A%20%20%20%20first%20%3D%20np.full(6%2C%201%20%2F%206)%0A%20%20%20%20second%20%3D%20(1%20-%20bias.value)%20*%20np.full(6%2C%201%20%2F%206)%0A%20%20%20%20second%5B-1%5D%20%2B%3D%20bias.value%0A%20%20%20%20joint%20%3D%20np.outer(first%2C%20second)%0A%20%20%20%20sums%20%3D%20np.arange(2%2C%2013)%0A%20%20%20%20probabilities%20%3D%20np.convolve(first%2C%20second)%0A%20%20%20%20mean%20%3D%20float(sums%20%40%20probabilities)%0A%20%20%20%20return%20first%2C%20mean%2C%20probabilities%2C%20second%2C%20sums%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20distribution_chart(mean%2C%20mo%2C%20plt%2C%20probabilities%2C%20sums)%3A%0A%20%20%20%20distribution_figure%2C%20axes%20%3D%20plt.subplots(figsize%3D(7%2C%203))%0A%20%20%20%20axes.bar(sums%2C%20probabilities%2C%20color%3D%22%232b8cbe%22%2C%20width%3D0.75)%0A%20%20%20%20axes.set(xlabel%3D%22Sum%22%2C%20ylabel%3D%22Probability%22%2C%20xticks%3Dsums)%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20distribution_figure%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Expected%20sum%3A%20**%7Bmean%3A.2f%7D**.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Total%20probability%3A%20**%7Bprobabilities.sum()%3A.3f%7D**.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20joint_table(first%2C%20mo%2C%20second)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%7C%20X%20%2F%20Y%20%7C%201%20%7C%202%20%7C%203%20%7C%204%20%7C%205%20%7C%206%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20---%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7C%20%7Bi%20%2B%201%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%7C%20%22.join(f%22%7Bfloat(first%5Bi%5D%20*%20second%5Bj%5D)%3A.4f%7D%22%20for%20j%20in%20range(6))%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20%7C%22%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20range(6)%0A%20%20%20%20%20%20%20%20)%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
5d9041a9631d49fbdb7cf3fdaec7f94e