2 % @brief Importance sampling
for cache normalizing constant
4 % @author LINE Development Team
8 % @brief Computes cache normalizing constant via importance sampling
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.
18 % [E, lE] = cache_is(gamma, m)
19 % [E, lE] = cache_is(gamma, m, samples)
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)
32 % <tr><th>Name<th>Description
33 % <tr><td>E<td>Normalizing constant estimate
34 % <tr><td>lE<td>Log of normalizing constant
37function [E, lE] = cache_is(gamma, m, samples)
38% [E, LE] = CACHE_IS(GAMMA, M, SAMPLES)
40% Importance sampling estimation of cache normalizing constant.
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.
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
51% E - normalizing constant estimate
52% lE - log of normalizing constant
54if nargin < 3 || isempty(samples)
58% Remove items with zero gamma (no contribution)
59gamma = gamma(sum(gamma, 2) > 0, :);
62mt = sum(m); % total cache capacity
72 line_warning(mfilename,
'Number of items (%d) less than cache capacity (%d).', n, mt);
79 % All items must be in cache - only one valid configuration
80 E = cache_erec(gamma, m);
85% Pre-compute log(gamma)
86log_gamma = log(gamma + 1e-300);
88% Pre-compute log(m(j)!)
89log_m_fact = sum(factln(m));
91% Log of number of ways to choose mt items from n
92log_combinations = factln(n) - factln(mt) - factln(n - mt);
94lZ_samples = zeros(samples, 1);
97 % Sample mt items uniformly without replacement
98 selected = randperm(n, mt);
100 % Assign items to levels uniformly at random
101 assignment = assign_items_to_levels(m, selected);
103 % Compute log of unnormalized state probability
104 log_state_prob = log_m_fact;
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);
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;
118 lZ_samples(s) = log_state_prob - log_proposal;
121lE = logmeanexp(lZ_samples);
126%% Helper: Assign items to cache levels uniformly
127function assignment = assign_items_to_levels(m, selected)
129 assignment = cell(1, h);
132 perm = randperm(length(selected));
133 shuffled = selected(perm);
137 assignment{j} = shuffled(idx:(idx + m(j) - 1));