-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Implement high performance rolling_rank #9481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have added it to the master issue. Its actuallly pretty straightforward to do this. Note that all that is necessary is a new method, want to do a pull-request here? |
Yes that sounds like what I was thinking, pandas.stats.moments.rolling_rank with the same arguments as rank, allowing the user to select a competition method (min, avg....) and whether ranks should be ascending. Not sure I'm familiar enough with the pandas code yet to make any modifications my self to implement the best solution in the environment for this though. Happy to test though. |
I got rolling_indmax and rolling_indxmin working just as @jreback said, recording the index. No extra memory allocation or computation on top of the normal algorithm, just an additional return array from roll_max2/roll_min2 holding the indices. At the PyCon sprint @jreback suggested I return an int64 from the Cython code to store the indices, but I'm pretty sure it has to be a float64 due to possible NaN's, right? Also, @PH82 requested rolling_idxaverage but is this really necessary? |
@cing you can use -1 to mark missing values values as long as you only use positive indices otherwise (that's pretty standard in pandas) |
@cing, The average I mention is to determine how to rank ties. Common methods for how to rank ties are:
|
I'll have to take a look at this again, some of the test cases didn't succeed for rolling_idxmax and rolling_idxmin. I didn't implement a rolling_idxavg though, is that truly useful? |
I would say so, for my immediate purposes no, but it is a valid tie method |
I am glad to see there is an issue ticket already, for this rolling rank. |
@Dmoonleo the labels indicate the status, meaning that its open and unclaimed. you are welcome to submit a pull-request. |
Is this going to be implemented? |
@mcherkassky you're welcome take take a crack at it. Let us know if you need help getting started. |
I still have the code I wrote back from the good ol' days, but it's worth taking a fresh look because window.pyx has been touched a few times. As a novice, I'd just comment that the windowed rolling algorithm is a bit more complicated than the "novice" tag might suggest! Nothing teamwork can't crack though. |
does anyone have an algorithm reference to implement the rolling rank? it doesn't seem trivial i'm happy to give it a stab but i don't really follow the discussion above about rolling_idxmax/rolling_idxmin and how that helps get the rolling rank |
Is there anyone still working on the efficient implementation of rolling rank? |
no would love to have this! |
I propose an algorithm to calculate rolling_rank efficiently. Suppose window size is fixed, and rank is defined when the window number is sorted in monotone increasing. We can use a balanced tree to store window data, as it only takes O(logM) for insert, delete and finding operations, where M is the window size. In the procedure of insert, delete operation, we should maintain a field called size of each node representing the count of nodes of this sub-tree, which will be used in calculating rank. If a balanced tree maintain the size field initially (such as Size-Balanced Tree or Weight Balanced Tree), it would be better. Due to numbers are organized orderly in a balanced tree, when we want to get the rank of a number in this window, we can just find the corresponding node starting from the tree root, and sum the size of all the left child tree through the finding path. The sum is the rank. This operation is also O(logM). From the above, when calculating a rolling rank in a length N sequence where window size is M, the total time complexity is O(NlogM). It's much better than that of the naive algorithm (sort and get the rank in each window) O(NMlogM). This algorithm could be M times fast than the original one. |
If there is still interest, my workaround for this is:
The Running it on my small laptop:
|
In case it's useful I'm using it like this:
|
I implemented it. Computational complexity( n: input length w: rolling window size )
|
@contribu thanks for the implementaton. Ideally this would port almost directly to cython and embedded in the current infrastructure. we don't have very much c++ code in pandas and mostly use cython. if you could do this would be fantastic. |
xref SO issue here
Im looking to set the rolling rank on a dataframe. Having posted, discussed and analysed the code it looks like the suggested way would be to use the pandas Series.rank function as an argument in rolling_apply. However on large datasets the performance is particularly poor. I have tried different implementations and using bottlenecks rank method orders of magnitude faster, but that only offers the average option for ties. It is also still some way off the performance of rolling_mean. I have previously implemented a rolling rank function which monitors changes on a moving window (in a similar way to algos.roll_mean I believe) rather that recalculating the rank from scratch on each window. Below is an example to highlight the performance, it should be possible to implement a rolling rank with comparable performance to rolling_mean.
python: 2.7.3
pandas: 0.15.2
scipy: 0.10.1
bottleneck: 0.7.0
The text was updated successfully, but these errors were encountered: