LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
cache_spm.m
1%{ @file cache_spm.m
2 % @brief Computes normalizing constant using saddle-point method
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Computes normalizing constant using saddle-point approximation
9 %
10 % @details
11 % This function computes the normalizing constant for cache models using
12 % the saddle-point method (SPM) for approximation.
13 %
14 % @par Syntax:
15 % @code
16 % [Z, lZ, xi] = cache_spm(gamma, m)
17 % [Z, lZ, xi] = cache_spm(gamma, m, xi0)
18 % @endcode
19 %
20 % @par Parameters:
21 % <table>
22 % <tr><th>Name<th>Description
23 % <tr><td>gamma<td>Item popularity probabilities
24 % <tr><td>m<td>Cache capacity vector
25 % <tr><td>xi0<td>(Optional) Initial guess for Lagrange multipliers
26 % </table>
27 %
28 % @par Returns:
29 % <table>
30 % <tr><th>Name<th>Description
31 % <tr><td>Z<td>Normalizing constant
32 % <tr><td>lZ<td>Log of normalizing constant
33 % <tr><td>xi<td>Lagrange multipliers
34 % </table>
35%}
36function [Z,lZ,xi]=cache_spm(gamma,m,xi0)
37gamma=gamma(find(sum(gamma,2)>0),:); %#ok<FNDSB>
38h=length(m);
39n=length(gamma);
40mt=sum(m);
41if n==mt
42 line_warning(mfilename,'The number of items equals the cache capacity.\n');
43 Z=cache_erec(gamma,m);
44 lZ=log(Z);
45 xi = cache_xi_iter(gamma,m);
46 return
47end
48
49if nargin<3
50 xi = cache_xi_iter(gamma,m);
51else
52 xi = cache_xi_iter(gamma,m,xi0);
53end
54
55for k=1:n
56 S(k) = 0;
57 for l=1:h
58 S(k) = S(k) + gamma(k,l) * xi(l);
59 end
60end
61
62%% phi
63phi = 0;
64for k=1:n
65 phi = phi + log(1+S(k)) ;
66end
67phi = phi - log(xi) * m';
68
69%% A
70delta=eye(h);
71for j=1:h
72 for l=1:h
73 C1=0;
74 for k=1:n
75 C1=C1+gamma(k,j)/(1+S(k));
76 end
77 C2=0;
78 for k=1:n
79 C2=C2+gamma(k,j)*gamma(k,l)/(1+S(k))^2;
80 end
81 C(j,l) = delta(j,l) * C1 - xi(j) * C2;
82 end
83end
84
85%%
86Z = exp(phi) * sqrt(2*pi)^(-h) * prod(factorial(m)) / prod(sqrt(xi)) / sqrt(det(C));
87lZ = (-h) * log(sqrt(2*pi)) + (phi) + sum(factln((m))) - sum(log(sqrt(xi))) - log(sqrt(det(C)));
88lZ=real(lZ); % remove small imaginary part roundoffs
89
90end