(learning to program Python)
(hi I'm Dan)
http://slides.com/dansandiford/deck-4
In this workshop you'll learn how to combine
For loops & Random numbers
To simulate the answers to profound mathematical questions
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
But instead of digging out this formula, we could just simulate the process. Ultimately, this relies on two things:
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 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
...
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 3A Python for loop has some key components:
http://slides.com/dansandiford/deck-4
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)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]
6http://slides.com/dansandiford/deck-4
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...
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
You've tackled a real statistical problem using simulation. Along the way you learnt some components of the Python programming language:
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
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