Studying Mathematics in Python: Solving Simple Probability Problems

probability

There is a problem that it is not related to Python, but Set the settings so that you are happy with the calculator and solve.

(Q1)

The player pulled out one of the 52 playing cards excluding the joker and placed it face down. When the dealer randomly picked out 3 cards from the remaining mountains and turned them to the front, they were all diamonds.

  1. A person from the manufacturer of playing cards came and told me that the playing cards I am using now are defective products that have duplicate A of diamonds instead of A of hearts. Find the probability that the first card you pull out is a diamond.
  2. As additional information, it was found that the dealer had attached a gun and deliberately extracted only the diamond. Find the probability that the first card you extracted is a diamond.

(Solution)

  1. Conditional probability problem.
P_0 = sym.Rational(1, 4)
P_0dia = sym.Rational(13*12*11, 51*50*49)
P_dia = sym.Rational(14*13*12, 52*51*50)
P_0 * P_0dia / P_dia
\frac{143}{686}
  1. \frac{7}{26}

(Q2)

There is a lottery with a winning probability of $ p $. If all the people on the earth draw lots once, the probability of losing all will be 0.5 or more How much does $ p $ have to be? The population of the earth is 7.2 billion.

(Solution) Approximate with Poisson distribution.

N = 7200000000
F = sym.exp(-p*N) - 0.5
sym.solve([F], [p])
p < 9.627 \cdot 10^{-11}

By the way, if you try this much, the normal distribution will be about 7 times the standard deviation. In the Pareto distribution, it is not strange that values that are more than 100 million times apart appear.

(Q3)

  1. There are 12 red balls and 8 black balls in the jar. Find the probability that there are two red balls when you take out three balls.
  2. Try to take out $ 3 \ cdot 10 ^ {24} $ red balls and $ 2 \ cdot 10 ^ {24} $ black balls. Find the probability that the number of red balls taken out is $ 2 \ cdot 10 ^ {24} $.
  3. When trying (2), let $ x $ be the absolute value of the difference between the number of red balls taken out and $ 2 \ cdot 10 ^ {24} $. If the probability that the result will be less than $ x $ is $ P_x $, how much is $ x $ for $ P_x> 0.5 $?

(Solution)

  1. This is called a hypergeometric distribution.
import sympy as sym
A = sym.functions.combinatorial.numbers.nC(12, 2)
B = sym.functions.combinatorial.numbers.nC(8, 1)
C = sym.functions.combinatorial.numbers.nC(20, 3)
A*B/C
\frac{44}{95}
  1. The number close to the Avogadro number was set. Problems from statistical mechanics. From the definition of hypergeometric distribution $ \begin{eqnarray} P &=& \frac{C(4 \cdot 10^{24}, 2 \cdot 10^{24}) \cdot C(2 \cdot 10^{24}, 10^{24})}{C(6 \cdot 10^{24}, 3 \cdot 10^{24})} \\\\ &=& \frac{(4 \cdot 10^{24})!{(3 \cdot 10^{24})!}^2}{(6 \cdot 10^{24})!(2 \cdot 10^{24})!{(10^{24})!}^2} \end{eqnarray}$ Use the approximation of $ \ log (n!) \ Eqsim n \ log n -n + \ frac {1} {2} \ cdot \ log (2 \ pi n) $.
log_n = n * sym.log(n) - n + sym.log(2 * Pi * n) / 2
A1 = log_n.subs([(n, 4*10**24)])
A2 = log_n.subs([(n, 3*10**24)])
B1 = log_n.subs([(n, 6*10**24)])
B2 = log_n.subs([(n, 2*10**24)])
B3 = log_n.subs([(n, 1*10**24)])
Log_P = A1 + 2 * A2 - B1 - B2 - 2 * B3
sym.N(Log_P, 10)

−28.0006535 $\therefore P \simeq e^{-28} \simeq 6.9144 \cdot 10^{-13}$ 3. The number of red balls $ X $ is approximated by the binomial distribution $ B (3 * 10 ^ {24}, 2/3) . If you approximate it with a normal distribution, $Y = \frac{X-2\cdot 10^{24}}{\sqrt{3 \cdot 10^{24}\cdot 2/3 \cdot 1/3}}\\ = \sqrt{\frac{3}{2}} \frac{X-2 \cdot 10^{24} }{10^{12}}$$ $ Y $ follows a standard normal distribution. Cumulative density exceeds 0.5 from the standard normal distribution table -0.68 X=2 \cdot 10^{24} \pm 0.68 \cdot \sqrt{\frac{2}{3}} 10^{12} x \simeq 5.55 \cdot 10^{11}

