Skip to content
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

Fix duplicate X-values in KDE distribution #362

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

michael-nml
Copy link
Collaborator

This PR addresses an issue for KDE distribution calculation returning duplicate X-values. This is an edge case of the statsmodel.nonparametric.KDEUnivariate function.

Reproduction example:

from statsmodels import api as sm
from matplotlib import pyplot as plt

data = [-0.100865, -0.100865, -0.100865, -0.100865, -0.100865]
kde = sm.nonparametric.KDEUnivariate(data)
kde.fit()

print("nr duplicate X-values:", len(kde.support) - len(np.unique(kde.support)))
plt.plot(kde.support, kde.density)

Output:
nr duplicate X-values: 505
image

Normally input data containing a single value would result in an exception, but in this case it does not due to some form of precision error.

Running the above example with different input data yields:

  • data = np.array([-0.100865, -0.100865, -0.100865, -0.100865, -0.100865], dtype='float16') => error
  • data = np.array([-0.100865, -0.100865, -0.100865, -0.100865, -0.100865], dtype='float32') => error
  • data = np.array([-0.100865, -0.100865, -0.100865, -0.100865, -0.100865], dtype='float64') => same output as example above

@nikml
Copy link
Contributor

nikml commented Feb 12, 2024

LGTM

@nnansters nnansters merged commit fc374e1 into main Feb 12, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants