
% Lukáš Žúbor, 318061, 1.ročník, 2.stupeň, 3ZIJ1A 
clc;
clear;
A = readmatrix("data01.txt","Delimiter"," ");
A = transpose(A);
sorted = sortrows(A);
sorted = round(sorted,3);
[~,~,ix] = unique(sorted);
unique_values = unique(sorted);
class_size = length(unique_values)/7;

numbers = table();
for i = 1:7
    lowerLimit = (i-1)*(class_size);
    upperLimit = i*class_size;
    lower_limit_rounded = ceil(lowerLimit)+1;
    upper_limit_rounded = ceil(upperLimit);
    number_at_lower_limit = (unique_values(lower_limit_rounded));
    number_at_upper_limit = (unique_values(upper_limit_rounded));
    mid = round((number_at_upper_limit+number_at_lower_limit)/2,3);
    mid_count = numel(sorted(sorted==mid));
    disp("At lower limit "+lower_limit_rounded+" is number "+number_at_lower_limit);
    disp("At upper limit "+upper_limit_rounded+" is number "+number_at_upper_limit);
    disp("Mean value of this interval is "+mid+" with incidence of "+mid_count);
    numbers.Class(i)=strcat(num2str(number_at_lower_limit)," - ",num2str(number_at_upper_limit));
    numbers.Count(i)=numel(sorted(sorted>=number_at_lower_limit & sorted<=number_at_upper_limit));
    numbers.Expected_value(i) = mid;
    numbers.Expected_value_count(i) = mid_count;
    numbers.Expected_value_point(i) = "-";
    numbers.Expected_value_interval(i) = "-";
    numbers.Dispersion_point(i) = "-";
    numbers.Dispersion_interval(i) = "-";
    numbers.Standard_deviation_point(i) = "-";
    numbers.Standard_deviation_interval(i) = "-";
end
expected_value_point = 0;

dispersion_point = 0;

for i=1:7
    expected_value_point = expected_value_point + numbers.Expected_value(i)*numbers.Expected_value_count(i);
end
expected_value_point = (1/sum(numbers.Expected_value_count)*expected_value_point);
numbers.Class(8) = "Total";
numbers.Count(8) = (sum(numbers.Count));
numbers.Expected_value(8) = round((max(sorted)+min(sorted))/2,3);
numbers.Expected_value_count(8) = sum(numbers.Expected_value_count);
numbers.Expected_value_point(8) = expected_value_point;
for i=1:7
    dispersion_point = dispersion_point + (numbers.Expected_value(i)*(numbers.Expected_value_count(i)-expected_value_point))^2;
end
dispersion_point = (1/(sum(numbers.Expected_value_count)-1))*dispersion_point;
numbers.Dispersion_point(8) = dispersion_point;
numbers.Standard_deviation_point(8) = sqrt(dispersion_point);
average = mean(sorted);
expected_value_lower_interval = average - (sqrt(dispersion_point)/sqrt(length(sorted)*0.196));
expected_value_upper_interval = average + (sqrt(dispersion_point)/sqrt(length(sorted)*0.196));
dispersion_lower_interval = ((length(sorted)-1)*var(sorted))/2003920;
dispersion_upper_interval = ((length(sorted)-1)*var(sorted))/1996080;
standard_deviation_lower_interval = sqrt(dispersion_lower_interval);
standard_deviation_upper_interval = sqrt(dispersion_upper_interval);
numbers.Expected_value_interval(8) = expected_value_lower_interval+" - "+expected_value_upper_interval;
numbers.Dispersion_interval(8) = dispersion_lower_interval+" - "+dispersion_upper_interval;
numbers.Standard_deviation_interval(8) = standard_deviation_lower_interval+" - "+standard_deviation_upper_interval;
disp(average);
disp(numbers);