1%{ @file cache_prob_is.m
2 % @brief Computes cache hit probabilities
using importance sampling
4 % @author LINE Development Team
8 % @brief Computes cache hit probabilities via importance sampling
11 % This function estimates cache hit probability distribution
using
12 % Monte Carlo importance sampling. For each item i, it estimates the
13 % probability that item i
is cached at level j (hit) or not cached (miss).
17 % prob = cache_prob_is(gamma, m)
18 % prob = cache_prob_is(gamma, m, samples)
23 % <tr><th>Name<th>Description
24 % <tr><td>gamma<td>Item popularity probabilities (n x h matrix)
25 % <tr><td>m<td>Cache capacity vector (1 x h)
26 % <tr><td>samples<td>(Optional) Number of Monte Carlo samples (
default: 1e5)
31 % <tr><th>Name<th>Description
32 % <tr><td>prob<td>Cache hit probability distribution (n x h+1 matrix)
33 % prob(i,1) = miss probability for item i
34 % prob(i,1+j) = hit probability for item i at level j
37function prob = cache_prob_is(gamma, m, samples)
38% PROB = CACHE_PROB_IS(GAMMA, M, SAMPLES)
40% Importance sampling estimation of cache hit probabilities.
43% gamma - (n x h) item popularity probabilities at each cache level
44% m - (1 x h) cache capacity vector
45% samples - (optional) number of Monte Carlo samples, default 1e5
48% prob - (n x h+1) hit probability matrix
49% prob(i,1) = miss probability for item i
50% prob(i,1+j) = hit probability for item i at cache level j
52if nargin < 3 || isempty(samples)
59% Initialize probability matrix
60prob = zeros(n, h + 1);
64 prob(:, 1) = 1; % all items miss
69 line_warning(mfilename,
'Number of items (%d) less than cache capacity (%d).', n, mt);
75 % All items must be in cache - use exact method
76 prob = cache_prob_erec(gamma, m);
80log_gamma = log(gamma + 1e-300);
81log_m_fact = sum(factln(m));
82log_combinations = factln(n) - factln(mt) - factln(n - mt);
83log_multinomial = factln(mt) - log_m_fact;
85% Accumulators
for importance sampling estimates
86item_level_weight = zeros(n, h); % sum of weights when item i
is at level j
87total_weight = 0; % sum of all weights
90 % Sample mt items uniformly without replacement
91 selected = randperm(n, mt);
93 % Assign items to levels
94 assignment = assign_items_to_levels(m, selected);
96 % Compute unnormalized state probability
97 log_state_prob = log_m_fact;
99 items_in_level = assignment{j};
100 for idx = 1:length(items_in_level)
101 i = items_in_level(idx);
102 log_state_prob = log_state_prob + log_gamma(i, j);
106 % Compute proposal probability
107 log_proposal = -log_combinations - log_multinomial;
109 % Importance weight
for this sample
110 log_is_weight = log_state_prob - log_proposal;
111 is_weight = exp(log_is_weight - 50); % scale to avoid overflow
113 % Update accumulators
114 total_weight = total_weight + is_weight;
116 items_in_level = assignment{j};
117 for idx = 1:length(items_in_level)
118 i = items_in_level(idx);
119 item_level_weight(i, j) = item_level_weight(i, j) + is_weight;
124% Compute probabilities from accumulated weights
128 prob(i, 1 + j) = item_level_weight(i, j) / total_weight;
130 prob(i, 1) = max(0, 1 - sum(prob(i, 2:end)));
133 % Fallback: all items miss
139%% Helper: Assign items to cache levels uniformly
140function assignment = assign_items_to_levels(m, selected)
142 assignment = cell(1, h);
145 perm = randperm(length(selected));
146 shuffled = selected(perm);
150 assignment{j} = shuffled(idx:(idx + m(j) - 1));