""" You can use Monte Carlo simulations to estimate quantities that are hard to calculate manually. In this demo, you will calculate Pi at a precision of two digits using only points placed randomly within a quarter circle. You will use the NaNoPy package introduced in chapter *Packages and NumPy* to visualize the simulation. Make sure that you have added it to your conda environment before proceeding with this exercise. *Background* Suppose you have a quarter circle of radius 1 embedded within a square of side-length 1. The quarter circle then has surface area `pi / 4` and the square has surface area 1. This means that a point picked at random within the square has probability `p_inside = pi / 4` to lie within the circle. We will use a Monte Carlo simulation to figure out this probability `p_inside` and use it to estimate pi with `pi_estimate = 4 * p_inside`. *Using NaNoPy* (1) (a) Define a scale for your visualization (e.g. 500). (b) Setup your Canvas to have `height=scale` and `width=scale`. (c) Create a Writer that can draw on the screen. (d) Draw a circle at (0, 0) with radius `scale`. """ import numpy as np from NaNoPy import Canvas, Writer, Color from time import sleep scale = 500 screen = Canvas("Let's Calculate Pi!", scale, scale) pen = Writer(screen) pen.draw_circle(0,0,scale,Color.white, filled=False) pen.draw_line(scale, 0, scale, scale, color=Color.white) screen.update() sleep(4) """ *Drawing random points to the screen* (2) (a) Define how many points you want to pick within your square. (b) For each point, generate a random coordinate with `x ∈ [0, 1)` and `y ∈ [0, 1)`. (c) After you generate a point, draw a pixel at its scaled up position to the screen and update the screen. Example: You generate coordinate (0.2, 0.5). You defined `scale=500`. Then the scaled up pixel location on the screen is (100, 250). """ """ *Calculating Pi* (3) (a) For each generated point, calculate its distance from the origin (0, 0). (b) Calculate the fraction of particles that lie within the quarter circle. (c) Estimate Pi using the formula: `pi_estimate = 4 * p_inside` (d) Calculate the standard error (`SE = standard_deviation / sqrt(n_points)`) and the radius of the 95% confidence interval (1.96 * SE). (e) Print your result as a full sentence. Hint: You may be underestimating the standard deviation of your result. """ """ *Separating logic from visualization* Updating the screen is slow and having to draw each point to the screen before generating the next point prevents us from using the vectorized versions of NumPy's random functions. If computer memory is not a concern, it is often a good idea to first calculate and then draw. Doing it this way gives you finer control about your code. (4) (a) Generate all points before drawing anything on the screen. (b) Estimate Pi *before* drawing anything to the screen. (c) Draw ten points to the screen at once between refreshes to speed up your visualization. """ """ *Advanced Bonus: Visualizing the statistics* You may wonder how our estimate for Pi evolves as we add more points. In this advanced bonus you will extend your screen horizontally and draw a graph that updates your estimate for every point generated. *Disclaimer: You should know how to define functions if you attempt this.* """