% The score distribution is considered as a mixture of two Gaussian
% distributions.
%
% GMM.m         The parameters of two Gaussian distributions are estimated by the
%               EM algorithm.
%
% Synopsis:     [F, P, pi_1, pi_2, mu_1, sigma_1, mu_2, sigma_2] = GMM(score, X);
%
% Input:        score   = the score to be classified
%               X       = the scores of the whole class
%
% Output:       F       = the probability of belonging to the secondary group 
%               P       = the probability of belonging to the leading group
%               pi_1    = prior of the leading group
%               mu_1    = mean of the  leading group
%               sigma_1 = standard deviation of the leading group
%               pi_2    = prior of the secondary group
%               mu_2    = mean of the secondary group
%               sigma_2 = standard deviation of the secondary group
%
% Plot_GMM.m    Plot the original score distribution and their approximation
%               by using two Gaussian distributions
% Synopsis:     Plot_GMM(X, pi_1, pi_2, mu_1, sigma_1, mu_2, sigma_2);
%
% Input:        X       = the scores of the whole class
%               pi_1    = prior of the leading group
%               mu_1    = mean of the  leading group
%               sigma_1 = standard deviation of the leading group
%               pi_2    = prior of the secondary group
%               mu_2    = mean of the secondary group
%               sigma_2 = standard deviation of the secondary group

clear all;
close all;
clc;

theta = [.5 80 10 .5 40 10];

score_0 = 100;   % the score of your homework 1
score_1 = 55;    % the score of your quiz 1
score_2 = 55;    % the score of your quiz 2
score_3 = 55;    % the score of your Midterm
score_4 = 55;    % the score of your quiz 3
score_5 = 55;    % the score of your quiz 4
score_6 = 55;    % the score of your quiz 5
score_7 = 55;    % the score of final exam

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Homework 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X0 = 100*ones(78,1)';        % the scores of the whole class
X0(11) = 0;
X0(17) = 0;
X0(28) = 0;
X0(41) = 60;
X0(53) = 60;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUIZ 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X1 = [0,50,20,50,40,50,45,55,95,90,50,70,100,90,65,100,0,50,55,95,80,90,95,85,80,100,40,80,35,50,...
   40,20,75,85,50,45,40,40,90,30,50,50,0,40,60,50,45,80,90,80,40,40,50,50,60,50,40,65,40,65,...
   70,50,60,55,75,30,30,10,40,70,80,20,20,60,10,40,20,10];

