[PYTHON] You will be an engineer in 100 days --Day 65 --Programming --Probability 3

Click here until yesterday

You will become an engineer in 100 days --Day 63 --Programming --Probability 1

You will become an engineer in 100 days-Day 59-Programming-Algorithms

You will become an engineer in 100 days --- Day 53 --Git --About Git

You will become an engineer in 100 days --Day 42 --Cloud --About cloud services

You will become an engineer in 100 days --Day 36 --Database --About the database

You will be an engineer in 100 days-Day 24-Python-Basics of Python language 1

You will become an engineer in 100 days --Day 18 --Javascript --JavaScript basics 1

You will become an engineer in 100 days --Day 14 --CSS --CSS Basics 1

You will become an engineer in 100 days --Day 6 --HTML --HTML basics 1

Probability of being familiar

Let's think about the problem of probability that is relatively familiar by programming.

What is the probability of having students with the same birthday in a 30-person class?

I think the number of elementary and junior high school classes is about this The probability that there is even one pair of people with the same birthday in a 30-person class How long will it be?

Probability that even one set has the same birthday = 1 --Probability that everyone's birthday is different

If it is a calculation formula

1- \frac{365 \times 364 \times 363 \times ... (365-29) }{365^{30}}

Will be.

It's annoying, so let's solve it with Python.

from functools import reduce
from operator import mul

n = 30
a1 = [i for i in range(365,365-n,-1)]
a2 = [365 for i in range(n)]
print('{:%}'.format(1- reduce(mul, a1)/reduce(mul, a2)))

70.631624%

It's about 70%. If you get 30 people, there is a 70% chance that one pair will celebrate the same birthday.

** What is the probability of up to 50 people? ** **

The figures are shown together.

from functools import reduce
from operator import mul
import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(16,5))
x,y = [n for n in range(2,51)] , []
for n in x:
    a1 = [i for i in range(365,365-n,-1)]
    a2 = [365 for i in range(n)]
    y.append(1-reduce(mul, a1)/reduce(mul, a2))

plt.bar(x,y)
plt.grid()
plt.show()

download.png

40% for 20 people About 97% in a 50-person class There seems to be someone with the same birthday.

If it's a company, I think there are 100 people, so there should be people with almost the same birthday.

Gacha with a probability of hitting 1%, what is the probability of losing 100 times in a row?

If you hit with a 1% chance when you draw a gacha, the probability of losing 100 times in a row How long will it be?

Is it sure to hit because I'm pulling only this? !! ?? !!

Probability of falling off 100 times in a row = Probability of pulling off once ^ (100)

In other words

(\frac{99}{100})^{100}

is.

When I calculate the ratio

print('{:%}'.format((99/100)**100))

36.603234%

Don't you think it will come off with a good probability?

Is it really a triple festival! !! !! There are times when I think.

** How much do you pull and how likely are you to win? **

Probability of hitting = 1-Probability of losing So, let's think about how many times you pull and how much you will win.

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(16,5))
x,y = [n for n in range(1,501)],[]
for n in x:
    y.append(1-(99/100)**n)

plt.bar(x,y)
plt.grid()
plt.show()

download-1.png

In this case, if you pull it 100 times, 30% will come off. After pulling 200 times, the hit will finally exceed 80%.

If you pull it 500 times, it seems that it will almost never come off. However, it is not a sure hit, so if you are unlucky, you may miss it.

Is there really a 1% chance of winning? Let's simulate.

Count the number of times you hit the gacha 100 times with a 1% chance.

import random
import matplotlib.pyplot as plt
%matplotlib inline

weights = [0.99,0.01]
d = {b:0 for b in range(101)}
for p in range(10000):
    a = 0
    for i in range(100):
        tf = random.choices([False,True], weights=weights)[0]
        if tf:
            a=i+1
            break
    d[a]+=1

plt.figure(figsize=(16,5))
x = list(d.keys())
y = list(d.values())
plt.bar(x,y)
plt.grid()
plt.show()

download-2.png

0 is the number of people who missed, and the others are the number of times they hit.

With the above probability, 36% of the people will be out of the game, so if it is 10,000, there will be about 3600 people out of the game.

If you take a survey of about 10,000 people and the number of people who missed this is even smaller It seems that the probability of gacha is being manipulated, In the first place, it is difficult to take a questionnaire to a large number of people.

What is the probability that the Japan Series will get tangled until the final race?

It's about baseball. The winner of the Japan Series is the one who bought it four times first, and it will be held seven times in total. When the probability of winning or losing is 50% for both Central League and Pacific League What is the probability of getting entangled until the 7th round?

It seems good to consider all combinations to find the probability. Because it will end when 4 wins or 4 losses come out Combinations that have not been matched up to 6 races

import itertools

