% Sample length data for basic statistics lecture
% Barukyah Shaparenko and John M. Cimbala

clear all
close all

% Import data from Excel
% Save Excel data in same folder as Matlab m-file
% Use xlsread or xlsxread

% sheet_number and data_range are optional
% Default is Sheet 1 with all (numerical) data imported

% xls_filename = 'Sample_basic_statistics_data.xls';
% sheet_number = 1;
% data_range = 'B8:B17';
% x = xlsread(xls_filename,sheet_number,data_range);

% Alternate way to read in data manually (without using Excel file):
x = [12.1; 12.3; 12.2; 12.2; 12.4; 12.3; 12.2; 12.4; 12.2; 12.5];

x_avg = mean(x); % Simple average
disp([' ']);
disp(['x_avg = ' num2str(x_avg)]);

n = length(x); % Number of data points in this sample
for i = 1:n
	d(i) = x(i) - x_avg;
	delta(i) = abs(d(i));
	d_2(i) = d(i)^2;
end

d_avg = mean(d);
delta_avg = mean(delta); % Average of magnitude of deviations
disp(['|d_i|_avg = ' num2str(delta_avg)]);
S = sqrt(sum(d_2)/(n-1)); % Calculate by its direct definition
S = std(x); % OR, Use Matlab's std function
disp(['S = ' num2str(S)]);
variance = S^2; % Square of the sample standard deviation
disp(['Variance = ' num2str(variance)]);
Mode = mode(x); % 4 out of 10 measurements
disp(['Mode = ' num2str(Mode)]);
Median = median(x); % Get this by arranging values in order
disp(['Median = ' num2str(Median)]);

% Basic statistics for this data set:

n = length(x);
sum_3 = 0;
sum_4 = 0;
for i = 1:n
  del_x_avg(i) = x(i) - mean(x);
  sum_3 = sum_3 + del_x_avg(i)^3;
  sum_4 = sum_4 + del_x_avg(i)^4;
end
  
Mean = mean(x);
Standard_Error = std(x)/sqrt(n);
Median = median(x);
Mode = mode(x);
Standard_Deviation = std(x);
Sample_Variance = Standard_Deviation^2;
Kurtosis = n*(n+1)/(n-1)/(n-2)/(n-3)*sum_4/Standard_Deviation^4 - 3*(n-1)^2/(n-2)/(n-3);
Skewness = n/(n-1)/(n-2)*sum_3/Standard_Deviation^3;
Range = max(x) - min(x);
Minimum = min(x);
Maximum = max(x);
Sum = sum(x);
Count = length(x);

fprintf('\n');
fprintf('Mean \t\t\t\t%g\n',Mean);
fprintf('Standard Error \t\t%g\n',Standard_Error);
fprintf('Median \t\t\t\t%g\n',Median);
fprintf('Mode \t\t\t\t%g\n',Mode);
fprintf('Standard Deviation \t%g\n',Standard_Deviation);
fprintf('Sample Variance \t%g\n',Sample_Variance);
fprintf('Kurtosis \t\t\t%g\n',Kurtosis);
fprintf('Skewness \t\t\t%g\n',Skewness);
fprintf('Range \t\t\t\t%g\n',Range);
fprintf('Minimum \t\t\t%g\n',Minimum);
fprintf('Maximum \t\t\t%g\n',Maximum);
fprintf('Sum \t\t\t\t%g\n',Sum);
fprintf('Count \t\t\t\t%g\n',Count);

