forked from mit-han-lab/streaming-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriterion.py
48 lines (35 loc) · 1.26 KB
/
criterion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from statistics import NormalDist
class bayesian:
def __init__(self, mu_0, sigma_0):
self.mu_0 = mu_0
self.sigma_0 = sigma_0
self.n = 0
self.x_sum = 0
self.x_sq_sum = 0
def _compute_posterior_mean(self):
sample_sigma = self._compute_sample_sigma(self)
return (
self.mu_0 / (self.sigma_0 ** 2) + \
self.x_sum / (sample_sigma ** 2)
) / \
(
1 / (self.sigma_0 ** 2) + \
self.n / (sample_sigma ** 2)
)
def _compute_posterior_sigma(self):
sample_sigma = self._compute_sample_sigma(self)
return (
1 / (self.sigma_0 ** 2) + \
self.n / (sample_sigma ** 2)
) ** -1
def _compute_sample_sigma(self):
return ((self.x_sq_sum - (self.x_sq_sum) ** 2 / self.n) / self.n) ** 0.5
def _compute_posterior_params(self):
return self._compute_posterior_mean(), self._compute_posterior_sigma()
def bayesian_update(self, x):
self.n += 1
self.x_sum += x
self.x_sq_sum += x ** 2
def posterior_cdf(self, x):
mu, sigma = self._compute_posterior_params()
return NormalDist(mu=mu, sigma=sigma).cdf(x)