# Kolmogorov-Smirnov test values

**URL:** <https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868>\
**Category:** TMVA\
**Created:** [February 22, 2019, 5:50pm UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868 "2019-02-22T17:50:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Einsiedler](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Einsiedler](https://root-forum.cern.ch/u/Einsiedler)\
**Post date:** [February 22, 2019, 5:50pm UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/1 "2019-02-22T17:50:02Z")

</div>

Dear TMVA users,

I have a very quick question regarding the KS test used to check for overtraining. I have been using the BDT method with the following number of events:

Signal – training events : 21889  
Signal – testing events : 21889  
Signal – training and testing events: 43778  
Background – training events : 53955  
Background – testing events : 53955  
Background – training and testing events: 107910

And when checking the BDT score distribution for training and test samples (see below), one can easily that the distributions for these two samples are very similar for both signal and background. It is my understanding that the greater the correspondence between the two distributions, the closer to 0.5 the KS tests. And yet, they are still very close to zero. Are these KS test values really significant or am I missing something here?

Thanks,  
K.  
 ![ks](https://root-forum.cern.ch/uploads/default/original/3X/c/6/c64be7e7f0662221236a0e2f9369f94559b0b7b3.png)

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [February 22, 2019, 6:20pm UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/2 "2019-02-22T18:20:47Z")

</div>

Hi,

Very valid question that I can only begin to answer. It is my understanding that your statement applies to the unbinned case. Then KS value is uniformly distributed between 0 and 1. The binned case then approximates this.

Now, TMVA does not do a straight KS-test, but rather repeats the KS test on a random subsample of the data 1000 times and reports the number of times the individual tests are \> 0.5 (normalised to the number of run tests -\> optimal value 0.5).

From practical experience, the two distributions need to be _very_ close to each other for the KS test to actually approach 0.5.

You could see if decreasing the number of bins improves the score, or use cross validation (increasing the effective size of the test set).

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![Einsiedler](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Einsiedler](https://root-forum.cern.ch/u/Einsiedler)\
**Post date:** [February 22, 2019, 6:30pm UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/3 "2019-02-22T18:30:21Z")

</div>

Thank you Kim for your fast reply. If the optimal case is hard to achieve, I would assume that one could solely rely on their eyes to see if overtraining is significant? I’ll pay around with the samples sizes and see if it improves the KS test values. However, how can I change the binning? I tried to do so directly with the “Editor” view on the plots themselves but I was unsuccessful. Maybe I can change it directly on the function that creates this plot?

Thanks again for your help,  
K.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [February 22, 2019, 6:42pm UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/4 "2019-02-22T18:42:34Z")

</div>

> [@Einsiedler](#):
>
> I would assume that one could solely rely on their eyes to see if overtraining is significant?

Yes, I think that in many cases this is what it boils down to.

> [@Einsiedler](#):
>
> However, how can I change the binning?

Unfortunately one has to either

1. do a retraining for this to take effect since a rederivation of the output histograms are beyond the current capabilities of the `TMVA::Gui`, or
2. manually evaluate the classifier using the `TMVA::Reader` constructing the histograms manually and finally running the KS test on this.

To change the binning for a retraining use `(TMVA::gConfig().GetVariablePlotting()).fNbinsMVAoutput = 300;`  
at the top of the training file.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![linghua](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/53a042/32.png) [@linghua](https://root-forum.cern.ch/u/linghua)\
**Post date:** [June 18, 2019, 10:05am UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/5 "2019-06-18T10:05:00Z")

</div>

Hi,

Can I ask a naive question? I’m puzzled with your description of " KS test on a random subsample of the data 1000 times and reports the number of times the **individual tests are \> 0.5**". Does it really compare to 0.5?

I found that TMVA uses KS test function of TH1 with option “X” (using pseudo-experiment) , in the header:  
[https://root.cern.ch/root/html608/mvas\_8cxx\_source.html](https://root.cern.ch/root/html608/mvas_8cxx_source.html).

In order to check what happens inside the function, I looked through [TH1::KolmogorovTest](https://root.cern.ch/root/html608/classTH1.html#a2747cabe9ebe61c2fdfd74ff307cef3a), I found that it compares the KS values from pseudo-experiments to the value from a direct KS test between the 2 distributions that we want to compare, rather than 0.5, please check the following 2 lines in the source code:  
7407 dfmax = TMath::Max(dfmax,TMath::Abs(rsum1-rsum2));  
7447 if (dSEXPT\>dfmax) prb3 += 1.0;

Thank you,

Cheers,  
Linghua

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [June 18, 2019, 11:42am UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/6 "2019-06-18T11:42:52Z")

</div>

Hi,

Thanks for pointing this out! Yes indeed, the `'X'` option performs several single K-S tests. It compares the maximum separation in the sub sample to the maximum separation of entire sample.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![linghua](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/53a042/32.png) [@linghua](https://root-forum.cern.ch/u/linghua)\
**Post date:** [June 20, 2019, 10:44am UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/7 "2019-06-20T10:44:11Z")

</div>

Hi,

```
  If I understand well, with option "X", uncertainties of histograms are not taken into account, when generating pseudo-experiments with the test mva PDF. But do we expect to see the same fluctuation in training and test? For me. it's a bit suspicious.

```

I have tried with KS test using [TMath::KolmogorovProb](https://root.cern.ch/doc/master/namespaceTMath.html#ac3d939fddcd79bd3cb1cfa4d805a8aa1)([z](https://root.cern.ch/doc/master/TRolke_8cxx.html#a89a026883dd7df087bf16c8c176caeed) ), which computes z in the formula with maximum separation and uncertainties in case of binned data. it’s impressive that it gives a much better result than the one with pseudo-experiments.

Do you have any idea for this problem?

thank you,

Cheers,

Linghua

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [June 20, 2019, 11:59am UTC](https://root-forum.cern.ch/t/kolmogorov-smirnov-test-values/32868/8 "2019-06-20T11:59:40Z")

</div>

Hi,

Sorry no, I personally have limited experience with this K-S test, and I am not aware of any paper discussing the relative merits of the pseudo-experiments in depth.

Maybe @moneta can offer additional insight?

Cheers,  
Kim
