How to get median and quartiles/percentiles of an array in JavaScript (or PHP)?

前端 未结 3 1173
我寻月下人不归
我寻月下人不归 2021-02-07 06:24

This question is turned into a Q&A, because I had struggle finding the answer, and think it can be useful for others

I have a JavaScript

3条回答
  •  执笔经年
    2021-02-07 06:49

    After searching for a long time, finding different versions that give different results, I found this nice snippet on Bastian Pöttner's web blog, but for PHP. For the same price, we get the average and standard deviation of the data (for normal distributions)...

    PHP Version

    //from https://blog.poettner.de/2011/06/09/simple-statistics-with-php/
    
    function Median($Array) {
      return Quartile_50($Array);
    }
    
    function Quartile_25($Array) {
      return Quartile($Array, 0.25);
    }
    
    function Quartile_50($Array) {
      return Quartile($Array, 0.5);
    }
    
    function Quartile_75($Array) {
      return Quartile($Array, 0.75);
    }
    
    function Quartile($Array, $Quartile) {
      sort($Array);
      $pos = (count($Array) - 1) * $Quartile;
    
      $base = floor($pos);
      $rest = $pos - $base;
    
      if( isset($Array[$base+1]) ) {
        return $Array[$base] + $rest * ($Array[$base+1] - $Array[$base]);
      } else {
        return $Array[$base];
      }
    }
    
    function Average($Array) {
      return array_sum($Array) / count($Array);
    }
    
    function StdDev($Array) {
      if( count($Array) < 2 ) {
        return;
      }
    
      $avg = Average($Array);
    
      $sum = 0;
      foreach($Array as $value) {
        $sum += pow($value - $avg, 2);
      }
    
      return sqrt((1 / (count($Array) - 1)) * $sum);
    }
    

    Based on the author's comments, I simply wrote a JavaScript translation that will certainly be useful, because surprisingly, it is nearly impossible to find a JavaScript equivalent on the web, and otherwise requires additional libraries like Math.js

    JavaScript Version

    //adapted from https://blog.poettner.de/2011/06/09/simple-statistics-with-php/
    function Median(data) {
      return Quartile_50(data);
    }
    
    function Quartile_25(data) {
      return Quartile(data, 0.25);
    }
    
    function Quartile_50(data) {
      return Quartile(data, 0.5);
    }
    
    function Quartile_75(data) {
      return Quartile(data, 0.75);
    }
    
    function Quartile(data, q) {
      data=Array_Sort_Numbers(data);
      var pos = ((data.length) - 1) * q;
      var base = Math.floor(pos);
      var rest = pos - base;
      if( (data[base+1]!==undefined) ) {
        return data[base] + rest * (data[base+1] - data[base]);
      } else {
        return data[base];
      }
    }
    
    function Array_Sort_Numbers(inputarray){
      return inputarray.sort(function(a, b) {
        return a - b;
      });
    }
    
    function Array_Sum(t){
       return t.reduce(function(a, b) { return a + b; }, 0); 
    }
    
    function Array_Average(data) {
      return Array_Sum(data) / data.length;
    }
    
    function Array_Stdev(tab){
       var i,j,total = 0, mean = 0, diffSqredArr = [];
       for(i=0;i

提交回复
热议问题