p4 = [i for i in itertools.product(['Win','negative'],repeat=4) if i.count('Win')==4 or i.count('negative')==4]#Ends in 4 races
p5 = [i for i in itertools.product(['Win','negative'],repeat=5) if (i[0:4].count('Win')<4 and i[0:4].count('negative')<4) and (i.count('Win')==4 or i.count('negative')==4)]#Ends in 5 races
p6 = [i for i in itertools.product(['Win','negative'],repeat=6) if (i[0:5].count('Win')<4 and i[0:5].count('negative')<4) and (i.count('Win')==4 or i.count('negative')==4)]#Ends in 6 races
p7 = [i for i in itertools.product(['Win','negative'],repeat=6) if i[0:6].count('Win')<4 and i[0:6].count('negative')<4]#Does not end in 6 races

print('Ends in 4 races' , len(p4))
for a in p4:
    print(' '.join(a))
print('Ends in 5 races' , len(p5))
for a in p5:
    print(' '.join(a))
print('Ends in 6 races' , len(p6))
for a in p6:
    print(' '.join(a))
print('Does not end in 6 races' , len(p7))
for a in p7:
    print(' '.join(a))

Ends in 4 races 2 Win win win win Negative Negative Negative

Ends in 5 races 8 Win Win Win Win Win Win Win Win Win Win Win Win Game Negative Negative Negative win win win Negative match Negative Negative Negative Game Negative Negative Negative Negative Game

Ends in 6 races 20 Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Game Negative Negative Win Win Win Win Win Win Win Win Win Game game game game game Win Win Win Win Game Negative Game Negative Game game game game game Negative win win win defeat Negative win win defeat win Negative Win Win Negative Negative Negative win Negative win Won Negative game Negative game Negative Negative game Negative game Negative Negative Win Win Win Negative Negative Win Negative Negative Negative Game Negative Game Negative Negative Negative Win

Does not end in 6 races 20 Win Win Win Win Negative Win Game Win Game Win Game Win Game Win Win Game Defeat Win Game game game game game Game game game game Game game game game game Game game game game game Game game game game game Win game win game win Negative Win Win Win Negative Negative Victory Victory Negative win Won defeat Negative game Negative game Negative win Negative win Negative win Negative win Negative Negative Win Win Win Negative defeat win defeat Negative Negative Win Negative Win Negative Negative Negative Win Win

Combination that does not end in 6 races / Combination up to 6 races Considering the combination, 20 / (2 + 8 + 20 + 20) is 0.4 = 40%.

Considering the combination that can be played in 7 games, the combination that does not end in these 6 games There are 40 ways, which is twice as many as 20.

Considering that the probability of winning or losing is one half 40 ways × (1/2) ^ 7 = 5/16 = 31.25%

Considering the probability, 31.25% seems to go to the 7th round.

In any case, 30% will be entangled until the 7th round.

Summary

If you think about various probability problems and incorporate them into the program I think you will improve your programming.

Let's solve various problems.

35 days until you become an engineer

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

You will be an engineer in 100 days --Day 63 --Programming --Probability 1
You will be an engineer in 100 days --Day 65 --Programming --Probability 3
You will be an engineer in 100 days --Day 64 --Programming --Probability 2
You will be an engineer in 100 days ――Day 71 ――Programming ――About scraping 2
You will be an engineer in 100 days ――Day 61 ――Programming ――About exploration
You will be an engineer in 100 days ――Day 74 ――Programming ――About scraping 5
You will be an engineer in 100 days ――Day 73 ――Programming ――About scraping 4
You will be an engineer in 100 days ――Day 75 ――Programming ――About scraping 6
You will be an engineer in 100 days --Day 68 --Programming --About TF-IDF
You will be an engineer in 100 days ――Day 70 ――Programming ――About scraping
You will be an engineer in 100 days ――Day 82 ――Programming ――About machine learning 7
You will be an engineer in 100 days ――Day 79 ――Programming ――About machine learning 4
You will be an engineer in 100 days ――Day 76 ――Programming ――About machine learning
You will be an engineer in 100 days ――Day 80 ――Programming ――About machine learning 5
You will be an engineer in 100 days ――Day 78 ――Programming ――About machine learning 3
You will be an engineer in 100 days ――Day 84 ――Programming ――About machine learning 9
You will be an engineer in 100 days ――Day 83 ――Programming ――About machine learning 8
You will be an engineer in 100 days ――Day 77 ――Programming ――About machine learning 2
You will be an engineer in 100 days ――Day 85 ――Programming ――About machine learning 10
You will be an engineer in 100 days --Day 27 --Python --Python Exercise 1
You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3
You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2
You will be an engineer in 100 days ――Day 24 ―― Python ―― Basics of Python language 1
You will be an engineer in 100 days ――Day 30 ―― Python ―― Basics of Python language 6
You will be an engineer in 100 days ――Day 25 ―― Python ―― Basics of Python language 2
You will be an engineer in 100 days ――Day 60 ――Programming ――About data structure and sorting algorithm
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
You become an engineer in 100 days ――Day 67 ――Programming ――About morphological analysis
You become an engineer in 100 days ――Day 66 ――Programming ――About natural language processing
If you draw an Omikuji with a probability of 1% 100 times in a row, will you win once?
If you write the View decorator in urls.py in Django, the list will be higher.
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1