Pretty Print Percentiles Of Decimal Numeric List

function sort_numeric_array {
    sort <(echo $@ | tr ' ' "\n") --numeric-sort | tr "\n" ' '
}

dataA=($(rand 101 1000))
dataB=($(rand 101 1000000))
for idx in {0..100}
do
    data[$idx]="${dataA[idx]}.${dataB[idx]}"
done

data=($(sort_numeric_array ${data[@]}))

len=${#data[@]}
data[$len]=0
function pctiles {
    echo scale=6
    for idx in {0..10}
    do
        if [ $idx == 0 ]
        then
            place=0
        else
            place=$((((len-1)*idx*10)/10))
        fi
        second=$((place%10))
        first=$((10-second))
        place=$((place/10))
        plus1=$((place+1))
        if [ $plus1 -lt $len ]
        then
            echo "0.1 * (${data[place]} * ${first} + ${data[plus1]} * ${second})"
        else
            echo ${data[$place]}
        fi
    done
    echo quit
}
function showPctiles {
    idx=0
    for pctil in  $(bc -q <(pctiles))
    do
        printf "%3i %3.6f\n" $idx $pctil
        idx=$((idx+10))
    done
}
column -t <(echo Pctile Value; cat <(showPctiles))

...

./median.sh 
Pctile  Value
0       9.124796
10      89.264966
20      138.368719
30      193.364719
40      294.754664
50      404.227673
60      514.926239
70      643.313126
80      740.374378
90      901.550589
100     975.788070

Comments

Popular posts from this blog

Bithon: Run Python Interactively Inside Bash

oche, lik echo but a bit easier to use.

Parsing Columns From Files WITHOUT awk