LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
Cox2.m
1classdef (Sealed) Cox2 < Markovian
2 % Static class to fit two-phase coxian statistical distribution
3 %
4 % Copyright (c) 2012-2026, Imperial College London
5 % All rights reserved.
6
7 methods (Static)
8 function cx = fit(MEAN,SCV,SKEW)
9 % CX = FIT(MEAN,SCV,SKEW)
10
11 cx = Cox2.fitCentral(MEAN,SCV*MEAN^2,SKEW);
12
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);
19 else
20 mu = line_dist.getParam(1).paramValue;
21 phi = line_dist.getParam(2).paramValue;
22 for i = 1:length(mu)
23 jline_mu.add(mu(i));
24 end
25
26 for i = 1:length(phi)
27 jline_phi.add(phi(i));
28 end
29 end
30 cx.obj = jline.lang.processes.Coxian(jline_mu, jline_phi);
31 end
32
33 function cx = fitCentral(MEAN,VAR,SKEW)
34 % CX = FITCENTRAL(MEAN,VAR,SKEW)
35
36 % Fit the distribution from first three central moments (mean,
37 % variance, skewness)
38 SCV = VAR/MEAN^2;
39 if nargin == 2
40 cx = Cox2.fitMeanAndSCV(MEAN,SCV);
41 return
42 end
43 e1 = MEAN;
44 e2 = (1+SCV)*e1^2;
45 e3 = -(2*e1^3-3*e1*e2-SKEW*(e2-e1^2)^(3/2));
46 % consider the two possible solutions
47 mu1 = [
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)
50 ];
51 mu2 = [
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]);
66 else
67 line_warning(mfilename,'Cox2.fitCentral: Third moment could not be fitted exactly.\n');
68 % fit is not feasible
69 if SCV>=0.5
70 %line_warning(mfilename,'Infeasible combination of central moments, fitting only mean and squared coefficient of variation.');
71 cx = Cox2.fitMeanAndSCV(MEAN, SCV);
72 else
73 %line_warning(mfilename,'Infeasible combination of central moments, fitting only mean.');
74 cx = Cox2.fitMean(MEAN);
75 end
76 end
77 end
78
79 function cx = fitMean(MEAN)
80 % CX = FITMEAN(MEAN)
81
82 p = 1.0-GlobalConstants.CoarseTol;
83 l0 = 1/MEAN;
84 l1 = 1/MEAN;
85 cx = Coxian([l0,l1],[p,1]);
86 end
87
88 function cx = fitMeanAndSCV(MEAN, SCV)
89 % CX = FITMEANANDSCV(MEAN, SCV)
90
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)));
95 p = 0.0;
96 elseif (SCV == 1.0)
97 l0 = 1/MEAN;
98 l1 = 1/MEAN;
99 p = 1.0;
100 else
101 l0 = 2/MEAN;
102 l1 = l0/(2*SCV);
103 p = 1 - l1/l0;
104 end
105 cx = Coxian([l0,l1],[p,1]);
106 end
107 end
108
109end