LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
cache_prob_is.m
1%{ @file cache_prob_is.m
2 % @brief Computes cache hit probabilities using importance sampling
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Computes cache hit probabilities via importance sampling
9 %
10 % @details
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).
14 %
15 % @par Syntax:
16 % @code
17 % prob = cache_prob_is(gamma, m)
18 % prob = cache_prob_is(gamma, m, samples)
19 % @endcode
20 %
21 % @par Parameters:
22 % <table>
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)
27 % </table>
28 %
29 % @par Returns:
30 % <table>
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
35 % </table>
36%}
37function prob = cache_prob_is(gamma, m, samples)
38% PROB = CACHE_PROB_IS(GAMMA, M, SAMPLES)
39%
40% Importance sampling estimation of cache hit probabilities.
41%
42% Input:
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
46%
47% Output:
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
51
52if nargin < 3 || isempty(samples)
53 samples = 1e5;
54end
55
56[n, h] = size(gamma);
57mt = sum(m);
58
59% Initialize probability matrix
60prob = zeros(n, h + 1);
61
62% Edge cases
63if n == 0 || mt == 0
64 prob(:, 1) = 1; % all items miss
65 return
66end
67
68if n < mt
69 line_warning(mfilename, 'Number of items (%d) less than cache capacity (%d).', n, mt);
70 prob(:, 1) = 1;
71 return
72end
73
74if n == mt
75 % All items must be in cache - use exact method
76 prob = cache_prob_erec(gamma, m);
77 return
78end
79
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;
84
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
88
89for s = 1:samples
90 % Sample mt items uniformly without replacement
91 selected = randperm(n, mt);
92
93 % Assign items to levels
94 assignment = assign_items_to_levels(m, selected);
95
96 % Compute unnormalized state probability
97 log_state_prob = log_m_fact;
98 for j = 1:h
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);
103 end
104 end
105
106 % Compute proposal probability
107 log_proposal = -log_combinations - log_multinomial;
108
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
112
113 % Update accumulators
114 total_weight = total_weight + is_weight;
115 for j = 1:h
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;
120 end
121 end
122end
123
124% Compute probabilities from accumulated weights
125if total_weight > 0
126 for i = 1:n
127 for j = 1:h
128 prob(i, 1 + j) = item_level_weight(i, j) / total_weight;
129 end
130 prob(i, 1) = max(0, 1 - sum(prob(i, 2:end)));
131 end
132else
133 % Fallback: all items miss
134 prob(:, 1) = 1;
135end
136
137end
138
139%% Helper: Assign items to cache levels uniformly
140function assignment = assign_items_to_levels(m, selected)
141 h = length(m);
142 assignment = cell(1, h);
143
144 % Shuffle and assign
145 perm = randperm(length(selected));
146 shuffled = selected(perm);
147
148 idx = 1;
149 for j = 1:h
150 assignment{j} = shuffled(idx:(idx + m(j) - 1));
151 idx = idx + m(j);
152 end
153end