Content and Objective

  • Plotting relative frequency of 'Kopf' for multiple samples of a coin
  • NOTE: Results are being based on identical history with only the most recent value being updated.

Import


In [1]:
# importing
import numpy as np

import matplotlib.pyplot as plt
import matplotlib

# showing figures inline
%matplotlib inline

In [2]:
# plotting options 
font = {'size'   : 20}
plt.rc('font', **font)
plt.rc('text', usetex=True)

matplotlib.rc('figure', figsize=(18, 6) )

Parameters


In [3]:
# length of sequences to be sampled    
len_sequence = 100

# number of sequences to be sampled
N_sequences = 100

Simulation


In [4]:
# initialize array for storing sampled sequences
results = np.zeros( ( N_sequences, len_sequence ) )

# vector of lengths in order to perform averaging
lengths = np.arange( 1, len_sequence + 1 )

# loop for sequence length
for _n in np.arange( N_sequences ):

    # sample sequence
    sequence = np.random.choice( [ 0 , 1 ], size = len_sequence, p = [ .5, .5 ] )

    # summing up for averaging and normalize each entry by the according length
    results[ _n, :] = np.cumsum( sequence ) / lengths

Plotting


In [5]:
# plotting
plt.figure()

for _n in np.arange( N_sequences ): 
    plt.plot( range( 1, len_sequence+1 ), results[ _n, : ], linewidth = 2.0 )    

plt.grid( True )
plt.xlabel('$N$')
plt.ylabel('$H_N($'+'Kopf'+'$)$')
plt.margins(.1)



In [ ]: