Sheldon Ross 10: Exercise 3.22

Question: Suppose that independent trials, each of which is equally likely to have any of m outcomes, are performed until the same outcome occurs k consecutive times. If N denotes the number of trials, show that

E[N] = mk-1m-1


Simulation Solution for m = 10

Simulation Solution for m = 15


Code

from sys import stdout
import sys
from mathematica.lists import EqualAll, Range
from mathematica.random_functions import RandomChoice

_folderPath = 'Your_Folder_Path_Here'
for _n in [10, 15]:
    _steps = 10000
    _subSteps = _steps // 100
    __n = _n
    _set = Range(_n)

    for _k in range(2, 5):
        _progressPad = "_" * 106
        _file = open(
            _folderPath + 'sheldon_ross_10_exercise_3.22_' + str(_n) + '_' + str(
                _k) + '.txt', 'w')

        print("\n\nWriting data to the file :" + _file.name)
        print(_progressPad)

        for i in range(0, _steps):
            _sample = RandomChoice(_set, _k)
            while True:
                if EqualAll(_sample[-_k:]):
                    break
                else:
                    _sample.append(RandomChoice(_set))
            _file.write(_sample.__str__().replace("[", "{").replace("]", "}") + '\n')
            if (i + 1) % _subSteps == 0:
                stdout.write("\r" + ("\r" + ("#" * (i // _subSteps + 1)).ljust(100)) + " " + str(
                    i // _subSteps + 1) + " %")
                stdout.flush()
    print("\nClosing the file: " + _file.name)
    _file.close()

End of the post 😉


.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.