LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
kpcfit_ph_manual.m
1function PH = kpcfit_ph_manual(E,varargin)
2% ** beta version **
3%
4% PH = kpcfit_ph_manual(E,'option1','val1','option2','val2',...)
5%
6% DESCRIPTION
7% Fit phase-type (PH) process using Kronecker Product Composition (KPC)
8% method
9%
10% INPUT
11%
12% E - vector of moments of consecutive order to be fitted
13%
14% EXAMPLE
15%
16% S % trace
17% E = [];
18% for k=1:6 E(k)=mean(S.^k); end
19% PH = kpcfit_ph_manual(E);
20%
21% OPTION LIST
22% 'WantHyper' - boolean, if true returns hyper-exponential distribution
23% 'NumStates' - integer, number of states of the PH distribution - **must be multiple of 2**, default: 4
24% 'NumPHs' - integer, number of PHs to be composed, default: 2 (i.e., PH(4))
25% 'NumMoms' - integer, moments considered in the moment matching problem, default: 6
26% 'MaxIterMM' - integer, maximum number of iterations for a single moment-matching fitting run
27
28%% options
29OptionNames = [
30 'WantHyper ';
31 'NumStates ';
32 'NumPHs ';
33 'NumMoms ';
34 'MaxIterMM ';
35 ];
36
37OptionTypes = [
38 'numeric';
39 'numeric';
40 'numeric';
41 'numeric';
42 'numeric'];
43
44OptionValues = [];
45for i = 1:size(OptionNames,1)
46 options.(deblank(OptionNames(i,:)))=[];
47end
48
49% Default settings
50options.NumStates = 4;
51options.NumPHs = 2;
52options.NumMoms = 6;
53options.MaxIterMM = 100;
54
55% Parse optional parameters
56options=ParseOptPara(options,OptionNames,OptionTypes,OptionValues,varargin);
57if mod(options.NumStates,2) ~= 0
58 error('kpcfit_ph: number of states must be multiple of 2');
59end
60%
61TOL=1e-10;
62EPSTOL=100*TOL;
63warning off
64optimoptions = optimset('Algorithm','active-set', ...
65 'LargeScale','off', ...
66 'MaxIter',options.MaxIterMM, ...
67 'MaxFunEvals',1e10, ...
68 'MaxSQPIter',500, ...
69 'TolCon',TOL, ...
70 'Display','off', ...
71 'OutputFcn',@kpcfit_ph_manual_outfun);
72
73stagnval = 0;
74stagniter = 0;
75x0=zeros(3*options.NumPHs,1); % each PH brings 3 degrees of freedomg
76lgkx = x0; % last good known x
77n=length(E); % number of moments
78nexact=2;
79
80for j=1:options.NumPHs
81 if j==1
82 PH=map_rand(2);
83 else
84 PH=map_feasblock(rand,1000*rand,-1,0); % hyperexponential
85 end
86 for i=1:3
87 x0((i-1)*options.NumPHs+j)=map_moment(PH,i);
88 end
89end
90
91fmincon(@objfun,x0,[],[],[],[],x0*0+EPSTOL,[],@nnlcon,optimoptions);
92xtopar(lgkx); % this recomputes PH
93PH=map_normalize(map_scale(PH,E(1)));
94
95if map_isfeasible(PH)<10
96 warning('PH distribution may not be valid, check for negative rates in D1 or in off-diagonal of D0')
97end
98PH=map_scale(PH,E(1));
99
100
101 function [Eest]=xtopar(x)
102 x=abs(x)+EPSTOL;
103
104 % extract moments
105 E1j=x(1:options.NumPHs)';
106 SCVj=x((options.NumPHs+1):2*options.NumPHs)';
107 E2j=(1+SCVj).*(E1j.^2);
108 E3j=x((2*options.NumPHs+1):3*options.NumPHs);
109
110 % fit ph
111 if options.WantHyper
112 PH = map_feasblock(E1j(1),E2j(1),E3j(1),rand);
113 else
114 PH = mmpp2_fit3(E1j(1),E2j(1),E3j(1),rand);
115 end
116 PH = map_scale(PH,E1j(1));
117
118 for j=2:options.NumPHs-1
119 [PHk]=map_feasblock(E1j(1),E2j(j),E3j(j),rand);
120 PH=map_kpc(PH,PHk);
121 end
122 E1j(options.NumPHs)=E(1)/map_moment(PH,1);
123 E2j(options.NumPHs)=E(2)*2/map_moment(PH,2);
124 if length(E)>2
125 E3j(options.NumPHs)=E(3)*6/map_moment(PH,3);
126 end
127 x(2)=E1j(options.NumPHs);
128 x(4)=E2j(options.NumPHs);
129 x(6)=E3j(options.NumPHs);
130% map_scv(PH)
131% map_isfeasible(PH)
132 [PH]=map_kpc(PH,map_feasblock(E1j(options.NumPHs),E2j(options.NumPHs),E3j(options.NumPHs),0));
133% F=map_feasblock(E1j(options.NumPHs),E2j(options.NumPHs),E3j(options.NumPHs),0);
134% F{1}
135% F{2}
136% %map_scv(PH)
137% map_isfeasible(PH)
138% %PH{1}
139% pause
140
141 PH{1}=diag(diag(PH{1}));
142 for k=1:length(E)
143 Eest(k)=map_moment(PH,k);
144 end
145 end
146
147 function [c,ceq]=nnlcon(x)
148 [Eest]=xtopar(x);
149 c=[];
150 ceq=[];
151 for k=3:nexact
152 ceq(end+1)=norm((E(k))-(Eest(k)),1);
153 end
154 end
155
156 function f=objfun(x)
157 [Eest]=xtopar(x);
158 f=0;
159 for k=(nexact+1):length(E)
160 f=f+norm(log10(E(k))-log10(Eest(k)),1);
161 end
162 end
163
164end
165
166function [MAP]=map_rand(K)
167if nargin<1
168 K=2;
169end
170D1=rand(K,K);
171D0=rand(K,K);
172
173MAP=cell(1,2);
174MAP{1}=D0;
175MAP{2}=D1;
176MAP=map_normalize(MAP);
177end