FAQ: Introduction to Probability Distributions - Using the Cumulative Distribution Function in Python

This community-built FAQ covers the “Using the Cumulative Distribution Function in Python” exercise from the lesson “Introduction to Probability Distributions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Master Statistics with Python

Probability

FAQs on the exercise Using the Cumulative Distribution Function in Python

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does the cdf show a slightly different output compared to the summed up pmf function while calculating the same probabilities? Shouldn’t they be the same?

prob_3 = stats.binom.cdf(5, 10, 0.5) - stats.binom.cdf(1, 10, 0.5)
print(prob_3)

## Output: 0.6123046874999999

print(stats.binom.pmf(2, n=10, p=.5) + stats.binom.pmf(3, n=10, p=.5) + stats.binom.pmf(4, n=10, p=.5) + stats.binom.pmf(5, n=10, p=.5))

## Output: 0.6123046875000006

1 Like

I think that’s just a limitation of floating point numbers and operations on them won’t give you perfect accuracy to an unlimited number of decimal places because there’s only a finite amount of space used to store the numbers.
(Calculating the cdf may also use different code than just the sum of the pmf internally.)

Although they’re both about 0.6123046875; that seems close enough.

1 Like

I just figured I should add this since it’s bugging me and perhaps anyone who reads this may find it useful. The exercise makes out like it is much harder to use the pmf for calculating the probability x falls within some range, but actually, I find it easier because you can simplify it by using the range() function and the .sum() method:

Suppose we want to calculate the probability that we observe between 2 and 7 heads. Using the cdf, we would write:

import scipy.stats as stats

prob_cdf = stats.binom.cdf(7, 10, 0.5) - stats.binom.cdf(1, 10, 0.5)
print(prob_cdf)
# output: 0.9345703125 

Which is not too tedious. But using the pmf, we can just write:

import scipy.stats as stats

prob_pmf = stats.binom.pmf(range(2, 8), 10, 0.5).sum()
print(prob_pmf)
# output: 0.9345703125

This kind of seems more intuitive to me so I just thought I would share in case others see this and were thinking the same thing.