Created
September 25, 2022 22:35
-
-
Save amirziai/f3e33572a76b3a89c6ba25b7e2a3ef4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from collections import defaultdict | |
def compute_slices( | |
n: int, | |
k: int, | |
seed: Optional[int] = None, | |
) -> List[float]: | |
np.random.seed(seed) | |
tokens = np.random.rand(n * k) | |
servers = np.random.choice(range(n), n * k) | |
idxs = np.argsort(tokens) | |
fractions = defaultdict(int) | |
for idx_prev, idx in zip(idxs, idxs[1:]): | |
server_prev = servers[idx_prev] | |
diff = tokens[idx] - tokens[idx_prev] | |
fractions[server_prev] += diff | |
last = 1 - tokens[idxs[-1]] | |
last += tokens[idxs[0]] | |
fractions[servers[-1]] += last | |
assert sum(fractions.values()) == 1 | |
return fractions | |
df_slices = pd.DataFrame( | |
dict( | |
k=k, | |
exp_idx=exp_idx, | |
server1=compute_slices(n=5, k=k, seed=exp_idx)[0], | |
) | |
for k in (1, 5, 10, 100, 1000, 10_000) | |
for exp_idx in range(100) | |
).fillna(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment