Binomial distribution: gamification for online casino

363 Views Asked by At

This'll be my first post here and I haven't done any statistics in 10 years, so I need some help getting back into it.

So I'm working on a gamification system for an online casino and I'm trying to figure out the likelyhood of certain events, and a fair prize for completing the event.

Slot $X$ has a hit frequency of $44.9$%. The odds for winning $3$ times in a row (binomial distribution) is $9.05188$%

How many times, on average, do I need to spin to win 3 times in a row?

Any help would be greatly appreciated!

4

There are 4 best solutions below

0
On

Let $P(n)$ be the probabilty that you hit 3 wins in a row for the first time on your $n$-th try.

You hit 3 wins in a row for the first time on your $n$-th try iff:

  • You win the $n$-th, $(n-1)$-th and $(n-2)$-th games,
  • You lose the $(n-3)$-th,
  • You never won three times in a row during your $n-4$ first tries.

Hence,

$P(0)=P(1)=P(2)=0$

$P(3)=0.449^3$

$P(n)=0.449^3*(1-0.449)*(1-\sum_{k=0}^{n-4}P(k))$

This doesn't yield a closed form easily but should be enough if you want to compute the result with an algorithm.

7
On

I will rephrase the problem in the familiar terminology of coin flipping. If I understand correctly, you are asking how many tosses are on average needed to get three consecutive heads. The coin is biased, and comes head with a probability $p = 0.449$.

An equation for $Z_3$, the expected number of tosses to get three successful outcomes, can be set as follows, as a sort of weighted average over likely values.

You perform the first toss. If it is tails (probability $1-p$), one trial is gone and you will still need $Z_3$ trials for your “three-in-a-row-heads” sought outcome.

