Click to See Complete Forum and Search --> : Inner Quartile Range w/ ASP?
I'm querying to get an array of numbers. I need to find the "Middle %50" of that array of numbers. Is there a way to find the Inner Quartile Range using an ASP function? I cannot find any math functions that would do this.
Could I do it manually? Sort the values, split the array up, split it again to find the "Middle %50"?
Thanks,
Jesse
baseiber
12-21-2004, 02:19 PM
Do you mean you mean you do not want the average of the numbers?
russell
12-21-2004, 03:09 PM
Put 'em in an array, sort em, then calculate the 25% index of the array. If you are getting the values from a database, let the db do the sorting.
something close to the below sample ought to work (you'll want to slightly modify) also, are you wanting the middle 50% around the median or around the mean?
ar = Array(1,1,1,2,2,3,4,4)
u = ubound(ar)
half = cint(u/2)
qtr = cint(half/2) '' 25% point
topEndOf50 = qtr + half
For i = qtr to topEndOf50
Response.Write ar(i) & "<br>"
Next
The median, but I think it gets tricky, because if the number of records is even, you need to take the average of the middle 2 values...which I guess wouldn't be a problem..
russell
12-22-2004, 11:46 AM
ok, here's how to find the median in a one dimensional array, regardless of whether it has an odd or even number of elements
Function Median(byVal ar)
Dim i
Dim odd
Dim u
u = ubound(ar)
ar = sort(ar)
odd = (u Mod 2 = 0)
if odd Then
Median = ar(u/2)
else
Median = (ar((u-1)/2) + ar((u-1)/2 + 1)) / 2
end if
End Function
Function sort(byVal arTemp)
'' Supports Median Function
Dim el
Dim stillSorting
dim i, j
for j = 0 to ubound(arTemp)
stillSorting = true
Do While (stillSorting)
For i = 1 to ubound(arTemp)
stillSorting = false
If arTemp(i-1) < arTemp(i) Then
el = arTemp(i-1)
arTemp(i-1) = arTemp(i)
arTemp(i) = el
stillSorting = true
End If
Next
Loop
Next
sort = arTemp
End Function
** EDIT -- uses a bubble sort. you may of course use different sorting algorithms
russell
12-22-2004, 11:48 AM
while i'm at it, here are two more very useful functions not available in ASP (at least not in vbScript)
Public Function Floor(num)
Dim othernum
othernum = Fix(num)
If othernum = num Then
Floor = CLng(num)
Exit Function
End If
If othernum < 0 Then
othernum = othernum - 1
End If
Floor = othernum
End Function
Public Function Ceil(num)
Ceil = -Int(-num)
End Function
PeOfEo
12-26-2004, 10:51 PM
I am just curious, what exactly are you doing with the iqr? I am taking a statistics class right now so I am just interested.