function [H1, H2, H12, H21, J, M, R] = information_analysis(x,y)

% [H1, H2, H12, H21, J, M, R] = information_analysis(x,y)
% 
% This function applies infromation analysis on the two input column
% vectors. It calculates entropies, conditional entropies, joint entropy,
% mutual information and correlation coefficient. To calculate H12, H21, J,
% M, and R the input vectors must have the same length. If there is only
% one input vector, then only entropy is returned.
% 
% x     - first input column vector
% y     - second input column vector
% H1    - entropy of vector H(X)
% H2    - entropy of vector H(Y)
% H12   - conditional entropy H(X|Y)
% H21   - conditional entropy H(Y|X)
% J     - joint entropy H(X,Y)
% M     - mutual information I(X,Y)
% R     - correlation coefficient

%% Initialize variables
H1 = [];
H2 = [];
H12 = [];
H21 = [];
J = [];
M = [];
R = [];

%% Probability and entropies
u_x = unique(x).';
p1 = sum(x(:,ones(1,length(u_x))) == ...
    u_x(ones(length(x),1),:))./length(x);

H1 = -sum(p1.*log2(p1));

if(nargin > 1)
    u_y = unique(y).';    
    p2 = sum(y(:,ones(1,length(u_y))) == ...
        u_y(ones(length(y),1),:))./length(y);

    H2 = -sum(p2.*log2(p2));

    if(length(x) == length(y))
        %% Joint entropy
        m = length(u_x);
        n = length(u_y);
        p12 = zeros(m,n);

        for i = 1:m
            for j = 1:n
                p12(i,j) = sum((x == u_x(i)).*(y == u_y(j)));
            end
        end

        p12 = p12./length(x);

        % Joint entropy
        tmp = reshape(p12,1,[]);
        tmp = tmp(tmp ~= 0);
        J = -sum(tmp.*log2(tmp));

        %% Mutual information
        M = H1 + H2 - J;

        %% Conditional entropies
        H12 = H1 - M;
        H21 = H2 - M;

        %% Correlation coefficient
        R = sum((x-mean(x)).*(y-mean(y)))/((length(x)-1)*std(x)*std(y));
    end
end