-
Notifications
You must be signed in to change notification settings - Fork 194
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
alternative characterization for weighted sample quantile #687
base: master
Are you sure you want to change the base?
Conversation
See #435 and linked PR. @matthieugomez may be able to answer. |
@salbert83 Does your quantile function gives the same thing as Base Julia for weights equal to one? |
The short answer is no, because the solution to the associated optimization problem might not be unique. I extended the writeup. |
There are several ways to extend unweighted quantile for non-frequency weights. The current implementation satisfies these three properties:
If you can show me that your definition satisfies these three properties, and that it can also be seen as the solution of a nice minimization problem, I'd be down to use it instead. |
The notes elaborate on the associated optimization problem, which may not have a unique solution. Thanks again for your time. |
In the pdf, you don't show that 1, 2, 3 are true, you just look at one example. |
The implementation largely follows the current implementation. A view is taken restricted to observations with nonzero weights, so (3) should be satisfied in all cases. The code should also be invariant to rescaling all the weights by the same positive constant, so (2) should be satisfied. (1) is not satisfied, as demonstrated by the example. In my humble opinion, for this case I find 30 easier to explain than 30.7 for the 30% quantile. Thanks again. |
Right, 2&3 are satisfied. And I agree with you — I don't particularly like the definition of unweighted quantile used in Julia. But I still think it's important to match it in case weights equal to one. |
The unweighted quantile is the one from Statistics.jl, right? I see Julia defaults to definition 7 of Hyndman and Fan (1996). It seems definition 4 is more consistent with the results of the optimization (achieved setting alpha = 0, beta =1 in optional params), whereas the Julia defaults for these optional parameters are alpha = beta = 1. I'm guessing since my main interest is in MLE calculations, would you suggest I reopen that PR with the specific point that the Julia weighted quantile calculation is not correct for this case? Thank you. |
I guess you could open a PR to add a |
Statistics.quantile already supports the optional arguments, Your suggestion then is to add a kwarg to the StatsBase calculation? |
Oh, I see! So yeah, it'd be terrific to add the kwargs |
I implemented the MLE consistent calculation using an optional kwarg. It doesn't cover the full range regarding alpha and beta settings available in Statistics.quantile, but does provide an option for those requiring this for MLE. I refactored the implementation so both methods share as much code as possible. Please review. Thanks. |
Even if you don't support arbitrary |
While I think alpha = 0 and beta = 1 are consistent with the optimization approach, I think it would be misleading. Statistics.quantile supports arbitrary alpha and beta in [0,1] as parameters controlling interpolation. The point of the kwarg here is to indicate its consistency with MLE. |
Sorry, closed accidentally. |
Why would it be misleading? That would make the different |
I did some further investigation to see whether this could work. Unfortunately, my previous speculation was incorrect, as demonstrated by the following examples. Consider the following 2 cases, where the function "f" is the cost function from my earlier notes. Example 1 Example 2: In Example 1, alpha = 0 and beta = 1 is consistent with the optimization approach. In Example 2, alpha = 1 and beta = 0 is consistent. In both these cases, the minima is unique and there is no ambiguity in the value that is selected by optimization. I believe selecting according to this optimization criteria doesn't quite fit the current framework of selecting interpolation parameters. |
Here are some examples with equal weights showing selection by optimization is different from choosing alpha and beta parameters for interpolation: z = [0.1576177442664426, 0.22588785878291495, 0.3472306214052947, 0.484404897858993, 0.5295720233336718, 0.5706659909590641, 0.6204132900748311, 0.6264793742353929, 0.9104324596368851, 0.9773671697505484] |
Thanks for checking. So you mean that this is yet another variant that isn't supported by the unweighted method in Statistics? Is it implemented in other statistical packages? If we add support for the weighted variant, we should probably support the unweighted one too (in Statistics). @matthieugomez What do you think? |
I'm not sure whether this is implemented in other statistical packages. I'm not a statistician by trade, so I can't claim expertise here. My motivation for this pull request started here: JuliaStats/Distributions.jl#1310 (comment) The weighted absolute deviation calculation arises if, for example, one uses Laplace distributions as part of a GMM or HMM. I'd made a submission regarding this to Distributions.jl, but was informed what was available (the weighted median) was good enough. This relied on the current weighted quantile method, which doesn't actually match the MLE for the distribution. I disagreed with the reviewer's assessment, but it did seem reasonable to see whether there was interest in a general weighted quantile calculation that could then be leveraged for my use case of interest. Thanks again. |
OK. Please do have a look at what other languages do, as it's generally a good idea to try using the same vocabulary for argument names if possible. @devmotion Do you have an opinion about this? |
I saw the following Python package, but I'm not sure how widely it is used, https://github.com/nudomarinero/wquantiles. My personal opinion is to design around use cases. My current use case is a correct implementation for parameter estimation for a weighted Laplace distribution. By correct, I mean returning a value which maximizes the likelihood function, noting that this need not uniquely determine the value. To resolve non-uniqueness, I imagine users would want to have a prior, in which case they would probably want to handle the details themselves. |
I am not completely sure, are you mainly interested in my opinion about keyword argument names? I just skimmed through the discussion quickly but I think it mixes up different ways to define (weighted) quantiles, based on the (smoothed) cumulative distribution function and based on quantile regression. As mentioned before by others, I think any implementation of weighted quantiles in StatsBase has to be consistent with the unweighted implementation in Statistics. I would go even further than property 1 above and say that it would be desirable that it 1+: equals unweighted quantile with replicated samples for integer weights It would be nice to add supports for parameter To me it seems, it would be better to implement estimates based on (weighted) quantile regression in a separate function. |
I'm not sure how to proceed here. My end goal is to have a weighted Laplace fit (similar to weighted fits for other distributions, that returns a correct answer. If the internal implementation needs to leverage the StatsBase weighted median function, then this would need to change. But then that causes inconsistency with the unweighted quantile calculations in Statistics. It seems to me the way to proceed is to introduce a separate weighted quantile regression (per the previous suggestion), and then submit a pull request for Laplace that leverages this function. If there's general agreement, I'll reimplement. Thanks. |
Yes, this would be my suggestion. What do you think @nalimilan? |
I am not familiar with the weighted quantile calculation provided by StatsBase.jl. It seems inconsistent with what one would want, for example, in a minimum weighted absolute deviation calculation. Based on the implementation, it seems this is intentional, but I am not sure why? Please review the document and sample code. Thank you.
QuantileCalc.pdf
quantile_chk.txt