48

A Monte Carlo Experiment For Calculating The Expected Value Of A Full Dice Roll

 4 years ago
source link: https://www.tuicool.com/articles/IJ77r2a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is the expected value of a dice roll until seeing all 6 numbers? A mathematical and programmatical explanation.

IJ73YvI.jpg!web

Monte Carlo, Wikipedia Commons by MartinP.

This question is a frequent flyer in many interviews, the following is a programmatic approach to solve it using random sampling. The idea is, to run many experiments, in which you roll the dice (sample from a uniform distribution) until you see all 6 numbers that appear in a normal and fair dice. Finally, you calculate the Expected Value .

The Math

Let’s talk about chance, the chance to roll a number for the first time is 1, the chance for seeing the second number is 5/6, etc.

We can generalize using this formula:

In order to solve the question and due to the fact that the results are geometrically distributed . The probability for a number to appear is 1/p, which means that each calculated figure from the formula above should be placed instead of ‘p’ as the denominator.

Finally, we calculate the mean time needed to roll the dice until we see every number, which is 14.7.

The Code

We can do the same using a few lines of code, by running a Monte-Carlo experiment, i.e., 10K random experiments, we sample numbers from a uniform distribution, between 1 and 6, and we calculate how many times, in each experiment, it took us to roll all 6 numbers. The mean for all of these experiments is the expected value. Which will be around 14.7, you can change the number of rolls and see the effect it has in this simulated environment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK