⚡ [performance] Optimize max_drawdown calculation to O(N)#205
Conversation
Replaced the suboptimal O(N^2) nested loop in xalpha/indicator.py with a single-pass O(N) algorithm that tracks the running peak. Switched from iterrows() to itertuples(index=False) for improved iteration performance in Pandas. Added explicit handling for insufficient data to preserve legacy ValueError behavior. Co-authored-by: refraction-ray <35157286+refraction-ray@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What:
Optimized the$O(N^2)$ algorithm with a single-pass $O(N)$ peak-tracking algorithm. The implementation also switches from
max_drawdownmethod inxalpha/indicator.pyby replacing a nested loopiterrows()toitertuples(index=False)for more efficient Pandas iteration.🎯 Why:
The previous$O(N^2)$ implementation caused significant performance bottlenecks as the price series grew, making drawdown analysis of long-term portfolios or high-frequency data unnecessarily slow.
📊 Measured Improvement:
Using a benchmark with 2000 random data points:
The optimization preserves the original behavior, including chronological tie-breaking and raising a
ValueErrorwhen given fewer than 2 data points.PR created automatically by Jules for task 9225084732163863234 started by @refraction-ray