coins#
Throw fair coin, if head +1 else -1. Game ends if target reached or gone bankrupt. Starting from 100, chance of reaching exactly to 100 after 10 days?#
Mathematically,
5 ups and 5 downs, so (10 choose 5) * (1/2)^10 = 252/1024=0.24
Expected rolls to get n consecutive Heads#
Expectation to get 1 Head = 2
E(2 consecutive H) = 6
E(3 consecutive H) = 14
clearly 2,, 6, 14, …
E(N heads) = 2+4+8+..+2^n = 2^(n+1) - 2
Method 1:#
For N = 2:
E = (1 + 1/2*( 1 + 1/2 * E) + 1/2 * E)
or E = 1 + 1/2 + 1/4E + 1/2E
or 1/4E = 3/2
E = 6
Method 2:#
1/2 chance gets a tail, E = E+1
1/4 chance gets a head then tail, E = E+2
1/4 chance gets 2 head, E = 2
E = 1/2 (E + 1) + 1/4(E + 2) + 1/4 * 2
or E = 3/4E + 3/2
E = 6
For N:#
Two particles are each located at the opposite vertices of the same octagon. Each second, two coins are flipped, one for each particle, and this dictates whether that particle will move clock-wise or anticlockwise along an edge. On average, how many coins must be flipped for the two particles to meet?#
Simplifying: $\(0.5 E_2 = 1 + 0.25 E_4 \implies \mathbf{2 E_2 = 4 + E_4}\)$
2 coins are flipped every second, so 16 flips
Flip 6 coins, p(more heads than tails)?#
p(H=T) = (6 choose 3) / 2**6 = 20/64 = 10/32
p(H > T) = (1 - 10/32) / 2 = 11/32
p(at most 3 are Heads) = 2/3 = 0.66
Flip 7 coins, P(odd num of heads)?#
0.5
The number of heads in the first 6 coins has to be equally likely to be odd or even by symmetry. If the first 6 coins resulted in an even number of heads, the 7th coin must be Heads to make the total odd.If the first 6 coins resulted in an odd number of heads, the 7th coin must be Tails”
flip 100 coins, repeatedly remove in Head-Tail pairs until no longer possible. ev of number of coins left#
equivalent to: 100 coin flips, absolute difference of num heads and num tails?
say H is num of heads, then | H - (100 - H) | = | 2H - 100|
here approHimately \( H \sim \mathcal{N}(50, 25) \),
so \( 2H-100 \sim \mathcal{N}(0, 100) \)
Note $\( X \sim \mathcal{N}(0,1), \qquad \mathbb{E}[|X|] = \sqrt{\frac{2}{\pi}} \approx 0.798 \)$
So 2H - 100 = 10X,
E( |2H - 100|) = E (10 |X| ) = 7.98
import random
def trial():
coins = [random.randint(0,1) for _ in range(100)] # 1=H,0=T
H = sum(coins)
T = 100 - H
# remove HT pairs
remaining = abs(H - T)
return remaining
N = 1_000_0
sum(trial() for _ in range(N)) / N
7.9622
8 coin flips, p(3 of the same in a row anywhere)?#
number of combinatinos without any 3-in-a-row:
Flips (N) |
Logic |
Calculation |
Total Safe |
|---|---|---|---|
1 |
H, T |
2 |
2 |
2 |
HH, HT, TH, TT |
4 |
4 |
3 |
(Prev 2) + (Prev 1) |
4 + 2 |
6 |
4 |
6 + 4 |
10 |
10 |
5 |
10 + 6 |
16 |
16 |
6 |
16 + 10 |
26 |
26 |
7 |
26 + 16 |
42 |
42 |
8 |
42 + 26 |
68 |
68 |
Notice this follows fibbonacci
so for 8 flips, p(3-in-a-row anywhere) = (256-68)/256 = 0.73
0.73
import random
def trial():
seq = [random.choice([0,1]) for _ in range(8)]
for i in range(6):
if seq[i]==seq[i+1]==seq[i+2]:
return True
return False
N = 300_000
sum(trial() for _ in range(N))/N
0.7346733333333333
Stock worth 100, roll dice and flip a coin. If head, stock + dice number, else - dice number. Chance of > 120 after 10 days?#
Note this is random walk, each step Xi can be (-6,-5..-1,1,2,..,6) each with 1/21/6=1/12 so E(Xi)=0, var(Xi)=E(Xi^2)=91/6 Variance of total change in 10 days has var 1091/6=151.6667 so standard deviation=12.31 question asks P(U>20) for U~N(0, 12.31), or 0.0521
import numpy as np
S0 = 100
days = 10
N = 100000
def simulate_stock_price():
stock_price = S0
for _ in range(days):
dice_roll = np.random.randint(1, 7) # Roll a six-sided dice
coin_flip = np.random.choice(['heads', 'tails']) # Flip a coin
if coin_flip == 'heads':
stock_price += dice_roll
else:
stock_price -= dice_roll
return stock_price
count = 0
for _ in range(N):
final_price = simulate_stock_price()
if final_price > 120:
count += 1
Pr = count / N
print(Pr)
print(count)
0.04827
4827