% Class example problem for outlier points
% Barukyah Shaparenko and John M. Cimbala

% Given: Some experimental measurements of a single variable
% To do: Are there any outliers? If so, remove them.

clear all
close all

% First, input data

% Method 1: Explicit data values
x = [48.9; 49.2; 49.2; 49.3; 49.3; 49.8; 49.9; 50.1; 50.2; 50.5]; 

% ---OR---

% Method 2: Import from Excel
% NOTE: You must save the Excel file in the same folder as the Matlab m-file
% Use xlsread or xlsxread

% xls_filename = 'Outliers_and_Thompson_tau.xls';
% sheet_number = 1;
% data_range_1 = 'B8:B17';
% x = xlsread(xls_filename,sheet_number,data_range_1);
% If desired, sort the data by uncommenting the following line:
% x = sort(x);

avg = mean(x); % Average of all measurements
S = std(x); % Standard deviation of all measurements
tau = 1.798; % Modified Thompson tau value (from table, n=10)
%   NOTE: This value must be entered in manually from the table, and it
%   changes as n changes.
delta_crit = tau*S;

% Find the maximum delta - this is the one we most suspect as an outlier:
delta = abs(x-avg);
[delta_max,meas_num] = max(delta);

fprintf('\n----------------------\n');
fprintf('#\tmeas.\tdelta\n');
for i = 1:length(delta)
    fprintf('%g\t%g\t%g\n',i,x(i),delta(i));
end

fprintf('\nmean =\t%g\n',avg);
fprintf('S =\t\t%g\n',S);
fprintf('tau =\t%g\n',tau);
fprintf('tau*S =\t%g\n',delta_crit);

if delta_max > delta_crit
    fprintf('\nSince %g > %g, measurement %g IS an outlier.\n',delta_max,delta_crit,meas_num);
else
    fprintf('\nSince %g < %g, measurement %g IS NOT an outlier.\n',delta_max,delta_crit,meas_num);
end
