Probability density and cumulative functions in SciPy - Time & Space Complexity
We want to understand how the time to calculate probability density and cumulative functions changes as we increase the number of points.
How does the work grow when we ask for more values?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.stats import norm
x = np.linspace(-3, 3, 1000)
pdf_values = norm.pdf(x)
cdf_values = norm.cdf(x)
This code calculates the probability density function (pdf) and cumulative distribution function (cdf) for 1000 points from a normal distribution.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Computing pdf and cdf values for each point in the array.
- How many times: Once for each of the 1000 points (length of x).
Each additional point requires one more calculation for pdf and one more for cdf.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 calculations (10 pdf + 10 cdf) |
| 100 | About 200 calculations |
| 1000 | About 2000 calculations |
Pattern observation: The total work grows roughly in direct proportion to the number of points.
Time Complexity: O(n)
This means the time to compute pdf and cdf grows linearly with the number of points you evaluate.
[X] Wrong: "Calculating pdf and cdf for many points is constant time because the functions are built-in."
[OK] Correct: Even built-in functions must compute each value separately, so more points mean more work.
Understanding how function calls scale with input size helps you explain performance clearly and shows you can reason about efficiency in data science tasks.
"What if we used vectorized operations on GPU instead of CPU? How would the time complexity change?"
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
