So as 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.
In [1]:
def f(p):
return 1-p
print f(0.3)
In [2]:
def f(p):
return p*p
print f(0.3)
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.
In [5]:
def f(p):
return 3 * p * (1-p) * (1-p)
print f(0.5)
print f(0.8)
In [7]:
def f(p1,p2):
return p1 * p2
print f(0.5,0.8)
In [9]:
def f(p0,p1,p2):
return p0 * p1 +(1-p0) * p2
print f(0.3,0.5,0.9)
Answer
And the answer is 0.78. And the way I got this, you might have picked point C1. That happens with 0.3 probability, and then we have a 0.5 chance to find heads. Or we might have picked coin two, which has a probability of 1 minus 0.3, 0.7, and then chance of seeing head is 0.9. We work this all out, we get 0.78.
Screenshot taken from Udacity
- P(C) = p0 = 0.1
- P(Pos|C) = p1 = 0.9
- P(Neg|not C) = p2 = 0.8
- P(C|Pos) = P(C) x P(Pos|C) = 0.1 * 0.9 = 0.09
- P(not C|Pos) = P(not C) x P(Pos|not C) = 0.9 * 0.2 = 0.18
- P(Pos) = P(C|Pos) + P(not C|Pos) = 0.27
Screenshot taken from Udacity
So now I want you to write the computer code that accepts arbitrary P₀, P₁, P₂ and calculates the resulting probability of a positive test result.
In [13]:
#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):
return p0 * p1 + (1-p0) * (1-p2)
print f(0.1, 0.9, 0.8)
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.
- P(C|Pos) = P(C) x P(Pos|C) = 0.1 * 0.9 = 0.09
- P(not C|Pos) = P(not C) x P(Pos|not C) = 0.9 * 0.2 = 0.18
- P(Pos) = P(C|Pos) + P(not C|Pos) = 0.27
- P(C|Pos) = P(C|Pos)/P(Pos) = 0.09/0.27 = 0.8
Answer
Screenshot taken from Udacity
Screenshot taken from Udacity
In [16]:
#Return the probability of A conditioned on B given that
#P(A)=p0, P(B|A)=p1, and P(Not B|Not A)=p2
def f(p0,p1,p2):
return p0 * p1 / (p0 * p1 + (1-p0) * (1-p2))
print f(0.1, 0.9, 0.8)
print f(0.01, 0.7, 0.9)
Screenshot taken from Udacity
Answer
In [19]:
#Return the probability of A conditioned on Not B given that
#P(A)=p0, P(B|A)=p1, and P(Not B|Not A)=p2
def f(p0,p1,p2):
return p0 * (1-p1) / (p0 * (1-p1) + (1-p0) * p2)
print f(0.1, 0.9, 0.8)
print f(0.01, 0.7, 0.9)