cta-observatory/pyirf

Allow for event weights when calculating percentile cuts

Opened this issue · 3 comments

When calculating a cut using cuts.calculate_percentile_cut, it would be useful to have to the option to associate weight with the values argument.

This can be done through an extra argument weights to cuts.calculate_percentile_cut that defaults to equal weights for all values. Then the np.nanpercentile in cuts.calculate_percentile_cut would have to be replaced by a function that calculates weighted percentiles.

If this strategy seems ok I can quickly open a pull request

Yes, sounds good, thanks!

I didn't consider weights inside bins important as of now, since we I'd expect the binning to be fine enough so that it doesn't matter, however if you have a usecase that requires taking the weights into account in bins, fine to add this here.

However, we'd of course need functions supporting weights for quantiles, which I didn't find in numpy or scipy with a quick search. So we might need to implement this ourselves using cumsum and searchsorted as proposed here:

https://stackoverflow.com/a/75321415/3838691

Yes, the weighted quantile stuff I have implemented in a custom function similar to the one in stackoverflow. I'll open a pull request