LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
RandomPH.m
1% [alpha, A] = RandomPH(order, mean, zeroEntries, maxTrials, prec)
2%
3% Returns a random phase-type distribution with a given
4% order.
5%
6% Parameters
7% ----------
8% order : int
9% The size of the phase-type distribution
10% mean : double, optional
11% The mean of the phase-type distribution
12% zeroEntries : int, optional
13% The number of zero entries in the initial vector,
14% generator matrix and closing vector
15% maxTrials : int, optional
16% The maximum number of trials to find a proper PH
17% (that has an irreducible phase process and none of
18% its parameters is all-zero). The default value is
19% 1000.
20% prec : double, optional
21% Numerical precision for checking the irreducibility.
22% The default value is 1e-14.
23%
24% Returns
25% -------
26% alpha : vector, shape (1,M)
27% The initial probability vector of the phase-type
28% distribution.
29% A : matrix, shape (M,M)
30% The transient generator matrix of the phase-type
31% distribution.
32%
33% Notes
34% -----
35% If the procedure fails, try to increase the 'maxTrials'
36% parameter.
37
38function [alpha, A] = RandomPH(order, mean, zeroEntries, maxTrials, prec)
39
40 if ~exist('zeroEntries','var')
41 zeroEntries = 0;
42 end
43
44 if ~exist('prec','var')
45 prec = 1e-7;
46 end
47
48 if ~exist('mean','var')
49 mean = 1;
50 end
51
52 if ~exist('maxTrials','var')
53 maxTrials = 1000;
54 end
55
56 if zeroEntries > (order+1)*(order-1)
57 error('RandomPH: Too many zero entries requested! Try to decrease the zero entries number!');
58 end
59
60 % distribute the zero entries among the rows
61 function o = allZeroDistr (states, zeros)
62 if states==1
63 o = zeros;
64 else
65 o = [];
66 for iz=0:zeros
67 x = allZeroDistr (states-1, zeros-iz);
68 for jz=1:size(x,1)
69 xt = sort([x(jz,:), iz]);
70 % check if we have it already
71 found = 0;
72 for kz=1:size(o,1)
73 if sum((o(kz,:)-xt).^2)==0
74 found = 1;
75 break;
76 end
77 end
78 if ~found
79 o = [o; xt];
80 end
81 end
82 end
83 end
84 end
85
86 zeroDistr = allZeroDistr(order, zeroEntries);
87
88 trials = 1;
89 while trials<maxTrials
90 % select a configuration from zeroDistr: it is a list describing the zero entries in each row
91 zdix = randperm(size(zeroDistr,1));
92 for k=1:size(zeroDistr,1)
93 zDistr = zeroDistr(zdix(k),:);
94 B = zeros(order,order+2);
95 for i=1:order
96 rp = randperm(order+1);
97 a = zeros(1,order+1);
98 for j=1:order+1-zDistr(i)
99 a(rp(j)) = rand();
100 end
101 B(i,1:i-1) = a(1:i-1);
102 B(i,i+1:end) = a(i:end);
103 end
104 % construct PH parameters
105 A = B(:,1:order);
106 a = B(:,order+2);
107 A = A - diag(sum(A,2)+a);
108 alpha = B(:,order+1)';
109 % check if it is a proper PH (irreducible phase process & no full zero matrix)
110 if all(all(A==0.0)) || all(alpha==0.0) || all(a==0.0)
111 continue
112 end
113 alpha = alpha / sum(alpha);
114 D = A + a*alpha;
115 if rank(D) == order-1
116 pi = CTMCSolve(D);
117 if min(abs(pi)) > prec
118 % scale to the mean value
119 m = MomentsFromPH (alpha, A, 1);
120 A = A * m(1) / mean;
121 return;
122 end
123 end
124 trials = trials + 1;
125 end
126 end
127 error ('No feasible random PH found with such many zero entries!');
128end