1% x = SamplesFromPH(alpha, A, K, prec)
3% Generates random samples from a phase-type distribution.
7% alpha : matrix, shape (1,M)
8% The initial probability vector of the phase-type
10% A : matrix, shape (M,M)
11% The transient generator matrix of the phase-type
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.
21% x : vector, length(K)
22% The vector of random samples
24function x = SamplesFromPH(a,A,k)
26 global BuToolsCheckInput;
27 if isempty(BuToolsCheckInput)
28 BuToolsCheckInput =
true;
31 if BuToolsCheckInput && ~CheckPHRepresentation(a,A)
32 error(
'SamplesFromPH: input isn''t a valid PH representation!');
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);
48 % draw initial distribution
51 while cummInitial(state)<=r
55 % play state transitions
57 time = time - log(rand()) * sojourn(state);
60 while nextpr(state,nstate)<=r