MATLAB Answers

How to calculate the confidence interval

3,447 views (last 30 days)
Sepp
Sepp on 20 Oct 2014
Edited: Adam Danz on 15 Nov 2019
Hi
I have a vector x with e.g. 100 data point. I can easy calculate the mean but now I want the 95% confidence interval. I can calculate the 95% confidence interval as follows:
CI = mean(x)+- t * (s / square(n))
where s is the standard deviation and n the sample size (= 100).
Is there a method in matlab where I just can feed in the vector and then I get the confidence interval?
Or I can write my own method but I need at least the value of t (critical value of the t distribution) because it depends on the number of samples and I don't want to lookup it in a table everytime. Is this possible?
Would be very nice if somebody could give an example.
Last but not least, I want 95% confidence in a 5% interval around the mean. For checking that I just have to calculate the 95% confidence interval and then check if the retrieved value is less than 5% of my mean, right?

  2 Comments

Andrei Keino
Andrei Keino on 18 Jul 2017
How to calculate confidence interval for linear model. It's the same for mean value, but the number of degree of freedom (dof) is equal to 1
Adam Danz
Adam Danz on 5 Aug 2019
@ Sepp, this method assumes your data follow a normal distribution. If that assumption doesn't hold with your data, you can perform bootstrapped confidence intervals using bootci().

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 20 Oct 2014
This works:
x = randi(50, 1, 100); % Create Data
SEM = std(x)/sqrt(length(x)); % Standard Error
ts = tinv([0.025 0.975],length(x)-1); % T-Score
CI = mean(x) + ts*SEM; % Confidence Intervals
You have to have the Statistics Toolbox to use the tinv function. If you do not have it, I can provide you with a few lines of my code that will calculate the t-probability and its inverse.

  17 Comments

rihab
rihab on 20 Aug 2015
Hi I am facing the same problem. Could you please tell me matlab code that to calculate the t-probability and its inverse?(I dont have statistics toolbox).
Adam Danz
Adam Danz on 21 Aug 2019
Here's an anonymous function based on Star Strider's answer. It uses tinv() which means the stats toolbox is required. It also uses "omitnan" flags so that NaN values are ignored which requires r2016a or later. This method requires an approximately normal distribution.
% x is a vector, matrix, or any numeric array of data. NaNs are ignored.
% p is a the confident level (ie, 95 for 95% CI)
% The output is 1x2 vector showing the [lower,upper] interval values.
CIFcn = @(x,p)std(x(:),'omitnan')/sqrt(sum(~isnan(x(:)))) * tinv(abs([0,1]-(1-p/100)/2),sum(~isnan(x(:)))-1) + mean(x(:),'omitnan');
% Demo
x = randn(100,1) + 5;
p = 95;
CI = CIFcn(x,p)
The percentile method does not require a normal distribution and is therefore more robust and safe to use with bootstrapping methods.
% x is a vector, matrix, or any numeric array of data. NaNs are ignored.
% p is the confidence level (ie, 95 for 95% CI)
% The output is 1x2 vector showing the [lower,upper] interval values.
CIFcn = @(x,p)prctile(x,abs([0,100]-(100-p)/2));
To demonstrate the difference between these methods, consider this skewed distribution. The black reference lines are the 95% CIs produced by the first method using tinv() and they are obviously not within the middle 95% of the distribution. The magenta reference lines are the 95% CIs produced by the 2nd method using prctile() which does not require a normal distribution.
x = pearsrnd(0,1,1,4,100,1);
histogram(x);
CIFcn = @(x,p)std(x(:),'omitnan')/sqrt(sum(~isnan(x(:)))) * tinv(abs([0,1]-(1-p/100)/2),sum(~isnan(x(:)))-1) + mean(x(:),'omitnan');
CI = CIFcn(x,95);
arrayfun(@(x)xline(x,'-k','tinv'),CI);
CIFcn = @(x,p)prctile(x,abs([0,100]-(100-p)/2));
CI = CIFcn(x,95);
arrayfun(@(x)xline(x,'-m','prctile'),CI);

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!