Mathematicians !! Percentile/Quartile function

new topic     » topic index » view thread      » older message » newer message

I need some mathematical help, I want a function that calculates the 1st, 2nd
  and 3rd Quartiles (otherwise known as 25th, 50th and 75th percentiles) from a
  sequence of values. I can get the 2nd quartile as that is also the median and I
  can get the 1st and 3rd quartiles to agree with Excel and 123 if the qty of
  values is odd, but when I have an even quantity the 1st and 3rd quartiles are
  wrong.
i.e. for values of {2, 6, 7, 10, 14, 15, 16} the quartiles are 6.5, 10 and 14.5
but for a range of {2, 6, 7, 10, 14, 15} the correct quartiles seem to be 6.25,
8.5 & 13

my function is (not exactly optimised I know!)
function quartile(sequence dataset)
sequence lowerDataset
sequence upperDataset
integer n
atom firstQ
atom secondQ
atom thirdQ

n=length(dataset)
--dataset = sort(dataset)

-- first get Median
if integer(n/2) then -- even number of values
	secondQ = (dataset[(n/2)] + dataset[(n/2)+1] ) / 2
	lowerDataset = dataset[1..(n/2)] --	lower values
	upperDataset = dataset[(n/2)+1..$] -- upper values
	
else -- odd number of values
	secondQ = dataset[floor(n/2)+1]	
	lowerDataset = dataset[1..floor(n/2)+1]	-- half it and include median
	upperDataset = dataset[floor(n/2)+1..$] -- upper half with median
end if

n = length(lowerDataset)
if integer(n/2) then -- even number of values
	firstQ = (lowerDataset[(n/2)] + lowerDataset[(n/2)+1] ) / 2
	
else -- odd number of values
	firstQ = lowerDataset[floor(n/2)+1]	-- half it, floor it then add 1
end if

n = length(upperDataset)
if integer(n/2) then -- even number of values
	thirdQ = (upperDataset[(n/2)] + upperDataset[(n/2)+1] ) / 2
	
else -- odd number of values
	thirdQ = upperDataset[floor(n/2)+1]	-- half it, floor it then add 1
end if

return {firstQ, secondQ, thirdQ}
end function

sequence result
result = quartile({2,6,7,10,14,15})

? result[1]
? result[2]
? result[3]


Thanks, Pete

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu