LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
map_anfit.m
1function MAP=map_anfit(ls,rho,H,n,ds,SA,SAlags, iter_max, iter_tol)
2% MAP_ANFIT - Andersen and Nielsen MAP Fitting Algorithm
3% From A.T.Andersen and B.F.Nielsen, "A Markovian Approach for Modeling Packet Traffic with Long-Range Dependence", IEEE JSAC 16(5), 1998.
4% ls - mean arrival rate
5% rho - lag-1 autocorrelation of the counting process
6% H - Hurst coefficient (counting process)
7% n - number of time scales to be modeled
8% ds - number of IPP to be used
9% SA - set of autocorrelation coefficients of interarrival times
10% SAlags - lags used in SA
11%
12% EXAMPLE:
13% lstar=1; rho=0.8; H=0.92; n=5; ds=4; SA=[0.3,0.28]; SAlags=[1,2];
14% MAP=map_anfit(lstar,rho,H,n,ds,SA,SAlags);
15
16LSQFIT=0;
17if nargin>5 % if SA and SAlags are given in input, then perform least square fit of SA
18 LSQFIT=1;
19 SA=SA(SAlags);
20end
21if nargin<6
22 iter_max=100;
23end
24if nargin<7
25 iter_tol=1e-9;
26end
27
28%%
29beta=2-2*H;
30d=ds;
31k(2,1)=0.8;
32%%
33skip=0;
34while 1
35 if ~skip
36 d0=0; phi(d)=1; i=1; a=10^(n/(d-1));
37 end
38 S=0;
39 for j=0:(i-1)
40 S=S+phi(d-j)^2*exp(1-a^(i-j));
41 end
42 D=a^(i*beta)-S;
43 if D<0
44 phi(d-i)=0;
45 d0=d0+1;
46 if ds>d-d0
47 d=d+1;
48 skip=0;
49 continue;
50 else
51 i=i+1;
52 if i==d
53 break
54 else
55 skip=1;
56 continue;
57 end
58 end
59 else
60 phi(d-i)=sqrt(D);
61 i=i+1;
62 if i==d
63 break
64 else
65 skip=1;
66 continue;
67 end
68 end
69end
70%%
71for i=2:d
72 k(2,i)=a^(1-i)*k(2,1);
73end
74if ~(k(2,1)<1 && rho<0.5)
75 fprintf('warning: if necessary adjust k(2,1) and/or rho\n')
76end
77S=0;
78for i=1:d
79 kappa=k(2,i);
80 e=exp(-kappa);
81 S=S+phi(i)^2*kappa^(-2)*((1-e)^2-2*rho*(kappa-(1-e)));
82end
83eta=sqrt(4*rho*ls)/sqrt(S);
84L=eta*sum(phi(1:d))/2;
85if ls<L
86 lP=0;
87 for i=1:d
88 c(1,i)=L^2/(ls^2+L^2)*k(2,i);
89 c(2,i)=k(2,i)-c(1,i);
90 l(i)=phi(i)*(ls^2+L^2)/(ls*sum(phi(1:d)));
91 end
92else
93 lP=ls-L;
94 for i=1:d
95 c(2,i)=0.5*k(2,i);
96 c(1,i)=c(2,i);
97 l(i)=eta*phi(i);
98 end
99end
100
101
102if ~LSQFIT
103 %% No Least Square Fit, compose and return
104 MAP=map_exponential(lP);
105 for i=1:d
106 D0=[0,c(1,i);c(2,i),0];
107 D1=[l(i),0;0,0];
108 IPP=map_normalize({D0,D1});
109 MAP=map_super(MAP,IPP);
110 end
111 MAP=map_normalize(MAP);
112 return
113else
114 %% Least Square Fit
115 NSA=norm(SA,2);
116 for i=1:d
117 k(1,i)=(l(i)-0)^2*(c(1,i)*c(2,i))/(c(1,i)+c(2,i))^3;
118 ls(i)=(c(2,i)*l(i))/(c(1,i)+c(2,i));
119 end
120 MAP0=[];
121 r0=[];
122 for i=1:d
123 r0(end+1)=1+k(1,i)*k(2,i)/ls(i)^2;
124 end
125 [f,r]=minimize(@objfun,@nnlcon,r0, iter_max, iter_tol);
126 MAP=MAP0; % return the last MAP found in the lsq algorithm
127end
128 function [c,ceq]=nnlcon(r)
129 c=[];
130 ceq=[];
131 for i=1:d
132 c(end+1)=-(ls(i)-sqrt(k(1,i)*k(2,i)/r(i)));
133 end
134 end
135 function f=objfun(r)
136 MAP0={[-lP],[lP]};
137 for i=1:d
138 D0=[0,k(2,i)*r(i)/(1+r(i));k(2,i)/(1+r(i)),0];
139 D1=[ls(i)+sqrt(k(1,i)*k(2,i)*r(i)),0;0,ls(i)-sqrt(k(1,i)*k(2,i)/r(i))];
140 IPP=map_normalize({D0,D1});
141 MAP0=map_super(MAP0,IPP);
142 end
143 f=norm(map_acf(MAP0,SAlags)-SA',2);
144 end
145
146end
147
148
149%% MINIMIZE encapsulates MATLAB's nonlinear optimization solver
150% objfun - objective function
151% nnlcon - nonlinear constraints
152% x0 - initial solution
153% MAXITER - maximum number of iterations
154% TOL - constraint feasibility tolerance
155function [f,x]=minimize(objfun,nnlcon,x0,MAXITER,TOL)
156warning off;
157options = optimset('LargeScale','off','MaxIter',MAXITER, 'MaxFunEvals',1e10, 'MaxSQPIter',500, 'TolCon',TOL, 'Display','off');
158EPSTOL=100*TOL;
159xlb=EPSTOL+0*x0;
160[x,f]=fmincon(objfun,x0,[],[],[],[],xlb,[],nnlcon,options);
161end
162
163%% MAP_NORMALIZE normalizes the diagonal element of D0 and prevents the solver to step into complex numbers
164% MAP - 2x1 cell, element 1 is the matrix D0, element 2 is D1
165function [MAP]=map_normalize(MAP)
166nStates=size(MAP{1},1);
167nMAP=length(MAP);
168
169for n=1:nStates
170 MAP{1}(n,n)=0;
171 for b=1:nMAP
172 MAP{1}(n,n)=MAP{1}(n,n)-sum(MAP{b}(n,:));
173 end
174end
175for i=1:nStates
176 for j=1:nStates
177 MAP{1}(i,j)=real(MAP{1}(i,j));
178 MAP{2}(i,j)=real(MAP{2}(i,j));
179 end
180end
181end
182
183%% MAP_SUPER creates the superposed process MAPa \oplus MAPb
184%% (oplus=Kronecker sum)
185% MAPa - 2x1 cell, element 1 is the matrix D0, element 2 is D1
186% MAPb - 2x1 cell, element 1 is the matrix D0, element 2 is D1
187function MAP=map_super(MAPa,MAPb)
188D0s=krons(MAPa{1},MAPb{1});
189D1s=krons(MAPa{2},MAPb{2});
190MAP={D0s,D1s};
191MAP=map_normalize(MAP);
192end
193
194%% KRONS - Kronecker sum
195function AB=krons(A,B)
196AB=kron(A,eye(size(B)))+kron(eye(size(A)),B);
197end
198
199%% MAP_PROB - MAP equilibrium probabilities from D0 and D1
200function p=map_prob(MAP)
201Q=MAP{1}+MAP{2};
202p=ctmc_solve(Q);
203end
204
205%% CTMC_SOLVE solve Markov process by global balance
206function p=ctmc_solve(Q)
207% Q - infinitesimal generator of the process
208% p - steady state probabilities
209zerocol=find(sum(abs(Q))==0);
210if length(zerocol)>1
211 warning('two cols of Q are composed by zeros only.');
212 b=zeros(size(Q,1),1);
213elseif length(zerocol)==0
214 zerocol=size(Q,1);
215end
216b=zeros(size(Q,1),1); b(zerocol)=1;
217Q(:,zerocol)=1; % add normalization condition
218p=(Q')\b; % compute steady state probability vector
219p=p'; % p is a row vector
220end
221
222%% MAP_ACF autocorrelation coefficents of inter-arrival times
223% MAP - 2x1 cell, element 1 is the matrix D0, element 2 is D1
224% lagset - set of lag coefficients
225function acfun=map_acf(MAP,lagset)
226p=map_prob(MAP); %steady state probabilities
227lambda=p*MAP{2}*ones(size(MAP{2},2),1); % arrival rate
228e=ones(size(MAP{1},1),1);
229invD0=inv(-MAP{1});
230acfun=[];
231P=invD0*MAP{2};
232for lag=lagset
233 acfun(end+1)=lambda*p*(P^lag)*invD0*e;
234end
235acfun=(acfun-1)./map_scv(MAP);
236end