Closed Thread
Results 1 to 7 of 7

Algorithm for computing Excel PercentRank

  1. #1
    Kam Mistry
    Guest

    Algorithm for computing Excel PercentRank

    I am current trying to determine how Excel calculates PercentRank.

    The project I am working on requires the use of this function - and we
    cannot use COM automation to call Excel.

    The only documentation I could find was the following:

    Excel Function: PERCENTRANK(x, array)

    - If x matches one of the values in the array, this function is
    equivalent to the Excel formula = (RANK(x)-1)/(N-1) where N is the
    number of data points.

    - If x does not match one of the values, then the PERCENTRANK function
    interpolates.

    Rank(x) is easy to compute. The second statement is where I'm having
    trouble with, how does Excel "interpolates" the percentage rank? What
    method does it use?

    Thanks in advance,
    Kam Mistry
    Microsoft .NET Developer (C#)

  2. #2
    Harlan Grove
    Guest

    Re: Algorithm for computing Excel PercentRank

    Kam Mistry wrote...
    >I am current trying to determine how Excel calculates PercentRank.
    >
    >The project I am working on requires the use of this function - and we
    >cannot use COM automation to call Excel.

    ....

    But you need to duplicate Excel's PERCENTRANK sematics exactly? If so,
    PERCENTRANK(Rng,x) needs to return #N/A or some other error indicator
    when x > MAX(Rng).

    Also, PERCENTRANK rounds differently than the formula you mention. For
    example, with 1..8 in G1:G8 and 5 in G10,

    PERCENTRANK(G1:G8,G10,6) returns 0.571428
    ROUND((RANK(G10,G1:G8,1)-1)/7,6) returns 0.571429


    >Rank(x) is easy to compute. The second statement is where I'm having
    >trouble with, how does Excel "interpolates" the percentage rank? What
    >method does it use?


    What ever happened to exploratory data analysis? It's simple linear
    interpolation between the points in the range bracketting your second
    argument value. That is,

    =TREND(PERCENTRANK(Rng,
    {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),6),
    {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),x)

    or, since you're going to be programming this,


    if (YourErrorCheckFcnHere(pr = YourPercentRankFcnHere(rng, x)) {
    for (i = 0, xlo = xhi = x; i < sizeof(rng) / sizeof(*rng); ++i) {
    if (xlo < rng[i] && rng[i] < x) xlo = rng[i];
    if (xhi > rng[i] && rng[i] > x) xhi = rng[i];
    }

    ylo = YourPercentRankFcnHere(rng, xlo);
    yhi = YourPercentRankFcnHere(rng, xhi);

    pr = ((xhi - x) * ylo + (x - xlo) * yhi) / (xhi - xlo);
    }


    If rng is sorted, use binary search rather than simple iteration to
    locate the points in rng bracketting x. I assumed rng was a simple
    array. Adapt for more sophisticated data structures.


  3. #3
    Kam Mistry
    Guest

    Re: Algorithm for computing Excel PercentRank

    Thanks for you help! Our test cases against Excel PercentRank work.

    Cheers,
    Kam


    "Harlan Grove" <[email protected]> wrote in message
    news:[email protected]...
    > Kam Mistry wrote...
    > >I am current trying to determine how Excel calculates PercentRank.
    > >
    > >The project I am working on requires the use of this function - and we
    > >cannot use COM automation to call Excel.

    > ...
    >
    > But you need to duplicate Excel's PERCENTRANK sematics exactly? If so,
    > PERCENTRANK(Rng,x) needs to return #N/A or some other error indicator
    > when x > MAX(Rng).
    >
    > Also, PERCENTRANK rounds differently than the formula you mention. For
    > example, with 1..8 in G1:G8 and 5 in G10,
    >
    > PERCENTRANK(G1:G8,G10,6) returns 0.571428
    > ROUND((RANK(G10,G1:G8,1)-1)/7,6) returns 0.571429
    >
    >
    > >Rank(x) is easy to compute. The second statement is where I'm having
    > >trouble with, how does Excel "interpolates" the percentage rank? What
    > >method does it use?

    >
    > What ever happened to exploratory data analysis? It's simple linear
    > interpolation between the points in the range bracketting your second
    > argument value. That is,
    >
    > =TREND(PERCENTRANK(Rng,
    > {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),6),
    > {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),x)
    >
    > or, since you're going to be programming this,
    >
    >
    > if (YourErrorCheckFcnHere(pr = YourPercentRankFcnHere(rng, x)) {
    > for (i = 0, xlo = xhi = x; i < sizeof(rng) / sizeof(*rng); ++i) {
    > if (xlo < rng[i] && rng[i] < x) xlo = rng[i];
    > if (xhi > rng[i] && rng[i] > x) xhi = rng[i];
    > }
    >
    > ylo = YourPercentRankFcnHere(rng, xlo);
    > yhi = YourPercentRankFcnHere(rng, xhi);
    >
    > pr = ((xhi - x) * ylo + (x - xlo) * yhi) / (xhi - xlo);
    > }
    >
    >
    > If rng is sorted, use binary search rather than simple iteration to
    > locate the points in rng bracketting x. I assumed rng was a simple
    > array. Adapt for more sophisticated data structures.
    >




  4. #4
    Kam Mistry
    Guest

    Re: Algorithm for computing Excel PercentRank

    Here's the C# function I wrote where "listOfRankItems" is an array of
    struct:


    private float PercentRank(rankItem[] listOfRankItems, float valueX)
    {

    /*--------------------------------------------------------------------------
    ---------

    Calculate the PERCENTRANK(array, x)

    If X matches one of the values in the array, this function is
    equivalent to
    the Excel formula =(RANK(x)-1)/(N-1) where N is the number of data
    points.

    If X does not match one of the values, then the PERCENTRANK function
    interpolates.

    ----------------------------------------------------------------------
    -------------*/

    int numberOfItems = listOfRankItems.Length;
    float resultPR = valueX;
    bool foundX = false;

    for (int index = 0; index < numberOfItems; index++)
    {
    if (listOfRankItems[index].WealthAmount == valueX)
    {
    resultPR = ((float)index)/((float)(numberOfItems - 1));
    foundX = true;
    break;
    }
    }

    // calculate value using linear interpolation

    if (!foundX)
    {
    float x1, x2, y1, y2;

    x1 = x2 = valueX;

    foundX = false;

    for (int index = 0; index < numberOfItems - 1; index++)
    {
    if ((listOfRankItems[index].WealthAmount < valueX && valueX <
    listOfRankItems[index + 1].WealthAmount) ||
    (listOfRankItems[index].WealthAmount > valueX && valueX >
    listOfRankItems[index + 1].WealthAmount))
    {
    x1 = listOfRankItems[index].WealthAmount;
    x2 = listOfRankItems[index + 1].WealthAmount;
    foundX = true;
    break;
    }
    }

    if (foundX)
    {
    y1 = PercentRank(listOfRankItems, x1);
    y2 = PercentRank(listOfRankItems, x2);

    resultPR = (((x2 - valueX)*y1 + (valueX - x1)*y2)) / (x2 - x1);
    }
    else
    {
    // use the smallest or largest value in the set which ever is
    closer to valueX

    if (Math.Abs(listOfRankItems[0].WealthAmount - valueX) <
    Math.Abs(valueX - listOfRankItems[numberOfItems-1].WealthAmount))
    x1 = listOfRankItems[0].WealthAmount;
    else
    x1 = listOfRankItems[numberOfItems-1].WealthAmount;

    resultPR = PercentRank(listOfRankItems, x1);
    }
    }

    return resultPR;
    }



    "Harlan Grove" <[email protected]> wrote in message
    news:[email protected]...
    > Kam Mistry wrote...
    > >I am current trying to determine how Excel calculates PercentRank.
    > >
    > >The project I am working on requires the use of this function - and we
    > >cannot use COM automation to call Excel.

    > ...
    >
    > But you need to duplicate Excel's PERCENTRANK sematics exactly? If so,
    > PERCENTRANK(Rng,x) needs to return #N/A or some other error indicator
    > when x > MAX(Rng).
    >
    > Also, PERCENTRANK rounds differently than the formula you mention. For
    > example, with 1..8 in G1:G8 and 5 in G10,
    >
    > PERCENTRANK(G1:G8,G10,6) returns 0.571428
    > ROUND((RANK(G10,G1:G8,1)-1)/7,6) returns 0.571429
    >
    >
    > >Rank(x) is easy to compute. The second statement is where I'm having
    > >trouble with, how does Excel "interpolates" the percentage rank? What
    > >method does it use?

    >
    > What ever happened to exploratory data analysis? It's simple linear
    > interpolation between the points in the range bracketting your second
    > argument value. That is,
    >
    > =TREND(PERCENTRANK(Rng,
    > {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),6),
    > {1;0}*MAX(IF(Rng<=x,Rng))+{0;1}*MIN(IF(Rng>=x,Rng)),x)
    >
    > or, since you're going to be programming this,
    >
    >
    > if (YourErrorCheckFcnHere(pr = YourPercentRankFcnHere(rng, x)) {
    > for (i = 0, xlo = xhi = x; i < sizeof(rng) / sizeof(*rng); ++i) {
    > if (xlo < rng[i] && rng[i] < x) xlo = rng[i];
    > if (xhi > rng[i] && rng[i] > x) xhi = rng[i];
    > }
    >
    > ylo = YourPercentRankFcnHere(rng, xlo);
    > yhi = YourPercentRankFcnHere(rng, xhi);
    >
    > pr = ((xhi - x) * ylo + (x - xlo) * yhi) / (xhi - xlo);
    > }
    >
    >
    > If rng is sorted, use binary search rather than simple iteration to
    > locate the points in rng bracketting x. I assumed rng was a simple
    > array. Adapt for more sophisticated data structures.
    >




  5. #5
    Registered User
    Join Date
    11-13-2019
    Location
    San Diego
    MS-Off Ver
    Office 2016
    Posts
    12

    Re: Algorithm for computing Excel PercentRank

    You can replicate PERCENTRANK.EXC with other native Excel functions as follows:

    = ROUND(Rank/(N + 1) - 0.05%, 3)

    where:

    - Rank is found with the Excel function RANK()
    - N is the number of observations in your sample can be found with COUNT()
    - 3 corresponds to the desired level of significance in PERCENTRANK.EXC()

    I tested the formula above for N = 1 to 10000 and found no deviations from PERCENTRANK.EXC.

  6. #6
    Registered User
    Join Date
    01-21-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    1

    Re: Algorithm for computing Excel PercentRank

    What about the case of a single item array? Your formula returns .5 while PERCENTRANK.EXC returns 1

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Algorithm for computing Excel PercentRank

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1