Python and data for quants
A rolling mean, faster, part 1 of 3
A rolling mean, faster · Part 1 of 3
You compute a 50-day rolling mean over a list p of 1,000 prices, producing one value for each full window. Count one read each time a price is fetched from the list.
The naive version sums each window from scratch with sum(p[i - 49:i + 1]) / 50. How many reads does it make in total?
Answer with a number. Fractions, powers and expressions like 23/6 or C(52,5) are read correctly in practice.
The worked solution is in Premium
The answer, the full working and the one idea to take away – for this and all 1,322 questions in the bank. Answer it in practice and your working is marked, with a known mistake named when you make one.
Report an issue with this question
Learn the method
Performance: profiling, vectorising and when to leave Python
More python and data for quants questions
- You test x in container millions of times against a fixed collection of 100,000 ids.Foundation · Premium
- Profiling shows 60% of runtime in one function.Applied · Premium
- A NumPy array is C-ordered. Which loop is faster: summing along rows or along columns?Advanced · Premium
- A backtest computes pnl = signal * returns, where both are indexed by day. What is wrong?Applied · Free solution
- You need the quote in force at the instant of each trade. Which join?Applied · Free solution
- A US equity session runs 6.5 hours. How many five-minute bars does it contain?Foundation · Premium