2 % Abstract phase-type distribution with Markovian structure
4 % Defined by initial probabilities and transient subgenerator matrix.
6 % Copyright (c) 2012-2026, Imperial College London
10 function self = PH(alpha, T)
11 % PH Create a phase-type distribution instance
13 % @brief Creates a phase-type distribution with initial probabilities and subgenerator
14 % @param alpha Initial probability vector (must sum to ≤ 1)
15 % @param T Transient subgenerator matrix (must be nonsingular)
16 % @
return self PH distribution instance
17 self@Markovian(
'PH', 2);
18 self.setParam(1,
'alpha', alpha);
19 self.setParam(2,
'T', T);
20 T = self.getSubgenerator;
21 ph = {T,-T*ones(length(T),1)*self.getInitProb};
23 self.nPhases = length(self.process{1});
28 function alpha = getInitProb(self)
29 % ALPHA = GETINITPROB()
31 % Get vector of initial probabilities
32 alpha = self.getParam(1).paramValue(:);
33 alpha = reshape(alpha,1,length(alpha));
36 function T = getSubgenerator(self)
37 % T = GETSUBGENERATOR()
40 T = self.getParam(2).paramValue;
43 function X = sample(self, n)
46 % Get n samples from the distribution
47 if nargin<2 %~exist(
'n',
'var'),
50 X = map_sample(self.getProcess,n);
55 function update(self,varargin)
56 % UPDATE(SELF,VARARGIN)
58 % Update parameters to match the first n central moments
63 if length(varargin) > 3
64 line_warning(mfilename,
'Update in %s distributions can only handle 3 moments, ignoring higher-order moments.\n',
class(self));
68 e3 = -(2*e1^3-3*e1*e2-SKEW*(e2-e1^2)^(3/2));
71 [alpha,T] = APHFrom3Moments([e1,e2,e3]);
73 options = kpcfit_ph_options([e1,e2,e3],
'MinNumStates',2,
'MaxNumStates',2,
'Verbose',
false);
74 ph = kpcfit_ph_auto([e1,e2,e3],options);
76 alpha=map_pie(ph{1,1});
79 self.setParam(1,
'alpha', alpha);
80 self.setParam(2,
'T', T);
81 self.process = {T,-T*ones(length(T),1)*alpha};
84 function setMean(self,MEAN)
85 % UPDATEMEAN(SELF,MEAN)
87 % Update parameters to match the given mean
89 ph = map_scale(ph,MEAN);
90 self.setParam(1,
'alpha', map_pie(ph));
91 self.setParam(2,
'T', ph{1});
92 self.process = {T,-T*ones(length(T),1)*self.getInitProb};
95 % function setMeanAndSCV(self, MEAN, SCV)
96 % % UPDATEMEANANDSCV(MEAN, SCV)
98 % % Fit phase-type distribution with given mean and squared coefficient of
99 % % variation (SCV=variance/mean^2)
102 % [alpha,T] = APHFrom2Moments([e1,e2]);
103 % %options = kpcfit_ph_options([e1,e2],
'MinNumStates',2,
'MaxNumStates',2,
'Verbose',
false);
104 % %ph = kpcfit_ph_auto([e1,e2],options);
105 % %[alpha,T]=map2ph(ph{1,1});
107 % alpha=map_pie(ph{1,1});
109 % self.setParam(1,
'alpha', alpha);
110 % self.setParam(2,
'T', T);
111 % self.process = {T,-T*ones(length(T),1)*self.getInitProb};
117function ex = fit(MEAN, SCV, SKEW)
118 % EX = FIT(MEAN, SCV, SKEW)
120 % Fit the distribution from first three standard moments (mean,
122 if MEAN <= GlobalConstants.FineTol
126 ex.update(MEAN, SCV, SKEW);
127 ex.immediate =
false;
131 function ex = fitRawMoments(m1, m2, m3)
133 % Fit the distribution from first three moments
134 if m1 <= GlobalConstants.FineTol
138 ex.updateFromRawMoments(m1, m2, m3);
139 ex.immediate =
false;
143 function ex = fitCentral(MEAN, VAR, SKEW)
144 % EX = FITCENTRAL(MEAN, VAR, SKEW)
146 % Fit the distribution from first three central moments (mean,
147 % variance, skewness)
148 if MEAN <= GlobalConstants.FineTol
152 ex.update(MEAN, VAR/MEAN^2, SKEW);
153 ex.immediate =
false;
157 function ex = fitMeanAndSCV(MEAN, SCV)
158 % EX = FITMEANANDSCV(MEAN, SCV)
160 % Fit the distribution from first three central moments (mean,
161 % variance, skewness)
162 if MEAN <= GlobalConstants.FineTol
168 ex.update(MEAN, SCV, SKEW);
169 ex.immediate =
false;
173 % function ex = fromRepresentation(dep)
174 % % EX = FROMREPRESENTATION(DEP)
176 % % Build a phase-type distribution from a cell array {D0,D1}
178 % ex = PH(map_pie(dep),dep{1});