Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Your First Simulation: Estimating Pi

Estimate pi by throwing random darts at a square, see why the idea works, and vectorize the code with NumPy for speed.

The classic first Monte Carlo experiment estimates the number pi by throwing imaginary darts. It needs only a bit of geometry, yet it contains every idea used in serious simulations: random points, a yes/no test, and an average. In this lesson you will build it twice, once with plain loops and once with NumPy.

The geometry

Draw a square with corners at (-1, -1) and (1, 1). Its side is 2, so its area is 4. Inside it, draw the circle of radius 1 centered at the origin; its area is ฯ€ ยท 1ยฒ = ฯ€.

If a dart lands at a uniformly random spot in the square, the chance that it lands inside the circle is the ratio of areas:

Text
P(inside) = area of circle / area of square = ฯ€ / 4

So ฯ€ = 4 ยท P(inside). Estimate P(inside) by throwing many darts and counting, then multiply by 4.

A point (x, y) is inside the circle when xยฒ + yยฒ โ‰ค 1.

flowchart TD
  A["Pick x and y uniformly in -1 to 1"] --> B{"xยฒ + yยฒ โ‰ค 1 ?"}
  B -->|"yes"| C["count a hit"]
  B -->|"no"| D["count a miss"]
  C --> E["pi โ‰ˆ 4 ร— hits / N"]
  D --> E

One dart: a random point and a yes/no test.

Plain Python version

Python Dart-throwing estimate of pi

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The error generally shrinks as N grows, but not steadily. A lucky small run can beat an unlucky big one. That is normal for random estimates.

Vectorizing with NumPy

Python loops are slow. NumPy lets you generate all the darts at once as arrays and test them with a single expression. A vectorized version is often 50 times faster, which lets you use millions of points.

Python Vectorized pi estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

inside is an array of True/False values; its mean() is the fraction of True, exactly hits / n. Averaging a 0/1 indicator gives a probability, a trick you will use constantly.

Seeing the darts

A picture makes the method concrete. This plot draws 2,000 darts, blue inside and orange outside.

Python Plot the darts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How accurate is it?

The estimate of P(inside) is an average of many 0/1 values, so its error behaves like 1 / โˆšN. To gain one more correct decimal digit you need 100 times more darts. That slow rate is the price of the method, and the reason much of this course is about getting more accuracy from fewer samples. The next two lessons explain the error precisely.

Pi has far better algorithms than dart throwing; this is a teaching example. The value of the experiment is the method, which works for shapes and integrals where no better formula exists.

Common mistakes

  • Forgetting the factor 4. The hit fraction estimates ฯ€/4, not ฯ€.
  • Sampling the wrong region. If darts fall in a square of side 2, the area ratio is ฯ€/4. In the unit square [0,1]ยฒ, use the quarter circle and the same factor 4.
  • Using < or <= carelessly. For continuous random points the boundary has probability zero, so it makes no difference here.
  • Reusing a tiny N and trusting it. Always vary N to see how stable the answer is.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Darts land uniformly in the square [-1, 1] ร— [-1, 1]. What is the probability that a dart lands inside the unit circle?

    The circle has area ฯ€ and the square has area 4, so the probability is ฯ€ / 4.
  2. 2. In an experiment, 7,850 of 10,000 darts land inside the circle. What is the estimate of pi?

    pi โ‰ˆ 4 ร— 7850 / 10000 = 3.14.
  3. 3. To get roughly one more correct digit of accuracy, about how many more samples do you need?

    The error shrinks like 1/โˆšN, so cutting the error by 10 needs 100 times as many samples.
  4. 4. Why is the NumPy version faster than the loop version?

    Vectorized operations run in optimized compiled code over whole arrays.

Summary

  • Throwing random points into a square and testing xยฒ + yยฒ โ‰ค 1 estimates ฯ€/4, so ฯ€ โ‰ˆ 4 ร— hits / N.
  • Averaging a 0/1 indicator gives a probability.
  • Vectorizing with NumPy makes millions of samples practical.
  • The error shrinks like 1/โˆšN: ten times more accuracy costs a hundred times more work.
  • The same recipe (random points, a test, an average) powers all Monte Carlo methods.

Next: The Law of Large Numbers and Simulation Error