Skip to content

ENH: Implement weighted rolling var and std #27682

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

Merged
merged 26 commits into from
Nov 8, 2019

Conversation

ihsansecer
Copy link
Contributor

Used West's online algorithm. Here is an explanation of the algorithm and link to pdf of the paper. Tested implementation with win_type="boxcar" comparing it with the result of Rolling.std.

Additionaly, _get_window function is split into two. _get_kwargs function is used to split kwargs for window function and rolling function (since std and var takes an additional ddof argument).

Shared docs for std and var are moved to be used with Window functions

result = 0
else:
result = t * win / ((win - <float64_t>ddof) * sum_w)
if result < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this only be negative if the weight was negative which we should validate much earlier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it might be if one weight is negative and another is positive but not necessarily

@@ -173,6 +173,17 @@ def __getattr__(self, attr):
def _dir_additions(self):
return self.obj._dir_additions()

def _get_kwargs(self, **kwargs) -> Tuple[dict, dict]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you import Dict from typing and add subscription if possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only annotated separated kwargs of window. The other one is remaining of kwargs from apply function which seems intractable. I guess we don't need to annotate empty dict nor it is possible in python

@WillAyd WillAyd added the Window rolling, ewma, expanding label Aug 1, 2019

def _get_roll_func(
self, cfunc: Callable, check_minp: Callable, index: np.ndarray, **kwargs
) -> Callable:
def func(arg, window, min_periods=None, closed=None):
minp = check_minp(min_periods, len(window))
return cfunc(arg, window, minp)
return cfunc(arg, window, minp, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than allowing arbitrary kwargs, can we just add a default weights=None arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwargs here is for ddof which is passed to var and std functions

@ihsansecer
Copy link
Contributor Author

@jreback could you review this?

@jreback
Copy link
Contributor

jreback commented Sep 8, 2019

can you merge master and will review

@ihsansecer
Copy link
Contributor Author

@jreback merged

@ihsansecer ihsansecer requested a review from jreback September 12, 2019 19:23
@jreback
Copy link
Contributor

jreback commented Oct 6, 2019

can you merge master and will have another look

@ihsansecer
Copy link
Contributor Author

@jreback merged

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good. make sure to add to window.rst the new functions

# invalid method
with pytest.raises(AttributeError):
(DataFrame(vals).rolling(5, win_type="boxcar", center=True).std())
# std
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move these to a new test (may also need to rename the original to _mean)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I parameterized the test instead. Is that alright?

@WillAyd
Copy link
Member

WillAyd commented Nov 7, 2019

@ihsansecer I think this is good; can you merge master and fix up conflict?

@jreback jreback added this to the 1.0 milestone Nov 8, 2019
@jreback jreback merged commit 9cf6269 into pandas-dev:master Nov 8, 2019
@jreback
Copy link
Contributor

jreback commented Nov 8, 2019

thanks @ihsansecer nice patch.

can you do a followup to make these nogil (@jbrockmendel recently added most window functions like this, e.g. see roll_sum)

@ihsansecer
Copy link
Contributor Author

@jreback sure but I have been a bit busy these days. I will do it when I have time.

Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rolling standard deviation fails when used with win_type
5 participants