LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
map_sample.m
1function [SAMPLES,LAST,FIRST]=map_sample(MAP,nSamples,pi,seed)
2% [SAMPLES,LAST]=map_sample(MAP,NUM,S0, SEED) - Generate a random sample
3% of inter-arrival times
4%
5% Input:
6% MAP: a MAP in the form of {D0,D1}
7% NUM: number of samples to be generated
8% S0: phase where the MAP starts for generating the first sample (DEFAULT:
9% random). if it is a vector it is interpreted as the probability of
10% starting from a phase
11% SEED: seed used by the random number generator
12%
13%
14% Output:
15% SAMPLES: set of N inter-arrival time samples
16% LAST: vector of absorbing phases for each sample
17% FIRST: vector of entering phase for each sample
18%
19% Examples:
20% - [SAMPLES,LAST]=map_sample(MAP,10) - sample of 10 inter-arrival times
21% - [SAMPLES,LAST,FIRST]=map_sample(MAP,10,2) - sample of 10 inter-arrival
22% times starting from phase 2
23%
24% * WARNING: the script can be memory consuming and quite slow for
25% samples sizes greater than N=10000 *
26%
27
28if size(MAP{1})==1
29 SAMPLES=exprnd(map_mean(MAP),nSamples,1);
30 LAST=SAMPLES*0+1;
31 FIRST=SAMPLES*0+1;
32 return
33end
34if nargin<3
35 % pi=map_piq(MAP); % time stationary initialization
36 pi=map_pie(MAP); % interval stationary initialization
37end
38if nargin<4
39 seed=rand('seed');
40end
41% if length(pi)==1
42% pi=zeros(1,length(MAP{1}));
43% pi(initState)=1;
44% end
45%rand('seed',seed);
46r=rand();
47initState=min(find(r<=cumsum(pi)));
48RUNS=floor(nSamples/10000);
49LS=[initState];
50SAMPLES=[];
51LAST=[];
52FIRST=[];
53for i=1:RUNS
54 [S,L,F]=sub_map_sample(MAP,10000,LS);
55 SAMPLES(end+1:end+length(S),1)=S(:);
56 LAST(end+1:end+length(L),1)=L(:);
57 FIRST(end+1:end+length(L),1)=F(:);
58 LS = L(end);
59end
60[S,L,F]=sub_map_sample(MAP,mod(nSamples,10000),LS);
61SAMPLES(end+1:end+length(S),1)=S(:);
62LAST(end+1:end+length(L),1)=L(:);
63FIRST(end+1:end+length(L),1)=F(:);
64end
65
66function [SAMPLES,LAST,FIRST]=sub_map_sample(MAP,nSamples,initState)
67LAST=[];
68nStates=length(MAP{1});
69for b=0:1
70 for i=1:nStates
71 for j=1:nStates
72 p(i,b*nStates+j)=MAP{b+1}(i,j)/abs(MAP{1}(i,i));
73 end
74 end
75end
76for i=1:nStates
77 p(i,i)=0;
78end
79cdf=0*p;
80for i=1:nStates
81 cdf(i,:)=cumsum(p(i,:));
82end
83cdf=abs(cdf);
84curState=initState;
85visits=zeros(nSamples,20);
86maxpathlen=size(visits,2);
87for i=1:nSamples
88 arrival=0;
89 last=2;
90 visits(i,1)=curState;
91 while ~arrival
92 destState=find(cdf(curState,:)>=rand,1,'first');
93 if destState>nStates
94 arrival=1;
95 destState=destState-nStates;
96 curState=destState;
97 else
98 visits(i,last)=destState;
99 curState=destState;
100 last=last+1;
101 end
102 if last>maxpathlen
103 maxpathlen=maxpathlen+5;
104 visits(:,end+5)=0;
105 end
106 end
107end
108holdTimes=-1./diag(MAP{1});
109H=visits;
110for i=1:nSamples
111 LAST(i,1)=visits(i,max(find(visits(i,:))));
112end
113for i=1:nStates
114 H(find(visits==i))=holdTimes(i);
115end
116SAMPLES=0*H(:,1);
117for i=1:size(H,2)
118 SAMPLES=SAMPLES+exprnd(H(:,i));
119end
120FIRST=visits(:,1);
121end