Date: 2009-09-12 10:32 am (UTC)
I'm not sure exactly how to do it "by maths", but here's an approximation that assumes there will be fewer than 1024 coin tosses. It works by treating it as a state machine with a transition matrix, and applying the transition matrix lots:
#!/usr/bin/python

from numpy import matrix

tstates = ["HH", "HT", "TH", "TT"]

def tosscolumn(x, a):
    res = [0] * (4 + len(a))
    for n in ["H", "T"]:
        if x + n in a:
            res[4 + a.index(x + n)] += 0.5
        else:
            res[tstates.index((x + n)[1:])] += 0.5
    return res

def tosscolumns(a):
    return [tosscolumn(x, a) for x in tstates]

def fixcolumns(a):
    return [[0]*4 + [int(i == j) for j in range(len(a))]
        for i in range(len(a))]

def tossmatrix(a):
    return matrix(tosscolumns(a) + fixcolumns(a)).transpose()

def winodds(*a):
    initialodds = matrix([0.25]*4 + [0]*len(a)).transpose()
    m = tossmatrix(a)
    finalodds = (m ** 1024) * initialodds
    return [float(finalodds[i + 4][0]) for i in range(len(a))] 

print winodds("HHH", "THH")


Update: ah, here's how to do it by maths. I think eigenvectors come in to it :-)
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

djm4_lj: (Default)
djm4_lj

July 2015

S M T W T F S
    1234
567891011
12131415161718
19202122232425
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 28th, 2025 05:17 am
Powered by Dreamwidth Studios