.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_truncated_unimodal_kde_1d.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_truncated_unimodal_kde_1d.py: 1D truncated unimodal example ============================== This example shows how to use ``lightkde.kde_1d`` with and without a limit and how it compares to ``scipy.stats.gaussian_kde`` for a truncated unimodal distribution. .. GENERATED FROM PYTHON SOURCE LINES 9-10 Import packages .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: default import matplotlib.pyplot as plt import numpy as np from scipy.stats import gaussian_kde, norm from lightkde import kde_1d .. GENERATED FROM PYTHON SOURCE LINES 17-18 Generate synthetic data from a univariate normal distribution and truncate it: .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: default np.random.seed(42) sample = norm.rvs(size=2_000) sample = sample[sample > 0] .. GENERATED FROM PYTHON SOURCE LINES 23-24 Estimate kernel density using ``lightkde``: .. GENERATED FROM PYTHON SOURCE LINES 24-27 .. code-block:: default density_vec_without_x_min, x_vec_without_x_min = kde_1d(sample_vec=sample) density_vec_with_x_min, x_vec_with_x_min = kde_1d(sample_vec=sample, x_min=0) .. GENERATED FROM PYTHON SOURCE LINES 28-29 Estimate kernel density using ``scipy``: .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: default gkde = gaussian_kde(dataset=sample) scipy_density_vec = gkde.evaluate(x_vec_without_x_min) .. GENERATED FROM PYTHON SOURCE LINES 33-34 Plot the data against the kernel density estimates: .. GENERATED FROM PYTHON SOURCE LINES 34-49 .. code-block:: default fig, ax = plt.subplots(figsize=(6, 4)) ax.plot(x_vec_with_x_min, density_vec_with_x_min, "--r", label="lightkde; with x_min=0") ax.plot( x_vec_without_x_min, density_vec_without_x_min, ":r", label="lightkde; without x_min", ) ax.plot( x_vec_without_x_min, scipy_density_vec, zorder=1, label="scipy.stats.gaussian_kde" ) ax.hist(sample, bins=30, density=True, alpha=0.5, label="data") ax.legend() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_truncated_unimodal_kde_1d_001.png :alt: plot truncated unimodal kde 1d :srcset: /auto_examples/images/sphx_glr_plot_truncated_unimodal_kde_1d_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-53 When ``x_min=0`` is used, ``lightkde`` approximates well the histogram of the data. Without this option it behaves similarly to the ``scipy`` method, both extend to a region without data. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.908 seconds) .. _sphx_glr_download_auto_examples_plot_truncated_unimodal_kde_1d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_truncated_unimodal_kde_1d.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_truncated_unimodal_kde_1d.ipynb `