-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathComputeStationaryP.m
executable file
·55 lines (48 loc) · 1.3 KB
/
ComputeStationaryP.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
%
% This function computes stationary probability(P) and (dP) of each node given
% transition probability matrix(Q) and partial derivatives of trantition
% matrix(dQ)
%
% input:
% Q: transiton probability matrix, size(n,n)
% dQ: partial derivatives of Q wrt parameters, size(n,n,m)
% output:
% P: stationary distribution of nodes, size(1,n)
% dP: partial derivatives of P, size(n,m)
%
function [P, dP] = ComputeStationaryP(Q, dQ)
epsilon = 1e-12;
n = length(Q);
if length(size(dQ))==3
m = size(dQ)(3);
else
m = 1;
end
P = zeros(1,n);
dP = zeros(n,m);
P += 1/n;
t = 0;
printf("computestationaryp.m calculating P\n");
do
t += 1;
Pnew = P;
for i = 1:n
Pnew(i) = P*Q(:,i);
end
Pnew /= sum(Pnew);
temp = P; P = Pnew;
until (Converged(P, temp, epsilon) || t>100)
printf("computestationaryp.m calculating dP\n");
for j = 1:m
k = 0;
dQj = dQ(:,:,j);
do
k += 1;
dpjnew = dP(:,j);
for i = 1:n
dpjnew(i) = (Q(:,i)' * dP(:,j)) + (P * dQj(:,i));
end
temp = dP(:,j); dP(:,j) = dpjnew;
until (Converged(dP(:,j), temp, epsilon) || k>100)
end
end