Course 3.1Lesson 2 of 5
The Monte Carlo method estimates a quantity by random trials. The probability of an event is estimated as the share of trials in which it happens: for two dice, the sum is 7 or more in 58.4 percent of 200 000 trials, near the exact 21 over 36, and a group of 23 people shares a birthday with a probability of about 0.5. Throwing random points at a square and counting the share inside a quarter circle gives pi over 4, so four times the share estimates pi. The error of a Monte Carlo estimate is about proportional to one over the square root of the number of trials, so a hundred times more trials give an estimate that is ten times more accurate, which is slow, but the method works for problems with no formula. The accuracy can be seen by repeating the whole experiment.
Try the code and the simulations beside the lesson.
After this lesson you can
The Monte Carlo method, named after the casino, uses random trials to find an answer. The idea is simple: to find the probability of an event, do the experiment many times and count how often the event happens. By the law of large numbers, the share of successes comes near the probability.
This is useful when the calculation is hard. In physics, Monte Carlo methods follow the paths of particles through a detector, and the neutrons in a reactor. In finance, they price options. In every case, the idea is the one you see here.
Predict first. Of the 36 pairs of two dice, 21 have the sum 7 or more. What probability do you expect? Then run it.
The simulation says 0.584 and the exact value is . For the sum equal to 7: 0.166, and the exact value is . The simulation agrees with the exact answer to about three decimals. The simulation is easy to adapt: for three dice, or for a sum above 15, you change one line, while the exact count by hand gets harder.
Predict first. In a group of 23 people, what is the chance that two have the same birthday? Guess before you run it.
How the program works: each row of days is one group, with a random birthday (0 to 364) for each person. days.sort(axis=1) sorts each row, so that equal birthdays are next to each other, and np.diff(days, axis=1) == 0 finds a neighbour that is equal. .any(axis=1) asks whether a group has at least one such pair, and .mean() is the share of such groups.
You can find by throwing darts. Imagine a square of side 1, and inside it a quarter of a circle of radius 1. The square has the area 1, and the quarter circle has the area . If you throw points at random into the square, the share that land inside the quarter circle is . So that share.
The curve is a close formula. The code checks it the Monte Carlo way: make 10,000 random groups and count.
Predict first. If you use 100 times more points, how much more accurate will the answer be?
A hundred times more points makes the answer only about 10 times more accurate: the printed ratios are 9.8 and 8.1. The error of a Monte Carlo estimate falls as
where is the number of trials. This is slow. To get one more correct digit, you need 100 times as many trials. So Monte Carlo is not the best way to find : there are quick formulas for that. It is the best way to solve problems that have no quick method, such as problems in many dimensions. And the law is also true for the errors of measurements, as the last lesson shows.
True is the mean of the comparison.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.