Normal distribution in SciPy - Time & Space Complexity
We want to understand how the time it takes to work with the normal distribution changes as we use more data points.
Specifically, how does the time grow when we calculate probabilities or generate many random values?
Analyze the time complexity of the following code snippet.
from scipy.stats import norm
# Generate 1000 random values from a normal distribution
samples = norm.rvs(loc=0, scale=1, size=1000)
# Calculate the probability density function for each sample
pdf_values = norm.pdf(samples, loc=0, scale=1)
This code generates random values from a normal distribution and then calculates the probability density for each value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating the probability density function (pdf) for each sample.
- How many times: Once for each of the 1000 samples (or n samples in general).
Each additional sample requires one pdf calculation, so the total work grows directly with the number of samples.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pdf calculations |
| 100 | 100 pdf calculations |
| 1000 | 1000 pdf calculations |
Pattern observation: The time grows in a straight line as the number of samples increases.
Time Complexity: O(n)
This means the time to compute grows directly in proportion to the number of samples.
[X] Wrong: "Calculating the pdf for many samples takes the same time as for one sample because the function is fast."
[OK] Correct: Even if one calculation is quick, doing it many times adds up, so time grows with the number of samples.
Understanding how time grows with data size helps you explain your code's efficiency clearly and confidently in real-world tasks.
"What if we calculate the cumulative distribution function (cdf) instead of the pdf for each sample? How would the time complexity change?"
Practice
Solution
Step 1: Understand the normal distribution concept
The normal distribution describes how data points are spread around a central average (mean).Step 2: Identify the correct description
Among the options, only data spread around an average matches the normal distribution's purpose.Final Answer:
Data spread around an average value -> Option DQuick Check:
Normal distribution = data around average [OK]
- Thinking normal distribution only has positive values
- Confusing normal distribution with sorted data
- Assuming no variation in normal distribution
Solution
Step 1: Recall scipy.stats import syntax
The normal distribution functions are in scipy.stats under the name 'norm'.Step 2: Match correct import statement
Only 'from scipy.stats import norm' correctly imports the normal distribution functions.Final Answer:
from scipy.stats import norm -> Option BQuick Check:
Correct import = from scipy.stats import norm [OK]
- Using 'import scipy.norm' which does not exist
- Trying 'from scipy import normal' which is incorrect
- Importing 'stats.norm' without scipy prefix
from scipy.stats import norm result = norm.cdf(0, loc=0, scale=1) print(round(result, 2))
Solution
Step 1: Understand norm.cdf at mean
norm.cdf(x, loc, scale) gives the probability data is less than x. At x=0 with mean=0, scale=1, it is the 50th percentile.Step 2: Calculate and round the result
The cumulative probability at mean is 0.5, rounding to 2 decimals gives 0.50.Final Answer:
0.50 -> Option CQuick Check:
norm.cdf(mean) = 0.5 [OK]
- Confusing PDF with CDF values
- Expecting 0 or 1 instead of 0.5 at mean
- Rounding errors ignoring decimals
from scipy.stats import norm prob = norm.pdf(1, mean=0, scale=1) print(prob)
Solution
Step 1: Check parameter names for norm.pdf
norm.pdf uses 'loc' for mean, not 'mean'. Using 'mean' causes an error.Step 2: Identify correct parameter
Replace 'mean=0' with 'loc=0' to fix the code.Final Answer:
Parameter 'mean' should be 'loc' -> Option AQuick Check:
Use loc for mean in norm functions [OK]
- Using 'mean' instead of 'loc' parameter
- Assuming norm.pdf is missing
- Thinking scale must be integer
Solution
Step 1: Identify the function for cumulative probability
To find probability less than a value, use norm.cdf with the value, mean (loc), and std dev (scale).Step 2: Match parameters to the problem
Value is 13, mean is 10, std dev is 2, so norm.cdf(13, loc=10, scale=2) is correct.Final Answer:
norm.cdf(13, loc=10, scale=2) -> Option AQuick Check:
Use norm.cdf(value, loc=mean, scale=std) [OK]
- Using norm.pdf instead of norm.cdf
- Swapping loc and value parameters
- Using norm.sf which gives probability greater than value
Ready to prove it?
Pass the test and this pattern moves unknown
