error initial state defaulting to 1

3 views (last 30 days)
dpr
dpr on 4 Mar 2012
I have the following code, and when I run it I get:
initial state 0.4 0.6 is out of range
initial state defaulting to 1
What should I do? thanks
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 dimensionthe size of T
T = [ 0.85 0.15; 0.05 0.95 ];
s0 = [0.4 0.6];
n=100;
[r c]=size(T);
for k=1:r;
if sum(T(k,:)) ~= 1;
disp('error using markov function')
disp(['row ',num2str(k),' does not sum to one']);
disp(' it sums to :');
disp([ sum(T(k,:)) ]);
disp(['normalizing row ',num2str(k),'']);
T(k,:)=T(k,:)/sum(T(k,:));
end;
end;
V=[1:r];
[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;

Answers (1)

Daniel Shub
Daniel Shub on 4 Mar 2012
There is something wrong with what you have. It makes no sense for you function to take T, S0 and n as inputs only to overwrite them.
The simple solution is to change
s0 = [0.4 0.6];
to
s0 = [1 0.6];
or
if s0 < 1 |s0 > r;
to
if s0 < 0.4 |s0 > r;
While that will get rid of the warning, it may not be correct.

Categories

Find more on Regime-Switching Models in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!