Can GetQuantiles be used to calculate IQR?

I’m having trouble understanding the output of GetQuantiles().

I am using the following code:

void quantiles() {
    // demo for quantiles
    const Int_t nq = 3;
    TH1F *h = new TH1F("h","demo quantiles",100,-3,3);
    h->FillRandom("gaus",5000);
    Double_t xq[nq];  // position where to compute the quantiles in [0,1]
    Double_t yq[nq];  // array to contain the quantiles
    for (Int_t i=0;i<nq;i++) xq[i] = Float_t(i+1)/nq;
    h->GetQuantiles(nq,yq,xq);
    //show the original histogram in the top pad
    TCanvas *c1 = new TCanvas("c1","demo quantiles",10,10,700,900);
    c1->Divide(1,2);
    c1->cd(1);
    h->Draw();
    // show the quantiles in the bottom pad
    c1->cd(2);
    gPad->SetGrid();
    TGraph *gr = new TGraph(nq,xq,yq);
    gr->SetMarkerStyle(21);
    gr->Draw("alp");
}

taken from the GetQuantiles() entry of TH1 Class Reference.

I have set nq=3 because I am interested in calculating the Interquartile Range (IQR - https://en.wikipedia.org/wiki/Interquartile_range) so I can do Freedman-Diaconis binning – I’d put in the wikipedia link, but as a new user, I’m evidently limited to 2 link >:-| – on my histograms which have >5x10^6 entries.

When I run quantiles(), I get the following plot:

quantile.pdf (15.2 KB)

and I don’t know how to interpret the bottom plot.

What I need is the IQR of a set of values, i.e. the difference between the median of the upper half of the values and the median of the lower half of the values.

Maybe Quantiles() is the wrong function to use for getting these numbers, and maybe there is no ROOT function to get these numbers, and I’ll have to write the code to do so by hand.

But if there is a easy ROOT method to get these numbers, I’d rather have ROOT do the work for me.

Thanks for your help,
James N

Hello @pjamesnorris

As you can see here:
https://root.cern/doc/v610/quantiles_8C.html

GetQuantiles returns x-values which linked with distribution function like F(x<x_value) = yourQuantile.
In your case, you specified next q values: 1/3,2/3,1. Let’s see to the last one:
when you ask root to calculate quantile=1 it means what is x which more than any another x from your values field and Probability of this event equal to one. For your case this value is 3, because all of your x-values.

More formally it means F(x<x_q)=1 and x_q=3.
and so on,
q=0;
x_q=-3;
q=0.5
x_q=0;

If my understanding of IQR is correct, you should just add to your quantiles () {…}
this one

for (auto i=0; i<nq;++i) std::cout << "Q_" << i+1 << "=" << h->GetBinContent(h->FindBin(yq[i])) << std::endl;

and change definition quantiles array like:
Double_t xq[nq] = {0.25,0.5,0.75};
But here I’m not sure what exactly values are corresponding to IQR.

bdrum,

Thank you very much! This is what I needed.

Have a care,
James N

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.