4 % @brief Logistic sampling approximation
for normalizing constant.
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.
20function [Gn,lGn]=pfqn_ls(L,N,Z,I)
21% [GN,LGN]=PFQN_LS(L,N,Z,I)
23% PFQN_MCI Approximate solution of closed product-form queueing networks
26% [Gn,lGn]=pfqn_ls(L,N,Z,I)
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)
34% Gn : estimated normalizing constat
37% G. Casale. Accelerating performance inference over closed systems by
38% asymptotic methods. ACM SIGMETRICS 2017.
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
53 x0 = log(umax(1:M-1)/umax(M))'; % move to R^{K-1}
55 samples = mvnrnd(x0,iA,I);
57 % log-domain evaluation: exponentiating
the integrand or
the normal
58 % density overflows/underflows once lG
is large (hundreds of nats)
61 lT(i) = simplex_logfun(samples(i,:),L,N);
63 ldpdf = logmvnpdf_prec(samples,x0,A);
66 lGn = multinomialln([N,M-1]) + factln(M-1) + ...
67 lrmax + log(mean(exp(lr - lrmax)));
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
74 x0 = [log(umax(1:M-1)/umax(M))',log(vmax)]; % move to R^{K}
76 samples = mvnrnd(x0,iA,I);
80 eta = sum(N)+M*(1+eN);
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))));
88 lT(i) = lh(samples(i,:));
90 ldpdf = logmvnpdf_prec(samples,x0,A);
93 lGn = -sum(gammaln(1+N)) + lrmax + log(mean(exp(lr - lrmax)));
99function [u,d]=pfqn_le_fpi(L,N)
100% [U,D]=PFQN_LE_FPI(L,N)
102% find location of mode of gaussian
108while norm(u-u_1,1)>1e-10
113 u(i)=u(i)+N(r)/(sum(N)+M)*L(i,r)*u_1(i)/(u_1'*L(:,r));
120function [u,v,d]=pfqn_le_fpiZ(L,N,Z)
121% [U,V,D]=PFQN_LE_FPIZ(L,N,Z)
123% find location of mode of gaussian
129v_1=Inf*v; %#ok<NASGU>
132while norm(u-u_1,1)>1e-10
138 u(ist)=u(ist)+(N(r)/eta)*(Z(r)+v*L(ist,r))*u_1(ist)/(Z(r)+v*u_1'*L(:,r));
142 xi(r)=N(r)/(Z(r)+v*u_1(:)
'*L(:,r));
148 d=[d; abs(u-u_1)'+abs(v-v_1)];
153function hu=pfqn_le_hessian(L,N,u0)
154% HU=PFQN_LE_HESSIAN(L,N,U0)
156% find hessian of gaussian
163 hu(i,j)=-(Ntot+M)*u0(i)*u0(j);
165 hu(i,j)=hu(i,j)+N(r)*L(i,r)*L(j,r)*(u0(i)*u0(j))/(u0*L(:,r))^2;
168 hu(i,j)=(Ntot+M)*u0(i)*sum(allbut(u0,i));
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;
177function A=pfqn_le_hessianZ(L,N,Z,u,v)
178% A=PFQN_LE_HESSIANZ(L,N,Z,U,V)
180% find hessian of gaussian
186 csi(r)=N(r)/(Z(r)+v*u*L(:,r));
191 Lhat(k,r)=Z(r)+v*L(k,r);
198 A(i,j)=-eta*u(i)*u(j);
200 A(i,j)=A(i,j)+csi(r)^2*Lhat(i,r)*Lhat(j,r)*(u(i)*u(j))/N(r);
206 A(i,i)=-sum(allbut(A(i,:),i));
211 A(K,K)=A(K,K)-(csi(r)^2/N(r))*Z(r)*u*L(:,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));
223function y=allbut(y,xset)
226y=y(setdiff(1:length(y),xset));
229function mln=multinomialln(n)
230% MLN=MULTINOMIALLN(N)
232mln = factln(sum(n))- sum(factln(n));
241function lf=simplex_logfun(x,L,N)
242% LF=SIMPLEX_LOGFUN(X,L,N)
244% log of
the logistic-transformed integrand (kept in
the log domain to
245% avoid overflow
for models with large normalizing constants)
254lf=sum(N*log(v(:)'*L)
')+sum(x)-(sum(N)+M)*log(sum(v));
257function lp=logmvnpdf_prec(X,x0,A)
258% LP=LOGMVNPDF_PREC(X,X0,A)
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
263[C,flag] = chol((A+A')/2);
265 logdetA = 2*sum(log(diag(C)));
267 logdetA = log(abs(det(A)));
269D = X - repmat(x0,size(X,1),1);
271lp = 0.5*logdetA - (d/2)*log(2*pi) - 0.5*q;