On this forum I found this problem: What's the probability that a sequence of coin flips never has twice as many heads as tails?. Trying to understand the solution and having previously studied Markov chains, unfortunately, I still did not quite understand how the equations for $u$, $v$ and $w$ were composed. And also due to a lack of understanding of how these equations were compiled, I have not yet been able to solve this problem for an unfair coin, for which the probability of getting head is $p$. Please help me figure it out, because so far I can’t figure it out on my own, unfortunately.
-
1Several detailed solutions (to the $p=.5$ case) are presented in the duplicate... I would suggest going through one (or more) of them line by line using a general $p$. To be sure, some of the relevant sums look a bit delicate and it's possible that nice closed formulas aren't generally available. But numerical methods ought to work. – lulu Mar 18 '24 at 11:38
-
The leonbloy recursion seems generalizable to $P[n]=qP[n-1]+(1-q)P[n+2]$, a linear recurrence relation that is solvable by considering the cubic $r^2 = q+ (1-q)r^3$ and eliminating the $r=1$ solutions to reduce to a quadratic. You just need to be careful of the boudnary conditions on $P[-\infty]$. – Michael Mar 18 '24 at 18:42
1 Answers
Didier's answer gives the result for an unbalanced coin, i.e. general $p\ge \frac13$ (though he switched the question to twice as many tails as heads) in "Edit 1", namely $\frac32\left(2-p-\sqrt{p(4-3p)}\right)$.
Here is a numerical approach which calculates the probability of ever seeing the proportion of heads being $\frac ab$ with
- $p$ being the probability of the coin showing heads
- $\frac ab$ being the target proportion heads (in lowest terms)
- $q_n$ being the probability of hitting the proportion after $nb$ attempts so ${nb \choose na}p^{na}(1-p)^{nb-na}$ as a typical binomial
- $r_n$ being the probability of first hitting the proportion after $nb$ attempts so $q_n - \sum\limits_{m=1}^{n-1} q_mr_{n-m}$ as you want to exclude cases where you have previously hit the target ratio
- $\sum\limits_{n=1}^{\infty} r_n$ as the probability of ever hitting the target proportion
That sum to $\infty$ is a numerical issue when $p=\frac ab$ (or close), when the probability of every hitting is then $1$ (or close), but otherwise $r_n$ falls quickly enough that adding a finite number of terms should get to the answer up to rounding. In R you could use something like:
probeverhitexactproportion <- function(p, a, b, maxn){
q <- dbinom(a*(1:maxn), b*(1:maxn), p)
r <- q[1]
for (n in 2:maxn){
r[n] <- q[n] - sum(q[1:(n-1)] * r[(n-1):1])
}
return(sum(r))
}
and testing it with the original question shows how this works
probeverhitexactproportion(p=1/2, a=2, b=3, maxn=1000)
# 0.572949
p <- 1/2
3/2 * (2 - p - sqrt(p*(4-3*p)))
# 0.572949
You can also use this to check that for $\frac{a}{b}=\frac12$ you get a probability of ever hitting exactly of $1-|1-2p|$, so $2p$ when $p \le \frac12$, and $2-2p$ when $p \ge \frac12$.

- 157,058