If your first toss is heads, then you consider separately the two next chances: either heads again, or not. If not (probability $p (1-p)$, you have lost 2 trials, and still need $Z_3$ attempts. If yes, you consider the third attempt. If after two heads you get a tails (probability $p^2 (1-p)$ 3 trials are gone, and you still need $Z_3$. If the third is heads (probability $p^3$), you got the streak you were looking for, and $Z_3$ equals 3 with said probability. Putting all together, one gets

$$ Z_3 = (1-p) (Z_3+1) + p(1-p)(Z_3+2) + p^2 (1-p)(Z_3+3)+3p^3$$ A litte re-arrangement yields

$$ Z_3 = \frac{1 + p + p^2}{p^3} $$ and substituting the value you gave for $p$ one estimates that 19 tosses will be needed.

0
On

It should be possible to tackle this by treating it as a Markov process and setting up stochastic matrix to represent the transition between states.

Let 1 represent a success and 0 a failure and let $p$ represent the probability of a success. Assume we have a long stream of trials, for any three successive trials there are eight patterns of 1's and 0's and each of these is a state: $\{000, 001, \ldots 111\}$

For a given state, there are two possible states that can follow it. E.g. if the current state is $011$, the next individual trial will give a 1 or a 0 so the next state will either be $110$ (with probaility $q = 1-p$) or $111$ (with probability $p$).

We can build the $8 \times 8$ stochastic matrix $M$ to collect together all of these transitions.

Let $v_k$ represent the $8 \times 1$ state probability vector after $k+3$ trials.

We have $v_0 = (q^3, q^2p, \ldots , p^3)^T$ and and the transition matrix is applied as follows

$$ v_{k} = M v_{k-1} $$

So that

$$ v_k = M^{k-1} v_0 $$

Evaluating the successive powers of $M$ will give us the probabilities of all states after different numbers of trials. In particular it can be used to obtain the probability of the state $111$ (three successive wins) for different numbers of trials. I appreciate this is not quite the same as giving the average number of trials required for three successive wins but it does provide a distribution over all numbers of trials which may serve as well.


Edit: It was too tempting not to have a go at this myself. Below is the code I used to implement the outline above. The estimated number of steps matches that given by @BruceET.

this is in Python 2

Set up some variables:

import numpy as np

# prob success
p = 0.449
# prob fail
q = 1 - p

q3 = q**3
q2p = q*q*p
qp2 = q*p*p
p3 = p**3

#   indices:    0      1      2      3      4      5      6      7 
state_list = ['000', '001', '010', '011', '100', '101', '110', '111']
init_probs = [q3   , q2p  , q2p  , qp2  , q2p  , qp2  , qp2  , p3]

Set up transition matrix:

# transition matrix
M = np.zeros((8,8))

# Set the transition values from state i to state j:

M[0,0] = q
M[0,1] = p

M[1,2] = q
M[1,3] = p

M[2,4] = q
M[2,5] = p

M[3,6] = q
M[3,7] = p

M[4,0] = q
M[4,1] = p

M[5,2] = q
M[5,3] = p

M[6,4] = q
M[6,5] = p

# state '111' is an absorbing state.
M[7,7] = 1.0

Now run the transitions forward:

# Vector that will undergo transition
v = np.asarray(init_probs)

# Number of transitions to apply
n_reps = 1000

# Cumulative probability for state 111, the value at index k 
# will represent the sum of the probabilities of reaching this 
# state in the current or previous iterations. The k^th index
# of this array corresponds to the step k+3 in the game.
p_cumulative = np.zeros((n_reps,))

for k in range(n_reps):  
    p_cumulative[k] = v[-1]
    v = np.dot(M.T, v)

# Figure out the probabilities of reaching state 111 in each step.
p = p_cumulative.copy()

# This sets p[k] = p_cumulative[k] - p_cumulative[k-1] for k>=1
p[1:] = p[1:] - p[:-1]

Finally, estimate the expected number of steps taken to reach state '111'

steps_taken = range(3, 3+n_reps)

e_steps = np.sum(p * steps_taken)

print 'Estimated steps needed: {:0.4f}'.format(e_steps)

Estimated steps needed: 18.2349

0
On

Suppose we repeatedly toss a coin with $P(H) = .449$, until we get three heads in a row. How many tosses on average are required? Here is an exact mathematical answer and a simulation.

This problem can be solved precisely by considering a Markov Chain with state space $S = \{0,1,2,3\},$ where the state indicates the number of consecutive Heads encountered to date, and state 3 is an absorbing state. We seek the number of tosses until absorption. [This is similar to the approach suggested by @Paul (+1).]

The one-step $4 \times 4$ transition matrix $\mathbf{P}$ is shown in the R code below. (Note that matrices have rows and columns 1 through 4, which we interpret as states 0 through 3. The matrix of transitions among the transient states is denoted $\mathbf{Q}$ and the matrix $\mathbf{M} = (\mathbf{I - Q})^{-1}$ has mean numbers of visits $m_{ij}$ to state $j$ before absorption into $3$, starting at state $i$. (Also, $\mathbf{I}$ denotes the $3 \times 3$ identity matrix.) Thus vector $\mathbf{m}$ that sums the rows of $\mathbf{M}$ shows the mean times to absorption giving the starting state. In particular, the desired answer is its first element $m_0 = 18.23489$ is the desired answer.

p = .449;  q = 1-p
P = matrix(c(q,p,0,0,  q,0,p,0,  q,0,0,p, 0,0,0,1), byrow=T, nrow=4); P
#      [,1]  [,2]  [,3]  [,4]
#[1,] 0.551 0.449 0.000 0.000
#[2,] 0.551 0.000 0.449 0.000
#[3,] 0.551 0.000 0.000 0.449
#[4,] 0.000 0.000 0.000 1.000

Q = P[1:3,1:3]; Q
#      [,1]  [,2]  [,3]
#[1,] 0.551 0.449 0.000
#[2,] 0.551 0.000 0.449
#[3,] 0.551 0.000 0.000

I = diag(3)
M = solve(I-Q); M; rowSums(M)
#          [,1]     [,2]     [,3]
#[1,] 11.047423 4.960293 2.227171
#[2,]  8.820251 4.960293 2.227171
#[3,]  6.087130 2.733121 2.227171

#[1] 18.23489 16.00772 11.04742  # sums of rows of M

This chain can also be simulated a million times to get a good approximation to $m_0$ (and also the approximate SD of the number of tosses until absorption).

set.seed(1234) # retain this line to duplicate simulation shown, delete for new simulation
## Sim m times; get avg nr tosses
B = 10^6; t = numeric(m);  p = .449

for(i in 1:B) {
  # Sim Once to get 3 H
  x = 0; xh = x

    while(x < 3){
    u = runif(1)
    if (u < p) {x = x+1}
    else{x = 0}
    xh = c(xh, x)
   }

  t[i] = length(xh)-1
  }

mean(t)
#[1] 18.2302
sd(t)
## 16.12498
summary(t)
# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 3.00    7.00   13.00   18.23   24.00  220.00 

Notes:

(a) It might be possible to write and solve difference equations for this absorbing Markov Chain instead of using matrix algebra--similar to the difference equations for the famous gambler's ruin problem.

(b) In gamification with payoffs, it may be prudent to consider not only the mean number of tosses required (about 18.23), but also of the approximate distribution of the number of tosses required--particularly the large variability. About a quarter of the 'games' require only 7 tosses, while some games require many more. Simulation gives a good idea of the distribution. A histogram is shown below. enter image description here