Skip to content

Commit

Permalink
binning
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Jan 20, 2025
1 parent 5f7de59 commit e02130a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
50 changes: 40 additions & 10 deletions coq/clients/lsp/mul_bandit.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
from bisect import bisect
from collections import defaultdict
from datetime import timedelta
from math import exp, gamma, inf, log
from math import exp, floor, gamma, inf, log
from random import random, uniform
from typing import AbstractSet, MutableSet, Optional
from typing import AbstractSet, MutableSet, Optional, Sequence

from std2.itertools import pairwise

def _kumaraswamy_inv_cdf(y: float, alpha: float, beta: float) -> float:
"""
https://en.wikipedia.org/wiki/Kumaraswamy_distribution
"""

return (1 - (1 - y) ** (1 - beta)) ** (1 / alpha)
def _logit(x: float) -> float:
return log(x / (1 - x))


def _random_variate_sampling(a: float, b: float) -> float:
def _bins(k: float, n: int) -> Sequence[float]:
return tuple((exp(k * i) - 1) * 1000 for i in range(n)) + (inf,)

u = uniform(0, 1)
return _kumaraswamy_inv_cdf(u, alpha=a, beta=b)

class _Dist:
def __init__(self) -> None:
self._bins = _bins(k=0.03, n=6)
self._decay = 0.99
self._cdf = [0.0 for _ in pairwise(self._bins)]
self._sum = 0.0

def update(self, x: float) -> None:
binned, inc = False, 0.0
for i, (lo, hi) in enumerate(pairwise(self._bins)):
binned |= lo <= x < hi
inc += binned
self._cdf[i] = self._cdf[i] * self._decay + binned
self._cdf[-1] = self._cdf[-1] * self._decay + inc

def inv_cdf(self, p: float) -> float:
assert 0 <= p <= 1

i = round((len(self._cdf) - 1) * p)
print(self._cdf[i + 1])
frac = (p - self._cdf[i]) / (self._cdf[i + 1] - self._cdf[i])
return self._bins[i] + frac * (self._bins[i + 1] - self._bins[i])


class MultiArmedBandit:
Expand All @@ -31,3 +52,12 @@ def update(
self, clients: AbstractSet[str], client: Optional[str], elapsed: timedelta
) -> None:
self._clients |= clients


d = _Dist()
d.update(30)
d.update(80)
d.update(161)

print(d._bins, d._cdf)
print(d.inv_cdf(0.1))
2 changes: 1 addition & 1 deletion coq/clients/lsp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def _request(self, context: Context) -> AsyncIterator[LSPcomp]:
weight_adjust=self._options.weight_adjust,
context=context,
chunk=self._max_results,
clients=set(),
clients={"tabby_ml"}
)
async for row, peers, elapsed in rows:
self._stats.update(peers, client=row.client, elapsed=elapsed)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
std2@https://github.com/ms-jpq/std2/archive/205d1f52e9b5438ef2b732c77e1144847cafa8d0.tar.gz
std2@https://github.com/ms-jpq/std2/archive/c3ab55a0147b061a24c65cb0abf1e467c92c670c.tar.gz
pynvim_pp@https://github.com/ms-jpq/pynvim_pp/archive/34e3a027c595981886d7efd1c91071f3eaa4715d.tar.gz
PyYAML

0 comments on commit e02130a

Please sign in to comment.