Percentile function

CODAP Forums CODAP Help Forum Percentile function

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #990 Score: 0

    Does anyone know the exact formula that CODAP uses to calculate percentiles? I know there are several variations for what can be used for index formulas, and I’ve tried a few (e.g., the one used by Excel), but I’m having trouble reproducing the numbers from CODAP.

    #991
    Bill Finzer
    Keymaster

    Here is CODAP’s source code for the computation:

    /**
     * Get the quantile
     * @param iArray Sorted array of finite numeric values (no non-numeric or missing values allowed)
     * @param iQuantile {Number} quantile [0.0-1.0] to calculate, e.g. first quartile = 0.25
     * @return {Number} median value or undefined if ioArray.length===0
     */
    quantileOfSortedArray: function (iSortedArray, iQuantile) {
      var lastIndex = iSortedArray.length - 1,
          i = lastIndex * iQuantile, // quantile's numeric-real index in 0-(n-1) array
          i1 = Math.floor(i),
          i2 = Math.ceil(i),
          fraction = i - i1;
      if (i < 0) {
        return undefined; // length === 0, or iQuantile < 0.0
      } else if (i >= lastIndex) {
        return iSortedArray[lastIndex]; // iQuantile >= 1.0
      } else if (i === i1) {
        return iSortedArray[i1]; // quantile falls on data value exactly
      } else {
        // quantile between two data values;
        // note that quantile algorithms vary on method used to get value here, there is no fixed standard.
        return (iSortedArray[i2] * fraction + iSortedArray[i1] * (1.0 - fraction));
      }
    },

    As you can see in the last line, when desired quantile falls between two values, the returned value is the lower value plus the fraction of the distance between it and the next value.

    #992

    Thanks for the response!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.