Markov chain calc E[rounds | A wins]

 avatar
unknown
python
18 days ago
1.3 kB
4
Indexable
def game2ExpectedLen(p=6/10, n=10):
    #state = (A wins, B wins, winner of prev)
    states = [(a,b,c) for a in range(n) for b in range(n)
              for c in ([''] if a==0 and b==0 else (['A'] if b==0 else ['A', 'B']) ) ]
    sToI = {s: i for i,s in enumerate(states)}
    matN = len(states)
    qMat = matrix(QQ, matN)
    rArr = [0]*matN
    for i1, (a,b,prevWinner) in enumerate(states):
        #A wins the round
        score2 = a+(3 if prevWinner=='A' else 2)
        if score2>=n:
            rArr[i1] += p
        else:
            s2 = (score2, b, 'A')
            qMat.add_to_entry(i1, sToI[s2], p)
        #B wins the round
        score2 = b+(3 if prevWinner=='B' else 2)
        if score2>=n:
            pass #only recording the A-win column
        else:
            s2 = (a, score2, 'B')
            qMat.add_to_entry(i1, sToI[s2], 1-p)
    fundMat = (matrix.identity(matN) - qMat)^(-1)
    pVec = fundMat*vector(rArr)
    qMat2 = matrix(QQ, matN)
    for i1 in range(matN):
        for i2 in range(matN):
            qMat2[i1,i2] = qMat[i1,i2]*pVec[i2]/pVec[i1]
    fundMat2 = (matrix.identity(matN)-qMat2)^(-1)
    return sum(fundMat2[sToI[(0,0,'')]])


expL = game2ExpectedLen(p=6/10, n=10)
print ("E(rounds | A wins) = %s = %f" %(expL, expL) )
Leave a Comment