# Expected number of trials [duplicate]

This question already has an answer here:

In a box there are 4 balls. Red, Yellow, Blue and Green. We draw a ball from the box. If the ball is red then we replace it by a yellow ball, if the ball is yellow then we replace it by a blue ball, if the ball is blue then we replace it by a green ball, if the ball is green then we replace it by a red ball. Find the expected no of drawings such that all balls will be of same colour.

## marked as duplicate by Shaun, Did, NCh, Misha Lavrov, José Carlos SantosApr 5 '18 at 21:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• Welcome to MSE. You will get a lot more help and many fewer down votes if you give some context for your questions. Where does this problem come from? What attempts have you made to solve it? What difficlties have you encountered? – saulspatz Apr 3 '18 at 14:36
• To get started: what is the probability that the first two draws will have the same color? What is the probability that the first three draws will have the same color? – lulu Apr 3 '18 at 14:38
• @lulu Isn't the OP asking for the expected number of drawings until the balls in the box are of the same color? – saulspatz Apr 3 '18 at 14:41
• Its my mistake..sry..Drawings are done untill the box has all the ball of same colour for the 1st tym..And in the 1st drawing probability of draw any ball are same.. – user363727 Apr 3 '18 at 14:46
• @saulspatz Yes, my reading doesn't make sense. It's impossible to draw two of the same color in a row. – lulu Apr 3 '18 at 14:46

## 1 Answer

This is an absorbing Markov chain with $35$ states, as indicated by lulu in his hint. The four "monochromatic" states are absorbing. The procedure for solving this problem is given in the Wikipedia article cited in my hint.

I wrote the following python script to solve the problem:

from collections import namedtuple
from itertools import product
import numpy as np

Box = namedtuple("Box", 'r y b g'.split())

def children(box):
if max(box) ==4:
return 4*[box]
answer = []
r,y,b,g=box
for _ in range(r):
answer.append(Box(r-1, y+1, b, g))
for _ in range(y):
answer.append(Box(r, y-1, b+1, g))
for _ in range(b):
answer.append(Box(r, y, b-1, g+1))
for _ in range(g):
answer.append(Box(r+1, y, b, g-1))
return answer

boxes = [Box(r,y,b,g) for r,y,b,g in product(range(5), repeat=4) if r+y+b+g==4]
boxes.sort(key=lambda x:max(x))
boxes = {b:i for i,b in enumerate(boxes)}
P=np.zeros((35,35), dtype = float)
for b in boxes:
i = boxes[b]
for c in children(b):
j = boxes[c]
P[i,j] += 1
P /= 4
Q= P[:31, :31]
I = np.eye(31)
N=np.linalg.inv(I-Q)
one = np.ones(31)
start = boxes[(1,1,1,1)]
wait=sum(N.transpose())[start]
print('Expected waiting time:', wait)


It printed

Expected waiting time: 69.4666666667