MATLAB Answers

Ase U
0

plot confidence interval of a signal

Asked by Ase U
on 8 Aug 2018
Latest activity Commented on by Star Strider
on 11 Aug 2018
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.

1 Answer

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

  8 Comments

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.
Ase U
on 11 Aug 2018
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.

Sign in to comment.