LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
kpcfit_manual.m
1function [bestMAP,fac,fbc,subMAPs,otherMAPs, otherFACs, otherFBCs, otherSubMAPs]=kpcfit_manual(NumMAPs,E,AC,ACLags,BC,BCLags,varargin)
2%
3% DESCRIPTION
4% Fitting of trace S into a Markovian Arrival Process based on Kronecker
5% Product Composition (KPC) theory using manual parameter specification.
6%
7% OPTION LIST
8% 'OnlyAC' - boolean, if true fitting is performed on moments and
9% 'MaxIterAC' - integer, maximum number of iterations for a single autocorrelation fitting run
10% 'MaxIterBC' - integer, maximum number of iterations for a single bicovariance fitting run
11% 'MaxRunsAC' - maximum number of autocorrelation fitting runs
12% 'MaxRunsBC' - maximum number of bicovariance fitting runs
13% 'MaxResAC' - maximum number of autocorrelation fitting further analyzed for bicovariance fitting
14warning off
15%% options
16OptionNames = [
17 'MaxIterAC ';
18 'MaxIterBC ';
19 'MaxRunsAC ';
20 'MaxRunsBC ';
21 'MaxResAC ';
22 'OnlyAC ';
23 'AnimateAC ';
24 'MaxRetMAPs';
25 'ParallelBC';
26 ];
27
28OptionTypes = [
29 'numeric';
30 'numeric';
31 'numeric';
32 'numeric';
33 'numeric';
34 'numeric';
35 'numeric';
36 'numeric';
37 'numeric'];
38
39OptionValues = [];
40for i = 1:size(OptionNames,1)
41 options.(deblank(OptionNames(i,:)))=[];
42end
43% Default settings
44options.OnlyAC = false; % by default fit also bicovariances
45options.AnimateAC = false; % by default fit also bicovariances
46options.MaxIterAC = 300; % iterate up to 300 times to fit autocorrelations
47options.MaxIterBC = 10; % iterate up to 10 times to fit bicorrelations
48options.MaxRunsAC = 50; % maximum number of runs for AC fitting
49options.MaxRunsBC = 30; % maximum number of runs for AC fitting
50options.MaxResAC = min([options.MaxRunsAC,10]); % maximum number of values returned for AC fitting
51options.MaxRetMAPs = 1; % maximum number of MAPs returned
52options.ParallelBC = 0; % parallelize BC runs
53
54
55% Parse Optional Parameters
56options=ParseOptPara(options,OptionNames,OptionTypes,OptionValues,varargin);
57
58
59%% fit autocorrelations
60disp(' ');
61[resSCV,resG2,fobjAC] = kpcfit_sub_acfit(E,AC,ACLags,NumMAPs,options.MaxIterAC,options.MaxRunsAC,options.MaxResAC,options.AnimateAC);
62
63%% fit bicovariances
64disp(' ');
65resE1 = cell(length(resSCV),1);
66resE3 = cell(length(resSCV),1);
67fobjBC = zeros(length(resSCV),1);
68
69if options.OnlyAC == false
70 if options.ParallelBC > 0
71 parfor i = 1:length(resSCV)
72 fprintf(1,'fitting: processing acfit subresult %d of top %d\n',i,length(resSCV));
73 [E1j,E3j,foBC] = kpcfit_sub_bcfit(E,resSCV{i},resG2{i},BC,BCLags,options.MaxIterBC,options.MaxRunsBC);
74 resE1{i} = E1j;
75 resE3{i} = E3j;
76 fobjBC(i) = foBC;
77 end
78 else
79 for i = 1:length(resSCV)
80 fprintf(1,'fitting: processing acfit subresult %d of top %d\n',i,length(resSCV));
81 [E1j,E3j,foBC] = kpcfit_sub_bcfit(E,resSCV{i},resG2{i},BC,BCLags,options.MaxIterBC,options.MaxRunsBC);
82 resE1{i} = E1j;
83 resE3{i} = E3j;
84 fobjBC(i) = foBC;
85 end
86 end
87elseif options.OnlyAC == true
88 for i = 1:length(resSCV)
89 resE1{i} = ones(1,NumMAPs);
90 resE3{i} = (3/2+0.01).*(1+resSCV{i}).^2;
91 fobjBC(i) = -1;
92 end
93else
94 error('value of OnlyAC must be true or false')
95end
96
97%% determine best result
98[v,ind] = sort(fobjBC,1,'ascend');
99
100% truncate bicorrelations when it gets negative
101tag=[];
102for index=1:size(BCLags,1)
103 if find(diff(BCLags(index,:))<0)
104 tag(end+1,1)=index;
105 end
106end
107BCLags(tag,:)=[];
108BC(tag)=[];
109
110%% compose intermediate results into final MAPs
111composedMAPs = 0;
112MAPs = cell(1,0);
113subs = cell(1,0);
114FACs = [];
115FBCs = [];
116
117for k=1:length(ind)
118 index = ind(k);
119
120 COMPOSE_VERBOSE=1;
121 [newMAP,newSubMAPs,errorCode]=kpcfit_sub_compose(resE1{index},resSCV{index},resE3{index},resG2{index}, COMPOSE_VERBOSE);
122 if errorCode ~= 0 || isempty(newMAP)
123 fprintf("[%d] Discarded (errorCode=%d).\n", k, errorCode);
124 continue
125 end
126
127 % scale MAP to match mean exactly
128 newMAP=map_scale(newMAP,E(1));
129
130 % Compute value of obj. functions for the resulting MAP
131 [newfAC,newfBC]=evaluate_obj_function(newMAP);
132
133 composedMAPs = composedMAPs + 1;
134
135 MAPs{composedMAPs} = newMAP;
136 FACs(composedMAPs) = newfAC;
137 FBCs(composedMAPs) = newfBC;
138 subs{composedMAPs} = newSubMAPs;
139
140 if COMPOSE_VERBOSE > 0
141 fprintf(1, "[%d] OK - fac= %f; fbc= %f\n", k, newfAC, newfBC);
142 end
143
144 if composedMAPs == options.MaxRetMAPs
145 break
146 end
147end
148
149
150%%
151if composedMAPs == 0
152 fprintf(2, "+++ KPC FAILED +++\n");
153else
154 bestMAP=MAPs{1};
155 subMAPs=subs{1};
156 fbc=FBCs(1);
157 fac=FACs(1);
158 fprintf(1, "Returned %d MAPs:\n", composedMAPs);
159 fprintf(1, '1) fAC=%f, fBC=%f, SCV=%f, ACF(1)=%f, SKEW=%f\n', ...
160 fac, fbc, map_scv(bestMAP), map_acf(bestMAP,1), map_skew(bestMAP));
161
162 otherMAPs=cell(1,0);
163 otherFBCs=[];
164 otherFACs=[];
165 otherSubMAPs=cell(1,0);
166
167
168 for i=1:composedMAPs-1
169 otherMAPs{end+1} = MAPs{1+i};
170 otherFACs(end+1) = FACs(1+i);
171 otherFBCs(end+1) = FBCs(1+i);
172 otherSubMAPs{end+1} = subs{1+i};
173 fprintf(1, '%d) fAC=%f, fBC=%f, SCV=%f, ACF(1)=%f, SKEW=%f\n', ...
174 i+1, otherFACs(i), otherFBCs(i), map_scv(otherMAPs{i}), ...
175 map_acf(otherMAPs{i},1), map_skew(otherMAPs{i}));
176 end
177
178 fprintf(1, '\n');
179end
180
181
182
183 function [objAC,objBC] = evaluate_obj_function (map)
184 tSCV=(E(2)-E(1)^2)/E(1)^2;
185 objAC=norm((AC-map_acf(map, ACLags)'),1)/norm(AC,2) + (map_scv(map) - tSCV)^2/tSCV^2;
186
187 mapBC=ones(1,size(BCLags,1));
188 for j=1:size(BCLags,1)
189 mapBC(j)=map_joint(map,BCLags(j,:),[1,1,1]);
190 end
191 objBC = norm(BC-mapBC,2)/norm(BC,2);
192 end
193
194
195warning on
196end