function [b,A,C,dd] = is_markov(P,m,n) % is the distribution P over S^n a Markov one? % that is, is P(X(t)|X(1:t-1)) = P(X(t)|X(t-1))? % if yes, returns the Markov kernels A tol = 1e-8; A = pnormdim(ones(m,m,n-1),1); C = pnormdim(ones(m,1, 1),1); dd = []; b = 1; for t=2:n prefdim = repmat([m],[1 t-1]); for ipref = 1:m^(t-1) long = ind2coord(ipref,prefdim); shrt = long(end); plong = getPX(P,m,n,long,1:t-1) + realmin; pshrt = getPX(P,m,n,shrt, t-1) + realmin; for Xt = 1:m pjlong = getPX(P,m,n,[long Xt], 1:t); pjshrt = getPX(P,m,n,[shrt Xt],t-1:t); full = pjlong / plong; part = pjshrt / pshrt; %if abs(full-part) > tol % b = 0; % dif = abs(full-part) % return %end dd(end+1) = abs(full-part); %if dd(end)>.01 % keyboard %end A(Xt,shrt,t-1) = full; end end end % compute the initial prob C for x1 = 1:m C(x1) = getPX(P,m,n,x1,1); end b = (max(dd)