Probability Density and Cumulative Functions with SciPy
📖 Scenario: You work as a data analyst for a company that studies daily rainfall amounts. You want to understand the likelihood of different rainfall amounts using a common probability model called the normal distribution.
🎯 Goal: Build a small program that uses SciPy to calculate the probability density and cumulative probability for specific rainfall amounts using the normal distribution.
📋 What You'll Learn
Create a normal distribution with mean 50 and standard deviation 10
Calculate the probability density function (PDF) for rainfall amounts 40, 50, and 60
Calculate the cumulative distribution function (CDF) for rainfall amounts 40, 50, and 60
Print the PDF and CDF results clearly
💡 Why This Matters
🌍 Real World
Understanding rainfall probabilities helps in planning for agriculture, water management, and flood prevention.
💼 Career
Data analysts and scientists often use probability distributions to model and predict real-world events.
Progress0 / 4 steps
1
Create the rainfall amounts list
Create a list called rainfall_amounts with the exact values 40, 50, and 60.
SciPy
Hint
Use square brackets to create a list with the numbers 40, 50, and 60 inside.
2
Set up the normal distribution parameters
Create two variables: mean set to 50 and std_dev set to 10.
SciPy
Hint
Use simple assignment to create mean and std_dev variables with the given numbers.
3
Calculate PDF and CDF values using SciPy
Import norm from scipy.stats. Then create a normal distribution object called normal_dist using mean and std_dev. Use normal_dist.pdf() to calculate the PDF values for rainfall_amounts and store in pdf_values. Use normal_dist.cdf() to calculate the CDF values for rainfall_amounts and store in cdf_values.
SciPy
Hint
Use norm(loc=mean, scale=std_dev) to create the distribution. Then call pdf() and cdf() with the list rainfall_amounts.
4
Print the PDF and CDF results
Print the pdf_values and cdf_values variables. Use print() statements with clear labels: "PDF values:" and "CDF values:".
SciPy
Hint
Use two print statements. Print the label first, then the variable.
Practice
(1/5)
1. What does the Probability Density Function (PDF) represent in a data distribution?
easy
A. The average value of the data
B. The likelihood of a value occurring at a specific point
C. The total probability up to a certain value
D. The maximum value in the data
Solution
Step 1: Understand PDF meaning
The PDF shows how dense or likely values are at specific points in a distribution.
Step 2: Differentiate from CDF
The CDF shows total probability up to a point, not the likelihood at a point.
Final Answer:
The likelihood of a value occurring at a specific point -> Option B
Quick Check:
PDF = Likelihood at point [OK]
Hint: PDF shows likelihood at exact points, not cumulative [OK]
Common Mistakes:
Confusing PDF with CDF
Thinking PDF gives total probability
Assuming PDF gives average or max value
2. Which of the following is the correct way to import the cumulative distribution function (CDF) for a normal distribution from scipy.stats?
easy
A. import norm from scipy.stats; norm.cdf(x)
B. import scipy.stats.norm as norm; norm.cdf(x)
C. from scipy import norm; norm.cdf(x)
D. from scipy.stats import norm; norm.cdf(x)
Solution
Step 1: Recall scipy.stats import syntax
The correct import for normal distribution functions is from scipy.stats import norm.
Step 2: Use norm.cdf(x) to get CDF
After import, norm.cdf(x) computes the cumulative distribution function at x.
Final Answer:
from scipy.stats import norm; norm.cdf(x) -> Option D
Quick Check:
Correct import and usage = from scipy.stats import norm; norm.cdf(x) [OK]
Hint: Use 'from scipy.stats import norm' for normal distribution functions [OK]
Common Mistakes:
Wrong import syntax causing errors
Using 'import scipy.stats.norm as norm' which is invalid
Trying to import norm directly from scipy
3. What is the output of the following code snippet?
from scipy.stats import norm
result = norm.pdf(0, loc=0, scale=1)
print(round(result, 3))
medium
A. 0.399
B. 0.500
C. 1.000
D. 0.000
Solution
Step 1: Understand norm.pdf at mean
For a normal distribution with mean 0 and std 1, PDF at 0 is 1/sqrt(2*pi) ≈ 0.3989.
Step 2: Round the result
Rounding 0.3989 to 3 decimals gives 0.399.
Final Answer:
0.399 -> Option A
Quick Check:
PDF at mean = 0.399 [OK]
Hint: PDF at mean of standard normal ≈ 0.399 [OK]
Common Mistakes:
Confusing PDF with CDF value
Rounding incorrectly
Assuming PDF at mean is 0.5
4. Identify the error in this code that tries to calculate the CDF of a normal distribution at x=1:
import scipy.stats as stats
cdf_value = stats.norm.pdf(1, loc=0, scale=1)
print(cdf_value)
medium
A. Using pdf() instead of cdf() to get cumulative probability
B. Missing import statement for norm
C. Incorrect parameters for norm.pdf
D. Syntax error in print statement
Solution
Step 1: Check function used for CDF calculation
The code uses norm.pdf which gives probability density, not cumulative probability.
Step 2: Correct function for cumulative probability
To get cumulative probability, norm.cdf should be used instead of norm.pdf.
Final Answer:
Using pdf() instead of cdf() to get cumulative probability -> Option A
Quick Check:
Use cdf() for cumulative probability [OK]
Hint: Use cdf() for cumulative, pdf() for density [OK]
Common Mistakes:
Mixing pdf and cdf functions
Assuming pdf returns cumulative values
Ignoring function purpose
5. You have a dataset of exam scores that roughly follow a normal distribution with mean 70 and standard deviation 10. Using scipy, how would you find the probability that a student scored less than 85?
hard
A. Use norm.cdf(70, loc=85, scale=10) to get cumulative probability
B. Use norm.pdf(85, loc=70, scale=10) to get cumulative probability
C. Use norm.cdf(85, loc=70, scale=10) to get cumulative probability
D. Use norm.pdf(70, loc=85, scale=10) to get cumulative probability
Solution
Step 1: Identify correct function for cumulative probability
To find probability of scoring less than 85, use the cumulative distribution function norm.cdf.
Step 2: Use correct parameters for mean and std
Set loc=70 (mean) and scale=10 (std deviation), and evaluate at 85.
Final Answer:
Use norm.cdf(85, loc=70, scale=10) to get cumulative probability -> Option C
Quick Check:
norm.cdf(value, loc=mean, scale=std) = cumulative probability [OK]
Hint: CDF with loc=mean, scale=std, value=threshold [OK]
Common Mistakes:
Using pdf instead of cdf for cumulative probability
Swapping mean and value parameters
Confusing scale with loc
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.