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