If you can write a for loop you can
Do more for less
(learning to program Python)
(hi I'm Dan)
http://slides.com/dansandiford/deck-4
MATHS is Hard. Using programming skills, it can be easy.
In this workshop you'll learn how to combine
For loops & Random numbers
To simulate the answers to profound mathematical questions
A problem
Let's say you toss a coin 30 times, and you get 22 heads. The question you want to ask is "is this a fair coin?"
http://slides.com/dansandiford/deck-4
Direct Simulation
But instead of digging out this formula, we could just simulate the process. Ultimately, this relies on two things:
- Multiple repetitions of the experiment
- A random number generator
But First - Variables
In programming, we often create variables - to store values, or more complex data structures.
x = 3
y = x + 2
print(x, y)
...
(3, 5)
So we can take a value, assign it to a variable, then use the variable as if it was the value
http://slides.com/dansandiford/deck-4
For loops
For loops are a kind of template for repeating actions in Programming. But why should you care?
Fundamentally, they mean less work. Monotonous tasks become easy with for loops in Python:
for i in range(100):
print("We do need no education")
We do need no education
We do need no education
We do need no education
We do need no education
We do need no education
We do need no education
...
For loops
A for loop allows us to loop or cycle through an object, and repeat some action each time we do so.
for i in range(3):
j = i + 1
print(i, j)
...
0 1
1 2
2 3
A Python for loop has some key components:
- the word for
- a 'loop variable': i (call this whatever you like)
- a collection to loop through: range()
- a colon to signify the start of the action :
- indented code - the set of actions performed
http://slides.com/dansandiford/deck-4
Random numbers
The only bit we're missing now is the ability to generate random numbers. To do this we're going to import a function from the library called Numpy (numeric Python)
from numpy.random import randint
randint(3, size=30)
Putting it together
When we code, we often start with small pieces . Then assemble these together into bigger units of code. Let's try and connect our loop and our random numbers.
for loopVar in range(3):
trial = randint(2, size=10)
print(trial)
numberOfOnes = sum(trial)
print(numberOfOnes)
...
[1 0 1 0 1 0 0 1 0 1]
5
[0 0 1 0 0 1 1 0 0 1]
4
[0 1 0 0 0 1 1 1 1 1]
6
http://slides.com/dansandiford/deck-4
Your Turn!
Now we're going to try this together. Start a Python session on the Cloud by going to the website:
https://tmpnb.org/
Is everyone happy? Okay, let's get back to our coin tossing simulation...
Challenge!
I've added one last trick to the for loop : In addition to creating my random numbers and computing their sum, I also ask Python is the variable numberOfOnes greater than 2? And if this is True, I add 1 to another variable called count
from numpy.random import randint
count = 0
trials = 1000
for expNum in range(trials):
trial = randint(2, size=10)
numberOfOnes = trial.sum()
if numberOfOnes > 2:
count = count +1
print(count/trials)
Your challenge is make some (minor) changes to this code, so that it computes the probability of a getting 22 heads (or greater) from 30 coin tosses.
Bonus... what is the prob. of EXACTLY 22 heads?
http://slides.com/dansandiford/deck-4
Wrap up
You've tackled a real statistical problem using simulation. Along the way you learnt some components of the Python programming language:
- variables
- for loops
- random numbers
- algorithm design
To consolidate these concepts, you can check out our Python course notes: https://dansand.gitbooks.io/resguides-research-with-jupyter/content/
To stay in touch with Python at Unimelb join our facebook group: research with jupyter
Acknowlegments
This workshop was inspired by Jake Van Der Plas' Pycon presentation: https://www.youtube.com/watch?v=Iq9DzN6mvYA
More problems with simulation and random numbers in Python can be found here: http://hplgit.github.io/primer.html/doc/pub/random/random-readable.html
http://slides.com/dansandiford/deck-4
deck
By Dan Sandiford
deck
- 455