Percentile Calculation of an Array

 

What would be the way to calculate the n-th percentile of an array of data in mql4?

I know I probably need to sort the array, but then what?


Thanks

 
tonycashflow:

What would be the way to calculate the n-th percentile of an array of data in mql4?

I know I probably need to sort the array, but then what?

Thanks

Hi tonycashflow,

I'm not so sure that I understand what you meant - so I could be wrong here.

If you meant, "How to calculate the n-th percent of data (in array)", then maybe something like this;

Let's just say that we want to calculate the total sum of 65.43 % data in an array

int start()
  {
   double Percent = 65.43; // 65.43%
   double Array [10] = {1,2,3,4,5,6,7,8,9,10};
   double Total_Sum = 0.0;
   
   int index, Percent_Data;
   
   Percent_Data = Percent/100 * ArraySize (Array);
   
   for (index = 0; index < Percent_Data; index ++)
     {
     Total_Sum += Array [index];
     Print ("Index ",index," value ",Array [index]," total ",Total_Sum);
     }
   
   Print (Total_Sum);
   
   return(0);
  }

:D

 
onewithzachy:

Hi tonycashflow,

I'm not so sure that I understand what you meant - so I could be wrong here.

If you meant, "How to calculate the n-th percent of data (in array)", then maybe something like this;

Let's just say that we want to calculate the total sum of 65.43 % data in an array

:D


Hi onewithzachy,

This isnt what i meant. Read here: http://en.wikipedia.org/wiki/Percentile

I would like to able to do the same as you would using the Percentile function in Excel. http://www.ehow.com/how_7290250_use-excel-percentiles.html

Thanks nonetheless ;)

 
tonycashflow:

Hi onewithzachy,

This isnt what i meant. Read here: http://en.wikipedia.org/wiki/Percentile

I would like to able to do the same as you would using the Percentile function in Excel. http://www.ehow.com/how_7290250_use-excel-percentiles.html

Thanks nonetheless ;)

You have the formulas, just code it.
 
tonycashflow:

Hi onewithzachy,

This isnt what i meant. Read here: https://en.wikipedia.org/wiki/Percentile

I would like to able to do the same as you would using the Percentile function in Excel. http://www.ehow.com/how_7290250_use-excel-percentiles.html

Thanks nonetheless ;)

Ooo ... something like iReg ( https://www.mql5.com/en/code/8417 ) or iStdDevOnArray ( https://docs.mql4.com/indicators/iStdDevOnArray ) ?
 

Ok I coded it using the first method mentioned on the Wiki page named "Nearest Rank" which isnt the most accurate but fine for my purposes (and the only one I am able to code at this point anyway).

Here it is:

double ArrayPercentile (double array[], double rank)
{
   double copy[];
   int size=ArraySize(array);
   ArrayResize(copy,size);
   ArrayCopy(copy,array,0,0,WHOLE_ARRAY);
   ArraySort(copy,WHOLE_ARRAY,0,MODE_ASCEND);
   
   double percentile = (rank/100)*size+0.5;
   
   int arrayindex = MathRound(percentile);
   percentile = copy[arrayindex-1];
   
   return(percentile);
}
Reason: