We use probability density and cumulative functions to understand how likely values are in a range and how probabilities add up over that range.
Probability density and cumulative functions in SciPy
from scipy.stats import distribution_name # Probability Density Function (PDF) pdf_value = distribution_name.pdf(x, *params) # Cumulative Distribution Function (CDF) cdf_value = distribution_name.cdf(x, *params)
distribution_name is the name of the distribution like norm (normal), expon (exponential), etc.
x is the value or array of values where you want to calculate the function.
from scipy.stats import norm # PDF at x=0 for standard normal distribution pdf_val = norm.pdf(0)
from scipy.stats import norm # CDF at x=1 for standard normal distribution cdf_val = norm.cdf(1)
from scipy.stats import expon # PDF at x=2 for exponential distribution with scale=1 pdf_val = expon.pdf(2, scale=1)
This program calculates and prints the PDF and CDF values at 0 for a normal distribution. It also plots the PDF and CDF curves from -3 to 3 to show how probabilities are distributed and accumulated.
from scipy.stats import norm import numpy as np import matplotlib.pyplot as plt # Values from -3 to 3 x = np.linspace(-3, 3, 100) # Calculate PDF and CDF for standard normal distribution pdf = norm.pdf(x) cdf = norm.cdf(x) # Print some values print(f"PDF at 0: {norm.pdf(0):.4f}") print(f"CDF at 0: {norm.cdf(0):.4f}") # Plot PDF and CDF plt.plot(x, pdf, label='PDF') plt.plot(x, cdf, label='CDF') plt.title('Normal Distribution PDF and CDF') plt.xlabel('x') plt.ylabel('Probability') plt.legend() plt.grid(True) plt.show()
PDF values are not probabilities themselves but densities; they can be greater than 1.
CDF values always range from 0 to 1 and show cumulative probability up to x.
Use the same parameters for PDF and CDF to compare correctly.
PDF shows how dense or likely values are at specific points.
CDF shows the total probability up to a point.
Both help understand and work with data distributions.
Practice
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 BQuick Check:
PDF = Likelihood at point [OK]
- Confusing PDF with CDF
- Thinking PDF gives total probability
- Assuming PDF gives average or max value
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 DQuick Check:
Correct import and usage = from scipy.stats import norm; norm.cdf(x) [OK]
- Wrong import syntax causing errors
- Using 'import scipy.stats.norm as norm' which is invalid
- Trying to import norm directly from scipy
from scipy.stats import norm result = norm.pdf(0, loc=0, scale=1) print(round(result, 3))
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 AQuick Check:
PDF at mean = 0.399 [OK]
- Confusing PDF with CDF value
- Rounding incorrectly
- Assuming PDF at mean is 0.5
import scipy.stats as stats cdf_value = stats.norm.pdf(1, loc=0, scale=1) print(cdf_value)
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 AQuick Check:
Use cdf() for cumulative probability [OK]
- Mixing pdf and cdf functions
- Assuming pdf returns cumulative values
- Ignoring function purpose
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:
Usenorm.cdf(85, loc=70, scale=10)to get cumulative probability -> Option CQuick Check:
norm.cdf(value, loc=mean, scale=std) = cumulative probability [OK]
- Using pdf instead of cdf for cumulative probability
- Swapping mean and value parameters
- Confusing scale with loc
Ready to prove it?
Pass the test and this pattern moves unknown