(Q4)

koushi.png

Consider a random walk that starts from the yellow part in the center of the triangular grid in the figure and moves to the adjacent grid with equal probability every second. Let $ P_t $ be the probability of being in the center after $ t $ seconds.

  1. Find $ P_ {10} $.
  2. Find $ \ lim_ {n \ to \ infinty} P_n $.

(Solution)

  1. From symmetry, let $ Q_t $ be the probability of being at each point at distance 1 and $ R_t $ be the probability of being at each point at distance 2. $P_t+6Q_t+3R_t=1$
N = 1
P = 0
Q = sym.Rational(1, 6)
R = 0
Pn = sym.Rational(3, 2) * q
Qn = p/6 + q/2 + r/2
Rn = q/2
while N < 10:
    N += 1
    Ptemp = Pn.subs([(q, Q)])
    Qtemp = Qn.subs([(p, P), (q, Q), (r, R)])
    Rtemp = Rn.subs([(q, Q)])
    P = Ptemp
    Q = Qtemp
    R = Rtemp
print(N)
print(P)
print(Q)
print(R)

10 171/1024 341/3072 57/1024 2.

F1 = sym.Rational(3, 2) * q - p
F2 = p/6 + q/2 + r/2 - q
F3 = q/2 - r
Ft = p + 6*q + 3*r - 1
sym.solve([F1, F2, F3, Ft], [p, q, r])
\\left \\{ p : \\frac{1}{6}, \\quad q : \\frac{1}{9}, \\quad r : \\frac{1}{18}\\right \\}

(Q5)

Suppose that $ n $ uniform random numbers in the range of 0 to 65535 are generated.

  1. Find the value of $ n $ that has a probability of containing the same random number greater than 0.5.
  2. Find the value of $ n $ that has a probability of containing 3 or more of the same random numbers that exceeds 0.5.

(Solution)

  1. It is a birthday attack.
def birthday(N, K):
    return sym.functions.combinatorial.numbers.nP(N, K)/N**K
N = 65536
K = 255 #You can start with the square root
P = 0.0
while P <= 0.5:
    K += 1
    P = 1 - birthday(N, K)
print(K)
sym.N(P, 10)

302 0.5007224895 2. A combination of two $ N $ random numbers containing two of the same random numbers is

\sum_{k=1}^{K/2} C(N-k,k)P(N,K-k)

The boundary part of $ K / 2 $ does not have much effect, so add it without worrying about it.

def birthday2(N, K, P1):
    return (sym.functions.combinatorial.numbers.nP(N,K) + P1) / N**K
def P1(N,K,X):
    return sym.functions.combinatorial.numbers.nC(N-X,X)*sym.functions.combinatorial.numbers.nP(N,K-X)
N = 65536
K = 300
P = 0.0
while P <= 0.5:
    K += 1
    L = 1
    SUM = 0
    while L <= K/2:
        SUM += P1(N, K, L)
        L += 1
    P = 1 - birthday2(N, K, SUM)
print(K)
sym.N(P, 10)

473 0.5035181768

Recommended Posts

Studying Mathematics in Python: Solving Simple Probability Problems
Simple gRPC in Python
Combining problems in Python
○○ Solving problems in the Department of Mathematics by optimization
Simple regression analysis in Python
Solve optimization problems in Python
Simple IRC client in python
Solving 2015 Center Test for University Admissions Mathematics IIB in Python
First simple regression analysis in Python
Simple OAuth 2 in Python (urllib + oauthlib)
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
Simple gacha logic written in Python
Studying python
A simple HTTP client implemented in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Markov chain transition probability written in Python
Precautions when solving DP problems with Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Get Precipitation Probability from XML in Python
Set up a simple HTTPS server in Python 3
Statistical test grade 2 probability distribution learned in Python ②
A simple Pub / Sub program note in Python
Notes for implementing simple collaborative filtering in Python
Create a simple momentum investment model in Python
Solving the equation of motion in Python (odeint)
Set up a simple SMTP server in Python
Statistical test grade 2 probability distribution learned in Python ①
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
[Python] Start studying
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.