0% found this document useful (0 votes)
95 views31 pages

Python Random Walk Tutorial

This document discusses random numbers and random walks. It introduces Python's random number generator and shows how to simulate coin tosses and random walks. Repeating the random walk simulation with more trials and plotting the results in a histogram reveals the binomial distribution that emerges from the random process.

Uploaded by

saw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views31 pages

Python Random Walk Tutorial

This document discusses random numbers and random walks. It introduces Python's random number generator and shows how to simulate coin tosses and random walks. Repeating the random walk simulation with more trials and plotting the results in a histogram reveals the binomial distribution that emerges from the random process.

Uploaded by

saw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Random Numbers

I N T E R M E D I AT E P Y T H O N

Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
Can't go below step 0

0.1 % chance of falling down


the stairs

Bet: you'll reach step 60

INTERMEDIATE PYTHON
How to solve?
Analytical

Simulate the process


Hacker statistics!

INTERMEDIATE PYTHON
Random generators
import numpy as np
[Link]() # Pseudo-random numbers

0.9535543896720104 # Mathematical formula

[Link](123) # Starting from a seed


[Link]()

0.6964691855978616

[Link]()

0.28613933495037946

INTERMEDIATE PYTHON
Random generators
[Link](123)
[Link]()

0.696469185597861 # Same seed: same random numbers!

[Link]() # Ensures "reproducibility"

0.28613933495037946

INTERMEDIATE PYTHON
Coin toss
[Link]

import numpy as np

[Link](123)

coin = [Link](0,2) # Randomly generate 0 or 1

print(coin)

INTERMEDIATE PYTHON
Coin toss
[Link]

import numpy as np
[Link](123)
coin = [Link](0,2) # Randomly generate 0 or 1
print(coin)

if coin == 0:
print("heads")

else:
print("tails")

0
heads

INTERMEDIATE PYTHON
Let's practice!
I N T E R M E D I AT E P Y T H O N
Random Walk
I N T E R M E D I AT E P Y T H O N

Hugo Bowne-Anderson
Data Scientist at DataCamp
Random Step

INTERMEDIATE PYTHON
Random Walk
Known in Science

Path of molecules

Gambler's nancial status

INTERMEDIATE PYTHON
Heads or Tails
[Link]

import numpy as np
[Link](123)
outcomes = []
for x in range(10) :
coin = [Link](0, 2)
if coin == 0 :
[Link]("heads")
else :
[Link]("tails")
print(outcomes)

['heads', 'tails', 'heads', 'heads', 'heads',


'heads', 'heads', 'tails', 'tails', 'heads']

INTERMEDIATE PYTHON
Heads or Tails: Random Walk
[Link]

import numpy as np
[Link](123)
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)
print(tails)

[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]

INTERMEDIATE PYTHON
Step to Walk
outcomes

['heads', 'tails', 'heads', 'heads', 'heads',


'heads', 'heads', 'tails', 'tails', 'heads']

tails

[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]

INTERMEDIATE PYTHON
Let's practice!
I N T E R M E D I AT E P Y T H O N
Distribution
I N T E R M E D I AT E P Y T H O N

Hugo Bowne-Anderson
Data Scientist at DataCamp
Distribution

INTERMEDIATE PYTHON
Random Walk
[Link]

import numpy as np
[Link](123)
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)

INTERMEDIATE PYTHON
100 runs
[Link]

import numpy as np
[Link](123)
final_tails = []

for x in range(100) :
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)

final_tails.append(tails[-1])
print(final_tails)

[3, 6, 4, 5, 4, 5, 3, 5, 4, 6, 6, 8, 6, 4, 7, 5, 7, 4, 3, 3, ..., 4]

INTERMEDIATE PYTHON
Histogram, 100 runs
[Link]

import numpy as np

import [Link] as plt

[Link](123)
final_tails = []
for x in range(100) :
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)
final_tails.append(tails[-1])

[Link](final_tails, bins = 10)


[Link]()

INTERMEDIATE PYTHON
Histogram, 100 runs

INTERMEDIATE PYTHON
Histogram, 1,000 runs
[Link]

import numpy as np
import [Link] as plt

[Link](123)
final_tails = []
for x in range(1000) : # <--
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)
final_tails.append(tails[-1])

[Link](final_tails, bins = 10)


[Link]()

INTERMEDIATE PYTHON
Histogram, 1,000 runs

INTERMEDIATE PYTHON
Histogram, 10,000 runs
[Link]

import numpy as np
import [Link] as plt

[Link](123)
final_tails = []
for x in range(10000) : # <--
tails = [0]
for x in range(10) :
coin = [Link](0, 2)
[Link](tails[x] + coin)
final_tails.append(tails[-1])

[Link](final_tails, bins = 10)


[Link]()

INTERMEDIATE PYTHON
Histogram, 10,000 runs

INTERMEDIATE PYTHON
Let's practice!
I N T E R M E D I AT E P Y T H O N

You might also like