import%20marimo%0A%0A__generated_with%20%3D%20%220.13.15%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Estimating%20%CF%80%20using%20Monte%20Carlo%0A%0A%20%20%20%20This%20notebook%20demonstrates%20how%20to%20estimate%20the%20value%20of%20%CF%80%20(pi)%20using%20a%20Monte%20Carlo%20method.%0A%0A%20%20%20%20**How%20it%20works%3A**%0A%20%20%20%201.%20We%20generate%20random%20points%20within%20a%20square%20with%20side%20length%202%2C%20centered%20at%20the%20origin.%0A%20%20%20%202.%20We%20count%20how%20many%20points%20fall%20within%20a%20circle%20of%20radius%201%2C%20also%20centered%20at%20the%20origin.%0A%20%20%20%203.%20The%20ratio%20of%20points%20inside%20the%20circle%20to%20the%20total%20number%20of%20points%2C%20multiplied%20by%204%2C%20approximates%20%CF%80.%0A%0A%20%20%20%20This%20works%20because%20the%20ratio%20of%20the%20area%20of%20the%20circle%20(%CF%80r%C2%B2)%20to%20the%20area%20of%20the%20square%20(4r%C2%B2)%20is%20%CF%80%2F4.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Create%20a%20slider%20to%20control%20the%20number%20of%20points%0A%20%20%20%20num_points%20%3D%20mo.ui.slider(3%2C%207%2C%20step%3D1%2C%20value%3D3%2C%20label%3D%22Number%20of%20points%2010%5E%7Bn%7D%22)%0A%0A%20%20%20%20num_points%0A%0A%20%20%20%20return%20(num_points%2C)%0A%0A%0A%40app.cell%0Adef%20_(num_points)%3A%0A%20%20%20%20%23%20Generate%20random%20points%20in%20a%202x2%20square%20centered%20at%20the%20origin%0A%20%20%20%20np.random.seed(42)%20%20%23%20For%20reproducibility%0A%20%20%20%20n%20%3D%2010**num_points.value%0A%20%20%20%20print(n)%0A%20%20%20%20points%20%3D%20np.random.uniform(-1%2C%201%2C%20(n%2C%202))%0A%0A%20%20%20%20%23%20Calculate%20distances%20from%20origin%0A%20%20%20%20distances%20%3D%20np.sqrt(points%5B%3A%2C%200%5D%20**%202%20%2B%20points%5B%3A%2C%201%5D%20**%202)%0A%0A%20%20%20%20%23%20Determine%20which%20points%20are%20inside%20the%20circle%0A%20%20%20%20inside_circle%20%3D%20distances%20%3C%3D%201%0A%0A%20%20%20%20%23%20Count%20points%20inside%20the%20circle%0A%20%20%20%20count_inside%20%3D%20np.sum(inside_circle)%0A%0A%20%20%20%20%23%20Estimate%20pi%0A%20%20%20%20pi_estimate%20%3D%204%20*%20count_inside%20%2F%20n%0A%0A%20%20%20%20%23%20Create%20points%20dataframe%20for%20plotting%0A%20%20%20%20points_inside%20%3D%20points%5Binside_circle%5D%0A%20%20%20%20points_outside%20%3D%20points%5B~inside_circle%5D%0A%0A%20%20%20%20return%20pi_estimate%2C%20points_inside%2C%20points_outside%0A%0A%0A%40app.cell%0Adef%20_(pi_estimate%2C%20points_inside%2C%20points_outside)%3A%0A%20%20%20%20%23%20Create%20a%20scatter%20plot%20with%20plotly%0A%20%20%20%20fig%20%3D%20go.Figure()%0A%0A%20%20%20%20%23%20Add%20points%20inside%20the%20circle%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dpoints_inside%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpoints_inside%5B%3A%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3D%7B%22color%22%3A%20%22blue%22%2C%20%22size%22%3A%205%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Inside%20Circle%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20points%20outside%20the%20circle%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dpoints_outside%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpoints_outside%5B%3A%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3D%7B%22color%22%3A%20%22red%22%2C%20%22size%22%3A%205%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Outside%20Circle%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Draw%20the%20circle%0A%20%20%20%20theta%20%3D%20np.linspace(0%2C%202%20*%20np.pi%2C%20100)%0A%20%20%20%20x%20%3D%20np.cos(theta)%0A%20%20%20%20y%20%3D%20np.sin(theta)%0A%0A%20%20%20%20fig.add_trace(go.Scatter(x%3Dx%2C%20y%3Dy%2C%20mode%3D%22lines%22%2C%20line%3D%7B%22color%22%3A%20%22black%22%2C%20%22width%22%3A%202%7D%2C%20name%3D%22Circle%22))%0A%0A%20%20%20%20%23%20Update%20layout%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3Df%22Monte%20Carlo%20Estimation%20of%20%CF%80%20%3D%20%7Bpi_estimate%3A.6f%7D%20(True%20value%3A%20%7Bnp.pi%3A.6f%7D)%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y%22%2C%0A%20%20%20%20%20%20%20%20yaxis%3D%7B%22scaleanchor%22%3A%20%22x%22%2C%20%22scaleratio%22%3A%201%7D%2C%0A%20%20%20%20%20%20%20%20width%3D700%2C%0A%20%20%20%20%20%20%20%20height%3D700%2C%0A%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20square%20boundary%0A%20%20%20%20fig.add_shape(%0A%20%20%20%20%20%20%20%20type%3D%22rect%22%2C%0A%20%20%20%20%20%20%20%20x0%3D-1%2C%0A%20%20%20%20%20%20%20%20y0%3D-1%2C%0A%20%20%20%20%20%20%20%20x1%3D1%2C%0A%20%20%20%20%20%20%20%20y1%3D1%2C%0A%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20%22black%22%2C%20%22width%22%3A%202%7D%2C%0A%20%20%20%20%20%20%20%20fillcolor%3D%22rgba(0%2C0%2C0%2C0)%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pi_estimate)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%23%23%20Results%0A%0A%20%20%20%20-%20Estimated%20value%20of%20%CF%80%3A%20**%7Bpi_estimate%3A.6f%7D**%0A%20%20%20%20-%20True%20value%20of%20%CF%80%3A%20**%7Bnp.pi%3A.6f%7D**%0A%20%20%20%20-%20Absolute%20error%3A%20**%7Babs(pi_estimate%20-%20np.pi)%3A.6f%7D**%0A%20%20%20%20-%20Relative%20error%3A%20**%7B100%20*%20abs(pi_estimate%20-%20np.pi)%20%2F%20np.pi%3A.4f%7D%25**%0A%0A%20%20%20%20%23%23%23%20How%20to%20improve%20the%20estimate%3F%0A%0A%20%20%20%20The%20accuracy%20of%20the%20Monte%20Carlo%20method%20improves%20as%20the%20number%20of%20points%20increases.%0A%20%20%20%20Try%20adjusting%20the%20slider%20to%20see%20how%20the%20estimate%20changes%20with%20more%20points.%0A%0A%20%20%20%20The%20error%20in%20this%20method%20decreases%20proportionally%20to%201%2F%E2%88%9An%2C%20where%20n%20is%20the%20number%20of%20points.%0A%20%20%20%20This%20means%20you%20need%20to%20quadruple%20the%20number%20of%20points%20to%20halve%20the%20error.%0A%20%20%20%20%22%22%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
f4df80130fdea62e4ab4569672c69c3f792b61fa2e1c77fbf892d7827643cb35