Re: Mathematicians !! Percentile/Quartile function
- Posted by Pete Stoner <stoner.pete at gmail.com> Sep 07, 2005
- 587 views
Lucius L. Hilley III wrote: > > <a > href="http://www.vias.org/tmdatanaleng/cc_quartile.html">http://www.vias.org/tmdatanaleng/cc_quartile.html</a> > > Depends on how you want to do it. > I would think that the quartile would be sort of a median between medians > but... > It looks as though the above links definition differs slightly. > It defines the median as the average of the 2 middle numbers if you have an > even number of values. > However, it defines the first and third quartile as a specific value that is > in the list of values. > > }}} <eucode> > --Warning: Code should be close enough to rounding for positive numbers. > function positive_round(object x) > return floor(x+0.5) > end function > > --Warning: Code should be close enough to rounding for negative numbers. > function negative_round(object x) > return floor(x-0.5) > end function > > function round(object x) > object px, nx > > px = x * (x > 0) > nx = x * (0 > x) > > return positvie_round(px) + negative_round(nx) > end function > > function quartile(sequence list) > integer n, q1_index, q2_index, q3_index > atom median > > n = length(list) > q1_index = round((n+1)/4) -- This could easily be optimized > q2_index = n/2 > q3_index = round((n+1)*3/4) -- This could be optimized as well > > median = list[q2_index] > if (n = floor(n)) then -- if even(n) then > median += list[q2_index+1] > median /= 2 > end if > > sequence {list[q1_index], median, list[q3_index]} > end function > > print(1, quartile({2,6,7,10,14,15})) > puts(1, 10) -- new line > machine_proc(26, 0) -- wait_key() cheat !!! > <font color="#330033"></eucode> {{{ </font> > > This code was proudly presented by: > Lucius L. Hilley III - Unkmar > Nice try Lucius, but that gives 2, 8.5 and 10.. Correct median but 1st and 3rd quartiles are way off.. Regards Pete.