-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeGroot.m
61 lines (48 loc) · 1.26 KB
/
DeGroot.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
% DeGroot_1974_examples
% Written by Michael Hatcher
% Based on DeGroot (1974), Reaching a Consensus, JASA
clc; clear; close all
% Calibration
N = 50; %No. of simulated periods
%Example 1
%T = [0.5 0.5; 0.25 0.75]; p = [0; 1];
%Example 2
%T = [0.5 0.5 0; 0.25 0.75 0; 1/3 1/3 1/3]; p = [0; 1; 0.5];
%Example 3
%T = [0.5 0.5 0 0; 0.5 0.5 0 0; 0 0 0.5 0.5; 0 0 0.5 0.5];
%p = [0 1; 0.5; 0.2];
%Example 4
%T = [1 0; 0 1]; p = [0; 1];
%Example 5
n = 8; %Size of network (Ex. 5)
T0 = rand(n); p = rand(n,1);
for i=1:n
for j=1:n
T(i,j) = T0(i,j)/sum(T0(i,1:n));
end
end
pnew = p; I = eye(length(p));
vec1 = ones(1,length(p));
vec0 = zeros(length(p),1);
% Stationary prob. vector (weights in final consensus)
w = transpose([ T'-I; vec1 ] \ [ vec0; 1 ]);
p_end = w*pnew; %Terminal belief vector
for t=1:N
pnew = T*pnew;
for i=1:length(p)
pe(i,t) = pnew(i); %Dynamics
end
end
disp('Terminal belief vector is')
pnew
Check = abs(p_end - pnew);
if sum(Check)/length(Check) < 1e-10
disp('Stationary prob vector is')
w
end
pe = [p pe]; %Inc. initial belief in plot
figure(1)
for i=1:length(pnew)
plot(pe(i,1:N+1)), hold on
end
title('Individual beliefs'), xlabel('Time')