LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
APHFrom3Moments.m
1% [alpha, A] = APHFrom3Moments(moms, maxSize)
2%
3% Returns an acyclic PH which has the same 3 moments as
4% given. If detects the order and the structure
5% automatically to match the given moments.
6%
7% Parameters
8% ----------
9% moms : vector of doubles, length(3)
10% The moments to match
11% maxSize : int, optional
12% The maximal size of the resulting APH. The default value
13% is 100.
14%
15% Returns
16% -------
17% alpha : vector, shape (1,M)
18% The initial probability vector of the APH
19% A : matrix, shape (M,M)
20% Transient generator matrix of the APH
21%
22% Raises an error if the moments are not feasible with an
23% APH of size "maxSize".
24%
25% References
26% ----------
27% .. [1] A. Bobbio, A. Horvath, M. Telek, "Matching three
28% moments with minimal acyclic phase type
29% distributions," Stochastic models, pp. 303-326, 2005.
30
31function [alpha, A] = APHFrom3Moments (moms, maxSize, prec)
32
33 if ~exist('prec','var')
34 prec=1e-14;
35 end
36 if ~exist('maxSize','var')
37 maxSize = 100;
38 end
39
40
41 m1 = moms(1);
42 m2 = moms(2);
43 m3 = moms(3);
44
45 % detect number of phases needed
46 n = 2;
47 while n<maxSize && (APH2ndMomentLowerBound(m1, n) > m2 || APH3rdMomentLowerBound(m1, m2, n) >= m3 || APH3rdMomentUpperBound(m1, m2, n) <= m3)
48 n = n + 1;
49 end
50
51 % if PH is too large, adjust moment to bounds
52 if APH2ndMomentLowerBound(m1, n) > m2
53 m2 = APH2ndMomentLowerBound(m1, n);
54 end
55
56 if APH3rdMomentLowerBound(m1, m2, n) > m3
57 m3 = APH3rdMomentLowerBound(m1, m2, n);
58 end
59
60 if APH3rdMomentUpperBound(m1, m2, n) < m3
61 m3 = APH3rdMomentUpperBound(m1, m2, n);
62 end
63
64 % compute normalized moments
65 nmoms = NormMomsFromMoms ([m1, m2, m3]);
66 n1 = nmoms(1);
67 n2 = nmoms(2);
68 n3 = nmoms(3);
69
70
71 if n2>2 || n3 < 2*n2 - 1
72 b = real(2*(4-n*(3*n2-4)) / (n2*(4+n-n*n3) + sqrt(n*n2)*sqrt(12*n2^2*(n+1)+16*n3*(n+1)+n2*(n*(n3-15)*(n3+1)-8*(n3+3)))));
73 a = (b*n2-2)*(n-1)*b / (b-1) / n;
74 p = (b-1) / a;
75 lambda = (p*a+1) / n1;
76 mu = (n-1)*lambda / a;
77 % construct representation
78 alpha = zeros(1,n);
79 alpha(1) = p;
80 alpha(n) = 1.0-p;
81 A = zeros(n,n);
82 A(n,n) = -lambda;
83 for i=1:n-1
84 A(i,i) = -mu;
85 A(i,i+1) = mu;
86 end
87 return;
88 else
89 c4 = n2*(3*n2-2*n3)*(n-1)^2;
90 c3 = 2*n2*(n3-3)*(n-1)^2;
91 c2 = 6*(n-1)*(n-n2);
92 c1 = 4*n*(2-n);
93 c0 = n*(n-2);
94 fs = roots([c4 c3 c2 c1 c0]);
95 for i=1:length(fs)
96 f = fs(i);
97 if abs((n-1)*(n2*f^2-2*f+2)-n)<prec
98 continue;
99 end
100 a = 2*(f-1)*(n-1) / ((n-1)*(n2*f^2-2*f+2)-n);
101 p = (f-1)*a;
102 lambda = (a+p) / n1;
103 mu = (n-1) / (n1 - p/lambda);
104 if isreal(p) && isreal(lambda) && isreal(mu)&& p>=0 && p<=1 && lambda>0 && mu>0
105 alpha = zeros(1,n);
106 alpha(1) = p;
107 alpha(2) = 1-p;
108 A = zeros(n,n);
109 A(1,1) = -lambda;
110 A(1,2) = lambda;
111 for j=2:n
112 A(j,j) = -mu;
113 if j<n
114 A(j,j+1) = mu;
115 end
116 end
117 return;
118 end
119 end
120 end
121 error('No APH found for the given 3 moments!');
122end