Professional Documents
Culture Documents
Sign In
Sign Up
Introduction to Statistics
st101 Week 2
CLASSROOM
PROGRESS
These are draft notes extracted from subtitles. Feel free to improve them. Contributions are most welcome. Thank you!
Contents
1 11. Programming Bayes Rule (Optional)
1.1 01 Printing Number
DISCUSSION
OVERVIEW
History
Please check the wiki guide for some tips on wiki editing.
MATERIALS
View
01 Printing Number
So this unit is optional, but I find it very empowering to program what we've learned on the computer, because once you've
programmed it, you can apply it forever. So, let me show you what I mean.So, here's our programming environment again,
and I've given you a very, very simple program.You can process it, it's called print 0.3 and in the next window,I just want you
to hit the run button to see what happens.
03 Functions
We will now give a slightly more complicated problem of the form we're going to be using we're going to be using and I hope
you won't be too confused.So, this is it. There's two parts to it.There's the print command as before but rather than printing
0.3 directly,we're going to print the function that computes something from 0.3.Right now it's identity is going to print out
exactly the same value but we're doing this is to set ourselves up to print something else.And here is how f is defined--we
define f with the element p that will be set to 0.3,to be just a function that returns exactly the same value.Why do we do
this? Well, to practice programming.So, hit the run button and see what happens.
#Write a function to return p as described in the video
def f(p):
Do you need professional PDFs? Try PDFmyURL!
04 Functions Solution
And once again, we get 0.3 and the reason is, as we go up, 0.3 is being funneled into the function f.F starts over here and p
is now 0.3.We return them and the value is 0.3 straight from the input and then the return of this is being printed.Sounds
complex--well, from now on all I want you to do is to modify what's inside this function.
#Write a function to return p as described in the video
def f(p):
#Insert your code here
return p
print f(0.3)
05 Complement
So the first exercise, say this is the probability, let's print the probability of the inverse event.Let's make the function over
here that takes p but returns 1 - p.So please go ahead and modify this code such that the return value is 1 minus p and not
p.
N ote:
Do you need professional PDFs? Try PDFmyURL!
The template code, which you're suppose to modify, is missing the following line at the bottom:
print f(0.3)
06 Complement Solution
This modification just replaces p by 1-p in the return function and then I run it I get 0.7.So the nice thing about our
complimentary probability we can now plug ina different value over here, say 0.1--with 0.1, I get as an output 0.9.So
congratulations, you've implemented the very first example of probability where the event probability is 0.1 and the
complementary event and negation of itis encapsulated in this function over here.
def f(p):
#Insert your code here
return 1-p
print f(0.3)
07 Two Flips
Do you need professional PDFs? Try PDFmyURL!
Here is my next quiz for you. Supposed we have a coin with probability p.For example, p might be 0.5.You flip the coin twice
and I want to compute the probability that this coin comes up head and heads in these 2 flips--obviously that's 0.5 times
0.5.But I want to do in a way that I can use any arbitrary value for p using the same style of code as before.So all your going
to modify is the 1-p into something that if I give a probability p returns to me the probability of seeing heads twice in this
coin--that is the probability of heads.
#Given theat the probability of one head is p, return the probability of
#two flips resulting in two heads
def f(p):
#Insert your code here
print f(0.1)
09 Three Flips
So let's up the ante and say we have a coin that has a certain probability of coming up with heads--again, it might be
Do you need professional PDFs? Try PDFmyURL!
0.5.Just like before it will be an input to the function f and now I'm going to flip the coin 3 times and I want you to calculate
the probability that the heads comes up exactly once.Three is not a variable so you could only works for 3 flips not for 2 or 4
but the only input variable is going to be the coin probability 0.5.So please change this code to express that number.
#Return the probability of exactly one head in three flips
def f(p):
#Insert your code here
print f(0.5)
15 Program Flipping
So the task for you now is to implement the function with three input arguments that it computes this number over here so
that it can vary any of those and still get the absolute correct answer for this function over here.If you've never programmed
before, this is tricky.You have to add one more argument and you have to change the returned function to implement a
Do you need professional PDFs? Try PDFmyURL!
formula just like this but this using p0, p1, p2as arguments not just the fixed numerical numbers here.
#Two coins have probabilities of heads of p1 andd p2
#The probability of selecting the first coin is p0
#Return the probability of a flip landing on heads
def f(p0,p1,p2):
#Insert your code here
17 Cancer Example 1
Let's go the cancer example. These are prior possibility of cancer we should call P. This is a probability give a positive test
given cancer. I call this Pand careful, these are probably a given negative test result for don't have cancer and I call this
P.Just to check suppose probability of cancer is 0.1, the sensitivity 0.9, specificity is 0.8.Given the probability that a test
will come out positive.It's not Bayes rule yet, it's a simpler calculation and you should know exactly how to do this.
Do you need professional PDFs? Try PDFmyURL!
19 Calculate Total
So now I want you to write the computer code that accepts arbitrary P, P, Pand calculates the resulting probability of a
Do you need professional PDFs? Try PDFmyURL!
So now I want you to write the computer code that accepts arbitrary P, P, Pand calculates the resulting probability of a
positive test result.
#Calculate the probability of a positive result given that
#p0=P(C)
#p1=P(Positive|C)
#p2=P(Negative|Not C)
def f(p0,p1,p2):
#Insert your code here
return p1*p0+(1-p2)*(1-p0)
20 Calculate Total
Here's my answer.My code does exactly what I've shown you before.It first considers the possibility of cancer, multiplies it
with the test sensitivity p1and then it observes the opposite possibility and of the course the specificity over here refers to
a negative test result so we take 1 minus this to get the positive one.Adding these two products up gives us the desired
result.So let's try this. It gives me a function f with the parameters we just assumed and if I hit run, I get 0.27.Obviously I can
change these parameters,so, suppose I make it much less likely to have cancer in the prior from 0.1 to 0.01 then my 0.27
changes to 0.207.Now I realise it's not the posterior in Bayes' Rule.It's just the probability of getting a positive test
result.You can see this if you change the prior probability of cancer to 0which means we don't have cancer, no matter what
the test result says.But there still is 0.2 chance of getting a positive test result and the reason is our test has a specificity
of 0.8 that is, even in the absence of cancer,there is a 0.2 chance of getting a positive test result.
#Calculate the probability of a positive result given that
#p0=P(C)
#p1=P(Positive|C)
#p2=P(Negative|Not C)
def f(p0,p1,p2):
#Insert your code here
21 Cancer Example 2
Now, let's go to the holy grail and implement today's work. Let's look at the posterior probability of cancer given that we
received the positive test result,and let's first do this manually for the example given up here.So what do you think it is?
27 Conclusion
So I really hope you enjoyed all this, and the reason is I really want you to do a program basic statistics by yourself.So, for
the rest of your life, you know how to program it.When you know how to program it, you know how to do it,and it will
empower you to solve basic problems in probability.I should tell you in my own life, I've built a lot of robots, and I've applied
Do you need professional PDFs? Try PDFmyURL!
Bayes rule like crazy.My job talk at Stanford was all about Bayes' rule applied to robotics.It's a very powerful paradigm and I
hope you enjoyed it.If you want to dive deeper, there's always Udacity CS373,which talks about programming and about half
of the classes variations of Baye's rule.I'll spare you with the details here because it's a basic introduction class,but stay
tuned for the next unit, I have another great surprise in store for you.
INFORMATION
COMMUNITY
UDACITY
What We Offer
Blog
About
Meetups
Jobs
Feedback Program
Contact Us
Legal
Udacity for iPad
FOLLOW US ON