1classdef (Sealed) Cox2 < Markovian
2 % Static
class to fit two-phase coxian statistical distribution
4 % Copyright (c) 2012-2026, Imperial College London
8 function cx = fit(MEAN,SCV,SKEW)
9 % CX = FIT(MEAN,SCV,SKEW)
11 cx = Cox2.fitCentral(MEAN,SCV*MEAN^2,SKEW);
13 jline_mu = java.util.ArrayList();
14 jline_phi = java.util.ArrayList();
15 if length(line_dist.params) == 3
16 jline_mu.add(cx.getParam(1).paramValue);
17 jline_mu.add(cx.getParam(2).paramValue);
18 jline_phi.add(cx.getParam(3).paramValue);
20 mu = line_dist.getParam(1).paramValue;
21 phi = line_dist.getParam(2).paramValue;
27 jline_phi.add(phi(i));
30 cx.obj = jline.lang.processes.Coxian(jline_mu, jline_phi);
33 function cx = fitCentral(MEAN,VAR,SKEW)
34 % CX = FITCENTRAL(MEAN,VAR,SKEW)
36 % Fit the distribution from first three central moments (mean,
40 cx = Cox2.fitMeanAndSCV(MEAN,SCV);
45 e3 = -(2*e1^3-3*e1*e2-SKEW*(e2-e1^2)^(3/2));
46 % consider the two possible solutions
48 (2*(e3 - 3*e1*e2))/(- 3*e2^2 + 2*e1*e3) + (3*e1*e2 - e3 + (24*e1^3*e3 - 27*e1^2*e2^2 - 18*e1*e2*e3 + 18*e2^3 + e3^2)^(1/2))/(- 3*e2^2 + 2*e1*e3)
49 (2*(e3 - 3*e1*e2))/(- 3*e2^2 + 2*e1*e3) - (e3 - 3*e1*e2 + (24*e1^3*e3 - 27*e1^2*e2^2 - 18*e1*e2*e3 + 18*e2^3 + e3^2)^(1/2))/(- 3*e2^2 + 2*e1*e3)
52 -(3*e1*e2 - e3 + (24*e1^3*e3 - 27*e1^2*e2^2 - 18*e1*e2*e3 + 18*e2^3 + e3^2)^(1/2))/(- 3*e2^2 + 2*e1*e3)
53 (e3 - 3*e1*e2 + (24*e1^3*e3 - 27*e1^2*e2^2 - 18*e1*e2*e3 + 18*e2^3 + e3^2)^(1/2))/(- 3*e2^2 + 2*e1*e3)];
54 % Completion probability of phase 1. With the two rates fixed by
55 % the second and third moments, the mean determines it:
56 % e1 = 1/mu1 + (1-phi)/mu2 => phi = 1 - mu2*e1 + mu2/mu1.
57 % Verified in sage/proofs/distribution_fitters.py: with this phi
58 % the Coxian matches e1, e2 and e3 exactly on both branches.
59 phi = 1 - mu2.*e1 + mu2./mu1;
60 if phi(1)>=0 && phi(1)<=1 && mu1(1) >= 0 && mu2(1) >= 0
61 %
if the first solution
is feasible
62 cx = Coxian([mu1(1),mu2(1)],[phi(1),1]);
63 elseif phi(2) >=0 && phi(2) <=1 && mu1(2) >= 0 && mu2(2) >= 0
64 %
if the second solution
is feasible
65 cx = Coxian([mu1(2),mu2(2)],[phi(2),1]);
67 line_warning(mfilename,
'Cox2.fitCentral: Third moment could not be fitted exactly.\n');
70 %line_warning(mfilename,
'Infeasible combination of central moments, fitting only mean and squared coefficient of variation.');
71 cx = Cox2.fitMeanAndSCV(MEAN, SCV);
73 %line_warning(mfilename,
'Infeasible combination of central moments, fitting only mean.');
74 cx = Cox2.fitMean(MEAN);
79 function cx = fitMean(MEAN)
82 p = 1.0-GlobalConstants.CoarseTol;
85 cx = Coxian([l0,l1],[p,1]);
88 function cx = fitMeanAndSCV(MEAN, SCV)
89 % CX = FITMEANANDSCV(MEAN, SCV)
91 % Fit a 2-phase Coxian distribution with given mean and squared coefficient of variation (SCV=variance/mean^2)
92 if (SCV <1 && SCV >=0.5)
93 l0 = 2/MEAN/(1+sqrt(1+2*(SCV-1)));
94 l1 = 2/MEAN/(1-sqrt(1+2*(SCV-1)));
105 cx = Coxian([l0,l1],[p,1]);