Obtaining the stationary distribution for a Markov Chain using eigenvectors from large matrix in MATLAB

ぐ巨炮叔叔 提交于 2021-01-28 04:04:45

问题


I am trying to find the stationary distribution of a Markov chain. I have a transition probability matrix (TPM). Here is the code:

[V, D] = eigs(double(TPM'),1); 
Py = abs(V)/sum(V);

My problem is that sum(V) < 0 so it gives me some negative values in the vector Py. I've tested the code using another probability matrix and it gives sum(V) > 0.

I don't know what is the problem, is it the TPM or the code I am using?

EDIT:

Here is a more "elaborated" version of the code (including the answer of "SecretAgentMan", see method 3):

function [Py1, Py2, Py3] = testStatDist(TPM)

% method 1
[V1, D1] = eigs(double(TPM'),1);

% method 2
[V2, D2] = iterator(TPM);

% method 3
V3 = null(eye(size(TPM))-TPM.'); 

if(abs(D1 - 1) > 1e-5)
    warning(0, 'testStatDist.m: Check Transition Matrix');
    exit;
end

if(abs(D2 - 1) > 1e-5)
    warning(0, 'testStatDist.m: Check Transition Matrix');
    exit;
end

% res 1
Py1 = abs(V1)/sum(V1);

% res 2
Py2 = abs(V2)/sum(V2);

% res 3
Py3 = abs(V3)/sum(V3);
end

Here is the function "iterator":

function [V, D] = iterator(TPM)

pi(1:length(TPM)) = 1/length(TPM);
error = 1;
budget = 1000;
while(error >= 1e-10)
    pi2 = pi*TPM;
    error = sum((pi - pi2).^2);
    pi = pi2;
    budget = budget - 1;
    if(budget == 0)
        break;
    end
end
V = pi';
D = 1;

And here is one of the TPMs that I am using:

TPM = [0.987665198237886    0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0.000881057268722467    0.985682819383260   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0.00154185022026432 0.00176211453744493 0.983700440528634   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0.988986784140969   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0.00440528634361233 0   0   0   0.983259911894273   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0.00440528634361233 0   0   0.000881057268722467    0.981277533039648   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0.00440528634361233 0   0.00154185022026432 0.00176211453744493 0.979295154185022   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0.00440528634361233 0   0   0   0.984581497797357   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0.00881057268722467 0   0   0   0.978854625550661   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0.00881057268722467 0   0   0.000881057268722467    0.976872246696035   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0.00881057268722467 0   0.00154185022026432 0.00176211453744493 0.974889867841410   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0.00881057268722467 0   0   0   0.980176211453745   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0.0132158590308370  0   0   0   0.974449339207049   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0.0132158590308370  0   0   0.000881057268722467    0.972466960352423   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0.0132158590308370  0   0.00154185022026432 0.00176211453744493 0.970484581497797   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0.0132158590308370  0   0   0   0.975770925110132   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0.0176211453744493  0   0   0   0.970044052863436   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0.0176211453744493  0   0   0.000881057268722467    0.968061674008811   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0176211453744493  0   0.00154185022026432 0.00176211453744493 0.966079295154185   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0176211453744493  0   0   0   0.971365638766520   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0220264317180617  0   0   0   0.965638766519824   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0220264317180617  0   0   0.000881057268722467    0.963656387665198   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0220264317180617  0   0.00154185022026432 0.00176211453744493 0.961674008810573   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0220264317180617  0   0   0   0.966960352422908   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0264317180616740  0   0   0   0.961233480176212   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0264317180616740  0   0   0.000881057268722467    0.959251101321586   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0264317180616740  0   0.00154185022026432 0.00176211453744493 0.957268722466960   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0264317180616740  0   0   0   0.962555066079295   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0308370044052863  0   0   0   0.956828193832599   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0308370044052863  0   0   0.000881057268722467    0.954845814977974   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0308370044052863  0   0.00154185022026432 0.00176211453744493 0.952863436123348   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0308370044052863  0   0   0   0.958149779735683   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0352422907488987  0   0   0   0.952422907488987   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0352422907488987  0   0   0.000881057268722467    0.950440528634361   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0352422907488987  0   0.00154185022026432 0.00176211453744493 0.948458149779736   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0352422907488987  0   0   0   0.953744493392071   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0396475770925110  0   0   0   0.948017621145375   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0396475770925110  0   0   0.000881057268722467    0.946035242290749   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0396475770925110  0   0.00154185022026432 0.00176211453744493 0.944052863436123   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0396475770925110  0   0   0   0.949339207048458   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0440528634361234  0   0   0   0.943612334801762   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0440528634361234  0   0   0.000881057268722467    0.941629955947137   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0440528634361234  0   0.00154185022026432 0.00176211453744493 0.939647577092511   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0440528634361234  0   0   0   0.944933920704846   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0484581497797357  0   0   0   0.939207048458150   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0484581497797357  0   0   0.000881057268722467    0.937224669603524   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0484581497797357  0   0.00154185022026432 0.00176211453744493 0.935242290748899   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0484581497797357  0   0   0   0.940528634361234   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0528634361233480  0   0   0   0.934801762114538   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0528634361233480  0   0   0.000881057268722467    0.932819383259912   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0528634361233480  0   0.00154185022026432 0.00176211453744493 0.930837004405286   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0528634361233480  0   0   0   0.936123348017621   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0572687224669604  0   0   0   0.930396475770925   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0572687224669604  0   0   0.000881057268722467    0.928414096916300   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0572687224669604  0   0.00154185022026432 0.00176211453744493 0.926431718061674   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0572687224669604  0   0   0   0.931718061674009   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0616740088105727  0   0   0   0.925991189427313   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0616740088105727  0   0   0.000881057268722467    0.924008810572687   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0616740088105727  0   0.00154185022026432 0.00176211453744493 0.922026431718062   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0616740088105727  0   0   0   0.927312775330397   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0660792951541850  0   0   0   0.921585903083701   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0660792951541850  0   0   0.000881057268722467    0.919603524229075   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0660792951541850  0   0.00154185022026432 0.00176211453744493 0.917621145374449   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0660792951541850  0   0   0   0.922907488986784   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0704845814977974  0   0   0   0.917180616740088   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0704845814977974  0   0   0.000881057268722467    0.915198237885463   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0704845814977974  0   0.00154185022026432 0.00176211453744493 0.913215859030837   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0704845814977974  0   0   0   0.918502202643172   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0748898678414097  0   0   0   0.912775330396476   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0748898678414097  0   0   0.000881057268722467    0.910792951541850   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0748898678414097  0   0.00154185022026432 0.00176211453744493 0.908810572687225   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0748898678414097  0   0   0   0.914096916299560   0   0   0   0.0110132158590308  0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0792951541850220  0   0   0   0.908370044052864   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0792951541850220  0   0   0.000881057268722467    0.906387665198238   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0792951541850220  0   0.00154185022026432 0.00176211453744493 0.904405286343612   0.00198237885462555 0   0   0.0110132158590308  0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0792951541850220  0   0   0   0.909691629955947   0   0   0   0.0110132158590308  0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0837004405286344  0   0   0   0.903964757709251   0.000220264317180617    0.000440528634361233    0.000660792951541850    0.0110132158590308  0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0837004405286344  0   0   0.000881057268722467    0.901982378854626   0.00110132158590308 0.00132158590308370 0   0.0110132158590308  0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0837004405286344  0   0.00154185022026432 0.00176211453744493 0.900000000000000   0.00198237885462555 0   0   0.0110132158590308  0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0837004405286344  0   0   0   0.905286343612335   0   0   0   0.0110132158590308
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0881057268722467  0   0   0   0.910572687224670   0.000220264317180617    0.000440528634361233    0.000660792951541850
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0881057268722467  0   0   0.000881057268722467    0.908590308370044   0.00110132158590308 0.00132158590308370
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0881057268722467  0   0.00154185022026432 0.00176211453744493 0.906607929515419   0.00198237885462555
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0881057268722467  0   0   0   0.911894273127753];

Question 1:

Each of the 3 methods yields different result and I am not sure which one to use. For the iterator method, I know it's an approximation that can be used in the case of big TPMs.

Why there is such a difference in the values?

Question 2:

Why I am getting negative values in the stationary distribution? I used a bigger TPM of size 2600x2600 and I am not getting any negative values, but it's difficult to put 2600x2600 matrix here.


回答1:


No Toolbox Required:
We know the steady state probabilities are the unique solution to equations below for a Discrete Time Markov Chain (DTMC).

You can rewrite this to show that this distribution must lie in the nullspace of the transition probability matrix (see below). This permits using MATLAB's null().

Normalizing the sum gives the unique solution and satisfies the other equation.

P = [.2 .3 .3 0 .2;
    .1 .2 .3 .4 0;
    0 .5 .4 .1 0;
    .2 .2 .2 .3 .1;
    .5 .2 .1 .1 .1];
ssp0 = null(eye(size(P))-P.');
SteadyStateProb = ssp0./sum(ssp0)          % [0.1263 0.3017 0.2969 0.2224 0.0528]

Notice the interim ssp0 has negative values yet the final answer, SteadyStateProb, is correct in that all values are valid probabilities and all probabilities sum to 1.


Update based on OP's edit: Conditioning the Matrix

One of the problems with your transition probability matrix, the 84×84 sized TPM in your edit, is that the row sum is greater than one. You can verify this with find(sum(TPM,2) > 1) to get the row indices where this occurs. Further, this "overage" is very small: max(sum(TPM,2))-1 shows the largest row sum exceeds 1 by 4.4409e-16 with for me is twice the eps (2.2204e-16).

Using the very low-tech code below, I normalized each row.

% Re-Normalize:  Divide the kth row by the sum of that row
TPM2 = TPM;
for k = 1:size(TPM2,1)
    TPM2(k,:) = TPM2(k,:)/sum(TPM2(k,:));   
end

This results in no row sum exceeding one; you can verify with find(sum(TPM2,2) > 1) which returns a 0×1 empty double column vector.

Without further massaging the matrix we can obtain the distribution in steady state.

ssp0 = null(eye(size(TPM2))-TPM2.');
SteadyStateProb_TPM2 = ssp0./sum(ssp0);

Notice the min(SteadyStateProb_TPM2) is -9.6502e-16 (zero!) and the sum is essentially 1 (abs(1-sum(SteadyStateProb_TPM2))).

I would now just force non-negativity.

SteadyStateProb_TPM2(SteadyStateProb_TPM2<0) = 0; 

Compare to an iterative approximation:
We know that TPM2^n converges to SteadyStateProb_TPM2 as n gets large enough. So let's check it.

We multiply the transition matrix by itself many times (here we get the 2500 step transition matrix). We can see it seems to be converging more or less on target.

Approx = TPM2^500;
Approx = Approx*Approx*Approx*Approx*Approx; 

ind = find(SteadyStateProb_TPM2 > 0.05); % find states with steady state prob > 0.05

SteadyStateProb_TPM2(ind).'
Approx(1:6,ind)

Note the states are [4 8 12 16 20 24].

ans =

0.0821    0.2052    0.2565    0.2138    0.1336    0.0668

ans =

0.0727    0.1818    0.2273    0.1894    0.1184    0.0592
0.0760    0.1900    0.2374    0.1979    0.1237    0.0618
0.0763    0.1908    0.2385    0.1988    0.1242    0.0621
0.0821    0.2052    0.2565    0.2138    0.1336    0.0668
0.0727    0.1818    0.2273    0.1894    0.1184    0.0592
0.0760    0.1900    0.2374    0.1979    0.1237    0.0618

Depending on your application, you may consider this iteration method good enough, but I wouldn't use it when the direct analytic methods are still viable. I imagine a 2600×2600 matrix is doable in MATLAB if you take time to ensure it is well-conditioned.

Update re: OP's comment: What about the eigenvalue method?

It works fine.

% Using the 5x5 P matrix from above
[V,D] = eigs(double(P.'),1);
Py = -abs(V)/sum(V)

Or alternatively,

Pyalt = V./sum(V)

You can verify this works on the 84x84 as well:

[V2,D2] = eigs(double(TPM2'),1);
Py_TPM2 = -abs(V2)/sum(V2);
Py_TPM2alt = V2./sum(V2); 

% Evaluate to compare
SteadyStateProb_TPM2(ind).'         
Py_TPM2(ind).'
Py_TPM2alt(ind).'

which gives the following. Note the states are [4 8 12 16 20 24].

SteadyStateProb_TPM2(ind).' = 0.0821 0.2052 0.2565 0.2138 0.1336 0.0668
Py_TPM2(ind).' = 0.0821 0.2052 0.2565 0.2138 0.1336 0.0668
Py_TPM2alt(ind).' = 0.0821 0.2052 0.2565 0.2138 0.1336 0.0668



来源:https://stackoverflow.com/questions/59950322/obtaining-the-stationary-distribution-for-a-markov-chain-using-eigenvectors-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!