# Model a PDF (probability density) from data

**URL:** <https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095>\
**Category:** TMVA\
**Created:** [March 21, 2021, 9:08pm UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095 "2021-03-21T21:08:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [March 21, 2021, 9:08pm UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/1 "2021-03-21T21:08:14Z")

</div>

I’ve read TMVA documentation, slides and looked at the tutorials, but couldn’t find an example how to model a pdf.

I have background (mostly uniform in the detector, but with strange regions that I don’t want to model parametrically). On top of that known background (from a run without a source) there is a calibration signal that I want to model parametrically (that’s why I want background pdf to be calculated fast). I read that method of k nearest neighbours is fast enough, but couldn’t find an example how to get data from there.

There is a method

> Double\_t TMVA::MethodKNN::GetMvaValue(Double\_t \*err = `0`, Double\_t \*errUpper = `0`),

but I’m not sure what its output is (it’s not written in its documentation).

There is also a PDEFoam method GetCellValue(), but I don’t understand its arguments and return value.  
If my background decreases exponentially, will PDEFoam model it well? I read that it models pdf by constant cells.

Could someone please give an example how to initialize, train TMVA and get PDF value at the given point (maybe just give some methods names)? Maybe there is something other in ROOT for that? Thank you.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 22, 2021, 8:04am UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/2 "2021-03-22T08:04:19Z")

</div>

I guess @moneta can help you.

---

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [April 3, 2021, 2:49pm UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/3 "2021-04-03T14:49:25Z")

</div>

I hope this topic doesn’t get closed, because I still need the way to do that. Thanks.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 13, 2021, 8:00am UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/4 "2021-04-13T08:00:38Z")

</div>

@moneta ping

---

<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:** [April 13, 2021, 8:48am UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/5 "2021-04-13T08:48:53Z")

</div>

Hi,

If you want to model a PDF given some data, you can use either an histogram (TH1 class) or a Kernel density estimator, see `TKDE` class ([ROOT: TKDE Class Reference](https://root.cern.ch/doc/master/classTKDE.html) ).  
You don’t need to use TMVA for this

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [April 24, 2021, 6:41pm UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/6 "2021-04-24T18:41:23Z")

</div>

Thanks for the reply, Lorenzo. Unfortunately I need to model a 3-dimensional pdf. Maybe I don’t need to get its value, though, because in fact I just want to use it in a fit with RooFit.  
TH1 and TKDE don’t suit for 3-dimensional data.  
I read in tutorials that TMVA is good for such non-parametric fits, or what should I use?

---

<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:** [April 26, 2021, 7:23am UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/7 "2021-04-26T07:23:07Z")

</div>

Hi,

For the multi-dim case you can use the multi-dimensional KDE class that is available in RooFit,  
see [ROOT: RooNDKeysPdf Class Reference](https://root.cern.ch/doc/master/classRooNDKeysPdf.html)  
In TMVA, there are some methods that rely to estimate the multi-dimensional likelihood density but those are designed to use for classification. It is not trevial to use them just for estimating the density.  
Some of these include PDEFoam or KNN (k nearest neighbour)

Lorenzo

---

<div class="post-metadata">

**Author:** ![ynikitenko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ynikitenko/32/25120_2.png) [@ynikitenko](https://root-forum.cern.ch/u/ynikitenko)\
**Post date:** [April 26, 2021, 9:44am UTC](https://root-forum.cern.ch/t/model-a-pdf-probability-density-from-data/44095/8 "2021-04-26T09:44:07Z")

</div>

Thank you. Now I understand. I’ll try that. I think this answers the question.
