LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
RandomDPH.m
1% [alpha, A] = RandomDPH(order, mean, zeroEntries, maxTrials, prec)
2%
3% Returns a random discrete phase-type distribution with a
4% given mean value.
5%
6% Parameters
7% ----------
8% order : int
9% The size of the discrete phase-type distribution
10% mean : double, optional
11% The mean of the discrete 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 DPH
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, or increase the mean value.
37
38function [alpha, A] = RandomDPH(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 = 10;
50 end
51
52 if ~exist('maxTrials','var')
53 maxTrials = 1000;
54 end
55
56 if zeroEntries > (order+1)*(order-1)
57 error('RandomDPH: 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 DPH parameters
105 A = B(:,1:order);
106 a = B(:,order+2);
107 sc = sum(A,2)+a;
108 if any(sc==0)
109 continue;
110 end
111 A = diag(1./sc)*A;
112 a = diag(1./sc)*a;
113 alpha = B(:,order+1)';
114 % check if it is a proper PH (irreducible phase process & no full zero matrix)
115 if all(all(A==0.0)) || all(alpha==0.0) || all(a==0.0)
116 continue;
117 end
118 alpha = alpha / sum(alpha);
119 if rank(eye(order)-A) == order
120 if min(abs(alpha*inv(eye(order)-A))) > prec
121 % diagonals of matrix A:
122 d = rand(1,order);
123 % scale to the mean value
124 m = MomentsFromDPH (alpha, diag(1-d)*A+diag(d), 1);
125 d = 1 - (1-d)*m(1)/mean;
126 A = diag(1-d)*A+diag(d);
127 if CheckDPHRepresentation(alpha,A,prec)
128 return;
129 end
130 end
131 end
132 trials = trials + 1;
133 end
134 end
135 error ('No feasible random DPH found with such many zero entries! You can also try to increase the mean value!');
136end