Technically, this is not a VB question, but in my latest project I need to apply some statistics calculations. Does anyone know the formula for calculating "weighted percentiles" for a group of values? (I know how to get "regular" percentiles.)
Printable View
Technically, this is not a VB question, but in my latest project I need to apply some statistics calculations. Does anyone know the formula for calculating "weighted percentiles" for a group of values? (I know how to get "regular" percentiles.)
I've done quite a bit of statistics, what exactly do you mean by Weighted percentiles? I can never remember the exact definitions for different terms (statistics si full of different names for similar formulae and I can never remember them all)
Part of the problem is that I'm creating some of the functionality of an old (gulp)COBOL program into this VB project, and folks here are having trouble recollecting exactly what these values are supposed to represent.
Anyway, "regular" percentiles deal with a sorted array of values. To get the value at the 25th perecentile in an array containing 11 elements, you calculate the position in the array (1-based) as follows:
(11 * 25)/100 = 2.75 (if the result has a remainder, add 1 to the result, so you get 3). Therefore the item at index (3) in the array represents the value at the 25th percentile.
With "weighted percentiles", the array of values has an associated list of values, or weights, that somehow affect the calculation above. This is what I'm trying to find.
[Edited by BruceG on 08-07-2000 at 11:07 AM]
Ok, I think I've got you (something is stirring in the obscure part of my brain that listened in Statistics lectures)
so you should have a UDT like this
So, if you have an ArrayCode:Private Type ArrayMember
Value As Double
Weight As Double
End Type
You Can Get the Weighted Percentile Something like thisCode:Dim Data() As ArrayMember
Code:
Private Type ArrayMember
Value As Double
Weight As Double
End Type
Dim uData() As ArrayMember
Private Function WeightedPercentile(Percentile as Single) As Double
'NB, Percentile is a single because it is still valid to get the 10.5th percentile etc
Dim dblTotal As Double
Dim dblRequredValue As Double
Dim i As Integer
'Check we have a valid Percentile
If (Percentile < 0) OR (Percentile > 100) Then _
Exit Sub
'Get the Total Value of the Array
dblTotal = GetArrayTotal
'Get the Value we Need
dblRequiredValue = dblTotal * dblPercentile / 100
'Now we no longer need the Total Value of the Array, so dblTotal will be a running Total
dblTotal = 0
For i = LBound(uData) To UBound(uData)
'Add The Weight of the member to our total
dblTotal = dblTotal + uData(i).Weight
If dblTotal > dblRequiredValue Then _
Exit For
Next i
WeightedPercentile = uData(i).Value
Exit Function
Private Function GetArrayTotal() As Double
Dim i As Integer
Dim dblRunningTotal As Integer
For i = LBound(uData) To UBound(uData)
'Add The Weight of the member to our total
dblTotal = dblTotal + uData(i).Weight
Next i
GetArrayTotal = dblRunningTotal
Exit Function
That'lll work
You got it, Sam! I desk-checked your logic, applying it to the results on this old green-bar report here and it worked perfectly. Thank you very much!
BTW (not to bite the hand that feeds me) but you had two typos in your code: In GetArrayTotal, where you have "dblTotal", it should be "dblRunningTotal"; and in the WeightedPercentile function, "dblPercentile" should just be "Percentile". But no problem, I saw exactly what you were doing.
Again, much thanks - I had been searching on the net and in old Statistics books all weekend to no avail.
Sorry about the typos, I typed the code straigt in without checking, I didn't have VB open at the time, and checking it properly would have meant making up a whole **** load of data, which I didn't want to do. So I didn't check it at all, I'm usually fairly good at typing it in right, but not today. Fortunatly they were all invalid names, rather than the name of the wrong variable, which would have confused you.