## Why ¶

Monday night beer and statistics sessions

Best known on campus were the Monday night beer and statistics sessions Box hosted at his home for students and other researchers. The gatherings sparked lively discussions about how statistics could help solve research problems posed by speakers from a wide range of disciplines.

1. 游戏中的概率问题——有趣的概率问题（一）: 之前和@SY讨论过的一些问题感觉很有意思，就拜托他找时间整理了下来。
2. A Fair Game Problem: 中大考研复试的一道题，@SY给我讲了两遍没听懂，又找Tomorrow学长帮忙写的一个解析:)
3. Three doors and three prisoners

1. 只是看似取巧而已，其能快速解决问题还是因为这种解法更加接近于问题的本质。

## Past/Now/Future¶

### Now¶

#### The Sock Drawer¶

##### Problem¶
Question

A drawer contains red socks and black socks. When two socks are drawn at random, the probability that both are red is $$\frac{1}{2}$$

(a) How small can the number of socks in the drawer be?

(b) How small if the number of black socks is even?

(a) 抽屉里的袜子数量最少可以是多少？

(b) 如果黑袜子的数量是偶数，最少可以是多少？

##### Tip¶
Tip

Use probability equations to solve for radicals, Pell's equation

##### Solutions¶
###### Solution1: Analysis¶
Solution1: Analysis

Let the number of red socks be denoted as $$R \in \mathbb{Z}^{+}$$, and the total number of socks be denoted as $$N \in \mathbb{Z}^{+}$$, where $$N > R$$. The probability of randomly picking a red sock on the first draw is $$\frac{R}{N}$$, and the probability of picking another red sock after picking a red sock on the first draw is $$\frac{R-1}{N-1}$$. Therefore, we have:

$\frac{1}{2} = \frac{R}{N}\frac{R-1}{N-1}$

Simplifying this equation, we get:

$N^2 - N + 2R(1-R) = 0$

Since our goal is to find the minimum value of $$N$$, combining the above conditions, the original problem can be formulated as the following integer programming problem:

\begin{align*} \text{minimize} \quad & N \\ \text{subject to} \quad & R \in \mathbb{Z}^{+} \\ & N \in \mathbb{Z}^{+} \\ & N > R \\ & N^2 - N + 2R(1-R) = 0 \end{align*}

As for the canonical solution to this integer programming problem... I'm not quite familiar with it (feel free to submit a pull request to supplement it). Here, we'll use a relatively simple approach.

Based on $$N^2 - N + 2R(1-R) = 0$$, since our goal is to find $$N$$, we can treat it as an unknown and use the quadratic formula to derive the corresponding values. Therefore, we have:

$N = \frac{1 \pm \sqrt{1 - 8R(1-R)}}{2}$

Considering the conditions that $$N$$ and $$R$$ are both positive integers, we can calculate:

$N = \frac{1 + \sqrt{1 - 8R(1-R)}}{2}$

And it must hold that $$\sqrt{1 - 8R(1-R)}$$ is an odd number, which requires $$1 - 8R(1-R)$$ to be a perfect square.

We can find values that satisfy these conditions through an iterative approach:

import math

def is_square(n: int) -> bool:
for i in range(n // 2 + 1):
if i * i == n:
return True
return False

def is_odd(n: int) -> bool:
return n % 2 == 1

def find_numbers(r_limit: int = 50) -> None:
for r in range(1, r_limit):
under_sqrt = 1 - 8 * r * (1 - r)
if is_square(under_sqrt) and is_odd(int(math.sqrt(under_sqrt))):
n = int((1 + int(math.sqrt(under_sqrt))) / 2)
print(f"R = {r}, N = {n}, B = N - R = {n - r}\n")

find_numbers()


R = 3, N = 4, B = N - R = 1

R = 15, N = 21, B = N - R = 6

(a): At least 4 socks are needed (3 red and 1 black), in this case, we have $$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$$

(b): At least 21 socks are needed (15 red and 6 black), in this case, we have $$\frac{R}{N}\frac{R-1}{N-1} = \frac{15}{21}\frac{14}{20} = \frac{1}{2}$$

$\frac{1}{2} = \frac{R}{N}\frac{R-1}{N-1}$

$N^2 - N + 2R(R-1) = 0$

\begin{align*} \text{minimize} \quad & N \\ \text{subject to} \quad & R \in \mathbb{Z}^{+} \\ & N \in \mathbb{Z}^{+} \\ & N > R \\ & N^2 - N + 2R(1-R) = 0 \end{align*}

$N = \frac{1 \pm \sqrt{1 - 8R(1-R)}}{2}$

$N = \frac{1 + \sqrt{1 - 8R(1-R)}}{2}$

import math

def is_square(n: int) -> bool:
for i in range(n // 2 + 1):
if i * i == n:
return True
return False

def is_odd(n: int) -> bool:
return n % 2 == 1

def find_numbers(r_limit: int = 50) -> None:
for r in range(1, r_limit):
under_sqrt = 1 - 8 * r * (1 - r)
if is_square(under_sqrt) and is_odd(int(math.sqrt(under_sqrt))):
n = int((1 + int(math.sqrt(under_sqrt))) / 2)
print(f"R = {r}, N = {n}, B = N - R = {n - r}\n")

find_numbers()


R = 3, N = 4, B = N - R = 1

R = 15, N = 21, B = N - R = 6

(a): 最少有4只袜子(3红1黑)，此时有$$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$$

(b): 最少有21只袜子(15红6黑)，此时有$$\frac{R}{N}\frac{R-1}{N-1} = \frac{15}{21}\frac{14}{20} = \frac{1}{2}$$

###### Solution2: Simulation¶
Solution2: Simulation

The above method is to analyze the simplified problem first and then solve it. We can also make full use of the powerful computing power of the computer to directly solve this problem. Let’s take a look at the "violent aesthetics" of computers!

def is_close(f1: float, f2: float) -> bool:
return abs(f1 - f2) < 1e-6

def simulation(r_limit: int = 50, n_limit: int = 50) -> None:
for r in range(1, r_limit):
for n in range(r + 1, n_limit):
if is_close(1 / 2, (r / n) * ((r - 1) / (n - 1))):
print(f"R = {r}, N = {n}, B = N - R = {n - r}\n")

simulation()


R = 3, N = 4, B = N - R = 1

R = 15, N = 21, B = N - R = 6

(a): At least 4 socks are needed (3 red and 1 black), in this case, we have $$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$$

(b): At least 21 socks are needed (15 red and 6 black), in this case, we have $$\frac{R}{N}\frac{R-1}{N-1} = \frac{15}{21}\frac{14}{20} = \frac{1}{2}$$

def is_close(f1: float, f2: float) -> bool:
return abs(f1 - f2) < 1e-6

def simulation(r_limit: int = 50, n_limit: int = 50) -> None:
for r in range(1, r_limit):
for n in range(r + 1, n_limit):
if is_close(1 / 2, (r / n) * ((r - 1) / (n - 1))):
print(f"R = {r}, N = {n}, B = N - R = {n - r}\n")

simulation()


R = 3, N = 4, B = N - R = 1

R = 15, N = 21, B = N - R = 6

(a): 最少有4只袜子(3红1黑)，此时有$$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$$

(b): 最少有21只袜子(15红6黑)，此时有$$\frac{R}{N}\frac{R-1}{N-1} = \frac{15}{21}\frac{14}{20} = \frac{1}{2}$$