Percentile of a value based on array of data

80 visualizaciones (últimos 30 días)
Maximzzz
Maximzzz el 8 de Mzo. de 2015
Comentada: Thomas Possidente el 1 de Dic. de 2021
Hello!
I have an array with historical data and would like to know where an exogenous variable fits in this array using percentile.
Let's assume a vector with 1:1000 and 950 as a given, the function should return 95%.
It would be possible to create a function to do the task, but maybe matlab has a built-in function to do this ?
Thanks a lot, Max.

Respuesta aceptada

David Young
David Young el 8 de Mzo. de 2015
Editada: David Young el 8 de Mzo. de 2015
% Test data
historicalData = rand(1000, 1);
exogenousVariable = 0.7;
% Compute centile
nless = sum(historicalData < exogenousVariable);
nequal = sum(historicalData == exogenousVariable);
centile = 100 * (nless + 0.5*nequal) / length(historicalData);
This actually computes 94.95 rather than 95 for your example, because the test value is equal to one of the data points. You can round to the nearest whole number or to any other number of significant figures using the round function.
  2 comentarios
Maximzzz
Maximzzz el 8 de Mzo. de 2015
Hello, thanks for your answer! After some research on my own I came up with another solution
function x = comp_percentile(datas,value)
perc = prctile(datas,1:100);
[c index] = min(abs(perc'-value));
x = index+1;
end
Thomas Possidente
Thomas Possidente el 1 de Dic. de 2021
Note that the original answer by David Young is more robust than the version in OP's comment. The version in OP's comment will only give percentile answers to the nearest integer percent. Additionally, David Young's answer can be easily modified to perform the operations on multiple exogenousVariables at once like so -
function centile = comp_percentile(data,value)
data = data(:)';
value = value(:);
nless = sum(data < value, 2);
nequal = sum(data == value, 2);
centile = 100 * (nless + 0.5.*nequal) / length(data);
end

Iniciar sesión para comentar.

Más respuestas (2)

the cyclist
the cyclist el 8 de Mzo. de 2015
Sounds like you want the prctile function.
You could have found this yourself by typing
docsearch percentile
at the command prompt.
  1 comentario
Maximzzz
Maximzzz el 8 de Mzo. de 2015
Thank you for your answer!
If I am correct, prctile returns the value of a certain percentile (i.e. returns the value for the 42nd percentile) ? I would like juste the contrary, given a value I would like to know the closest percentile related to that value.

Iniciar sesión para comentar.


the cyclist
the cyclist el 8 de Mzo. de 2015
This is the inverse of the percentile function, right? There is not a built-in function for that, as far as I know. There are a few contributions in the File Exchange. (They show up if you google the keywords inverse percentile matlab .) Here is one example.

Categorías

Más información sobre Descriptive Statistics en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by