# How the TMVA PDF makes the KDE estimation from histograms

**URL:** <https://root-forum.cern.ch/t/how-the-tmva-pdf-makes-the-kde-estimation-from-histograms/46849>\
**Category:** TMVA\
**Tags:** python, hist\
**Created:** [September 15, 2021, 4:45pm UTC](https://root-forum.cern.ch/t/how-the-tmva-pdf-makes-the-kde-estimation-from-histograms/46849 "2021-09-15T16:45:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mverissi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mverissi](https://root-forum.cern.ch/u/mverissi)\
**Post date:** [September 15, 2021, 4:45pm UTC](https://root-forum.cern.ch/t/how-the-tmva-pdf-makes-the-kde-estimation-from-histograms/46849/1 "2021-09-15T16:45:30Z")

</div>

Dear experts,

I know that KDE estimation uses unbinned data, but the TMVA uses a histogram (TH1F). I want to know how TMVA deals with the histogram, I mean they get the histogram and resample unbinned data according to the histogram (just like in the piece of code that I put here) or there is another approach to make the KDE estimation?

```auto
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as sts

n = 100000

# generate some random multimodal histogram data
samples = np.concatenate([np.random.normal(np.random.randint(-8, 8), size=n)*np.random.uniform(.4, 2) for i in range(4)])
h,e = np.histogram(samples, bins=100, density=True)
x = np.linspace(e.min(), e.max())

# plot the histogram
plt.figure(figsize=(8,6))
plt.bar(e[:-1], h, width=np.diff(e), ec='k', align='edge', label='histogram')

# plot the real KDE
kde = sts.gaussian_kde(samples)
plt.plot(x, kde.pdf(x), c='C1', lw=8, label='KDE')

# resample the histogram and find the KDE.
resamples = np.random.choice((e[:-1] + e[1:])/2, size=n*5, p=h/h.sum())
rkde = sts.gaussian_kde(resamples)

# plot the KDE
plt.plot(x, rkde.pdf(x), '--', c='C3', lw=4, label='resampled KDE')
plt.title('n = %d' % n)
plt.legend()
plt.show()

```

Thanks!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [September 16, 2021, 6:09am UTC](https://root-forum.cern.ch/t/how-the-tmva-pdf-makes-the-kde-estimation-from-histograms/46849/2 "2021-09-16T06:09:01Z")

</div>

Welcome to the ROOT Forum! Maybe @moneta can help you with this

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [September 28, 2021, 3:28pm UTC](https://root-forum.cern.ch/t/how-the-tmva-pdf-makes-the-kde-estimation-from-histograms/46849/3 "2021-09-28T15:28:57Z")

</div>

Hi,  
Sorry for the late reply. I think TMVA builds using the un-binned data set an histogram using the kernel estimation. .Basically it computes the kernel density estimators only in a grid ( the bin centres) and then uses an interpolation ( I think linear) for the other points.

Cheers

Lorenzo
