about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/SLP/scoring/calculate_mutual_information_array.m
blob: 1128e411401441d9bfaec1967384fee4f17dd8dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function [mi] = calculate_mutual_information_array(data)
% FUNCTION [MI_ARRAY] = CALCULATE_MUTUAL_INFORMATION_ARRAY(DATA)
% calculates the mutual information between all pairs of variables
% Data must be discrete, and take values 1,2,...,size
% data(i,m) is the node i in the case m.

[num_nodes num_examples] = size(data);

node_sizes = max(data');
for i = 1:num_nodes
  for ic = 1:node_sizes(i) % I CLASS ic
    px(i,ic) = sum(data(i,:)==ic);
    for j = 1:num_nodes    % J CLASS jc
      for jc = 1:node_sizes(j)
        pxy(i,ic,j,jc) = sum( (data(i,:)==ic) & (data(j,:)==jc) );
      end
      mi(i,j) = 0;
    end
  end
end

for i = 1:num_nodes
  for ic = 1:node_sizes(i)
    for j = 1:num_nodes
      for jc = 1:node_sizes(j)
        if( pxy(i,ic,j,jc)~=0 & px(i,ic)~=0 & px(j,jc)~= 0)
          mi(i,j) = mi(i,j) + pxy(i,ic,j,jc)*log2( num_examples*pxy(i,ic,j,jc)/(px(i,ic)*px(j,jc)) )/num_examples; 
        end
      end
    end
  end
end