How can I get this markov code working?

I am trying to replicate this code however I always receive an error "not enough input arguments (Error in line 27: [r c]=size(T);). T is defined so is s0,n and V. Can anyone help?
function [chain,state]=markov(T,n,s0,V);
%function [chain,state]=markov(T,n,s0,V);
% chain generates a simulation from a Markov chain of dimension
% the size of T
%
% T is transition matrix
% n is number of periods to simulate
% s0 is initial state
% V is the quantity corresponding to each state
% state is a matrix recording the number of the realized state at time t
%
%
[r c]=size(T);
if nargin == 1;
V=[1:r];
s0=1;
n=100;
end;
if nargin == 2;
V=[1:r];
s0=1;
end;
if nargin == 3;
V=[1:r];
end;
%
%if r ~= c;
% disp('error using markov function');
% disp('transition matrix must be square');
% return;
%end;
%
%for k=1:r;
% if sum(T(k,:)) ~= 1;
% disp('error using markov function')
% disp(['row ',num2str(k),' does not sum to one']);
% disp(['normalizing row ',num2str(k),'']);
% T(k,:)=T(k,:)/sum(T(k,:));
% end;
%end;
[v1 v2]=size(V);
if v1 ~= 1 |v2 ~=r
disp('error using markov function');
disp(['state value vector V must be 1 x ',num2str(r),''])
if v2 == 1 &v2 == r;
disp('transposing state valuation vector');
V=V';
else;
return;
end;
end
if s0 < 1 |s0 > r;
disp(['initial state ',num2str(s0),' is out of range']);
disp(['initial state defaulting to 1']);
s0=1;
end;
%
%T
%rand('uniform');
X=rand(n-1,1);
s=zeros(r,1);
s(s0)=1;
cum=T*triu(ones(size(T)));
%
for k=1:length(X);
state(:,k)=s;
ppi=[0 s'*cum];
s=((X(k)<=ppi(2:r+1)).*(X(k)>ppi(1:r)))';
end;
chain=V*state;
if true
% code
end

7 件のコメント

Dennis
Dennis 2018 年 7 月 9 日
function works for me, how do you call it?
Vanessa Kunzmann
Vanessa Kunzmann 2018 年 7 月 9 日
Whenever I call it with input variables it shows that state is not defined. When only calling the code, in line "[r c]=size(T);" occurs the error...
Dennis
Dennis 2018 年 7 月 9 日
does this work?
markov(randi(3,3,3),50,3,1:3)
Vanessa Kunzmann
Vanessa Kunzmann 2018 年 7 月 9 日
it does work, however I receive this message:
error using markov function
row 1 does not sum to one
normalizing row 1
error using markov function
row 2 does not sum to one
normalizing row 2
error using markov function
row 3 does not sum to one
normalizing row 3
Dennis
Dennis 2018 年 7 月 9 日
編集済み: Dennis 2018 年 7 月 9 日
In the code you have shown the line that produces that output is commented. Did your remove the % ?
And are the rows of T supposed to sum to 1? There might be a difference between no error message and working as intended.
Vanessa Kunzmann
Vanessa Kunzmann 2018 年 7 月 9 日
Thanks for all the help! Removed the % but still cant get over the line [r c]=size(T);. And yes the Transition Probabilities have to sum up to one.
Dennis
Dennis 2018 年 7 月 9 日
Thats actually the first line of your function, and it does not work because it does not know T. Thats why i asked how you call markov function.
My best guess is that your are somehow trying to use markov() without passing parameters.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParticle & Nuclear Physics についてさらに検索

質問済み:

2018 年 7 月 9 日

コメント済み:

2018 年 7 月 9 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by