LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
pfqn_ls.m
1%{
2%{
3 % @file pfqn_ls.m
4 % @brief Logistic sampling approximation for normalizing constant.
5%}
6%}
7
8%{
9%{
10 % @brief Logistic sampling approximation for normalizing constant.
11 % @fn pfqn_ls(L, N, Z, I)
12 % @param L Service demand matrix (MxR).
13 % @param N Population vector (1xR).
14 % @param Z Think time vector (1xR).
15 % @param I Number of samples (default: 1e5).
16 % @return Gn Estimated normalizing constant.
17 % @return lGn Logarithm of normalizing constant.
18%}
19%}
20function [Gn,lGn]=pfqn_ls(L,N,Z,I)
21% [GN,LGN]=PFQN_LS(L,N,Z,I)
22
23% PFQN_MCI Approximate solution of closed product-form queueing networks
24% by logistic sampling
25%
26% [Gn,lGn]=pfqn_ls(L,N,Z,I)
27% Input:
28% L : MxR demand matrix. L(i,r) is the demand of class-r at queue i
29% N : 1xR population vector. N(r) is the number of jobs in class r
30% Z : 1xR think time vector. Z(r) is the total think time of class r
31% I : number of samples (default: 1e5)
32%
33% Output:
34% Gn : estimated normalizing constat
35%
36% Reference:
37% G. Casale. Accelerating performance inference over closed systems by
38% asymptotic methods. ACM SIGMETRICS 2017.
39% Available at: http://dl.acm.org/citation.cfm?id=3084445
40
41Lsum = sum(L,2);
42L = L(Lsum > 1e-4,:);
43[M,R]=size(L);
44samples=[];
45
46if isempty(L) || sum(L(:))<1e-4 || isempty(N) || sum(N)==0
47 lGn = - sum(factln(N)) + sum(N.*log(sum(Z,1)));
48elseif nargin<3 || isempty(Z) %~exist('Z','var') || isempty(Z)
49 umax=pfqn_le_fpi(L,N);
50 A = pfqn_le_hessian(L,N,umax'); % slightly faster than pfqn_le_hessianZ
51 A = (A+A')/2; % let's get rid of small numerical perturbations
52 iA = inv(A);
53 x0 = log(umax(1:M-1)/umax(M))'; % move to R^{K-1}
54 if isempty(samples)
55 samples = mvnrnd(x0,iA,I);
56 end
57 % log-domain evaluation: exponentiating the integrand or the normal
58 % density overflows/underflows once lG is large (hundreds of nats)
59 lT = zeros(I,1);
60 for i=1:I
61 lT(i) = simplex_logfun(samples(i,:),L,N);
62 end
63 ldpdf = logmvnpdf_prec(samples,x0,A);
64 lr = lT - ldpdf;
65 lrmax = max(lr);
66 lGn = multinomialln([N,M-1]) + factln(M-1) + ...
67 lrmax + log(mean(exp(lr - lrmax)));
68 Gn = exp(lGn);
69else % Z>0
70 [umax,vmax]=pfqn_le_fpiZ(L,N,Z);
71 A = pfqn_le_hessianZ(L,N,Z,umax',vmax);
72 A = (A+A')/2; % let's get rid of small numerical perturbations
73 iA = inv(A);
74 x0 = [log(umax(1:M-1)/umax(M))',log(vmax)]; % move to R^{K}
75 if isempty(samples)
76 samples = mvnrnd(x0,iA,I);
77 end
78 epsilon=1e-10;
79 eN = epsilon*sum(N);
80 eta = sum(N)+M*(1+eN);
81 K=M;
82 % log of the integrand (previously wrapped in exp(), which overflows
83 % for large lG; the exp(-gammaln) prefactor then underflowed to 0,
84 % yielding 0*Inf = NaN)
85 lh = @(x) -exp(x(K))+K*(1+eN)*x(M)+sum(N*log( (L(K,:)*exp(x(K))+Z) + exp(x(1:K-1))*(L(1:K-1,:)*exp(x(K))+repmat(Z,K-1,1)))') +sum(x(1:K-1)) -eta*log(1+sum(exp(x(1:K-1))));
86 lT = zeros(I,1);
87 for i=1:I
88 lT(i) = lh(samples(i,:));
89 end
90 ldpdf = logmvnpdf_prec(samples,x0,A);
91 lr = lT - ldpdf;
92 lrmax = max(lr);
93 lGn = -sum(gammaln(1+N)) + lrmax + log(mean(exp(lr - lrmax)));
94 Gn = exp(lGn);
95end
96Gn=exp(lGn);
97end
98
99function [u,d]=pfqn_le_fpi(L,N)
100% [U,D]=PFQN_LE_FPI(L,N)
101
102% find location of mode of gaussian
103[M,R]=size(L);
104u=ones(M,1)/M;
105u_1=Inf*u;
106d=[];
107coder.varsize('d');
108while norm(u-u_1,1)>1e-10
109 u_1=u;
110 for i=1:M
111 u(i)=1/(sum(N)+M);
112 for r=1:R
113 u(i)=u(i)+N(r)/(sum(N)+M)*L(i,r)*u_1(i)/(u_1'*L(:,r));
114 end
115 end
116 d=[d; abs(u-u_1)'];
117end
118end
119
120function [u,v,d]=pfqn_le_fpiZ(L,N,Z)
121% [U,V,D]=PFQN_LE_FPIZ(L,N,Z)
122
123% find location of mode of gaussian
124[M,R]=size(L);
125eta = sum(N)+M;
126u=ones(M,1)/M;
127v=eta+1;
128u_1=Inf*u;
129v_1=Inf*v; %#ok<NASGU>
130d=[];
131coder.varsize('d');
132while norm(u-u_1,1)>1e-10
133 u_1=u;
134 v_1=v;
135 for ist=1:M
136 u(ist)=1/eta;
137 for r=1:R
138 u(ist)=u(ist)+(N(r)/eta)*(Z(r)+v*L(ist,r))*u_1(ist)/(Z(r)+v*u_1'*L(:,r));
139 end
140 end
141 for r=1:R
142 xi(r)=N(r)/(Z(r)+v*u_1(:)'*L(:,r));
143 end
144 v=eta+1;
145 for r=1:R
146 v=v-xi(r)*Z(r);
147 end
148 d=[d; abs(u-u_1)'+abs(v-v_1)];
149end
150
151end
152
153function hu=pfqn_le_hessian(L,N,u0)
154% HU=PFQN_LE_HESSIAN(L,N,U0)
155
156% find hessian of gaussian
157[M,R]=size(L);
158Ntot=sum(N);
159hu=zeros(M-1);
160for i=1:(M-1)
161 for j=1:(M-1)
162 if i~=j
163 hu(i,j)=-(Ntot+M)*u0(i)*u0(j);
164 for r=1:R
165 hu(i,j)=hu(i,j)+N(r)*L(i,r)*L(j,r)*(u0(i)*u0(j))/(u0*L(:,r))^2;
166 end
167 else % i=j
168 hu(i,j)=(Ntot+M)*u0(i)*sum(allbut(u0,i));
169 for r=1:R
170 hu(i,j)=hu(i,j)-N(r)*L(i,r)*u0(i)*(allbut(u0,i)*L(allbut(1:M,i),r))/(u0*L(:,r))^2;
171 end
172 end
173 end
174end
175end
176
177function A=pfqn_le_hessianZ(L,N,Z,u,v)
178% A=PFQN_LE_HESSIANZ(L,N,Z,U,V)
179
180% find hessian of gaussian
181[K,R]=size(L);
182Ntot=sum(N);
183A=zeros(K);
184csi = zeros(1,R);
185for r=1:R
186 csi(r)=N(r)/(Z(r)+v*u*L(:,r));
187end
188Lhat = zeros(K,R);
189for k=1:K
190 for r=1:R
191 Lhat(k,r)=Z(r)+v*L(k,r);
192 end
193end
194eta=Ntot+K;
195for i=1:K
196 for j=1:K
197 if i~=j
198 A(i,j)=-eta*u(i)*u(j);
199 for r=1:R
200 A(i,j)=A(i,j)+csi(r)^2*Lhat(i,r)*Lhat(j,r)*(u(i)*u(j))/N(r);
201 end
202 end
203 end
204end
205for i=1:K
206 A(i,i)=-sum(allbut(A(i,:),i));
207end
208A=A(1:(K-1),1:(K-1));
209A(K,K)=1;
210for r=1:R
211 A(K,K)=A(K,K)-(csi(r)^2/N(r))*Z(r)*u*L(:,r);
212end
213A(K,K)=v*A(K,K);
214for i=1:(K-1)
215 A(i,K)=0;
216 for r=1:R
217 A(i,K)=A(i,K)+v*u(i)*((csi(r)^2/N(r))*Lhat(i,r)*(u*L(:,r))-csi(r)*L(i,r));
218 end
219 A(K,i)=A(i,K);
220end
221end
222
223function y=allbut(y,xset)
224% Y=ALLBUT(Y,XSET)
225
226y=y(setdiff(1:length(y),xset));
227end
228
229function mln=multinomialln(n)
230% MLN=MULTINOMIALLN(N)
231
232mln = factln(sum(n))- sum(factln(n));
233end
234
235function lf=factln(n)
236% LF=FACTLN(N)
237
238lf = gammaln(1+n);
239end
240
241function lf=simplex_logfun(x,L,N)
242% LF=SIMPLEX_LOGFUN(X,L,N)
243
244% log of the logistic-transformed integrand (kept in the log domain to
245% avoid overflow for models with large normalizing constants)
246x=x';
247M=length(x)+1;
248v=zeros(1,M);
249for i=1:(M-1)
250 v(i)=exp(x(i));
251end
252v(M)=1;
253
254lf=sum(N*log(v(:)'*L)')+sum(x)-(sum(N)+M)*log(sum(v));
255end
256
257function lp=logmvnpdf_prec(X,x0,A)
258% LP=LOGMVNPDF_PREC(X,X0,A)
259
260% log-density of N(x0, inv(A)) evaluated at the rows of X, computed
261% from the precision matrix A to avoid mvnpdf underflow
262d = size(X,2);
263[C,flag] = chol((A+A')/2);
264if flag == 0
265 logdetA = 2*sum(log(diag(C)));
266else
267 logdetA = log(abs(det(A)));
268end
269D = X - repmat(x0,size(X,1),1);
270q = sum((D*A).*D,2);
271lp = 0.5*logdetA - (d/2)*log(2*pi) - 0.5*q;
272end
273
Definition Station.m:245