function [myMI] = myMutualInformation(myt1, myt2)

% calculate the mutual information using the joint entropy
% for random variables X and Y, joint entropy is:
% H(X,Y) = -1 * sum(p_XY * log(p_XY)) where p_XY is the joint pdf

% calculate joint-histogram
myHist = myJointHistogram(myt1,myt2);

% to find joint pdf normalize the joint-histogram
[rowNo,colNo] = size(myHist);
p_XY = myHist/(rowNo * colNo);

% to find marginal pdfs (p_X and p_Y) sum rows and columns of normalized
% histogram
p_Y = sum(p_XY);    % sum the rows
p_X = sum(p_XY');   % sum the columns

% calculate joint entropy
H_XY = 0;
for i = 1:rowNo
    for j = 1:colNo
        if p_XY(i,j) ~= 0
            H_XY = H_XY + ((-1)*(p_XY(i,j) * log2(p_XY(i,j))));
        end
    end
end
% calculate the marginal entropies
H_Y = 0;
for i = 1:rowNo
    if p_Y(i) ~= 0
        H_Y = H_Y + ((-1)*(p_Y(i) * log2(p_Y(i))));
    end
end
H_X = 0;
for i = 1:colNo
    if p_X(i) ~= 0
        H_X = H_X + ((-1)*(p_X(i) * log2(p_X(i))));
    end
end
% then mutual information is
myMI = (H_X + H_Y) - H_XY;

end