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 phi = (6*e1^3 - 6*e2*e1 + e3)/(- 6*e1^3 + 3*e2*e1);
48 mu1 = [
49 (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)
50 (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)
51 ];
52 mu2 = [
53 -(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)
54 (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)];
55 if phi>=0 && phi<=1 && mu1(1) >= 0 && mu2(1) >= 0
56 % if the first solution is feasible
57 cx = Coxian([mu1(1),mu2(1)],[phi,1]);
58 elseif phi >=0 && phi <=1 && mu1(2) >= 0 && mu2(2) >= 0
59 % if the second solution is feasible
60 cx = Coxian([mu1(2),mu2(2)],[phi,1]);
61 else
62 line_warning(mfilename,'Cox2.fitCentral: Third moment could not be fitted exactly.\n');
63 % fit is not feasible
64 if SCV>=0.5
65 %line_warning(mfilename,'Infeasible combination of central moments, fitting only mean and squared coefficient of variation.');
66 cx = Cox2.fitMeanAndSCV(MEAN, SCV);
67 else
68 %line_warning(mfilename,'Infeasible combination of central moments, fitting only mean.');
69 cx = Cox2.fitMean(MEAN);
70 end
71 end
72 end
73
74 function cx = fitMean(MEAN)
75 % CX = FITMEAN(MEAN)
76
77 p = 1.0-GlobalConstants.CoarseTol;
78 l0 = 1/MEAN;
79 l1 = 1/MEAN;
80 cx = Coxian([l0,l1],[p,1]);
81 end
82
83 function cx = fitMeanAndSCV(MEAN, SCV)
84 % CX = FITMEANANDSCV(MEAN, SCV)
85
86 % Fit a 2-phase Coxian distribution with given mean and squared coefficient of variation (SCV=variance/mean^2)
87 if (SCV <1 && SCV >=0.5)
88 l0 = 2/MEAN/(1+sqrt(1+2*(SCV-1)));
89 l1 = 2/MEAN/(1-sqrt(1+2*(SCV-1)));
90 p = 0.0;
91 elseif (SCV == 1.0)
92 l0 = 1/MEAN;
93 l1 = 1/MEAN;
94 p = 1.0;
95 else
96 l0 = 2/MEAN;
97 l1 = l0/(2*SCV);
98 p = 1 - l1/l0;
99 end
100 cx = Coxian([l0,l1],[p,1]);
101 end
102 end
103
104end