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")
no subject
Date: 2009-09-12 10:32 am (UTC)Update: ah, here's how to do it by maths. I think eigenvectors come in to it :-)