I confirmed with junior high school students whether it would be profitable with the double betting method (Martingale method) with Python

Introduction

My son, who likes math, seems to know how to double bet somewhere, and he explained it in a funny way.

[According to Wikipedia](https://en.wikipedia.org/wiki/%E3%83%99%E3%83%83%E3%83%86%E3%82%A3%E3] by the martingale method % 83% B3% E3% 82% B0% E3% 82% B7% E3% 82% B9% E3% 83% 86% E3% 83% A0)

The most classic and famous method, which has long been loved as a casino winning method. Also known as the double betting method. First, you bet 1 unit, if you lose, you bet 2 units, and if you lose, you bet 4 units, and if you win even once, you immediately return to 1 unit. Regardless of the number of trials, you will get 1 credit when you win. In many cases, you win a small amount, but when you lose, you lose a lot. If you lose continuously, you will soon reach the upper limit of betting called puncture or table limit.

I knew that it wouldn't work.

However, I hadn't tried how to actually lose, so I decided to try it. Try without using probability so that the son of a junior high school student can understand.

Bet function

import random

def play():
    a = random.randint(0, 1)
    if a == 0:
        return 'win'
    else:
        return 'lose'

Define a function that returns the result of the game.

def test():
    win = 0
    lose = 0
    for i in range(1000):
        if play() == 'win':
            win += 1
        else:
            lose += 1

    print('win: ' + str(win) + ' , lose: ' + str(lose))
    
for i in range(10):
    test()

The result of trying this 1,000 times is displayed 10 times to check how the results are scattered.


win: 498 , lose: 502
win: 491 , lose: 509
win: 479 , lose: 521
win: 509 , lose: 491
win: 513 , lose: 487
win: 520 , lose: 480
win: 521 , lose: 479
win: 495 , lose: 505
win: 526 , lose: 474
win: 524 , lose: 476

It is like this. There is no problem because the winning percentage is around 50%.

Enforcement of the Martingale Act

Since he is a junior high school student, he cannot gamble, but he will use a fair amount of 10,000 yen as military funds. The first stake is 100 yen.

If you lose, double the bet and repeat until you win.

def martingale(money, bet):
    times = 1
    while(True):
        if play() == 'win':
            money += bet
            return money, times
        else:
            money -= bet
            bet += bet
            if money <= bet:
                return money, times
        times += 1

Try 10 times and display the result.

def test():
    money, times = martingale(10000, 100)
    
    if money < 0:
        print('Lose at: ' + str(times) + ', money: ' + str(money))
    else:
        print('Win at: ' + str(times) + ', money: ' + str(money))

for i in range(10):
    test()

The result.

Win at: 4, money: 10100
Win at: 3, money: 10100
Win at: 1, money: 10100
Win at: 2, money: 10100
Win at: 2, money: 10100
Win at: 4, money: 10100
Win at: 1, money: 10100
Win at: 1, money: 10100
Win at: 3, money: 10100
Win at: 2, money: 10100

Actually, I did this many times, but sometimes I lost. You can definitely win. As my son thinks.

When repeated

I want to see how long this can last. It ends when the amount is doubled to compare with the case of betting 10,000 yen at one time. In addition, if the amount is doubled and the stake is insufficient, the policy is to start over from 100 yen.

def iterate(money, bet):
    day = 1
    duble = money * 2
    while(True):
        money, times = martingale(money, 100)
        if money <= 0:
            return money, day
        elif duble <= money:
            return money, day
        day += 1

I prefer ʻelse if` to writing or conditions. If the post-judgment process is likely to be complicated, write a function that returns the judgment result. I don't care ...

Treat the number of repetitions as the number of days. If you start from 100 yen with 10,000 yen as military funds, it will double in 100 days and end. If you lose even once

Now, if you try this 10 times as usual ...

def test():
    money, day = iterate(10000, 100)
    
    print('day: ' + str(day) + ', money: ' + str(money))

for i in range(10):
    test()

Result is

day: 164, money: 20000
day: 100, money: 20000
day: 164, money: 20000
day: 100, money: 20000
day: 98, money: 0
day: 64, money: 0
day: 100, money: 20000
day: 90, money: 0
day: 104, money: 0
day: 20, money: 0

Hmm? It's 5 wins and 5 losses. I will lose quite a bit. It's my personal preference whether I should bet 10,000 yen from the beginning or feel that I could play a lot, but I wanted to check the actual winning percentage.

Try a little more

So, I will display about 10 times to try about 1000 times.

def test():
    win = 0
    lose = 0
    for i in range(1000):
        money, day = iterate(10000, 100)
        if 0 < money:
            win += 1
        else:
            lose += 1
            
    print('win: ' + str(win) + ', lose: ' + str(lose))

for i in range(10):
    test()

I tried it on paiza.io, but it became TimeOut, so I divided it in half and executed it twice.

win: 510, lose: 490
win: 498, lose: 502
win: 502, lose: 498
win: 467, lose: 533
win: 496, lose: 504
win: 498, lose: 502
win: 500, lose: 500
win: 527, lose: 473
win: 500, lose: 500
win: 508, lose: 492

The winning percentage is almost 50%.

I tried to increase military funds

So what about 1 million yen?

def test():
    win = 0
    lose = 0
    for i in range(1000):
        money, day = iterate(1000000, 100)
        if 0 < money:
            win += 1
        else:
            lose += 1
            
    print('win: ' + str(win) + ', lose: ' + str(lose))

for i in range(10):
    test()

It was a heavy process as the number of bets increased.

win: 507, lose: 493
win: 489, lose: 511
win: 490, lose: 510
win: 521, lose: 479
win: 497, lose: 503
win: 467, lose: 533
win: 493, lose: 507
win: 497, lose: 503
win: 496, lose: 504
win: 510, lose: 490

As a result, the winning rate is 50%, which means that the result does not change even if you bet at once or double bet and increase the number of times.

Even if you prepare 1 million yen to make 100 yen, it seems that there is no point in increasing the funds any more. It was a shame, my son.

Summary

It's a program to explain to my son in junior high school, so I decided to actually try it, not probability. He may have been able to explain the calculations because he has a high level of math skills, but he was also likely to run into problems with my abilities ...

As a matter of fact, if you bet twice, the probability of losing is halved, but since the amount of money you lost is doubled, the winning rate when you repeat is the same as when you bet at once. It's natural to be calm, but when it comes to winning, you'll be fooled. (It's the same as a perpetual motion)

In that sense, actually trying it seemed to be a very effective method.

Addendum-I later learned that this verification method is called the Monte Carlo method. (I only knew the name) I'm not confident, so please comment if anyone says that this is true.

Impressions

I wrote it in python, which I'm not used to. Another son was writing python and was a little interested. I feel a little quirky when writing, but it should be easy to read.

This is a secret, but the first program I built had a bug in the boundary conditions and the calculation result was a little profitable ...

Recommended Posts

I confirmed with junior high school students whether it would be profitable with the double betting method (Martingale method) with Python
I tried to refactor the code of Python beginner (junior high school student)
I tried to remodel the code of Python beginner (junior high school student) into object-oriented crunchy
Life game with Python [I made it] (on the terminal & Tkinter)