Skip to content

BACKGROUND OF THE PROJECT

I am walking with my friend, Maxwell to the library. When we got to the base of the stairs of the library, my friend brought up a game. I throw a die 100 times and the outcome determines my next step. If 1 or 2 appears, I move one step down the stairs. If 3, 4 or 5 appears, I go one step up the stairs. If I am fortunate and 6 appears, I throw again and walk up the outcome. Logically, I can not walk down past 0 step. It is actually my first time climbing such a high stairs so I am a bit clumsy such that I have 0.1% chance of falling down the stairs. If I fall, I start again from the base with step 0.

I bet my friend I will reach 60 steps high.

OBJECTIVE AND METHOD

The objective is to determine the chance I have in reaching 60 steps high. We are going to employ Hacker's statistics to achieve this.

# Importing necessary modules
import numpy as np
import matplotlib.pyplot as plt

# setting a seed, step and rolling the dice
seed = np.random.seed(123)
step = 0
dice = np.random.randint(1, 7)
#determining my next move
#with the dice rolled,
if dice < 3:
    step = step - 1
elif dice < 6:
    step = step + 1
else:
    step = step + dice
Hidden output

INTEGRATING CLUMSINESS: MY CHANCE OF FALLING DOWN

Climbing the stairs to the 8th floor is something I have not done before so there is a possibility that I can fall down the stairs. This chance of falling down the stairs is .1% or 0.001. If I fall, I start again from the base with step 0. So, we are going to generate a random float between 0 and 1, the range for probability. If this value is less than or equal to 0.001, I should reset step to 0. We are going to update our random walk code to include this chance for every step taken.

if dice < 3:
    step = step - 1
elif dice < 6:
    step = step + 1
else:
    step = step + dice
if np.random.rand() <= 0.001: # clumsiness condition
    step = 0

Now, we're going to throw the dice 100 times and generate a single random walk.

random_walk = [0] # the random walk starts with the first step, 0.
for i in range (100):
    step = random_walk[-1] # current step is equal to last element in random_walk
    dice = np.random.randint(1, 7)
    if dice < 3:
        step = max(0, step - 1) # Ensuring step don't go below 0. max() returns the biggest among the two. so                                     whenever step - 1 is negative, 0 is returned.
    elif dice < 6:
        step = step + 1
    else:
        step = step + np.random.randint(1, 7)
    if np.random.rand() <= 0.001: # clumsiness condition
        step = 0
    
    random_walk.append(step)
print (random_walk)

VISUALIZING THE WALK Here comes the work of matplotlib.pyplot that was imported earlier. We are going to visualize the walk graphically, to make decisions.

plt.plot(random_walk) # a line plot.
plt.xlabel("index of walk")
plt.ylabel("random walk, cummulative")
plt.title("A line graph showing a single random walk")
plt.show()

SIMULATING MULTIPLE WALKS We are going to throw the dice a number of 100 times! This will help us to simulate the multiple walks and predict my chance of walking 60 steps up.

# We assign these number of 100 times roll to all_walks
all_walks =  [] # we start all_walks with empty list because we have'nt started a walk yet.
for walk in range (10): # we are simulating the random_walk 10 times.
    random_walk = [0] # the random walk starts with the first step, 0.
    for i in range (100): # Now, we put the entire 100 dice rolling code in the bigger "for loop"
        step = random_walk[-1] # current step is equal to last element in random_walk
        dice = np.random.randint(1, 7)
        if dice < 3:
            step = max(0, step - 1) # Ensuring step don't go below 0. max() returns the biggest among the two.                                       so whenever step - 1 is negative, 0 is returned.
        elif dice < 6:
            step = step + 1
        else:
            step = step + np.random.randint(1, 7)
        if np.random.rand() <= 0.001: # clumsiness condition
            step = 0
        random_walk.append(step)
    all_walks.append(random_walk)
print (all_walks)
# Visualizing the 10 times simulations
np_all_walks = np.transpose(np.array(all_walks)) # converting and transposing to be able to plot each single one of the lits.
 #_________Use either this approach_________
np_all_walks_tp = np.transpose(np.array(all_walks))
plt.plot(np_all_walks_tp) # a line plot.plt.plot(random_walk) # a line plot.
plt.xlabel("index of walk")    
plt.ylabel("random walk, cummulative steps")
plt.title("A line graph showing 10 times simulated random walks")
#________________________Or this____________________
#for random_walk in all_walks:
    #plt.plot(random_walk)
    #plt.xlabel("index of walk")
    #plt.ylabel("random walk, cummulative steps")
    #plt.title("A line graph showing random walks")
#plt.show()