fork download
  1. function result = kl_divg(p, q)
  2. epsilon = 1e-10;
  3. p = max(p, epsilon);
  4. q = max(q, epsilon);
  5. result = sum(p .* log2(p ./ q));
  6. end
  7.  
  8. E_plus = [0.89 0.03 0.08; 0.42 0.05 0.35; 0.73 0.02 0.06; 0.85 0.02 0.05];
  9. E_minus = [0.23 0.21 0.08; 0.02 0.18 0.35; 0.08 0.84 0.06; 0.43 0.25 0.05];
  10.  
  11. E_i_set = {
  12. [0.89 0.03 0.08; 0.42 0.18 0.35; 0.08 0.02 0.89; 0.80 0.05 0.11],
  13. [0.23 0.11 0.64; 0.03 0.13 0.82; 0.73 0.08 0.15; 0.73 0.14 0.10],
  14. [0.52 0.05 0.26; 0.04 0.10 0.85; 0.68 0.06 0.26; 0.43 0.25 0.13],
  15. [0.74 0.10 0.16; 0.02 0.05 0.89; 0.08 0.84 0.06; 0.85 0.05 0.09],
  16. [0.68 0.21 0.08; 0.05 0.06 0.87; 0.13 0.09 0.75; 0.65 0.02 0.05]
  17. };
  18.  
  19. for i = 1:length(E_i_set)
  20. E_i = E_i_set{i};
  21. dgs_plus = zeros(size(E_i, 1), 1);
  22. dgs_minus = zeros(size(E_i, 1), 1);
  23.  
  24. for j = 1:size(E_i, 1)
  25. p = E_i(j, :) ./ sum(E_i(j, :));
  26. q_plus = E_plus(j, :) ./ sum(E_plus(j, :));
  27. q_minus = E_minus(j, :) ./ sum(E_minus(j, :));
  28.  
  29. m_plus = (p + q_plus) / 2;
  30. dgs_plus(j) = 0.5 * (kl_divg(p, m_plus) + kl_divg(q_plus, m_plus));
  31.  
  32. m_minus = (p + q_minus) / 2;
  33. dgs_minus(j) = 0.5 * (kl_divg(p, m_minus) + kl_divg(q_minus, m_minus));
  34. end
  35.  
  36. closeness_coefficient = mean(dgs_minus) / (mean(dgs_plus) + mean(dgs_minus));
  37. fprintf('E%d: DGS(Ei, E+) = %.4f, DGS(Ei, E-) = %.4f, Closeness Coefficient = %.4f\n', i, mean(dgs_plus), mean(dgs_minus), closeness_coefficient);
  38. end
  39.  
  40.  
Success #stdin #stdout 0.1s 46284KB
stdin
Standard input is empty
stdout
E1: DGS(Ei, E+) = 0.1575, DGS(Ei, E-) = 0.2947, Closeness Coefficient = 0.6517
E2: DGS(Ei, E+) = 0.1675, DGS(Ei, E-) = 0.1993, Closeness Coefficient = 0.5433
E3: DGS(Ei, E+) = 0.1312, DGS(Ei, E-) = 0.1868, Closeness Coefficient = 0.5875
E4: DGS(Ei, E+) = 0.2395, DGS(Ei, E-) = 0.0767, Closeness Coefficient = 0.2425
E5: DGS(Ei, E+) = 0.1929, DGS(Ei, E-) = 0.1989, Closeness Coefficient = 0.5077