Skip to main content

Tools for fast and robust univariate and multivariate kernel density estimation

Project description

Software Overview

fastKDE calculates a kernel density estimate of arbitrarily dimensioned data; it does so rapidly and robustly using recently developed KDE techniques. It does so with statistical skill that is as good as state-of-the-science ‘R’ KDE packages, and it does so 10,000 times faster for bivariate data (even better improvements for higher dimensionality).

Please cite the following papers when using this method:

O’Brien, T. A., Kashinath, K., Cavanaugh, N. R., Collins, W. D. & O’Brien, J. P. A fast and objective multidimensional kernel density estimation method: fastKDE. Comput. Stat. Data Anal. 101, 148–160 (2016).

O’Brien, T. A., Collins, W. D., Rauscher, S. A. & Ringler, T. D. Reducing the computational cost of the ECF using a nuFFT: A fast and objective probability density estimation method. Comput. Stat. Data Anal. 79, 222–234 (2014).

Example usage:

For a standard PDF

#!python

import numpy as np
from fastkde import fastKDE
import pylab as PP

#Generate two random variables dataset (representing 100000 pairs of datapoints)
N = 2e5
var1 = 50*np.random.normal(size=N) + 0.1
var2 = 0.01*np.random.normal(size=N) - 300

#Do the self-consistent density estimate
myPDF,axes = fastKDE.pdf(var1,var2)

#Extract the axes from the axis list
v1,v2 = axes

#Plot contours of the PDF should be a set of concentric ellipsoids centered on
#(0.1, -300) Comparitively, the y axis range should be tiny and the x axis range
#should be large
PP.contour(v1,v2,myPDF)
PP.show()

For a conditional PDF

The following code generates samples from a non-trivial joint distribution

from fastkde import fastKDE
import pylab as PP
import numpy as np

#***************************
# Generate random samples
#***************************
# Stochastically sample from the function underlyingFunction() (a sigmoid):
# sample the absicissa values from a gamma distribution
# relate the ordinate values to the sample absicissa values and add
# noise from a normal distribution

#Set the number of samples
numSamples = int(1e6)

#Define a sigmoid function
def underlyingFunction(x,x0=305,y0=200,yrange=4):
     return (yrange/2)*np.tanh(x-x0) + y0

xp1,xp2,xmid = 5,2,305  #Set gamma distribution parameters
yp1,yp2 = 0,12          #Set normal distribution parameters (mean and std)

#Generate random samples of X from the gamma distribution
x = -(np.random.gamma(xp1,xp2,int(numSamples))-xp1*xp2) + xmid
#Generate random samples of y from x and add normally distributed noise
y = underlyingFunction(x) + np.random.normal(loc=yp1,scale=yp2,size=numSamples)

Now that we have the x,y samples, the following code calcuates the conditional

#***************************
# Calculate the conditional
#***************************
pOfYGivenX,axes = fastKDE.conditional(y,x)

The following plot shows the results:

#***************************
# Plot the conditional
#***************************
fig,axs = PP.subplots(1,2,figsize=(10,5))

#Plot a scatter plot of the incoming data
axs[0].plot(x,y,'k.',alpha=0.1)
axs[0].set_title('Original (x,y) data')

#Set axis labels
for i in (0,1):
    axs[i].set_xlabel('x')
    axs[i].set_ylabel('y')

#Draw a contour plot of the conditional
axs[1].contourf(axes[0],axes[1],pOfYGivenX,64)
#Overplot the original underlying relationship
axs[1].plot(axes[0],underlyingFunction(axes[0]),linewidth=3,linestyle='--',alpha=0.5)
axs[1].set_title('P(y|x)')

#Set axis limits to be the same
xlim = [np.amin(axes[0]),np.amax(axes[0])]
ylim = [np.amin(axes[1]),np.amax(axes[1])]
axs[1].set_xlim(xlim)
axs[1].set_ylim(ylim)
axs[0].set_xlim(xlim)
axs[0].set_ylim(ylim)

fig.tight_layout()

PP.savefig('conditional_demo.png')
PP.show()
Conditional PDF

Conditional PDF

How do I get set up?

A standard python build: python setup.py install

or

pip install fastkde

Download the source

Please contact Travis A. O’Brien TAOBrien@lbl.gov to obtain the latest version of the source.

Install pre-requisites

This code requires the following software:

  • Python >= 2.7.3

  • Numpy >= 1.7

  • scipy

  • cython

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psyneulink-fastkde-1.0.19.tar.gz (208.3 kB view details)

Uploaded Source

Built Distributions

psyneulink_fastkde-1.0.19-cp39-cp39-win_amd64.whl (217.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

psyneulink_fastkde-1.0.19-cp39-cp39-manylinux2010_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

psyneulink_fastkde-1.0.19-cp39-cp39-manylinux2010_i686.whl (994.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

psyneulink_fastkde-1.0.19-cp39-cp39-macosx_10_9_x86_64.whl (236.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

psyneulink_fastkde-1.0.19-cp39-cp39-macosx_10_9_universal2.whl (420.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

psyneulink_fastkde-1.0.19-cp38-cp38-win_amd64.whl (218.7 kB view details)

Uploaded CPython 3.8 Windows x86-64

psyneulink_fastkde-1.0.19-cp38-cp38-manylinux2010_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

psyneulink_fastkde-1.0.19-cp38-cp38-manylinux2010_i686.whl (1.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

psyneulink_fastkde-1.0.19-cp38-cp38-macosx_10_9_x86_64.whl (230.2 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

psyneulink_fastkde-1.0.19-cp37-cp37m-win_amd64.whl (212.9 kB view details)

Uploaded CPython 3.7m Windows x86-64

psyneulink_fastkde-1.0.19-cp37-cp37m-manylinux2010_x86_64.whl (952.8 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

psyneulink_fastkde-1.0.19-cp37-cp37m-manylinux2010_i686.whl (901.7 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

psyneulink_fastkde-1.0.19-cp37-cp37m-macosx_10_9_x86_64.whl (229.5 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

psyneulink_fastkde-1.0.19-cp36-cp36m-win_amd64.whl (212.5 kB view details)

Uploaded CPython 3.6m Windows x86-64

psyneulink_fastkde-1.0.19-cp36-cp36m-manylinux2010_x86_64.whl (961.7 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

psyneulink_fastkde-1.0.19-cp36-cp36m-manylinux2010_i686.whl (909.5 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

psyneulink_fastkde-1.0.19-cp36-cp36m-macosx_10_9_x86_64.whl (228.1 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

psyneulink_fastkde-1.0.19-cp35-cp35m-win_amd64.whl (204.7 kB view details)

Uploaded CPython 3.5m Windows x86-64

psyneulink_fastkde-1.0.19-cp35-cp35m-manylinux2010_x86_64.whl (921.4 kB view details)

Uploaded CPython 3.5m manylinux: glibc 2.12+ x86-64

psyneulink_fastkde-1.0.19-cp35-cp35m-manylinux2010_i686.whl (868.9 kB view details)

Uploaded CPython 3.5m manylinux: glibc 2.12+ i686

psyneulink_fastkde-1.0.19-cp35-cp35m-macosx_10_9_x86_64.whl (217.8 kB view details)

Uploaded CPython 3.5m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page