Professional Documents
Culture Documents
Professor S. Lall
0.3 0 0.1 0
P =
0.3 0
0 0.4
0
0
0.3 0
0.3 0.3
.
0 0.3
0.1 0
0.3 0.4
This matrix is given in passage_time_data.m so you dont have to type it into MATLAB.
We will compute several things about this Markov chain. Let the destination set be
the single state E = {1}, and let the initial state be i = 2. Recall that the first passage
time to the set E is
E = min{t > 0 | xt E}
The kth passage time for a Markov chain is defined recursively as
(k)
(k1)
E = min{t > E
| xt E},
(1)
where E = E .
(a) For the Markov chain above, compute and plot the distribution of the first passage
time conditioned on the initial state, i.e., compute
f (t) = Prob(E = t | x0 = i)
as a function of t.
1
(b) One may calculate the distribution of the second passage time by constructing a
new Markov chain, whose first passage time is equal to the second passage time
of the original chain. What is the transition graph of this chain?
(c) For t = 1, 2, . . ., use your construction to compute the distribution of the second
passage time, which is
(2)
0.12
0.1
0.08
0.06
0.04
0.02
10
15
20
25
t
30
35
40
45
50
You can find the matlab code to calculate the first passage time below.
function fp = first_passage(P,E,x0,T)
n = size(P,1);
% make E absorbing
P_a = P;
P_a(E,:) = 0; P_a(E,E) = 1;
% first passage time using distribution propagation
pi = zeros(T,n);
prob = zeros(T,1);
pi(1,x0) = 1;
for t = 1 : T
prob(t) = pi(t,E);
pi(t+1,:) = pi(t,:)*P_a;
end
fp = prob - [0; prob(1:end-1)];
(b) We will construct a Markov chain whose first passage time gives the second passage
time of the original Markov chain as follows. We replicate the Markov chain, so
now we have 2n states. Then, link each state i E in the first copy to the
corresponding nodes j in the second copy, as dictated by Pij . We will denote the
second copy of E by E2 , i.e., E2 = {i + n|i E}. The first passage time through
E2 for the newly constructed Markov chain will give the second passage time of
the original Markov chain.
The transition probability matrix for this Markov chain is
Pi,j
if 1 i, j n
Pi,jn
if i E, n + 1 j 2n
(2)
Pij =
P
if
n + 1 i, j 2n
in,jn
0
otherwise
To calculate the first passage time of this Markov chain, we will need to use the
method of part (a).
(c) The distribution is plotted below.
0.06
0.05
0.04
0.03
0.02
0.01
10
20
30
40
50
t
60
70
80
90
100
(d) Below see the plots of f (t) + s(t), as well as Prob(xt E) for t = 0, . . . , 100. We
notice that f (t) + s(t) = Prob(xt E) for the first few time steps, after which it
seems to have converged to a value around 0.175.
0.18
0.16
fpluss
probofxine
0.14
0.12
0.1
0.08
0.06
0.04
0.02
0
10
20
30
40
50
60
70
80
90
100
(e) Lets denote the first passage time starting from x0 = i by fi (t). Then,
r(t) =
Prob(E = t|x0 E)
0
t=0
= Pn
P
(E,
i)f
(t
1)
t
>0
i
i=1
The distribution of recurrence time, r(t), is shown below.
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
10
20
30
40
50
t
60
70
80
90
100
s(t)
f(t)*r(t)
0.05
0.04
0.03
0.02
0.01
10
20
30
40
50
60
70
80
90
100
(g) To compute the kth passage time, we would need to make k copies of the Markov
chain, so the number of states would be nk. We would connect the nodes in the
5
E for the ith copy to the corresponding nodes in the (i + 1)th copy. Calculating
the first passage time involves evaluating t P for t = 0, 1, . . ., which scales with
O(k 2 ) as k grows.
You can find the matlab implementation of parts (a)-(f) below.
clear all;
close all;
passage_time_data;
E = 1; % destination node
x0 = 2; % initial state
do_print = 1;
%% part a
T = 200;
fp = first_passage(P,E,x0,T);
plot([0:T-1], fp, b.-, LineWidth, 1, MarkerSize,16)
set(gca,xlim, [0 50])
xlabel(t)
ylabel(f)
if do_print
saveas(gcf,../figures/passage_1.eps, psc2);
end
%% part c
% second passage time
n = 6;
P2 = zeros(2*n,2*n);
P2(1:n,1:n) = P;
P2(n+1:2*n,n+1:2*n) = P;
P2(E,:) = [zeros(1,n) P(E,:)]; % make links from E to the second copy
E2 = E+n;
sp = first_passage(P2,E2,x0,T);
figure;
plot([0:T-1], sp, .-, LineWidth, 1, MarkerSize,16);
set(gca,xlim, [0 100])
xlabel(t)
ylabel(s)
if do_print
saveas(gcf,../figures/passage_2.eps, psc2);
end
%% part d
6
% Prob(x_t\in E | x_0)
pi = zeros(T,n);
pi(1,x0) = 1;
for t = 1:T-1
pi(t+1,:) = pi(t,:)*P;
end
figure;
plot([0:T-1], fp+sp, .-, LineWidth, 1, MarkerSize,16)
hold on
plot([0:T-1], pi(:,E), ro-, LineWidth, 1);
set(gca,xlim, [0 100]);
leg = legend(fpluss,probofxine);
set(leg,FontSize, 20);
if do_print
saveas(gcf,../figures/passage_fs_vs_prob.eps, psc2);
end
%% part e: recurrence time
%
loop over next states
r = zeros(T,1);
for i = 1 : n
if (P(E,i) ~= 0)
fp_i = first_passage(P, E, i, T);
r = r + P(E,i)*[0 ;fp_i(1:end-1)];
end
end
figure;
plot([0:T-1], r, .-, LineWidth, 1, MarkerSize,16);
hold on
set(gca,xlim, [0 100]);
xlabel(t)
ylabel(r)
if do_print
saveas(gcf,../figures/passage_recurrence.eps, psc2);
end
%% part f
figure;
plot([0:T-1], sp, b.-, LineWidth, 1, MarkerSize,16);
hold on
c= conv(fp,r);
7
*/
*/
*/
*/
function R = reachable_states(A)
For reference, our implementation of reachable_states is less than twenty lines.
Hint. The function find if you are using MATLAB may be useful.
(b) We write i j if i j and j i, and we define the communication matrix
C Rnn such that
(
1 i = j or i j,
Cij =
0 otherwise.
Explain how to construct the communication matrix from the reachability matrix.
(c) We define the transience vector t Rn such that
(
1 i is a transient state,
ti =
0 otherwise.
Explain how to construct the transience vector from the communication and reachability matrices.
(d) Write a function that implements algorithm 2.
L Empty List /* L will contain sorted nodes
S Set of all nodes with no incoming edges
while S is non empty do
Remove a node n from S
Add n to tail of L
for each node m with edge e from n to m do
Remove edge e from graph
if m has no other incoming edges then
Insert m into S
end
end
end
if Graph has edges then
return error: graph has at least one cycle
else
return L /* L a topologically sorted order
end
Algorithm 2: Computing topological sort
Use the following function header.
function L = topological_sort(A)
For reference, our implementation of topological_sort is less than twenty lines.
(e) Suppose that duplicate rows of C have been removed, and the rows have been
sorted so that the first nt rows represent the transient classes. (You need to
write code to do this; the commands unique and sort may be useful.) Note
that there is now a unique row of C corresponding to each class. For two classes
9
*/
*/
10
Rij >
j=1
n
X
Cij .
j=1
This condition says that we can reach more states from i than there are states
that communicate with i; this is what it means for i to be transient. We can use
this condition to construct t from R and C.
(d) An example implementation of topological_sort is given below.
function L = topological_sort(A)
L = [];
S = find(sum(A) == 0);
while ~isempty(S)
n = S(1);
S = S(2:end);
L = [L n];
for m = find(A(n,:))
A(n,m) = 0;
if sum(A(:,m)) == 0
S = [S m];
end
end
end
if any(A(:))
L = [];
end
end
(e) Let x and y be fixed representatives of Ci and Cj , respectively. We claim that
(At )ij = 1 if and only if Rxy = 1. The definition of At says that (At )ij = 1 if and
only if there exist x Ci and y Cj such that x y. Then, we have that
x x y y,
so the properties of the communication relation imply that x y if and only if
x y. This proves the claim.
(f) The following script uses the tools and observations developed above to compute
the class decomposition of a probability-transition matrix.
clear all; close all; clc;
class_decomposition_data;
R = reachable_states(P > 0);
C = (R & R) | eye(n);
11
12
20
40
60
80
100
120
20
40
60
nz = 1527
13
80
100
120
3. Markov web surfing model. A set of n web pages labeled 1, . . . , n contain (directed)
links to other pages. We define the link matrix L Rnn as
1 if page i links to page j
Lij =
0 otherwise.
We define o Rn as o = L1, which gives the number of outgoing links from each page.
A very crude model of a web surfer is a Markov chain on the pages, with transitions
described as follows. The model includes a parameter (0, 1), which (roughly) gives
the probability that the surfer follows a link from the current page. For a page with
oi > 0 (i.e., with at least one outgoing link) the surfer moves to each of the linkedto pages with probability /oi , and jumps to a page not linked to i with probability
(1 )/(n oi 1). For a page with no outgoing links (i.e., oi = 0) the surfer jumps
to a random page, chosen from a uniform distribution on (the other) pages.
We will assume that web surfer starts at a random page, uniformly distributed.
We earn a payment when the surfer follows (i.e., clicks on) a link, given by Rij 0.
This payment matrix satisfies Rij = 0 when Lij = 0 (i.e., we are not paid for random
jumps; only following links).
The following questions concern the specific instance of the problem with data given in
link_matrix_data.m.
(a) What is the most likely page the surfer is on, at time t = 10? at t = 100?
(b) Let J denote the expected total payment over t = 0, . . . , 50. Compute J three
ways:
Monte Carlo simulation (which gives an estimate of J, not the exact value).
Distribution propagation.
Value iteration.
Be sure to check that the values are consistent.
Remark. The Markov model described in this problem leads to Googles famous PageRank, which corresponds to the fraction of time spent at each site, when T . (The
current version of PageRank is based on far more than just the link topology, but the
first versions really did make heavy use of the Markov surfing model.)
Solution:
(a) To find the most likely page the surfer is on, we will calculate the state distributions
at times t = 10 and t = 100 by distribution propagation. The most likely page
at t = 10 is page 96, and the most likely page at t = 100 is page 83. Below you
can see a plot of the most likely page for all time t = 0, . . . , 100. Note that for
t = 1, all pages are equally likely to be visited, so the choice of page 1 in the plot
is arbitrary.
14
100
90
80
70
pmax
60
50
40
30
20
10
10
20
30
40
50
t
60
70
80
90
100
(b) We check the expected total payment using the three methods. We get J =
22.1335 using value iteration, J = 22.1335 using distribution propagation, and
J = 22.0188 using Monte Carlo simulation.
The code is shown below.
clear all; close all;
link_matrix_data;
% create the probability transition matrix
P = zeros(n,n);
for i = 1 : n
n_o = sum(L(i,:));
if n_o > 0
P(i,:) = (1-theta)/(n-n_o-1);
P(i,L(i,:)>0) = theta/n_o;
else
P(i,:) = 1/(n-1);
end
P(i,i) = 0;
end
% distribution propagation
T = 101;
pi = zeros(T,n);
pi(1,:) = 1/n;
exp_g = zeros(T,1);
for t = 1 : T
15
pi(t+1,:) = pi(t,:)*P;
exp_g(t) = sum(pi(t,:)*(R.*P));
end
% most likely page at t
max_prob = zeros(T,1);
for t = 1:T
[xx max_prob(t)] = max(pi(t,:));
end
stairs(0:T-1,max_prob,LineWidth, 2);
xlabel(t); ylabel(pmax);
set(gca,Ylim, [1 n]);
saveas(gcf,../figures/markovian_pagerank_ml_page.eps, psc2);
% cost over 50 time steps using distribution propagation
T = 51;
J_distprop = sum(exp_g(1:T));
% value iteration
V = zeros(n,T+1);
V(:,T+1) = 0;
for t = T:-1:1
V(:,t) = sum(P.*R,2) + P*V(:,t+1);
end
J_valiter = mean(V(:,1));
% monte carlo
N = 1000;
cost = zeros(N,1);
for iter = 1 : N
x = randsample(n,1,true,(1/n)*ones(n,1));
cost(iter) = 0;
for t = 1 : T
y = randsample(n,1,true,P(x,:));
cost(iter) = cost(iter) + R(x,y);
x = y;
end
end
J_mc = mean(cost);
16