clc 
clear 
clear all

%% a)
data = xlsread("assignment-data.xlsx");
%% b)
gnod  = []; 
mcv   = [];
edges = [];
for i = min(data(1,:)):3:max(data(1,:))
    x = 0;
    for j = i:i + 2
        
        col = find(data(1,:) == j);
        
        if col > 0
        
            x = x + data(2,col)
        end
    end
    mcv = [mcv, (i + (i + 3))/2];   
    gnod = [gnod, x];               
    edges = [edges, i];           
    
end

fprintf("\nGNOD: \n")
disp(gnod);
fprintf("MCV: \n")
disp(mcv);
hi = [];


for i = 1:length(data(1,:))
    
    for j = 1:data(2,i)
       
        hi = [hi, data(1,i)];
        
    end
end
edges = [mcv - 1.5, max(mcv) + 1.5];

%% c)
fig1 = figure('Name', 'Histogram and Polygon distribution of data set');

h = histogram(hi, edges);
h.FaceColor = 'b';
title('Histogram and Polygon distribution of data set');
xlabel('Number of Request Classes'); 
ylabel('Number of Days'); 
xticks(min(edges):1:max(edges));
yticks(0:1:max(gnod));

hold on;
m = mode(hi);
x1 = min(edges)+(range(edges)/2);

p = plot(mcv, gnod, 'color', 'r', 'LineWidth', 1.5);
txt = sprintf('Modal Value = %d',m);
text (x1,2, txt , 'FontSize', 13, 'FontWeight', 'bold', 'Color', 'y', 'HorizontalAlignment', 'center'); %todo align

hold off;

%% d)
freq = gnod/sum(gnod);
cumfreq = cumsum(freq);
invcumfreq = flip(cumfreq);
fig2 = figure('Name', 'Ogive Distribution Frequencies');
plot (mcv,cumfreq,'-o','Color','b','LineWidth',1.5,'LineStyle', '--');

grid on;
grid minor;
title('Ogive Distribution Frequencies');
xlabel('xi');
ylabel('Frequencies');
xticks(min(edges):1:max(edges));
yticks(0:0.1:1);
hold on
plot (mcv,invcumfreq,'-o','Color','r','LineWidth',1.5,'LineStyle', '-.')

nor = mcv-1.5;
strnor = [];
for i = 1:length(nor)
    x = nor(i);
    strnor = [strnor, sprintf("%f - %f", x, x+3)];
end

%% e)
fig = gcf;
lines= fig.Children.Children;
Interception = min(edges)+(range(edges)/2);


%% f)
T = table(["Number of Requests xi"; "Mid Class ci"; "Number of Days ni";
           "Frequency"; "Cumulated Frequency"; "Inversely Cumulated Frequency";
           "ni.ci"; "ni.ci^2"], [strnor; mcv; gnod; freq; cumfreq; invcumfreq; gnod.*mcv; gnod.*(mcv.^2)]);
writetable(T, "assignment-data.xlsx", "Sheet", "Sheet2");

%% G)
% Below is the calculated data for the descriptive statistics using 'data'.
mean(data)
std(data)
var(data)