1function [APH, isexact] = aph_fit(e1,e2,e3,nmax)
2% APH = aph_fit(e1,e2,e3,nmax) - moment matching of APH(n) distribution
3% nmax = max order (
default: 10)
5% Implementation of: A.Bobbio, A.Horvath, M.Telek, Matching three moments
6% with minimal acyclic phase type distributions, Stochastic Models
13if isinf(e2) || isinf(e3)
14 APH = map_exponential(e1);
26while (n2_feas ==
false || n3_lbfeas ==
false || n3_ubfeas ==
false ) && n < nmax
28 pn = ((n+1)*(n2-2)/(3*n2*(n-1)))*(-2*sqrt(n+1)/sqrt(4*(n+1)-3*n*n2) - 1);
29 an = (n2 - 2) / (pn*(1-n2) + sqrt(pn^2+pn*n*(n2-2)/(n-1)));
30 ln = ((3+an)*(n-1)+2*an)/((n-1)*(1+an*pn)) - (2*an*(n+1))/(2*(n-1)+an*pn*(n*an+2*n-2));
32 un = (1/(n^2*n2))*(2*(n-2)*(n*n2-n-1)*sqrt(1+n*(n2-2)/(n-1))+(n+2)*(3*n*n2-2*n-2));
33 if n2 >= (n+1)/n && n2 <= (n+4)/(n+1)
38 elseif n2 >= (n+4)/(n+1)
44 if n2 >= (n+1)/n && n2 <= n/(n-1)
57if (n2_feas ==
false || n3_lbfeas ==
false || n3_ubfeas ==
false ) || (n == nmax)
58 warning(
'cannot match moment set exactly');
65if n2 <= n/(n-1) || n3 <= 2*n2-1 %
case 1 of 2
66 b = 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))));
67 a = (b*n2-2)*(n-1)*b/((b-1)*n);
71 alpha = zeros(1,n); alpha(1)=p; alpha(end)=1-p;
72 T = diag(-mu*ones(1,n))+diag(mu*ones(1,n-1),1); T(end,end)=-lambda;
73 APH = map_scale(map_normalize({T,-T*ones(n,1)*alpha}),e1);
74elseif n2 > n/(n-1) && n3 > un_1 %
case 2 of 2
82 K8 = 3+3*n2^2+n3-3*n2*n3;
83 K9 = 108*K1^2*(4*K2^2*K3*n^2*n2+K1^2*K2*K4^2*n*n2^2+4*K1*K5*(K5^2-3*K2*K6*n*n2)+sqrt(-16*K1^2*K7^6+(4*K1*K5^3+K1^2*K2*K4^2*n*n2^2+4*K2*n*n2*(K4*n^2-3*K6*n2+K8*n))^2));
84 K10 = K4^2/(4*K3^2) - K5/(K1*K3*n2);
85 K11 = 2^(1/3)*(3*K5^2+K2*(K3+2*K4)*n*n2)/(K3*K9^(1/3)*n2);
86 K12 = K9^(1/3) / (3*2^(7/3)*K1^2*K3*n2);
87 K13 = sqrt(K10 + K11 + K12);
88 K14 = (6*K1*K3*K4*K5+4*K2*K3^2*n-K1^2*K4^3*n2) / (4*K1^2*K3^3*K13*n2);
90 K16 = sqrt(2*K10 - K11 -K12 -K14);
91 K17 = sqrt(2*K10 - K11 -K12 +K14);
92 K18 = 36*K5^3 + 36*K2*K4*K5*n*n2 + 9*K1*K2*K4^2*n*n2^2 - sqrt(81*(4*K5^3+4*K2*K4*K5*n*n2+K1*K2*K4^2*n*n2^2)^2-48*(3*K5^2+2*K2*K4*n*n2)^3);
93 K19 = -K5/(K1*K4*n2) -2^(2/3)*(3*K5^2+2*K2*K4*n*n2)/(3^(1/3)*K1*K4*n2*K18^(1/3)) - K18^(1/3)/(6^(2/3)*K1*K4*n2);
94 K20 = 6*K1*K3*K4*K5 + 4*K2*K3^2*n - K1^2*K4^3*n2;
95 K21 = K11 + K12 + K5/(2*n*K1*K3);
96 K22 = sqrt(3*K4^2/(4*K3^2) - 3*K5/(K1*K3*n2) + sqrt(4*K21^2 - n*K2/(n2*K1^2*K3)));
97 if n3 > un_1 && n3 < 3*n2/2
101 elseif n3 > 3*n2/2 && K20 > 0
108%%
this input does not seem to find an f
113 a = 2*(f-1)*(n-1)/((n-1)*(n2*f^2-2*f+2)-n);
117 alpha = zeros(1,n); alpha(1)=p; alpha(2)=1-p;
118 T = diag(-mu*ones(1,n))+diag(mu*ones(1,n-1),1); T(1,1)=-lambda; T(1,2)=lambda;
119 APH = map_scale(map_normalize({T,-T*ones(n,1)*alpha}),e1);
121 warning(
'moment set cannot be matched with an APH distribution');