3,447 views (last 30 days)

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?

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.

rihab
on 20 Aug 2015

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);

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/159417-how-to-calculate-the-confidence-interval#comment_469848

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/159417-how-to-calculate-the-confidence-interval#comment_469848

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/159417-how-to-calculate-the-confidence-interval#comment_732166

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/159417-how-to-calculate-the-confidence-interval#comment_732166

Sign in to comment.