-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
53 lines (39 loc) · 1.18 KB
/
utils.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
44
45
46
47
48
49
50
51
52
53
import contextlib
import numpy as np
from scipy.stats import norm, laplace
class norm_distr(object):
def __init__(self, mu, sigma=1):
self.mu = mu
self.sigma = sigma
self.distribution = norm(loc=mu, scale=sigma)
def rvs(self):
'''sample'''
return self.distribution.rvs()
def pdf(self, x):
return self.distribution.pdf(x)
def logpdf(self, x):
return self.distribution.logpdf(x)
def logdistr_grad(self, x):
return (self.mu-x)/(self.sigma**2)
class laplace_distr(object):
def __init__(self, mu, b=1):
self.mu = mu
self.b = b
self.distribution = laplace(loc=mu, scale=b)
def rvs(self):
'''sample'''
return self.distribution.rvs()
def pdf(self, x):
return self.distribution.pdf(x)
def logpdf(self, x):
return self.distribution.logpdf(x)
def logdistr_grad(self, x):
return (self.mu-x)/(np.fabs(x-self.mu)*self.b)
@contextlib.contextmanager
def printoptions(*args, **kwargs):
original = np.get_printoptions()
np.set_printoptions(*args, **kwargs)
try:
yield
finally:
np.set_printoptions(**original)