# Kernel Density EstimationΒΆ

In [1]:
import numpy as np
from scipy import stats
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.distributions.mixture_rvs import mixture_rvs


#### A univariate example.¶

In [2]:
np.random.seed(12345)

In [3]:
obs_dist1 = mixture_rvs([.25,.75], size=10000, dist=[stats.norm, stats.norm],
kwargs = (dict(loc=-1,scale=.5),dict(loc=1,scale=.5)))

In [4]:
kde = sm.nonparametric.KDEUnivariate(obs_dist1)
kde.fit()

In [5]:
fig = plt.figure(figsize=(12,8))
ax.hist(obs_dist1, bins=50, normed=True, color='red')
ax.plot(kde.support, kde.density, lw=2, color='black');

In [6]:
obs_dist2 = mixture_rvs([.25,.75], size=10000, dist=[stats.norm, stats.beta],
kwargs = (dict(loc=-1,scale=.5),dict(loc=1,scale=1,args=(1,.5))))

kde2 = sm.nonparametric.KDEUnivariate(obs_dist2)
kde2.fit()

In [7]:
fig = plt.figure(figsize=(12,8))
ax.hist(obs_dist2, bins=50, normed=True, color='red')
ax.plot(kde2.support, kde2.density, lw=2, color='black');


The fitted KDE object is a full non-parametric distribution.

In [8]:
obs_dist3 = mixture_rvs([.25,.75], size=1000, dist=[stats.norm, stats.norm],
kwargs = (dict(loc=-1,scale=.5),dict(loc=1,scale=.5)))
kde3 = sm.nonparametric.KDEUnivariate(obs_dist3)
kde3.fit()

In [9]:
kde3.entropy

Out[9]:
1.3343301918419768

In [10]:
kde3.evaluate(-1)

Out[10]:
array([ 0.1799])


#### CDF¶

In [11]:
fig = plt.figure(figsize=(12,8))
ax.plot(kde3.support, kde3.cdf);


#### Cumulative Hazard Function¶

In [12]:
fig = plt.figure(figsize=(12,8))
ax.plot(kde3.support, kde3.cumhazard);


#### Inverse CDF¶

In [13]:
fig = plt.figure(figsize=(12,8))

fig = plt.figure(figsize=(12,8))