function[CI] = confidence_T(alpha, input_data, tail)

% reference: t_inv - https://www.mathworks.com/matlabcentral/answers/260564-how-to-implement-tinv-manually-without-statistics-and-machine-learning-toolbox
% Inputs:
%
% alpha
% standard_dev: standard deviation of sample
% size: number of data points in sample
% tail: 
%    if tail = 1; 1-tailed t-distribution
%    if tail = 2; 2-tailed t-distribution

%% Local functions
% % t: t-statistic
% % v: degrees of freedom
tdist2T = @(t,v) (1-betainc(v/(v+t^2),v/2,0.5));                                % 2-tailed t-distribution
tdist1T = @(t,v) 1-(1-tdist2T(t,v))/2;                                          % 1-tailed t-distribution
t_inv = @(alpha,v) fzero(@(tval) (max(alpha,(1-alpha)) - tdist2T(tval,v)), 5);  % T-Statistic Given Probability ‘alpha’ & Degrees-Of-Freedom ‘v’

%% Calculations

v = length(x)-1;                            % degrees of freedom
SEM = std(x)/sqrt(length(x));               % Standard Error
ts = t_inv(alpha,v);                        % T-Score
CI = mean(x) + ts.*SEM;                      % Confidence Intervals

figure;
errorbar(mean(x),CI,'bo-');