LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
SamplesFromPH.m
1% x = SamplesFromPH(alpha, A, K, prec)
2%
3% Generates random samples from a phase-type distribution.
4%
5% Parameters
6% ----------
7% alpha : matrix, shape (1,M)
8% The initial probability vector of the phase-type
9% distribution.
10% A : matrix, shape (M,M)
11% The transient generator matrix of the phase-type
12% distribution.
13% K : integer
14% The number of samples to generate.
15% prec : double, optional
16% Numerical precision to check if the input phase-type
17% distribution is valid. The default value is 1e-14.
18%
19% Returns
20% -------
21% x : vector, length(K)
22% The vector of random samples
23
24function x = SamplesFromPH(a,A,k)
25
26 global BuToolsCheckInput;
27 if isempty(BuToolsCheckInput)
28 BuToolsCheckInput = true;
29 end
30
31 if BuToolsCheckInput && ~CheckPHRepresentation(a,A)
32 error('SamplesFromPH: input isn''t a valid PH representation!');
33 end
34
35 % auxilary variables
36 N = length(a);
37 cummInitial = cumsum(a);
38 sojourn = -1./diag(A);
39 nextpr = diag(sojourn)*A;
40 nextpr = nextpr - diag(diag(nextpr));
41 nextpr = [nextpr, 1-sum(nextpr,2)];
42 nextpr = cumsum(nextpr,2);
43
44 x = zeros(k,1);
45 for n=1:k
46 time = 0;
47
48 % draw initial distribution
49 r = rand();
50 state = 1;
51 while cummInitial(state)<=r
52 state=state+1;
53 end
54
55 % play state transitions
56 while state<=N
57 time = time - log(rand()) * sojourn(state);
58 r = rand();
59 nstate = 1;
60 while nextpr(state,nstate)<=r
61 nstate = nstate+1;
62 end
63 state = nstate;
64 end
65 x(n) = time;
66 end
67end