Uniform distribution in SciPy - Time & Space Complexity
We want to understand how the time to generate uniform random numbers grows as we ask for more numbers.
How does the cost change when we increase the amount of data we create?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.stats import uniform
# Generate 1D array of n uniform random numbers between 0 and 1
n = 1000
samples = uniform.rvs(size=n)
This code generates n random numbers from a uniform distribution between 0 and 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random number independently.
- How many times: Exactly
ntimes, once per sample.
Each additional number requires one more generation step, so the total work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 random number generations |
| 100 | 100 random number generations |
| 1000 | 1000 random number generations |
Pattern observation: Doubling n doubles the work needed.
Time Complexity: O(n)
This means the time to generate samples grows in a straight line with the number of samples requested.
[X] Wrong: "Generating multiple uniform samples is constant time because it's just one function call."
[OK] Correct: Each sample requires work, so more samples mean more time, not the same time.
Understanding how data generation scales helps you explain performance when working with simulations or sampling in real projects.
"What if we generated a 2D array of samples instead of 1D? How would the time complexity change?"
Practice
loc parameter represent in the scipy.stats.uniform distribution?Solution
Step 1: Understand the parameters of uniform distribution
The uniform distribution in scipy useslocas the starting point of the range andscaleas the length of the range.Step 2: Identify the role of
Theloclocparameter sets the minimum value where the uniform distribution starts.Final Answer:
The starting point (minimum value) of the uniform distribution range -> Option AQuick Check:
loc= start value [OK]
- Confusing loc with scale
- Thinking loc is the mean
- Mixing loc with variance
Solution
Step 1: Calculate scale from range
The range is from 5 to 15, so the length is 15 - 5 = 10. This is the scale.Step 2: Set loc and scale correctly
Setloc=5(start) andscale=10(length) to cover 5 to 15.Final Answer:
uniform(loc=5, scale=10) -> Option CQuick Check:
loc=5, scale=15-5=10 [OK]
- Swapping loc and scale values
- Using scale as the end value
- Setting loc to zero incorrectly
from scipy.stats import uniform rv = uniform(loc=2, scale=3) print(round(rv.pdf(3), 2))
Solution
Step 1: Understand PDF of uniform distribution
PDF is constant inside the range and equals 1/scale. Here, scale=3, so PDF = 1/3 ≈ 0.3333.Step 2: Check if x=3 is inside the range
Range is from loc=2 to 2+3=5. Since 3 is inside, PDF at 3 is 1/3 ≈ 0.33.Final Answer:
0.33 -> Option AQuick Check:
PDF = 1/scale = 1/3 = 0.33 [OK]
- Calculating PDF as zero inside range
- Using loc instead of scale for PDF
- Rounding errors without checking range
from scipy.stats import uniform rv = uniform(loc=1, scale=4) prob = rv.cdf(6) print(prob)
Solution
Step 1: Check parameters and range
loc=1 and scale=4 means range is from 1 to 5 (1+4=5).Step 2: Evaluate cdf at 6
6 is outside the range (greater than 5), so cdf(6) should return 1 (full probability).Step 3: Confirm method existence and output
Thecdfmethod exists for uniform and returns valid output even outside range.Final Answer:
No error; code runs correctly -> Option BQuick Check:
cdf exists and returns 1 for x > loc+scale [OK]
- Thinking scale must cover queried x
- Assuming cdf method is missing
- Believing loc must be zero
Solution
Step 1: Set correct loc and scale for temperature range
Range is 10 to 20, so loc=10 and scale=20-10=10.Step 2: Calculate CDF at 15
CDF at 15 is (15 - loc)/scale = (15-10)/10 = 0.5, which is the probability temperature is less than 15.Final Answer:
rv = uniform(loc=10, scale=10) prob = rv.cdf(15) -> Option DQuick Check:
prob = (15-10)/10 = 0.5 [OK]
- Using wrong scale length
- Setting loc to zero incorrectly
- Calculating CDF outside range without adjustment
Ready to prove it?
Pass the test and this pattern moves unknown
