MATLAB Answers

plot confidence interval of a signal

1,691 views (last 30 days)
Ase U
Ase U on 8 Aug 2018
Commented: Sharif Ahmed on 14 Jun 2020 at 17:05
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!

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Aug 2018
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.

  10 Comments

Show 7 older comments
Star Strider
Star Strider on 11 Aug 2018
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.
Robert Moore
Robert Moore on 25 Mar 2020
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.
Sharif Ahmed
Sharif Ahmed on 14 Jun 2020 at 17:05
I want a CI curve comparing real and simulated data. Is above code work for my problem?

Sign in to comment.

More Answers (0)