clc
clear all

%%**** Task a) ****
NOR=xlsread('Assignment_Data.xlsx','B1:P1');
NOD=xlsread('Assignment_Data.xlsx','B2:P2');

%%**** Task b) ****

%%Allocating vectors for number of days and mid class values%%
[rN,cN]=size(NOR);
LB=NOR(1);
span=3;
HB=LB+span;
daycount=0;
k=1;

for i=1:cN
    if NOR(i)<HB
        daycount=NOD(i)+daycount;
    else
        GNOD(k)=daycount;
        daycount=NOD(i);
        k=k+1;
        HB=HB+span;
    end
end
GNOD(k)=daycount;
GNOD;

MCV=LB+span/2:span:HB;
% fprintf('MCV= ')
% fprintf('%g',MCV)
% fprintf('\n')

%%**** Task c) ****

%%Plotting histogram from sampled data set%%

figure(1) %%Important to create different graphs throughout code.
histogram('FaceColor','b','BinEdge',LB:span:HB,'BinCounts',GNOD)
yticks([0:1:25])
xticks([LB:1.5:HB]) 
hold on %%to maintain the previous histogram on new plotted data.
plot(MCV,GNOD,'Color',[1 0 0]);
xlabel('Number of Request Classes');
ylabel('Number of Days');
title('Histogram and Polygon distribution of data set');
LOC=find(GNOD==max(GNOD));
Mode=MCV(LOC); %% Finding the Mode and plotting onto graph in text below.
text(Mode,2,['ModalValue=',num2str(Mode)],'Color',[1 1 0],'FontWeight','bold','FontSize',13,'HorizontalAlignment','Center');

%%**** Task d) ****
%%Plotting the ogive distribution.
n=sum(NOD); %%Total number of days.
FRQ=GNOD/n; %Frequency of group data.

%%Cumulative Frequency%%
CF=cumsum(FRQ,'forward'); %Cumulative frequency of data.
CFextra=[0 CF]; %Cumulative frequency extra.

%%Inverse Cumulitive Frequency%%
ICF=cumsum(FRQ,'reverse'); %%Inverse cumulative frequency of data in reverse line.
ICFextra=[ICF 0]; %Inverse Cumulative frequency ranges.

figure(2) %%This creates another graph for new plot.
%%Plot Cumulative frequency line on Figure 2 graph%%
plot(LB:span:HB,CFextra,'Color',[1 1 1],'MarkerEdgeColor',[0 0 1],'Marker','o');
hold on 
%%Plot Inverse Cumulative frequency line on Figure 2 graph%%
plot(LB:span:HB,ICFextra,'Color',[1 1 1],'MarkerEdgeColor',[1 0 0],'Marker','o');
grid on %%Create grid on graph.

YiB=interp1(LB:span:HB,CFextra,LB:0.1:HB,'pchip'); %%Interpolation method to creating a smoother curve.
YiR=interp1(LB:span:HB,ICFextra,LB:0.1:HB,'pchip'); %%Interpolation method to creating a smoother curve.

hold on
%%Plot New X interpolated lines with smoother curve.
plot(LB:0.1:HB,YiB,'Color',[0 0 1],'LineWidth',1.5,'LineStyle','--');
hold on
plot(LB:0.1:HB,YiR,'Color',[1 0 0],'LineWidth',1.5,'LineStyle','-.');
xlim([15 33]) %%This function limits the graph to stay 'tight' to required limits.

%%Figure 2 Axes and title labels%%
xlabel('xi');
ylabel('Frequencies');
title('Ogive Distribution Frequencies');
xticks([LB:1:HB])

%%**** Task e) ****   

%%Determine the median using the interpolated cumulitive and inverse
%%cumulitive distributions.

diff=abs(YiB-YiR); %%The absolute values for the new 
Mdiff=min(diff); %%The minimum difference between the two frequency lines. This determines the 'crossing' point when the two values are at their minimum.
ind=find(diff==min(diff));

xi=LB:0.1:HB;
Median=xi(ind)


%%**** Task f) ****

%%Excel table data. Firstly conveying the first column and then
%%subsequently each row. num2cell is used to break the numbers into
%%individual cells.

nc=GNOD.*MCV; %%f.x%%  
nc2=GNOD.*MCV.^2;   %%f.x^2%%
T=table({'Number of Requests';'Mid Class ci';'Number of Days ni';'Frequency';'Cumulated Frequency';'Inversely Cumulated Frequency';'ni.ci';'ni.ci^2'}, ...
    [{'[18,18[','[18,21[','[21,24[','[24,27[','[27,30[','[30,33]'};num2cell(MCV);num2cell(GNOD);num2cell(FRQ);num2cell(CF);num2cell(ICF);num2cell(nc);num2cell(nc2)]);
writetable(T,'Assignment_Data.xlsx','Sheet',2) %%Each column must have its own cell for the data hence num2cell.

%%****Task g) ****

%%Finding the mean: f.x/n.
Mn1=sum(nc);
Mean=Mn1/n

%%Finding the Variance: sum of f.x^2/n - the mean^2.
Variance=sum(nc2)/sum(n)- Mean.^2

%%Finding the Standard deviation: Sqrt of the variance.
Standar_deviation=sqrt(Variance)


