Financial data analysis pattern in Data Analysis Python - Time & Space Complexity
When analyzing financial data, we often process many records to find trends or calculate statistics.
We want to know how the time to analyze grows as the data size grows.
Analyze the time complexity of the following code snippet.
import pandas as pd
def calculate_moving_average(data, window_size):
moving_averages = []
for i in range(len(data) - window_size + 1):
window = data[i : i + window_size]
moving_averages.append(window.mean())
return moving_averages
# Example usage:
# prices = pd.Series([...])
# calculate_moving_average(prices, 5)
This code calculates the moving average of financial prices over a sliding window.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the data to calculate averages for each window.
- How many times: The loop runs once for each possible window, about n - window_size + 1 times.
- Inside the loop: Calculating the mean of each window, which itself looks at window_size elements.
As the data size grows, the number of windows grows roughly with n, and each window mean looks at window_size elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 6 windows x 5 elements = 30 operations |
| 100 | About 96 windows x 5 elements = 480 operations |
| 1000 | About 996 windows x 5 elements = 4980 operations |
Pattern observation: The total work grows roughly in a straight line with the size of the data.
Time Complexity: O(n * window_size)
This means the time to calculate moving averages grows directly with the number of data points and the window size.
[X] Wrong: "Calculating each window mean is constant time, so total time is constant."
[OK] Correct: Each window mean looks at multiple elements, so the total work adds up as data grows.
Understanding how data size affects analysis time helps you explain your approach clearly and shows you think about efficiency.
"What if we used a cumulative sum to calculate moving averages instead? How would the time complexity change?"