tommyod / KDEpy

Kernel Density Estimation in Python
https://kdepy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
584 stars 90 forks source link

how to get pseudo-uniform samples #144

Closed suhasghorp closed 1 year ago

suhasghorp commented 1 year ago

Hello, I am new to KDE and KDEpy. I understand that KDE returns an empirical PDF for the data provided. I need to get a CDF from the PDF returned by KDEpy and then generate pseudo-samples for a Uniform distribution. I am basically trying to duplicate following R code which uses kcde fucntion from ks library.

pseudo.samples = function(X){
    Fhat <- kcde(X) ## this directly returns CDF
    predict(Fhat,x=X)
}

pseudos = pseudo.samples(column_vector)
hist(pseudos)

Thank you.

tommyod commented 1 year ago

See the documentation

If you want to do it as you sketch, do something like

from KDEpy import FFTKDE
import numpy as np
data = np.random.randn(100)
x, y = FFTKDE().fit(data)()
cumulative = np.cumsum(y) / np.sum(y)

sample_idx = np.random.choice(np.arange(len(cumulative)), size=10)
cumulative[sample_idx]

I think that should do the trick. Hope it helps. I don't have the capacity to provide detailed solutions. You'll have to look into how numpy and Python works.