Repository navigation
adding weighted empirical CDF - #8192
Conversation
|
Hello @dwr-psandhu! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2022-04-15 21:39:46 UTC |
| # which would have raised a NameError if hit, so would need to be | ||
| # fixed. See GH#5701. | ||
|
|
||
| class EWCDF(StepFunction): |
There was a problem hiding this comment.
Could you call it WeightedECDF? This seems easier to discover
| >>> ewcdf([3, 55, 0.5, 1.5]) | ||
| array([0.42857143, 1., 0. , 0.28571429]) | ||
| """ | ||
| def __init__(self, x, w, side='right'): |
There was a problem hiding this comment.
use weights instead of w as keyword
(I guess those are frequency or case weights)
| # which would have raised a NameError if hit, so would need to be | ||
| # fixed. See GH#5701. | ||
|
|
||
| class WeightedECDF(StepFunction): |
There was a problem hiding this comment.
qualifier as postfix is better, keeps better alphabetical order
ECDFWeighted
|
Also, keep the weighted version as a separate class. Most likely ECDF will be expanded with methods that only apply for continuous distribution. |
|
Is your application for this mainly discrete distributions? |
@josef-pkt Both ECDF and ECDFWeighted are for discrete distributions. |
|
ECDF is not for discrete distribution, at least the plan is to expand it For discrete ECDF we could get pointwise confidence interval directly from confint_proportion, AFAIU I guess the _conf_set function in the module, and KS test based simultaneous confint would only be appropriate for continuous distributions. For discrete distributions we could add an option to compute the weights from a full data series, edit _conf_set is a simultaneous confidence band it's not explicitly mentioned in wikipedia and related |
|
Ok. So is weighted ecdf only for discrete distributions? I would guess so as each observation is being weighted. In that case, is this solution acceptable? |
Yes, we will merge what you have (with maybe more changes) The main thoughts I have is about future development. If we go that route, then I would prefer a name like Also if we follow that interpretation, then we need nobs for confidence intervals. |
| @@ -1,11 +1,11 @@ | |||
| from statsmodels.tools._testing import PytestTester | |||
| from .empirical_distribution import ECDF, monotone_fn_inverter, StepFunction | |||
| from .empirical_distribution import ECDF, ECDFWeighted, monotone_fn_inverter, StepFunction | |||
There was a problem hiding this comment.
pep-8 failure line too long
use parenthesis is move to next line
|
ok, what I suggest: rename option for discrete (or ordered categorical, maybe not needed users can just use numeric codes): then independently of this PR we can add plot and confint #8193 methods |
@josef-pkt I only half understood so take a deeper look at the frequency counts that I did if weights are None. The answer is now subtly different between the current ECDF and ECDFDiscrete. See my log below |
|
I check the code tomorrow the new result in your last comment looks correct, that's the difference between handling ties or not ecdf treats the two |
| array([0.42857143, 1., 0. , 0.28571429]) | ||
| """ | ||
| def __init__(self, x, freq_weights=None, side='right'): | ||
| x = np.array(x) |
There was a problem hiding this comment.
always use np.asarray instead of np.array
avoids copy if item is already an array
| if freq_weights is None: | ||
| x, freq_weights = np.unique(x, return_counts=True) | ||
| assert len(freq_weights) == len(x) | ||
| w = np.array(freq_weights) |
There was a problem hiding this comment.
asarray conversion is only needed in an else of if freq_weights is None
| ax = x.argsort() | ||
| x = x[ax] | ||
| y = np.cumsum(w[ax]) | ||
| y=y/sw |
There was a problem hiding this comment.
pep-8 spaces around operators y = y / sw
|
you can use your previous example for a unit test in "statsmodels\distributions\tests\test_ecdf.py" also we should use an unequal spaced, noninteger testcase, something like
I guess that is supported without having to make changes to the code. Do you know how to use git rebase to squash commits? |
I am adding a test. |
currently that's by design, I don't know if we should change it or if changing it is necessary The current assumption is that, if the user provides freq_weights, then the user already provides the counts for unique values of x. |
|
Would it be useful for you handle ties even if freq_weights is specified? for example |
Yes. that makes sense. I'll drop that assertion and put this one instead. |
@josef-pkt I did a commit with a passing test based on your comment. If it all looks good, I can rebase, squashing all commits into one. Feel free to give me a good log message. |
josef-pkt
left a comment
There was a problem hiding this comment.
pep-8 and docstring changes
code itself looks good
| @@ -1,11 +1,12 @@ | |||
| from statsmodels.tools._testing import PytestTester | |||
| from .empirical_distribution import ECDF, monotone_fn_inverter, StepFunction | |||
| from .empirical_distribution import (ECDF, ECDFDiscrete, | |||
There was a problem hiding this comment.
pep-8 flake failure, start list in next line,
from .empirical_distribution import (
ECDF, ECDFDiscrete, monotone_fn_inverter, StepFunction
)
| ) | ||
|
|
||
| __all__ = ['test', 'ECDF', 'monotone_fn_inverter', 'StepFunction', | ||
| __all__ = ['test', 'ECDF', 'ECDFDiscrete', 'monotone_fn_inverter', 'StepFunction', |
There was a problem hiding this comment.
line too long
We will convert this to having one item per line, sorted alphabetically.
You can either do this here, or just change the line break and it will be changed as part of a future PR
| x : array_like | ||
| Observations | ||
| freq_weights : array_like | ||
| Weights of the observations. sum(freq_weights) is interpreted as nobs for confint |
There was a problem hiding this comment.
add:
If freq_weights is None, then the frequency counts for unique values will be computed from the data x.
| Parameters | ||
| ---------- | ||
| x : array_like | ||
| Observations |
There was a problem hiding this comment.
change to:
Data values. If freq_weights is None, then x is treated as observations and the ecdf is computed from the frequency counts of unique values using nunpy.unique. If freq_weights is not None, then x will be taken as the support of the mass point distribution with freq_weights as counts for x values.
The x values can be arbitrary sortable values and need not be integers.
| e1 = ECDFDiscrete([3.5, 3.5, 1.5, 1, 4]) | ||
| e2 = ECDFDiscrete([3.5, 1.5, 1, 4], freq_weights=[2, 1, 1, 1]) | ||
| npt.assert_array_equal(e1.x, e2.x) | ||
| npt.assert_array_equal(e1.y, e2.y) No newline at end of file |
There was a problem hiding this comment.
modules need newline at end of file
| npt.assert_array_equal(e.x, [-np.inf, 1, 3, 4]) | ||
| npt.assert_array_equal(e.y, [0, 0.25, 0.75, 1]) | ||
| e1 = ECDFDiscrete([3.5, 3.5, 1.5, 1, 4]) | ||
| e2 = ECDFDiscrete([3.5, 1.5, 1, 4], freq_weights=[2, 1, 1, 1]) |
There was a problem hiding this comment.
maybe add this example to docstring, mentioning that these are the two equivalent ways of defining the ecdf
| >>> import numpy as np | ||
| >>> from statsmodels.distributions.empirical_distribution import ECDFDiscrete | ||
| >>> | ||
| >>> ewcdf = ECDFDiscrete([3, 3, 1, 4], [1, 1, 1, 1]) # For equal weights ewcdf result is same as ecdf (see above) |
There was a problem hiding this comment.
use example that illustrate proper use, not cases that leave duplicate x values e.g.
ewcdf = ECDFDiscrete([3, 3, 1, 4])
and example below
ewcdf = ECDFDiscrete([3, 1, 4], [1.25, 2.5, 5])
|
@josef-pkt Fixed for all your requested changes. |
|
@josef-pkt @bashtage If ok, I can resubmit with commits squashed. |
|
looks good to me squash and I will merge it sorry for the delay, I got lost in a poisson PR that kept growing and getting messy |
|
this has been squashed to one commit. merging |
|
trailing whitespace in statsmodels/distributions/empirical_distribution.py |
NumPy's guide.
Details
Adding a new feature for weighted ECDF. It collapses to ECDF if weights are the same.