Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created November 20, 2019 10:15
Show Gist options
  • Save LuxXx/fd5e0d86e116d29a1bf282eb5d7d7983 to your computer and use it in GitHub Desktop.
Save LuxXx/fd5e0d86e116d29a1bf282eb5d7d7983 to your computer and use it in GitHub Desktop.
KDE with StatsModels
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact\n",
"import ipywidgets as widgets\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy import stats\n",
"import statsmodels.api as sm\n",
"from statsmodels.distributions.mixture_rvs import mixture_rvs"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dist1_loc, dist1_scale, weight1 = 100 , 100, 0.5\n",
"dist2_loc, dist2_scale, weight2 = 300 , 40, 0.5\n",
"\n",
"X = mixture_rvs(prob=[weight1, weight2], size=250,\n",
" dist=[stats.norm, stats.norm],\n",
" kwargs = (dict(loc=dist1_loc, scale=dist1_scale),\n",
" dict(loc=dist2_loc, scale=dist2_scale)))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6580698853e1422ca04c436e08d33c26",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=30, description='bw', max=50, step=5), Dropdown(description='Kernel', in…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dd = widgets.Dropdown(\n",
" options=['biw', 'cos', 'epa', 'gau', 'tri', 'triw', 'uni'],\n",
" value='gau',\n",
" description='Kernel',\n",
" disabled=False,\n",
")\n",
"\n",
"@interact(bw=(0, 50, 5), kernel=dd)\n",
"def plot_kde(bw=30, kernel='gau'):\n",
" kde = sm.nonparametric.KDEUnivariate(X)\n",
" kde.fit(kernel=kernel, bw=bw, fft=False)\n",
"\n",
" fig = plt.figure(figsize=(12, 5))\n",
" ax = fig.add_subplot(111)\n",
" ax.set_xlim((-300, 500))\n",
" ax.set_ylim((0, 0.008))\n",
"\n",
" # Plot the histrogram\n",
" ax.hist(X, bins=20, density=True, label='Histogram from samples',\n",
" zorder=5, edgecolor='k', alpha=0.5)\n",
"\n",
" # Plot the KDE as fitted using the default arguments\n",
" ax.plot(kde.support, kde.density, lw=3, label='KDE from samples', zorder=10)\n",
"\n",
" # Plot the true distribution\n",
" true_values = (stats.norm.pdf(loc=100, scale=100, x=kde.support)*0.5\n",
" + stats.norm.pdf(loc=300, scale=40, x=kde.support)*0.5)\n",
" ax.plot(kde.support, true_values, lw=3, label='True distribution', zorder=15)\n",
"\n",
" ax.legend(loc='best')\n",
" ax.grid(True, zorder=-5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment