Notes on Monte Carlo Simulation

March 14, 2021
machine_learning python

Some notes taken from Prof. Guttag excellent discussion on the topic.

The technique was first developed by Stanislaw Ulam, a mathematician who worked on the Manhattan Project.

A method of estimating the value of an unknown quantity using the principles of inferential statistics.

Inferential Statistics

Confidence depends on:

Roulette Considerations

Gambler’s Fallacy: if a particular event occurs more frequently than normal during the past it is less likely to happen in the future (or vice versa), when it has otherwise been established that the probability of such events does not depend on what has happened in the past.

Regression to the mean: Following an extreme random event, it is likely that the next random event will be less extreme.

Quantifying Variation

$$ variance(X) = \frac{\sum_{x\in X}(x - \mu)^2}{|X|} $$

The mean $$\mu$$

$$\sigma(x) = \sqrt{variance(X)}$$

Empirical Rule

Assumptions

Probability Density Function

import random

class FairRoulette():
    def __init__(self):
        self.pockets = []
        for i in range(1, 37):
            self.pockets.append(i)
        
        self.ball=None

        self.pocket_odds = len(self.pockets) - 1

        random.seed(0)

    def spin(self):
        self.ball = random.choice(self.pockets)

    def bet_pocket(self, pocket, amount):
        '''
        pocket: pocket placing bet
        amount: sum being bet
        '''
        if str(pocket) == str(self.ball):
            return amount * self.pocket_odds
        else:
            return -amount
    def __str__(self) -> str:
        return 'fair roulette'

class EURoulette(FairRoulette):
    def __init__(self):
        super().__init__()
        self.pockets.append('O')
    def __str__(self) -> str:
        return 'EU Roulette'

class USRoulette(EURoulette):
    def __init__(self):
        super().__init__()
        self.pockets.append('OO')
    def __str__(self) -> str:
        return 'US Roulette'


def play_roulette(game, num_spins, pocket, bet):
    '''
    Arguments:
    game: Roulette game being played
    num_spins: number of spins for the simulation
    pocket: pocket placing bet
    bet: amount of bet
    '''
    total_pocket = 0
    for i in range(num_spins):
        game.spin()
        total_pocket += game.bet_pocket(pocket, bet)

    print(f'{num_spins} spins of {game}')
    print(f'expected return betting {pocket} = {str(100*total_pocket/num_spins)}%')

    return total_pocket/num_spins

if __name__ == "__main__":
    game = FairRoulette()
    for num_spins in (100, 1000000):
        for i in range(3):
            # betting 1 dollar on number 2 for num-spins trials
            play_roulette(game, num_spins, 2, 1)

Simple Python implementation of the Weiszfeld algorithm

March 14, 2021
machine_learning python

Weiszfeld Algorithm

March 12, 2021
machine_learning

Roc Display in Excel

October 3, 2020
machine_learning
comments powered by Disqus