Skip to content

Instantly share code, notes, and snippets.

@rileylev
Last active October 13, 2019 23:14
Show Gist options
  • Save rileylev/1082517f4003b25d9acbab925632ca01 to your computer and use it in GitHub Desktop.
Save rileylev/1082517f4003b25d9acbab925632ca01 to your computer and use it in GitHub Desktop.
Calculates the mean hitting time of a state in a homogenous markov chain
function K = fixedPoint(M)
[r c] = size(M);
assert(r==c);
I = eye(r);
K = -rref(I-M)(:,r);
K(r)=1;
assert(norm(M*K-K)<.0001*norm(K));
end
function p = projectFromPRn(X)
assert(X(1)!=0);
X/=X(1);
p=X(2:end);
end
function k = meanHittingTime(P,destination)
[r c] = size(P);
assert(r==c);
N = r+1;
M = [ones(N,1) [zeros(1,r); P]];
M(:,destination+1)=zeros(1,N);
k = projectFromPRn(fixedPoint(M));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment