Re: Mathematicians !! Percentile/Quartile function
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Sep 08, 2005
- 600 views
On Thu, 08 Sep 2005 02:47:46 -0700, CChris <guest at RapidEuphoria.com> wrote: >}}} <eucode> <snip> You obviously had the right idea, but it only worked for the first quartile. The main change I made was: -- interPoint=(3+length(data))/4 interPoint=(4-whichOne+length(data)*whichOne)/4 Based on your code, I wrote a complete test harness:
include sort.e type qtile(object n) return integer(n) and n>=0 and n<=4 end type function quartile(sequence data, qtile whichOne) -- matches what Excel and 123 return -- (which does not necessarily mean results are correct) integer lowerObs atom interPoint interPoint=(4-whichOne+length(data)*whichOne)/4 lowerObs=floor(interPoint) interPoint-=lowerObs data=sort(data)&0 -- extra 0 to avoid bounds check return (1-interPoint)*data[lowerObs]+interPoint*data[lowerObs+1] end function procedure test(sequence testset, sequence expected) integer testsetshown if length(expected)!=5 then ?9/0 end if testsetshown=0 for i=1 to 5 do if quartile(testset,i-1)!=expected[i] then if not testsetshown then ?testset testsetshown=1 end if printf(1,"quartile(%d) is %f, expected:%f\n", {i-1,quartile(testset,i-1),expected[i]}) end if end for end procedure --test({2}, -- data -- {2, 2, 2, 2, 2}) -- expected quartiles 0..4 test({2, 6, 7, 10, 14, 15, 16}, {2, 6.5, 10, 14.5, 16}) test({2, 6, 7, 10, 14, 15}, {2, 6.25, 8.5, 13, 15}) test({2,2,2,3,6,6,8,8,10,12,12,14,16,19,22,25,28,28,32,33}, {2, 6, 12, 22.75, 33}) test({2,2,2,3,6,6,8,8,10,12,12,14,16,19,22,25,28,28,32,33,34,35,36,38,40,41,45,48,49,51,53,56,57,57,57,59,61,61,62,62}, {2, 12, 33.5, 51.5, 62}) puts(1,"All Done\n") if getc(0) then end if
Regards, Pete (L)