Last active
January 11, 2021 06:22
-
-
Save pjoshi30/cbf51bb9ec73da7040062591a030ceaa to your computer and use it in GitHub Desktop.
Simple RBO implementation in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def rbo(list1, list2, p=0.9): | |
# tail recursive helper function | |
def helper(ret, i, d): | |
l1 = set(list1[:i]) if i < len(list1) else set(list1) | |
l2 = set(list2[:i]) if i < len(list2) else set(list2) | |
a_d = len(l1.intersection(l2))/i | |
term = math.pow(p, i) * a_d | |
if d == i: | |
return ret + term | |
return helper(ret + term, i + 1, d) | |
k = max(len(list1), len(list2)) | |
x_k = len(set(list1).intersection(set(list2))) | |
summation = helper(0, 1, k) | |
return ((float(x_k)/k) * math.pow(p, k)) + ((1-p)/p * summation) | |
# Example usage | |
rbo([1,2,3], [3,2,1]) # Output: 0.8550000000000001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment