clc 
clear 
clear all

%% Part a)
data = xlsread("assignment-data.xlsx");

%% Part b)
gnod  = []; 
mcv   = [];
edges = [];

% i iterates through the range of data(1,:) in steps of 3
for i = min(data(1,:)):3:max(data(1,:))
    
    x = 0;
    
    for j = i:i + 2
        
        col = find(data(1,:) == j);
        
        % col is 0 or null if j was not found in data(1,:)
        if col > 0
        
            x = x + data(2,col);
            
        end
    end
    
    mcv = [mcv, (i + (i + 3))/2];   % preallocating using zeros(1,length(data(1,:)
    gnod = [gnod, x];               % would be more efficient but these vectors 
    edges = [edges, i];             % are so tiny that it doesn't matter
    
end

fprintf("\nGNOD: \n")
disp(gnod);
fprintf("MCV: \n")
disp(mcv);

hi = [];

% manually extrapolate all data from the consolidated data
for i = 1:length(data(1,:))
    
    for j = 1:data(2,i)
       
        hi = [hi, data(1,i)];
        
    end
end

% using mid-range values, calculate entire range
edges = [mcv - 1.5, max(mcv) + 1.5];

%% Part 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;

%% Part d)
freq = gnod/sum(gnod);
cumfreq = cumsum(freq);
invcumfreq = flip(cumfreq);

fig2 = figure('Name', 'Ogive Distribution Frequencies');

% Cumulative
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

% Inverse Cumulative 
plot (mcv,invcumfreq,'-o','Color','r','LineWidth',1.5,'LineStyle', '-.')

nor = mcv-1.5;
strnor = [];

% create a string array for group ranges
for i = 1:length(nor)
    
    x = nor(i);
    strnor = [strnor, sprintf("%f - %f", x, x+3)];
    
end

%% Part e)
% Abscissa - x coordinate 

fig = gcf;
lines= fig.Children.Children;

% If the functions are inversed, the x coordinate at the 
% interception will always be in the middle of the range of values
% ??
Interception = min(edges)+(range(edges)/2);

% TODO somehow get the intersections from lines

%% Part 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)]);

% export table to worksheet
writetable(T, "assignment-data.xlsx", "Sheet", "Sheet2");

%% Part g)
% Question lacks specifics - what is the grouped data referenced?