[P, F, pri, mu, sigma] = GMM(score_1, X1, theta);
sigma
disp([' The score of your quiz 1 is ' num2str(score_1) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X1, pri, mu, sigma);
title('Quiz 1'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUIZ 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X2 = [100,100,75,90,65,100,100,100,100,100,90,100,100,100,100,100,90,20,80,65,90,100,100,95,100,100 ...
  ,40,100,60,50,100,100,100,100,100,100,90,65,65,100,100,100,0,100,90,90,75,100,80,100,100,100 ...
  ,90,60,75,65,100,60,90,50,80,20,100,75,100,100,100,100,100,100,100,30,35,90,75,75,90,75];

[P, F, pri, mu, sigma] = GMM(score_2, X2, theta);
disp([' The score of your quiz 2 is ' num2str(score_2) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X2, pri, mu, sigma);
title('Quiz 2'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Midterm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X3=[69,73,77,68,80,77,60,64,73,90,40,68,81,93,73,92,77,80,66,68,65,75,73,75,80,64,77 ...
  ,72,25,57,68,57,58,75,56,57,69,70,79,55,72,77,78,42,92,38,54,67,25,58,12,78,68,65 ...
  ,51,78,79,76,63,68,74,33,77,73,98,68,63,50,43,75,94,56,35,30,56,62,25,41];

[P, F, pri, mu, sigma] = GMM(score_3, X3, theta);
disp([' The score of your midterm is ' num2str(score_3) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X3, pri, mu, sigma);
title('Midterm'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUIZ 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X4 = [100,100,100,70,95,100,100,100,100,90,60,85,95,100,100,95,65,95,100,95,75 ...
      ,95,100,100,100,95,95,100,95,100,100,100,95,100,100,100,100,100,75,100,0 ...
      ,95,100,100,90,95,100,70,100,100,100,100,100,100,100,100,95,100,80,70,70 ...
      ,50,100,90,100,100,100,100,95,100,100,95,0,100,95,100,50,100];

[P, F, pri, mu, sigma] = GMM(score_4, X4, theta);
disp([' The score of your quiz 3 is ' num2str(score_4) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X4, pri, mu, sigma);
title('Quiz 3'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUIZ 4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X5 = [75,80,0,75,100,40,75,95,90,80,5,40,60,100,55,85,90,60,0,45,40,100,25,60,70 ...
     ,90,0,40,25,0,0,0,0,0,0,30,30,55,40,30,30,95,0,0,75,50,40,60,75,100,0,0,45,45 ...
     ,50,80,80,85,40,80,55,0,70,95,55,35,70,30,80,75,85,45,40,0,0,0,15,0];

[P, F, pri, mu, sigma] = GMM(score_5, X5, theta);
disp([' The score of your quiz 4 is ' num2str(score_5) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X5, pri, mu, sigma);
title('Quiz 4'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUIZ 5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X6 = [80,100,70,100,50,70,50,50,70,80,80,50,0,50,0,50,100,0,0,20,0,0,50,0,80,80 ...
     ,80,0,0,0,20,0,0,30,50,0,0,30,70,0,0,60,0,0,100,50,70,80,100,50,0,0,50,50 ...
     ,80,80,100,50,100,30,80,0,50,50,100,30,50,70,100,100,100,30,0,100,40,80,0,50];

[P, F, pri, mu, sigma] = GMM(score_6, X6, theta);
disp([' The score of your quiz 5 is ' num2str(score_6) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X6, pri, mu, sigma);
title('Quiz 5'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Final exam
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the scores of the whole class
X7 = [68 59 87 76 51 64 69 79 88 78 85 56 68 95 62 90 85 30 79 46 58 64 74 46 93 93 64 ...
64 56 48 49 32 66 56 76 70 63 55 48 46 65 83 0 58 73 72 56 77 78 100 0 39 83 70 ...
75 83 90 78 92 75 89 0 88 78 83 61 62 42 96 90 87 73 45 49 55 77 39 25];

[P, F, pri, mu, sigma] = GMM(score_7, X7, theta);
disp([' The score of your final exam is ' num2str(score_7) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(X7, pri, mu, sigma);
title('Final Examination'), xlabel('Score');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Cumulated score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
score   = 0.05*score_0 + 0.07*(score_1 + score_2 + score_4 + score_5 + score_6)...
        + 0.3*(score_3 + score_7);      % original score 
n_score = score*100/100;                % normalized score               

% the cumulated scores of the whole class
X       = 0.05*X0 + 0.07*(X1 + X2 + X4 + X5 + X6) + 0.3*(X3 + X7) ;
n_X     = X*100/100;                    % normalized cumulated score                 

[P, F, pri, mu, sigma] = GMM(n_score, n_X, theta);
disp([' Your cumulated score is ' num2str(n_score) ]);
disp([' The probability of belonging to the 1st group = ' num2str(P) ]);
disp([' The probability of belonging to the 2nd group = ' num2str(F) ]);
disp(' ');

Plot_GMM(n_X, pri, mu, sigma);
title('Cumulated score'), xlabel('Score');

% number of people whose final score are below 60 
n = size(find(n_X < 60),2);
disp([' There are ' num2str(n) ' people whose final scores are below 60.']);

% Perform classification on the normalized cumulated scores
p(1,:)  = (sqrt(2*pi)*sigma(1))^(-1)*exp(-1*0.5*(n_X - mu(1)).^2/(sigma(1)^2));
p(2,:)  = (sqrt(2*pi)*sigma(2))^(-1)*exp(-1*0.5*(n_X - mu(2)).^2/(sigma(2)^2));
tmp     = p(1,:)*pri(1) + p(2,:)*pri(2);
F       = p(2,:)*pri(2)./tmp;
n       = size(find(F > .5),2); % number of people who are classified as the 2nd group 
disp([' There are ' num2str(n) ' people who are classified as the 2nd group.']);

% mean of the normalized cumulated scores
disp([' The mean score is ' num2str(mean(n_X)) '.']);