Quantile function Julia vs Matlab
Asked Answered
E

1

5

I just recently came accross that there is different defition of quantile() in Julia and Matlab. I was unable to align the two definitions and always get different result.

Does anybody know why is this a case and how to align their definitions?

I tried the following:

A = [0.5377,    1.8339 ,  -2.2588 ,   0.8622 ,   0.3188,   -1.3077,   -0.4336];
Q = quantile(A,0.3);

which results in Q = -0.7832. When I write the same in Julia Statistics library:

A = [0.5377,    1.8339 ,  -2.2588 ,   0.8622 ,   0.3188,   -1.3077,   -0.4336];
Q1 = quantile(A,0.3);
Q2 = quantile(A,0.3,sorted=true);

which results in Q1=-0.60842 and Q2 = -1.44026. I also tried playing with alpha and beta parameters but it is super tedious and I have no way of knowing if my chosen paramerters hold on the whole range.

Exclamatory answered 10/9, 2023 at 18:42 Comment(2)
Well, when you say sorted=true but pass an unsorted array, you can’t expect a correct result, so you need to ignore that one. The other difference is just the different interpretation of the concepts. There are many choices to be made. For example, the MATLAB docs extensively describe what the computation is: mathworks.com/help/matlab/ref/quantile.html#btf91zmArrack
Maybe setting the alpha and beta parameters in Julia you can replicate what MATLAB does? docs.julialang.org/en/v1/stdlib/Statistics/#Statistics.quantile Otherwise you’d have to implement it yourself.Arrack
P
7

Set alpha=beta=0.5 as Matlab uses type 5 quantile definition.

Note though other software (like R or Python) use the same defaults as Julia (alpha=1 and beta=1).

Passive answered 10/9, 2023 at 21:8 Comment(3)
Thank you for your answer. Yes, you are right, by combining what @cris-luengo said about sorted and running your answer as: quantile(A,0.3,alpha=0.5,beta=0.5), it gives the same result as Matlab. Thank you.Exclamatory
Nonetheless, I find it misleading that sorted=true for the Julia quantile function says that the input array is sorted rather than sorting the array.Exclamatory
sorted=true does say that itr can be assumed to be sorted. That is: set it this way if it is you that guarantees that the data is sorted.Antisyphilitic

© 2022 - 2024 — McMap. All rights reserved.