LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
cache_is.m
1%{ @file cache_is.m
2 % @brief Importance sampling for cache normalizing constant
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Computes cache normalizing constant via importance sampling
9 %
10 % @details
11 % This function estimates the normalizing constant for cache models using
12 % Monte Carlo importance sampling. It provides an alternative to exact
13 % enumeration (cache_erec) and saddle-point approximation (cache_spm)
14 % that scales better for large item counts while providing confidence bounds.
15 %
16 % @par Syntax:
17 % @code
18 % [E, lE] = cache_is(gamma, m)
19 % [E, lE] = cache_is(gamma, m, samples)
20 % @endcode
21 %
22 % @par Parameters:
23 % <table>
24 % <tr><th>Name<th>Description
25 % <tr><td>gamma<td>Item popularity probabilities (n x h matrix)
26 % <tr><td>m<td>Cache capacity vector (1 x h)
27 % <tr><td>samples<td>(Optional) Number of Monte Carlo samples (default: 1e5)
28 % </table>
29 %
30 % @par Returns:
31 % <table>
32 % <tr><th>Name<th>Description
33 % <tr><td>E<td>Normalizing constant estimate
34 % <tr><td>lE<td>Log of normalizing constant
35 % </table>
36%}
37function [E, lE] = cache_is(gamma, m, samples)
38% [E, LE] = CACHE_IS(GAMMA, M, SAMPLES)
39%
40% Importance sampling estimation of cache normalizing constant.
41%
42% The normalizing constant E is defined as the sum over all valid cache
43% configurations of the product of gamma values for items in each level.
44%
45% Input:
46% gamma - (n x h) item popularity probabilities at each cache level
47% m - (1 x h) cache capacity vector
48% samples - (optional) number of Monte Carlo samples, default 1e5
49%
50% Output:
51% E - normalizing constant estimate
52% lE - log of normalizing constant
53
54if nargin < 3 || isempty(samples)
55 samples = 1e5;
56end
57
58% Remove items with zero gamma (no contribution)
59gamma = gamma(sum(gamma, 2) > 0, :);
60
61[n, h] = size(gamma);
62mt = sum(m); % total cache capacity
63
64% Edge cases
65if n == 0 || mt == 0
66 E = 1;
67 lE = 0;
68 return
69end
70
71if n < mt
72 line_warning(mfilename, 'Number of items (%d) less than cache capacity (%d).', n, mt);
73 E = 0;
74 lE = -Inf;
75 return
76end
77
78if n == mt
79 % All items must be in cache - only one valid configuration
80 E = cache_erec(gamma, m);
81 lE = log(E);
82 return
83end
84
85% Pre-compute log(gamma)
86log_gamma = log(gamma + 1e-300);
87
88% Pre-compute log(m(j)!)
89log_m_fact = sum(factln(m));
90
91% Log of number of ways to choose mt items from n
92log_combinations = factln(n) - factln(mt) - factln(n - mt);
93
94lZ_samples = zeros(samples, 1);
95
96for s = 1:samples
97 % Sample mt items uniformly without replacement
98 selected = randperm(n, mt);
99
100 % Assign items to levels uniformly at random
101 assignment = assign_items_to_levels(m, selected);
102
103 % Compute log of unnormalized state probability
104 log_state_prob = log_m_fact;
105 for j = 1:h
106 items_in_level = assignment{j};
107 for idx = 1:length(items_in_level)
108 i = items_in_level(idx);
109 log_state_prob = log_state_prob + log_gamma(i, j);
110 end
111 end
112
113 % Proposal probability is 1/(C(n,mt) * multinomial(mt; m))
114 % where multinomial(mt; m) = mt! / prod(m(j)!)
115 log_multinomial = factln(mt) - log_m_fact;
116 log_proposal = -log_combinations - log_multinomial;
117
118 lZ_samples(s) = log_state_prob - log_proposal;
119end
120
121lE = logmeanexp(lZ_samples);
122E = exp(lE);
123
124end
125
126%% Helper: Assign items to cache levels uniformly
127function assignment = assign_items_to_levels(m, selected)
128 h = length(m);
129 assignment = cell(1, h);
130
131 % Shuffle and assign
132 perm = randperm(length(selected));
133 shuffled = selected(perm);
134
135 idx = 1;
136 for j = 1:h
137 assignment{j} = shuffled(idx:(idx + m(j) - 1));
138 idx = idx + m(j);
139 end
140end