Skip to content

Commit 15328be

Browse files
authored
Add files via upload
feat: add calculate_weighted_exploration() for dynamic top-k adjustment
1 parent abed704 commit 15328be

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import math
2+
3+
def calculate_weighted_exploration(refined_scores, k, max_expansion=2.0):
4+
"""
5+
Calculate a new k value based on the distribution of similarity scores
6+
7+
Parameters:
8+
refined_scores: list of similarity_score
9+
k: current top-k
10+
max_expansion: maximum multiplier to expand k (default: 2.0)
11+
12+
Returns:
13+
tuple: (change_k: bool, k_new: int)
14+
"""
15+
# Check if there are no results or k <= 0
16+
if not refined_scores or k <= 0:
17+
return False, max(1, k)
18+
19+
mean_score = sum(refined_scores) / len(refined_scores)
20+
std_score = math.sqrt(sum((s - mean_score)**2 for s in refined_scores) / len(refined_scores))
21+
22+
# Calculate exploration_ratio based on the distribution of scores
23+
if std_score < 0.1 and mean_score < 0.5:
24+
# Results are uniformly low - need to expand more
25+
exploration_ratio = 0.9
26+
elif std_score > 0.3:
27+
# Results are scattered - moderate expansion
28+
exploration_ratio = 0.5
29+
else:
30+
# Normal case - ratio inversely proportional to mean
31+
exploration_ratio = 1 - mean_score
32+
33+
# Ensure k_new is a valid value and sufficiently different from the original k
34+
k_new = max(k + 1, int(min(max_expansion * k, exploration_ratio * max_expansion * k)))
35+
36+
# Only expand if k_new is sufficiently larger than k
37+
if k_new > k + max(1, int(0.1 * k)): # Ensure increase by at least 10% or +1
38+
return True, k_new
39+
else:
40+
return False, k

0 commit comments

Comments
 (0)