kalepy icon indicating copy to clipboard operation
kalepy copied to clipboard

Negative weights for KDE

Open Pablobala opened this issue 2 years ago • 2 comments

I wanted to use the kalepy package on a SWeighted distribution, so there are many events with negative weights and I can't normalize them as the distribution would change. There is a ValueError in kalepy/kde.py, line 214: "Invalid weights entries, all must be finite and > 0!". I would want to know if there is the possibility to adapt the code to run with negative weights or if the method just not allow the use of negative weights for some reason. Could someone clarify this to me?

Thanks in advance and Kind Regards.

Pablobala avatar Nov 21 '22 19:11 Pablobala

Hi, in principle it seems like this should be possible, but I've never explored negative weights. There will be some points in the code that need modification, for example, how does one calculate covariances using negative weights?

You're welcome to try implementing this in the code. If you disable the error message you're getting, it will allow negative weights, but then I think some numpy functions will probably throw errors.

lzkelley avatar Nov 26 '22 17:11 lzkelley

Hi, just to let you know. I didn't find a way to handle negative weights because of the errors numpy throws when trying to calculate the covariances. So what I did is to split my sample into positive and negative weights, apply the KDE to both samples, and then rest the weights from "negative" sample renormalizing them by the sum of the sWeights. This seems to work but there are some downsides. If someone could find a way for handling negative weights it would be better. Regards!

Pablobala avatar Mar 23 '23 12:03 Pablobala