Poisson distribution in SciPy - Time & Space Complexity
We want to understand how the time needed to calculate Poisson probabilities changes as we ask for more values.
How does the work grow when we increase the number of events we analyze?
Analyze the time complexity of the following code snippet.
from scipy.stats import poisson
lambda_val = 3.5
k_values = range(1000)
probabilities = [poisson.pmf(k, lambda_val) for k in k_values]
This code calculates the Poisson probability for each number of events from 0 to 999 with a fixed average rate.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating the Poisson probability mass function (pmf) for each event count.
- How many times: Once for each value in the range, here 1000 times.
Each new event count requires one pmf calculation, so the total work grows directly with the number of event counts.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pmf calculations |
| 100 | 100 pmf calculations |
| 1000 | 1000 pmf calculations |
Pattern observation: Doubling the number of event counts doubles the total calculations needed.
Time Complexity: O(n)
This means the time to compute probabilities grows in a straight line with the number of event counts we calculate.
[X] Wrong: "Calculating Poisson probabilities for many values is instant and does not depend on how many values we ask for."
[OK] Correct: Each probability calculation takes some time, so more values mean more total work and longer time.
Understanding how time grows with input size helps you explain your code's efficiency clearly and shows you can think about performance in real tasks.
"What if we used a vectorized function to calculate all probabilities at once? How would the time complexity change?"