javaiqr

Generation of Interquartile Positions With Increasing Depth


I wanted to generate the interquartile (IQR) positions programmatically so that I could use them as a basis for obtaining values within a min - max range, with increasing orders of granularity.

I got stuck on this for a while. Surprisingly, when I was looking for ideas, I found nothing on Stack Overflow. Because of that, I'll post the solution I came up with in order to help people in the future if they need.


Solution

  • This is the solution I came up with:

    final int DEPTH = 32 //2^n where n is the depth of the quartile tree
    
    for(int j = 1; j < DEPTH; j *= 2){
        double quotient = 1.0 / j;   //How many quartiles to generate
    
        for(int i = 1; i < j; ++i)
            if(i % 2 != 0)
                System.out.println((i * quotient));
        System.out.println("-------");
    }
    

    Doing this gives the following output:

    -------
    0.5
    -------
    0.25
    0.75
    -------
    0.125
    0.375
    0.625
    0.875
    -------
    0.0625
    0.1875
    0.3125
    0.4375
    0.5625
    0.6875
    0.8125
    0.9375
    -------