MATLAB Answers

# How to get upper and lower bounds values from an empirical cumulative distribution?

6 views (last 30 days)
Hamish Cavaye on 20 Oct 2020
Commented: Star Strider on 20 Oct 2020
Hi, thanks for looking at my question. I've been googling, looking on here, and reading the Matlab documentation all afternoon and I can't work this out. I'm sure it's very simple, but I'm new to Matlab syntax so I need some help please!
I have a vector, which is a list of parameters from some data fitting. Specifically it's 250 values.
I want to find the 95% confidence bounds for the numbers in this vector. I can plot an ecdf:
I can also get the [x,y] values for this ECDF using:
[cdf_x,cdf_y] = ecdf(data_vector)
cdf = [cdf_x,cdf_y]
Where I am struggling is I cannot work out how to get the 95% upper and lower values from the array "cdf" (or any of the previous variables I've generated along the way). It's all empirical, so I just want to know the two values of cdf_y where cdf_x equals 0.025 and 0.975.
How can I do this?
NB. cdf_x doesn't have values specifically at 0.025 or 0.975 so it needs to either interpolate or give the closest result if possible.

#### 0 Comments

Sign in to comment.

### Accepted Answer

Star Strider on 20 Oct 2020
Edited: Star Strider on 20 Oct 2020
Using the prctile funciton on ‘cdf_y’ may work to calculate the percentiles (that appear to be what you want). To get the approximate ‘cdf_x’ values that are associated with them, use the interp1 function.
The code would go something like this:
prctv = prctile(cdf_y, [2.5 97.5]);
xval = interp1(cdf_y, cdf_x, prctv);
I cann ot write exact code since I do not have your data.
EDIT — (20 Oct 2020 at 15:42)
Corrected typographical error.

#### 4 Comments

Show 1 older comment
Star Strider on 20 Oct 2020
Probably the easiest fix for that is to use the unique function.
See if this does what you want:
cdf_mtx = [cdf_x(:) cdf_y(:)]; % Create Matrix
[Uy,idx] = unique(cdf_mtx(:,2)); % Unique Values Of ‘cdf_y'cdf_y’
Ucdf_mtx = [cdf_mtx(idx,1) Uy]; % New Matrix Of Unique Values
Then, use the appropriate columns of ‘Ucdf_mtx’ in the prctile and interp1 calls. (This preserves the original vectors.) I partially tested this, however since I do not have your code to work with, I am posting it as UNTESTED CODE.
If you still have problems, consider using save to put ‘cdf_x’ and ‘cdf_y’ in a .mat file and attaching it so I can work with it.
Hamish Cavaye on 20 Oct 2020
Thanks a lot for your help. I will give this a try and if I need to come back to you I'll attach some files so you've got something specific to work with!
Star Strider on 20 Oct 2020
My pleasure!

Sign in to comment.

R2020a

### Community Treasure Hunt

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

Start Hunting!