Create rrf_safe_similarity.py
Browse files- rrf_safe_similarity.py +48 -0
rrf_safe_similarity.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def stabilize_vector(
|
| 5 |
+
vec: np.ndarray,
|
| 6 |
+
r_min: float = 1e-4,
|
| 7 |
+
r_max: float = 1e4,
|
| 8 |
+
):
|
| 9 |
+
"""
|
| 10 |
+
Applies RRF logarithmic norm stabilization.
|
| 11 |
+
Returns (stabilized_vector, audit_event | None)
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
norm = np.linalg.norm(vec)
|
| 15 |
+
|
| 16 |
+
audit_event = None
|
| 17 |
+
|
| 18 |
+
if norm < r_min:
|
| 19 |
+
scale = r_min / (norm + 1e-12)
|
| 20 |
+
vec = vec * scale
|
| 21 |
+
|
| 22 |
+
audit_event = {
|
| 23 |
+
"type": "stabilization_triggered",
|
| 24 |
+
"reason": "norm_below_min",
|
| 25 |
+
"original_norm": float(norm),
|
| 26 |
+
"target_norm": r_min,
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
elif norm > r_max:
|
| 30 |
+
scale = r_max / norm
|
| 31 |
+
vec = vec * scale
|
| 32 |
+
|
| 33 |
+
audit_event = {
|
| 34 |
+
"type": "stabilization_triggered",
|
| 35 |
+
"reason": "norm_above_max",
|
| 36 |
+
"original_norm": float(norm),
|
| 37 |
+
"target_norm": r_max,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return vec, audit_event
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def safe_cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:
|
| 44 |
+
"""
|
| 45 |
+
Cosine similarity with numerical safety.
|
| 46 |
+
"""
|
| 47 |
+
denom = (np.linalg.norm(a) * np.linalg.norm(b)) + 1e-12
|
| 48 |
+
return float(np.dot(a, b) / denom)
|