resource_efficiency_bin.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. function [Eres,prob_SPL] = resource_efficiency_bin(adj,lambda,SPL,M)
  2. % RESOURCE_EFFICIENCY_BIN Resource efficiency and shortest-path probability
  3. %
  4. % [Eres,prob_SPL] = resource_efficiency_bin(adj,lambda,SPL,M)
  5. %
  6. % The resource efficiency between nodes i and j is inversly proportional
  7. % to the amount of resources (i.e. number of particles or messages)
  8. % required to ensure with probability 0 < lambda < 1 that at least one of
  9. % them will arrive at node j in exactly SPL steps, where SPL is the
  10. % length of the shortest-path between i and j.
  11. %
  12. % The shortest-path probability between nodes i and j is the probability
  13. % that a single random walker starting at node i will arrive at node j by
  14. % following (one of) the shortest path(s).
  15. %
  16. % Inputs:
  17. %
  18. % adj,
  19. % Unweighted, undirected adjacency matrix
  20. %
  21. % lambda,
  22. % Probability (0 < lambda < 1)
  23. % set to NAN if computation of Eres is not desired
  24. %
  25. % SPL,
  26. % Shortest-path length matrix (optional)
  27. %
  28. % M,
  29. % Transition probability matrix (optional)
  30. %
  31. %
  32. % Outputs:
  33. %
  34. % Eres,
  35. % Resource efficiency matrix.
  36. %
  37. % prob_SPL,
  38. % Shortest-path probability matrix
  39. %
  40. % Notes:
  41. %
  42. % Global measures for both Eres and prob_SPL are well defined and can
  43. % be computed as the average across the off-diagonal elements:
  44. % GEres = mean(Eres(~eye(N)>0));
  45. % Gprob_SPL = mean(rob_SPL(~eye(N)>0));
  46. %
  47. %
  48. % Reference: Goñi J, et al (2013) PLoS ONE
  49. %
  50. %
  51. % Joaquin Goñi, IU Bloomington, 2012
  52. N = size(adj,1);
  53. EYE = logical(eye(N,N));
  54. flagResources = ~isnan(lambda);
  55. if flagResources
  56. if lambda<=0 || lambda>=1
  57. error('p_req_values must be non-zero probabilities')
  58. end
  59. z = zeros(N,N);
  60. end
  61. if nargin<4
  62. SPL = distance_wei_floyd(adj);
  63. end
  64. if nargin<5
  65. M = diag(sum(adj,2))\adj;
  66. end
  67. Lvalues = unique(SPL(:));
  68. Lvalues = Lvalues(~(Lvalues==0));
  69. prob_SPL = zeros(N,N); % a priori zero probability of going through SPL among nodes
  70. for indexSPL=1:length(Lvalues) %for each possible value of SPL for current component
  71. SPLvalue = Lvalues(indexSPL);
  72. [~, hcols] = find(SPL==SPLvalue);
  73. hvector = unique(hcols); clear hrows hcols
  74. entries = SPL==SPLvalue;
  75. if flagResources % compute Eres
  76. [prob_aux,z_aux] = prob_first_particle_arrival(M,SPLvalue,hvector,lambda);
  77. else % not compute Eres
  78. [prob_aux] = prob_first_particle_arrival(M,SPLvalue,hvector,[]);
  79. end
  80. prob_aux(~entries) = 0;
  81. prob_SPL = prob_SPL + prob_aux;
  82. if flagResources
  83. z_aux(~entries) = 0;
  84. z = z + z_aux;
  85. end
  86. end
  87. prob_SPL(EYE) = 0;
  88. if flagResources
  89. z(prob_SPL==1) = 1;
  90. Eres = 1./z;
  91. Eres(EYE) = 0;
  92. else
  93. Eres = nan;
  94. end
  95. function [prob,resources] = prob_first_particle_arrival(M,L,hvector,lambda)
  96. N = size(M,1);
  97. prob = zeros(N,N);
  98. if nargin<4
  99. hvector=1:N;
  100. end
  101. flagResources = ~isnan(lambda);
  102. if flagResources
  103. if lambda<=0 || lambda>=1
  104. error('p_req_values must be non-zero probabilities')
  105. end
  106. resources = zeros(N,N);
  107. end
  108. for hindex=1:length(hvector) %for each destination node h
  109. h = hvector(hindex);
  110. B_h = M;
  111. B_h(h,:) = 0; B_h(h,h) = 1; % h becomes absorbant state.
  112. B_h_L = B_h^L;
  113. term = 1-B_h_L(:,h);
  114. prob(:,h)= 1-term;
  115. if flagResources
  116. resources(:,h) = repmat(log(1-lambda),N,1)./repmat(log(term),1);
  117. end
  118. end