Hello, and welcome to a video on probability in python. We'll be working with the scipy.stats.norm.cdf
function. We won't need to load in any data today. So let's start by loading in our library. So from scipy.stats,
we'll import norm, okay? Now, we're going to be working on a very special
kind of problem that we'll have today called finding the area under the curve. And what that means is
we'll have a distribution. And we're looking for the area under the curve of that distribution. Here's what that
type of problem looks like. In a normally distributed distribution with a mean of eight and a standard deviation
of 12. What is the probability that a randomly selected score will fall below 92?
Let's take a look at a visualization of that. You can see here
we have a normal distribution. And this is where our 92 is.
We have our mean of 80 and
our standard deviation of 12. So right here would be the first standard deviation.
where it is on the other side. So we know that 92 is one standard deviation
And what we're looking for is all of this blue shaded area here. We
want to know how much area this contains.
So to do that, we're going to use norm.cdf. And it will ask us for a couple
of arguments. We're going to tell it that we have a score
of 92 that we're looking for. A mean of 80. And a standard deviation of 12.
And these are called loc and scale. You don't actually
need to include these argument labels. This line works just as well. If you just have 92 comma 80 comma
12. All right. Let's go ahead and run this and see what our answer is.
Okay. So this says that the probability of selecting a value below 92 is 0.8413
This looks about right. If we go back to our image, we
can see that this could be about 84% of our distribution. We know that we have most
of our distribution, most of the percent of our distribution in the center here.
So we can say that that visually checks out. So what if we want to find a value between two
values? Here's another type of problem you might run nto. What is the probability in a normally distributed
distribution of selecting an X value between 54 and 78 with a mean of 70 and
a standard deviation of 8? Let's go back to our visualization this
time. We're looking for this shaded area here.
How much of our distribution is between these two X values. To do
that we will subtract the smaller X value from the larger X value.
You always want to do it in that order, because we can't have negative values in probability. You can only have
them with a domain of zero and one or between zero and one.
So we're going to find probability between norm.cdf. And we're taking the
larger of our two X values of 78 with a mean of 70 and standard deviation of 8 minus
an X value of 54 with a mean of 70 and a standard deviation of 8.
to guess what it's going for just from looking at the picture. And if
you get something close to 82% then you are pretty correct.
We can take another look at this again and kind of mentally verify, because we know that not very much of our
distribution is in these two tail ends here. Most of it falls between the center. So again, we've got about 82%
of our distribution between two values.
So we've got another type of problem you might run into. What if we want to find the probability of randomly selecting a value
above X? Let's use that first problem as an example. What if we wanted to find the probability of selecting
a value above 92 with a mean of 80 and standard deviation of 12?
Let's go back to our visualization. This time we have that smaller part of the distribution that we're
looking for. So we know that norm.cdf will find anything below
So how would we tell it we want the opposite of that. And the answer is
that we subtract from one. So, prob_above equals one minus,
norm.cdf, 92, 80, 12.
And this will essentially cut away the part of
the distribution that we were looking at before. So we're saying one because probabilities only exist between zero
and one. One minus, this value here leaves us with
this red shaded area. Okay,
and that comes out to .1587, roughly. Which makes sense because
we got 0.8413 up here. And those two numbers together will equal
about one. So what if we'd like to calculate a percentile?
Let's say a clinic wants to identify patients who score low on a test. So the patients can be offered a new therapy.
The scores are normally distributed with a mean of 80 at a standard deviation 12. The clinic decides
on the lowest 40 percent of scores. What is the score that marks the 40 percentile? Now to do
this, you just use norm.ppf, give it the percentile,
give it the mean, give it your standard deviation. And that's it. It will find you the percentile, that information.
And here we have that our 40th percentile, the raw score is 76.95 now.
Okay, that's it for this lesson on finding the area under the curve, have fun coding.