-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_efficiency_wei.m
142 lines (119 loc) · 3.58 KB
/
resource_efficiency_wei.m
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function [Eres,prob_SPL] = resource_efficiency_wei(adj,lambda,SPL,M)
% RESOURCE_EFFICIENCY_BIN Resource efficiency and shortest-path probability
%
% [Eres,prob_SPL] = resource_efficiency_bin(adj,lambda,SPL,M)
%
% The resource efficiency between nodes i and j is inversly proportional
% to the amount of resources (i.e. number of particles or messages)
% required to ensure with probability 0 < lambda < 1 that at least one of
% them will arrive at node j in exactly SPL steps, where SPL is the
% length of the shortest-path between i and j.
%
% The shortest-path probability between nodes i and j is the probability
% that a single random walker starting at node i will arrive at node j by
% following (one of) the shortest path(s).
%
% Inputs:
%
% adj,
% Weighted or unweighted, undirected adjacency matrix
%
% lambda,
% Probability (0 < lambda < 1)
% set to NAN if computation of Eres is not desired
%
% SPL,
% Shortest-path length matrix (optional)
%
% M,
% Transition probability matrix (optional)
%
%
% Outputs:
%
% Eres,
% Resource efficiency matrix.
%
% prob_SPL,
% Shortest-path probability matrix
%
% Notes:
%
% Global measures for both Eres and prob_SPL are well defined and can
% be computed as the average across the off-diagonal elements:
% GEres = mean(Eres(~eye(N)>0));
% Gprob_SPL = mean(rob_SPL(~eye(N)>0));
%
%
% Reference: Goñi J, et al (2013) PLoS ONE
%
%
% Joaquin Goñi, IU Bloomington, 2012
% Modified: Dale Zhou, Chris Lynn, University of Pennsylvania, 2019
N = size(adj,1);
EYE = logical(eye(N,N));
flagResources = ~isnan(lambda);
if flagResources
if lambda<=0 || lambda>=1
error('p_req_values must be non-zero probabilities')
end
z = zeros(N,N);
end
if nargin<4
[~,SPL,~] = distance_wei_floyd(adj,'inv'); % retrieve edge count of shortest paths
end
if nargin<5
M = diag(sum(adj,2))\adj;
end
Lvalues = unique(SPL(:));
Lvalues = Lvalues(~(Lvalues==0));
prob_SPL = zeros(N,N); % a priori zero probability of going through SPL among nodes
for indexSPL=1:length(Lvalues) %for each possible value of SPL for current component
SPLvalue = Lvalues(indexSPL);
[~, hcols] = find(SPL==SPLvalue);
hvector = unique(hcols); clear hrows hcols
entries = SPL==SPLvalue;
if flagResources % compute Eres
[prob_aux,z_aux] = prob_first_particle_arrival(M,SPLvalue,hvector,lambda);
else % not compute Eres
[prob_aux] = prob_first_particle_arrival(M,SPLvalue,hvector,[]);
end
prob_aux(~entries) = 0;
prob_SPL = prob_SPL + prob_aux;
if flagResources
z_aux(~entries) = 0;
z = z + z_aux;
end
end
prob_SPL(EYE) = 0;
if flagResources
z(prob_SPL==1) = 1;
Eres = 1./z;
Eres(EYE) = 0;
else
Eres = nan;
end
function [prob,resources] = prob_first_particle_arrival(M,L,hvector,lambda)
N = size(M,1);
prob = zeros(N,N);
if nargin<4
hvector=1:N;
end
flagResources = ~isnan(lambda);
if flagResources
if lambda<=0 || lambda>=1
error('p_req_values must be non-zero probabilities')
end
resources = zeros(N,N);
end
for hindex=1:length(hvector) %for each destination node h
h = hvector(hindex);
B_h = M;
B_h(h,:) = 0; B_h(h,h) = 1; % h becomes absorbant state
B_h_L = B_h^L; % (U_j)^H
term = 1-B_h_L(:,h); % 1-(U_j)^H
prob(:,h)= 1-term;
if flagResources
resources(:,h) = repmat(log(1-lambda),N,1)./repmat(log(term),1);
end
end