The Scala Experience Safe Programming Can be Fun!

Download Report

Transcript The Scala Experience Safe Programming Can be Fun!

Programming for
Engineers in
Python
Lecture 3: Simulation
Autumn 2011-12
1
Lecture 2: Highlights
• Compiler, interpreter
• Variables, types, operators
2
Using the Editor
3
Today: Simulation
• What are simulations good for?
• Examples
• Flight simulator Typhoon Mawar 2005 computer simulation
• Meteorology
• Production line
4
Power Lines and Rare
Diseases
• Researchers decide to investigate
correlation between proximity to power
lines and rare diseases
• 100 diseases, occurrence rate for each is
known in the general population
• Sample 1000 people that live near power
lines, record number of ill people for
every disease
• For each disease, check whether there is a
significant difference (< 0.05) between the
sampled and the general occurrence rate
5
Power Lines and Rare Diseases
• Research results showed that few diseases correlate
with life near power lines
• Researchers concluded that living near power lines
are bad for you
• We will use simulation to prove they were wrong in
their analysis!
6
Simulation
• The plan:
• Simulate 1000 people from “general” population
• Count number of sick people for each disease
• Find diseases were the number of actual sick people from the
sampling is significantly higher than expected number of sick
people
• We know that there is no relation: if we find a disease with
occurrence rate is significantly higher than the general
population – the researchers conclusion might be wrong (why?)
• Technical issues:
• Define diseases
• How to simulate sick people?
• How to define significance?
7
Work plan
1. Randomly produce sampling set
2. For all diseases initiate observed sick people to be 0
3. For every person that has disease y, increase the
number of people observed with y by 1
4. If there is a significant increase in the observed
number of ill people - report
8
Code - Imports
• Import statement: “take” some piece of code
from somewhere else
• How did I know about random/binom_test
implementation in python?
9
Code - Preparations
• Why use constants?
• Explain syntax
10
11
Code (Cont’d)
12
Code (Cont’d)
13
Surprise!
• There are a few diseases that are correlated
to the sample
• Although the sample simulated the “general”
population
• What is going on?!
14
Intuition
Super Natural Powers
15
Intuition (Cont’d)
• We are looking for a person with supernatural
powers!
• Given 25 “hidden” cards guess for each card its
series (Spades, Hearts, Diamonds or Clubs)
• Probability to get one card correctly 0.25
• Guessing 25 cards is definitely super natural! We
are looking for people who maintain “some” super
natural powers (pval < 0.05…)
• We are looking among 100 people to find those with
“some” super natural powers
16
Intuition (Cont’d)
• It is obvious that some of the 100 people will
probably guess better than the average
• When so many trials are given (100), there will be
some “very lucky” guys that we will consider to
have supernatural powers
17
Intuition (Cont’d)
• This is the same for the power lines example!
• 1000 people near power lines, 25 cards
• 100 diseases, 100 “supernatural” candidates
• Expected probability per disease/candidate: “small”
(power lines), 0.25 (supernatural powers)
• Observed: number of sick people (power lines), number
of correct guessed cards (supernatural powers)
• Implementation of this simulation will be part of your next
homework assignment
18
19
20
21
Next Time Use Bonferroni
22
Summary
• Be careful with your statistics…
• Simulation
23
Summary (Implementation)
•
•
•
•
Plan before coding
Using Modules
Import
Constants
24
Tuples
• Fixed size
• Immutable (similarly to Strings)
• What are they good for (compared to list)?
• Simpler (“light weight”)
• Staff multiple things into a single container
• Immutable (e.g., records in database)
25
Dictionaries (Hash Tables)
• Key – Value mapping
• Fast!
• Usage:
• Database
• Dictionary
• Phone book
keys
values
26
Dictionaries (Cont.)
27
Dictionaries (Cont.)
28