🔁 Monte Carlo Method
🔁 Monte Carlo Method
Updated at 2017-07-11 16:06
The Monte Carlo Method: Pouring out a box of coins on a table and counting coins that land heads versus tails to reach to the conclusion of 50/50 probability. 🪙
Monte Carlo method is the brute force of probability mathematics. It's most useful when it is challenging to create probability formula. But can be used for simplicity, or if you feel lazy.
var heads = 0;
var tails = 0;
// 3 million trials should be enough, right?
for (var i = 0; i < 3000000; i++) {
if (Math.random() <= 0.5) {
heads++
} else {
tails++
}
}
console.log(heads, tails);
Monte Carlo relies on the law of large numbers. With a decent random generator and enough trials, you can get probability of anything you can simulate.
Example; Monte Carlo method vs. actual math. We want to get probability of drawing one of two specific cards in a 30-card deck where starting hand is 3 cards.
from copy import copy
from random import shuffle
trials = 30000
success_count = 0
cards_in_deck = list(range(0, 30))
for i in range(0, trials):
deck = copy(cards_in_deck)
shuffle(deck)
hand = [deck.pop(), deck.pop(), deck.pop()]
if any(card == 0 for card in hand) or any(card == 1 for card in hand):
success_count += 1
print('{}%'.format(success_count / trials * 100))
# => around 19%, which is close what hypergeometric distribution gives us
from operator import mul
from functools import reduce
from fractions import Fraction
def combinations(n, k):
return int(reduce(mul, (Fraction(n - i, i + 1) for i in range(k)), 1))
def hypergeometric(population, wins, sample_size, required_successes):
losses = population - wins
sample_successes = combinations(wins, required_successes)
sample_losses = combinations(losses, sample_size - required_successes)
all_combinations = combinations(population, sample_size)
return sample_successes * sample_losses / all_combinations
print('{}%'.format((hypergeometric(30, 2, 3, 1) * 100)))
# => 18.620689655172416%
# which you can double check http://stattrek.com/online-calculator/hypergeometric.aspx
# => 18.6206896551724%