forked from jdonley/SoundZone_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confidence_intervals.m
64 lines (56 loc) · 1.84 KB
/
confidence_intervals.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [ Conf_intervals ] = confidence_intervals( samples, interval, islognorm )
% Find the confidence intervals for a set of data for use with the errorbar function in MATLAB
%
% Syntax: [Conf_intervals] = CONFIDENCE_INTERVALS( samples, interval )
% Pass the samples to the function with the desired confidence interval (95
% for 95%). Samples should be in each row of a column where the column is
% the dataset to analyse
%
% Inputs:
% samples - A 1D or 2D array of samples where each column is a dataset
% for which the confidence intervals are calculated.
% interval - The confidence interval as a percentage (e.g. 95 for 95%
% confidence intervals).
% islognorm - Finds the confidence intervals using the log-normal
% variance if set to TRUE.
%
% Outputs:
% Conf_intervals - Confidence intervals for use with the errorbar
% function in MATLAB.
%
% Example Usage:
% load count.dat;
% samples = count';
% X=1:size(samples,2);
% ConfIntervals = confidence_intervals(samples,95);
% errorbar(X,mean(samples),ConfIntervals(:,1),ConfIntervals(:,2),'rx');
% axis([0 25 0 250]);
%
% See also: errorbar.m
% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Date: 19 September 2016
% Revision: 0.2
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 3
islognorm = false;
end
if nargin < 2
interval = 95; % Confidence = 95%
end
L = size(samples,1);
a = 1 - interval/100;
ts = tinv([a/2, 1-a/2],L-1); % T-Score
if islognorm
v = var(samples);
m = mean(samples);
sigm = sqrt( log( 1 + v./m.^2 ) );
else
sigm = std(samples);
end
Conf_intervals(:,1) = ts(1)*sigm/sqrt(L); % Confidence Intervals
Conf_intervals(:,2) = ts(2)*sigm/sqrt(L); % <-'
end