plot confidence interval of a signal

Hi all,
i have a signal so it's just data, that i load on Matlab and I have to plot 95% confidence interval according to student t-distribution of my signal. Exactly like photo, that i added. When i am reading some solutions about that, i am confuse because i am not good about statistics. If you help me, at least how can i start to make something, i would be very appreciated that.
Thank you!

1 Comment

Hello
I suggest using the confplot. Access here confplot. Really easy
example:
x = 1:0.1:10;
y = sin(x);
e = std(y)*ones(size(x));
confplot(x,y,e)
As long as you have x and y you will be fine even if you import from excel

Sign in to comment.

 Accepted Answer

In order to calculate the 95% confidence intervals of your signal, you first will need to calculate the mean and *|std| (standard deviation) of your experiments at each value of your independent variable. The standard way to do this is to calculate the standard error of the mean at each value of your independent variable, multiply it by the calculated 95% values of the t-distribution (here), then add and subtract those values from the mean. The plot is then straightforward. (The tinv function is in the Statistics and Machine Learning Toolbox.)
Example
x = 1:100; % Create Independent Variable
y = randn(50,100); % Create Dependent Variable ‘Experiments’ Data
N = size(y,1); % Number of ‘Experiments’ In Data Set
yMean = mean(y); % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N); % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv([0.025 0.975], N-1); % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)); % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(x, yMean) % Plot Mean Of All Experiments
hold on
plot(x, yCI95+yMean) % Plot 95% Confidence Intervals Of All Experiments
hold off
grid
This should get you started.

15 Comments

Thanks a lot for your answer, i have an idea now at least but i didn't get actually value "x" and "y" so independent and dependent variable . At my signal how should i define that?
As always, my pleasure.
According to the plot image you posted, your independent variable is Frequency and your dependent variable is Amplitude. You will need to use the loglog (link) function to plot it.
My impression is that you have several signals (or equivalently their Fourier transforms), and you want to find the mean and confidence intervals for them. For that, my code will work as posted.
If you are instead doing curve fitting of your signal (you did not mention that), you need to have a mathematical model of your signal to use as your ‘objective function’, then fit it to your data. The confidence intervals will then be the result of first using the Statistics and Machine Learning Toolbox functions nlinfit (link) to do the fit and generate the other necessary outputs, and then nlpredci (link) to calculate the confidence intervals.
Between my code and these functions, you will be able to do what you want.
thank you so much for your help!
As always, my pleasure.
Ase U
Ase U on 10 Aug 2018
Edited: Ase U on 10 Aug 2018
Sorry but i have an another question. My plot of mean value show like in picture, which i added. And I think size of mean value 1 1 but size of x and y signals is 5001. And for plot(x, yCI95+yMean) i have this error.
Error using plot Vectors must be the same lengths.
Error in Conf_Interval_prob_1 (line 31) plot(time, yCI95+yMean)
How should i solve this? If you have any idea, it would be great. Thank you!
I do not understand ‘... size of x and y signals is 5001.’
I do not know the dimensions of your vectors and matrices. You would get a scalar (1x1) mean value of a column vector.
Since your data may be column-oriented rather than row-oriented, see if adding a dimension argument and calculating them across the columns (rather than across the rows) as (for example, using some 2D matrix ‘z’):
zmean = mean(z,2);
and
zstd = std(z,[],2);
solves your problem.
If you simply want to expand your scalar mean value (that I call ‘scalarMean’ here) to be the same size as ‘x’, create it as:
meanvec = scalarMean*ones(size(x));
That will work.
thank you for your time. I mean size of my signals is [5001 1](lets say x and time) but mean value is just a number and size is [1 1]. And if a plot mean value, surely comes a line. But i want to find and plot upper and lower boundaries of signals according to confidence interval. Thats is the point i don't get it.
Size (5001x1) means 5001 rows and 1 column. Most MATLAB functions will operate column-wise, so the mean function will produce a scalar result for a column vector. If all your signals are vectors, then taking the mean of them will always produce a scalar result.
If all your data are vectors (not matrices of several experiments), they will not have confidence intervals. The only way you can calculate confidence intervals for them is to do curve-fitting and then calculate the confidence intervals on the fit. Use nlinfit and nlpredci in the Statistics and Machine Learning Toolbox for that. Note that you will need a mathematical model of the process that produced your data (the ‘objective function’) to do the regression.
If you are trying to do this with real data I would recommend using nanmean() instead of mean to avoid getting alot of nan values.
I want a CI curve comparing real and simulated data. Is above code work for my problem?
@Star Strider, Thanks for the CI plot, I just have one question, I need to calculate CI_width and CI_var, which is basically the 95% confidence interval width and variance acc. to given formulae
How should I move forward on this?
@Jitin Malhotra — Just calculate the statistics according to the relations posted. Beyond that, I have no idea.
@Star Strider, but how to calculate the cov(fi), as mentioned in the relations posted?
I suggest the cov function.
@Star Strider in my case also i want to compute CI for two vectors with dimension 51*1 you can see the picture that i have upload it

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!