-
Notifications
You must be signed in to change notification settings - Fork 430
/
utils.py
62 lines (44 loc) · 1.57 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
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
import warnings
from functools import wraps
import numpy as np
__all__ = ["sample_ball", "deprecated", "deprecation_warning"]
def deprecation_warning(msg):
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
def deprecated(alternate):
def wrapper(func, alternate=alternate):
msg = "'{0}' is deprecated.".format(func.__name__)
if alternate is not None:
msg += " Use '{0}' instead.".format(alternate)
@wraps(func)
def f(*args, **kwargs):
deprecation_warning(msg)
return func(*args, **kwargs)
return f
return wrapper
@deprecated(None)
def sample_ball(p0, std, size=1):
"""
Produce a ball of walkers around an initial parameter value.
:param p0: The initial parameter value.
:param std: The axis-aligned standard deviation.
:param size: The number of samples to produce.
"""
assert len(p0) == len(std)
return np.vstack(
[p0 + std * np.random.normal(size=len(p0)) for i in range(size)]
)
@deprecated(None)
def sample_ellipsoid(p0, covmat, size=1):
"""
Produce an ellipsoid of walkers around an initial parameter value,
according to a covariance matrix.
:param p0: The initial parameter value.
:param covmat:
The covariance matrix. Must be symmetric-positive definite or
it will raise the exception numpy.linalg.LinAlgError
:param size: The number of samples to produce.
"""
return np.random.multivariate_normal(
np.atleast_1d(p0), np.atleast_2d(covmat), size=size
)