Using python to analyze coin throw statistics

I studied python programming on edX , which is a very good course, and I still fully recommend it. Just by looking at the TED talk in Statistics, I thought it was a great way to realize the python skills that I chose in a real-world scenario. The guy gave an example of the likelihood that he constantly flips the coin and searches for two repeating sequences, which, as he explained, you would consider the same probability as you claimed, in fact they do not. Simply put, he claims that Heads Tails Heads are more likely than Heads Tails Tails, because at the end of the first sequence you are already one third to repeat the sequence again, where at the end of the second sequence you then need to throw one more head to again start the sequence. This makes sense, so I tried to prove it with my little python program shown here.

import random

HTH = 0
HTT = 0
myList = []
i = 0
numberOfTosses = 1000000

while i < numberOfTosses:
    myList.append(random.randint(0,1))
    i += 1

for i in range (len(myList)):

    if i+2 >= len(myList):
        break

    if myList[i] == 1 and myList[i+1] == 0 and myList[i+2] == 1:
        HTH +=1

    if myList[i] == 1 and myList[i+1] == 0 and myList[i+2] == 0:
        HTT +=1

print 'HTT :' ,numberOfTosses, HTT, numberOfTosses/HTT
print 'HTH :' ,numberOfTosses, HTH, numberOfTosses/HTH

, , , , , HTH evey 8 tosses HTT- 10, . , : ?

+4
5

, , , , - . , , , HTT 8 , HTH 10 .

, :

import random

HTH = 0
HTT = 0
numberOfTrials = 10000

for t in xrange( numberOfTrials ):
    myList = [ random.randint(0,1), random.randint(0,1), random.randint(0,1) ]
    flips = 3
    HTHflips = HTTflips = 0

    while HTHflips == 0 or HTTflips == 0:
        if HTHflips == 0 and myList[flips-3:flips] == [1,0,1]:
            HTHflips = flips
        if HTTflips == 0 and myList[flips-3:flips] == [1,0,0]:
            HTTflips = flips
        myList.append(random.randint(0,1))
        flips += 1

    HTH += HTHflips
    HTT += HTTflips


print 'HTT :', numberOfTrials, HTT, float(HTT)/numberOfTrials
print 'HTH :', numberOfTrials, HTH, float(HTH)/numberOfTrials

, 8 10 .

+4
import random

HTH = 0
HTT = 0
myList = []
numberOfTosses = 1000000

myList.append(random.randint(0,1))
myList.append(random.randint(0,1))

for x in range (3, numberOfTosses + 3):
    myList.append(random.randint(0,1))
    if myList[x-3:x] == [1,0,1]:
        HTH += 1
    elif myList[x-3:x] == [1,0,0]:
        HTT += 1

print (HTH, " ", HTT)
0

, :

import random

HTH = 0
HTT = 0

numberOfTosses = 1000000

myList = [random.randint(0,1) for x in range(numberOfTosses)]

for i in range(len(myList)-2):
    a,b,c= myList[i:i+3]
    HTH += int(a==c==1 and b==0)
    HTT += int(a==1 and b==c==0)

print 'HTT :' ,numberOfTosses, HTT, numberOfTosses/float(HTT)
print 'HTH :' ,numberOfTosses, HTH, numberOfTosses/float(HTH)

, , , . stats.stackexchange.com

0

, , , . HTH HTT-.

, .

3 , 8 , HTH, - HTT.

4 16 . 2 HTH, 2 HTT; 2 HTH, 2 HTT.

, , . . http://ideone.com/YtixtV

from __future__ import division
import random

def every_combination(n):
    bits = [2**i for i in range(n)]
    for value in xrange(2**n):
        yield [1 if value & bits[i] else 0 for i in range(n)]

for n in range(3, 16):
    HTH = 0
    HTT = 0
    numberOfTosses = 0

    for myList in every_combination(n):

        numberOfTosses += len(myList)
        for i in range (len(myList) - 2):

            if myList[i] == 1 and myList[i+1] == 0 and myList[i+2] == 1:
                HTH +=1

            if myList[i] == 1 and myList[i+1] == 0 and myList[i+2] == 0:
                HTT +=1

    print 'For number of rolls', n
    print 'HTT :' ,numberOfTosses, HTT, numberOfTosses/HTT
    print 'HTH :' ,numberOfTosses, HTH, numberOfTosses/HTH
0

, .

1/2 ^ [ ] - , .

TED, , Penney Game: http://en.wikipedia.org/wiki/Penney%27s_game , Penney , , FIRST ( ), - - . , TED (HHT, HTT.)

( ) Penney. , "" .

: HTH: 1000000 332854 3 3.00432021247 HHT: 1000000 667146 1 1.49892227488

import random

HTH = 0
HHT = 0
myList = []
i = 0

numberOfTests = 1000000
maxTosses = 10000

hthConditionMeant=0
hhtConditionMeant=0

while i < numberOfTests  :
    myList = []
    j = 0
    while (j < maxTosses):
        myList.append(random.randint(0,1))
        if myList[j-3:j] == [1,0,1]:
                HTH += 1
                break
        elif myList[j-3:j] == [1,1,0]:
                HHT += 1
                break
        j += 1
    i += 1


cyclesToSeeHTHprecise =  numberOfTests / float(HTH)
cyclesToSeeHHTprecise =  numberOfTests / float(HHT)

print 'HTH :' ,numberOfTests, HTH, numberOfTests/HTH, cyclesToSeeHTHprecise
print 'HHT :' ,numberOfTests, HHT, numberOfTests/HHT, cyclesToSeeHHTprecise`

`

0

Source: https://habr.com/ru/post/1526786/


All Articles