LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
kpcfit_ph_exact.m
1function PH_EXACT = kpcfit_ph_exact(E,options)
2% Not for direct call, auxiliary function.
3PH_EXACT = {};
4
5SCV = ( E(2) - E(1)^2 ) / E(1)^2;
6if SCV > 1
7 if options.Verbose
8 fprintf(sprintf('kpcfit_ph: HIGHER variability than an exponential (var/mean^2 = %f)\n\n',SCV));
9 fprintf('kpcfit_ph: starting exact hyper-exponential fitting method (Prony\''s method)\n');
10 end
11 for n = 2:options.MaxNumStates
12 if length(E) < 2*n-1
13 if options.Verbose
14 fprintf(sprintf('kpcfit_ph: not enough moments given in input to fit hyper-exp(%d)\n',n))
15 end
16 break
17 end
18 PH = kpcfit_ph_prony(E, n);
19 if map_isfeasible(PH) > 0
20 PH_EXACT{end+1} = PH;
21 if options.Verbose
22 fprintf(sprintf('\t\t\thyper-exp(%d): feasible, matched exactly %d moments. result saved.\n',n,2*n-1))
23 end
24 else
25 if options.Verbose
26 fprintf(sprintf('\t\t\thyper-exp(%d): infeasible to fit exactly.\n',n))
27 end
28 break
29 end
30 end
31elseif SCV < 1
32 if options.Verbose
33 fprintf(sprintf('kpcfit_ph: LOWER variability than an exponential (var/mean^2 = %f)\n\n',SCV))
34 end
35 n = 1;
36 while 1/n > SCV
37 n = n + 1;
38 end
39 if options.Verbose
40 fprintf(sprintf('kpcfit_ph: exact fitting of E[X^2] requires at least %d states\n',n))
41 end
42 if options.MinExactMom >= 2 & options.MaxNumStates < n
43 if options.Verbose
44 fprintf(sprintf('kpcfit_ph: impossible to fit exactly E[X^2] with MaxNumStates = %d, increasing to MaxNumStates = %d.\n',options.MaxNumStates,n))
45 end
46 return
47 end
48
49 if n == 2
50 if options.Verbose
51 fprintf(sprintf('kpcfit_ph: attempting PH(2) fitting method\n',n));
52 end
53 PH = map2_fit(E(1),E(2),E(3),0);
54 if isempty(PH)
55 PH = map2_fit(E(1),E(2),-1,0);
56 if map_isfeasible(PH)
57 PH_EXACT{end+1} = PH;
58 if options.Verbose
59 fprintf(sprintf('\t\t\tph(2): feasible, matched exactly 2 moments. result saved.\n',n))
60 end
61 else
62 error('anomalous set of moments, please check.');
63 end
64 else
65 PH_EXACT{end+1} = PH;
66 if options.Verbose
67 fprintf(sprintf('\t\t\tph(2): feasible, matched exactly 3 moments. result saved.\n',n))
68 end
69 end
70 elseif (SCV > 1/n - kpcfit_tol) && (SCV < 1/n + kpcfit_tol)
71 ERL = map_erlang(E(1),n); % exponential PH-renewal
72 if norm(E - map_moment(ERL,1:length(E))) < kpcfit_tol
73 if options.Verbose
74 fprintf(sprintf('kpcfit_ph: erlang moment set. fitted erlang-%d. result saved.\n',n))
75 end
76 PH_EXACT{end+1} = ERL;
77 end
78 else
79 % fprintf('kpcfit_ph: no exact fitting method available for this moment set.\n');
80 maxorder = options.MaxNumStates;
81 if options.Verbose
82 fprintf(sprintf('kpcfit_ph: fitting APH distribution (best effort, max order = %d).\n',maxorder));
83 end
84 warning off
85 PH = aph_fit(E(1),E(2),E(3),maxorder);
86 if map_isfeasible(PH)
87 % fprintf(sprintf('kpcfit_ph: fitted APH(%d) distribution.\n',length(PH{1})));
88 aph_matched = 0;
89 for k=1:(2*length(PH{1})-1)
90 if abs(E(k)-map_moment(PH,k)) < kpcfit_tol*map_moment(PH,k)
91 aph_matched = aph_matched + 1;
92 end
93 end
94 if options.Verbose
95 fprintf(sprintf('\t\t\t aph(%d): feasible, matched exactly %d moments. result saved.\n',length(PH{1}),aph_matched))
96 end
97 PH_EXACT{end+1} = PH;
98 else
99 if options.Verbose
100 fprintf('kpcfit_ph: cannot fit APH distribution.\n');
101 end
102 end
103 end
104
105else
106 if options.Verbose
107 fprintf(sprintf('kpcfit_ph: SAME variability than an exponential (var/mean^2 = %f)\n\n',SCV))
108 end
109 EXP = {[-1/E(1)],[1/E(1)]}; % exponential PH-renewal
110 if norm(E - map_moment(EXP,1:length(E))) < kpcfit_tol
111 if options.Verbose
112 fprintf('kpcfit_ph: exponential moment set. fitted exponential. result saved.\n')
113 end
114 end
115 PH_EXACT{end+1} = EXP;
116end
117
118end