function [MI,R,N ] = MarginallyIndependent_MutualInformation( LGObj, Var1,Var2 )
% calculate the mutual information between two variables
% Input:Vector VarData1 and VarData2 is the data of X1 and X2.
% Output: MI is the mutual information.
% r1 and r2 is the number of different values in VarData1 and
% VarData2
% N is the validate samples used
LG = struct( LGObj );
CaseLength = LG.CaseLength;
Range1 = LG.VarRange( Var1,: );
Dim1 = LG.VarRangeLength( Var1 );
VarData1 = LG.VarSample( :,Var1 );
Range2 = LG.VarRange( Var2,: );
Dim2 = LG.VarRangeLength( Var2 );
VarData2 = LG.VarSample( :,Var2 );
R = ( Dim1 - 1 ) *( Dim2 - 1 );
Frequency = zeros( Dim1,Dim2 );
N = 0;
for q = 1 : CaseLength % size(VarData1,1) = size(VarData2,1)
t1 = find( Range1( 1:Dim1 ) == VarData1( q ) );
t2 = find( Range2( 1:Dim2 ) == VarData2( q ) );
if isempty( t1 ) == 0 && isempty( t2 ) == 0 % There is possible that t1 or t2 is empty because of invalid value -1 exits.
Frequency( t1, t2 ) = Frequency( t1, t2 ) + 1;
N = N + 1;
end
end
% Frequency
Frequency = Frequency /N;
Fu = sum( Frequency,2 ); % Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.
Fv = sum( Frequency,1 );
%Fu
%Fv
MI=0;
for u=1 : Dim1
for v= 1 : Dim2
if Frequency( u,v ) ~= 0
MI = MI + Frequency(u,v)*log2( Frequency(u,v)/( Fu(u)*Fv(v) ));
end
end
end
end